//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014
var gParticleSystem = new ParticleSystem();

function ParticleSystem()
{
	this.types = new Array();
	this.particles = new Array();
	this.preRenders = new Array();
	this.preRenderFuncs = new Array();
	
	this.Init = function()
	{
	}
	
	this.Clear = function()
	{
		this.particles = new Array();
		this.DrawClear();
	}
	
	this.Update = function()
	{
		for(var i = 0; i < this.particles.length; i++)
		{
			if(this.particles[i].Update())
			{
				this.particles.splice(i, 1);
				i--;
			}
		}
	}
	
	this.Resize = function()
	{
		for(var key in this.preRenderFuncs)
		{
			if(this.preRenderFuncs.hasOwnProperty(key))
			{
				this.preRenderFuncs[key]();
			}
		}
	}
	
	this.DrawClear = function()
	{
		gDraw.particles.clearRect(-gDraw.horizontalCenterOffset, -gDraw.verticalCenterOffset, gDraw.canvas.width, gDraw.canvas.height);
		gDraw.foreparticles.clearRect(-gDraw.horizontalCenterOffset, -gDraw.verticalCenterOffset, gDraw.canvas.width, gDraw.canvas.height);
	}
	
	this.Draw = function()
	{
		for(var i = 0; i < this.particles.length; i++)
		{
			this.particles[i].Draw();
		}
	}
	
	this.Register = function(name, classType)
	{
		this.types[name] = classType;
	}
	
	this.AddPreRenderFunc = function(func)
	{
		this.preRenderFuncs.push(func);
		func();
	}
	
	this.AddPreRender = function(name, context)
	{
		this.preRenders[name] = context;
	}
	
	this.PreRender = function(name)
	{
		return this.preRenders[name];
	}
	
	this.Spawn = function(name, parameters)
	{
		var newParticle = new this.types[name](parameters);
		this.particles.push(newParticle);
	}

}