//! `Effect` type.

use crate::{bin::OptU16, err::*, fixed::Fixed};

/// Reads a `FXpx` chunk.
pub fn read(b: &[u8]) -> ResultS<(Effect, usize)>
{
	read_data! {
		endian: BIG, buf: b, size: 14, start: 0, data {
			let collection = u16[0];
			let shape      = u16[2];
			let pitch      = Fixed[4];
			let flags      = u16[8] flag EffectFlags;
			let delay      = OptU16[10];
			let delay_snd  = OptU16[12];
		}
	}

	Ok((Effect{collection, shape, pitch, flags, delay, delay_snd}, 14))
}

/// An effect definition.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct Effect {
	pub collection: u16,
	pub shape:      u16,
	pub pitch:      Fixed,
	pub flags:      EffectFlags,
	pub delay:      OptU16,
	pub delay_snd:  OptU16,
}

c_bitfield! {
	/// Flags for an effect.
	#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
	pub struct EffectFlags: u16 {
		END_ON_LOOP       = 0,
		END_ON_XFER_LOOP  = 1,
		SOUND_ONLY        = 2,
		MAKE_TWIN_VISIBLE = 3,
		MEDIA_EFFECT      = 4,
	}
}

// EOF
