/*	kd:
	
	GUNCASTER HD CONFIRMED
	
	*/

struct Gc_ScreenParticles {
	
	// kd: Pitifully simple but it works for us, cuz particles don't have to
	// communicate with each other.
	private Array <Gc_ScreenParticleGen>	generator;
	private int								count;
	
	// kd: lol gzd
	private Gc_Rng							rng;
	
	void OnBuild (Gc_Rng rng_) {
		rng = rng_;
	}
	
	void NextState () {
		for(let i = count; 0 <= --i;) {
			let gen = generator [i];
			
			if(gen == NULL || gen.IsDone()) {
				continue;
			}
			
			gen.NextState();
			gen.FinishState();
		}
	}
	
	void RemoveNulls () {
		for(let i = count; 0 <= --i;) {
			let gen = generator [i];
			
			if(!gen || gen.IsDone()) {
				Remove(i);
			}
		}
	}
	
	void DestroyGenerators () {
		for(let i = count; 0 <= --i;) {
			Remove(i);
		}
		
		generator.clear();
		count = 0;
	}
	
	void Remove (int index) {
		if(0 <= index && index < count) {
			let gen = generator [index];
			gen.Destroy();
			generator.delete(index);
			count--;
		}
	}
	
	void Add (Gc_ScreenParticleGen gen) {
		if(gen && rng) {
			gen.rng = rng;
			generator.push(gen);
			count++;
		}
	}
	
	int GeneratorCount () const {
		return count;
	}
	
	Gc_ScreenParticleGen GeneratorAt (int i) const {
		return generator [i];
	}
	
	ui void Draw (vector2 resolution, double t = 1) const {
		for(let i = 0; i < count; i++) {
			generator [i].Draw(resolution, t);
		}
	}
}

class Gc_ScreenParticleGen {
	
	// kd: Stuff to notify the manager.
	protected bool							is_done;
	
	// kd: What we're actually gonna be doing.
	protected vector2						origin;
	
	// kd: We need to know all of this for drawing.
	protected Array <TextureId>				sprite;
	// protected Array <Shape2d>				shape;
	protected Array <double>				pos_x;
	protected Array <double>				pos_y;
	protected Array <double>				vel_x;
	protected Array <double>				vel_y;
	protected Array <double>				alpha;
	protected Array <double>				vel_alpha;
	// protected Array <double>				rotation;
	// protected Array <double>				vel_rotation;
	protected Array <double>				scale;
	protected Array <double>				vel_scale;
	protected Array <int>					style;
	protected int							count;
	
	// kd: Some lifetime shenanigans.
	protected int							tics;
	
	// kd: lol
	Gc_Rng									rng;
	
	// kd: You should do init stuff here (cuz constructors aren't a thing yet).
	virtual void Trigger () {}
	
	// kd: This speaks with your manager. Avoid.
	bool IsDone () const {
		return is_done;
	}
	
	protected void Remove (int index) {
		if(0 <= index && index < count) {
			sprite.			delete(index);
			pos_x.			delete(index);
			pos_y.			delete(index);
			alpha.			delete(index);
			// rotation.		delete(index);
			scale.			delete(index);
			vel_x.			delete(index);
			vel_y.			delete(index);
			vel_alpha.		delete(index);
			// vel_rotation.	delete(index);
			vel_scale.		delete(index);
			style.			delete(index);
			// shape.			delete(index);
			count--;
		}
	}
	
	protected int Add (
	TextureId	sprite_,
	vector2		vel,
	vector2		offset			= (0, 0),
	double		alpha_			= 1,
	// double		rotation_		= 0,
	double		scale_			= 1,
	double		vel_alpha_		= 0,
	// double		vel_rotation_	= 0,
	double		vel_scale_		= 0,
	int			style_			= STYLE_ADD,
	vector2		coord_tl		= (0, 0),
	vector2		coord_bl		= (0, 1),
	vector2		coord_br		= (1, 1),
	vector2		coord_tr		= (1, 0)) {
		
		// kd: lol gzd
		sprite.push(0);
		sprite [max(0, count)] = sprite_;
		
		pos_x.			push(origin.x + offset.x);
		pos_y.			push(origin.y + offset.y);
		vel_x.			push(vel.x);
		vel_y.			push(vel.y);
		alpha.			push(alpha_);
		// rotation.		push(rotation_);
		scale.			push(scale_);
		vel_alpha.		push(vel_alpha_);
		// vel_rotation.	push(vel_rotation);
		vel_scale.		push(vel_scale_);
		style.			push(style_);
		
		/*// kd: Hm...
		Shape2d s = new("Shape2d");
		
		if(s) {
			s.Clear();
			s.PushVertex((-0.5, -0.5));
			s.PushVertex((-0.5,  0.5));
			s.PushVertex(( 0.5,  0.5));
			s.PushVertex(( 0.5, -0.5));
			
			s.pushCoord(coord_tl);
			s.pushCoord(coord_bl);
			s.pushCoord(coord_br);
			s.pushCoord(coord_tr);
			
			// kd: Not sure if this is correct! I just went agains clock sense.
			s.PushTriangle(0, 3, 1);
			s.PushTriangle(3, 2, 1);
		}
		
		shape.			push(s);*/
		
		// kd: Now wasn't that easy...
		count++;
		return count - 1;
	}
	
	// kd: So you can stick the generator somewhere.
	void SetOrigin (vector2 origin_) {
		origin = origin_;
	}
	
	// kd: This is the only thing we don't really know about, but there'll be
	// few of these ever on screen, so whatever. Set velocities and such here
	// and the old pals down there and in the manager will do the rest for you.
	
	// You don't even have to do that. If you like, you can make particles move
	// in a straight line using just add.
	virtual void NextState () {}
	
	// kd: You don't really wanna do this all the time, so I appended it.
	// You don't even have to call it.
	void FinishState () {
		tics++;
		
		for(let i = count; 0 <= --i;) {
			alpha		[i] += vel_alpha [i];
			
			if(alpha [i] <= 0) {
				Remove(i);
				continue;
			}
			
			scale		[i] += vel_scale [i];
			
			if(scale [i] <= 0) {
				Remove(i);
				continue;
			}
			
			pos_x		[i] += vel_x [i];
			pos_y		[i] += vel_y [i];
			// rotation	[i] += vel_rotation [i];
		}
	}
	
	virtual void Draw (vector2 resolution, double t = 1) const {
		for(let i = count; 0 <= --i;) {
			let scaled_res	= resolution / scale [i];
			let draw_pos_x	= pos_x [i] + t * vel_x [i];
			let draw_pos_y	= pos_y [i] + t * vel_y [i];
			
			draw_pos_x		*= scaled_res.x / resolution.x;
			draw_pos_y		*= scaled_res.y / resolution.y;
			
			Screen.DrawTexture(
				sprite [i],
				true,
				draw_pos_x,
				draw_pos_y,
				DTA_VIRTUALWIDTHF,		scaled_res.x,
				DTA_VIRTUALHEIGHTF,		scaled_res.y,
				DTA_KEEPRATIO,			true,
				DTA_CENTEROFFSET,		true,
				DTA_ALPHA,				alpha [i],
				DTA_LEGACYRENDERSTYLE,	style [i]);
		}
	}
}
