//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
function DeleteBlockParticle(block)
{
	for(var i = 0; i < 10; i++)
	{
		gParticleSystem.Spawn("DeletedBlockParticle",  {block: block, type: i < 3 ? 1 : 0});
	}
	//gParticleSystem.Spawn("DeletedCenterParticle", block);

	this.Update = function(){return true;}
	this.Draw = function() {}
}

gParticleSystem.Register("DeleteBlockParticle", DeleteBlockParticle);

function DeletedBlockParticle(data)
{
	this.x = (data.block.x * kTileSize) + (kTileSizeD2) + data.block.ox
	this.y = (data.block.y * kTileSize) + (kTileSizeD2) + data.block.oy

	this.type = data.type;	
	
	var direction = 0;
	if(data.block.dx != 0 || data.block.dy != 0)
	{
		var PId4 = Math.PI/4;
		var dir = Math.atan2(-data.block.dy, data.block.dx) + Math.PI/2;
		direction = Math.randomRange(dir-PId4, dir+PId4);
	}
	else
	{
		direction = Math.randomRange(0, Math.PI * 2);
	}
	
	var speed = this.type == 0 ? Math.randomRange(2, 6) : Math.randomRange(5, 10);
	this.dx = Math.sin(direction) * speed;
	this.dy = Math.cos(direction) * speed;
	
	this.x += this.dx * 3;
	this.y += this.dy * 3;
	
	this.deleteTime = 0;
	this.lifetime = Math.randomRange(20, 80);
	
	this.sizeScale = this.type == 1 ? Math.randomRange(0.2, 0.8) : Math.randomRange(0.05, 0.2);
	
	this.rgb = gStyles.GetBlockRGB(data.block.type, data.block.value, false);
	
	this.Update = function()
	{
		this.x += this.dx;
		this.y += this.dy;
		var decel = 0.97;
		this.dx *= decel;
		this.dy *= decel;
		if(this.deleteTime >= this.lifetime)
		{
			return true;
		}
		else
		{
			this.deleteTime++;
		}
		
		return false;
	}
	
	this.Draw = function()
	{
		switch(this.type)
		{
			case 0:
				this.DrawSphere();
				break;
			case 1:
				this.DrawLine();
				break;
		}
	}
	
	this.GetFraction = function()
	{
		return Math.max(0, 1 - this.deleteTime/this.lifetime);
	}

	this.DrawLine = function()
	{
		gDraw.foreparticles.beginPath();
		var fraction = this.GetFraction();
		var alpha = (fraction * 0.5) + 0.3;
		var size = kTileSize * fraction * this.sizeScale;
		gDraw.foreparticles.lineWidth = 1;
		gDraw.foreparticles.strokeStyle = gStyles.GetColorAlpha(this.rgb, alpha);
		gDraw.foreparticles.moveTo(this.x, this.y);
		var sumdxdy = Math.abs(this.dx) + Math.abs(this.dy);
		var ndx = this.dx/sumdxdy;
		var ndy = this.dy/sumdxdy;
		gDraw.foreparticles.lineTo(this.x + (ndx * size), this.y + (ndy * size));
		gDraw.foreparticles.stroke();
	}
	
	this.DrawSphere = function()
	{
		gDraw.particles.beginPath();
		var fraction = this.GetFraction();
		var alpha = (fraction * 0.5) + 0.3;
		var size = kTileSize * fraction * this.sizeScale;
		gDraw.particles.fillStyle = gStyles.GetColorAlpha(this.rgb, alpha);
		
		gDraw.particles.arc(this.x, this.y, size, 0, Math.PI * 2, false);
		gDraw.particles.fill();
	}
}

gParticleSystem.Register("DeletedBlockParticle", DeletedBlockParticle);


var gDeletedCenterParticleTime = 5;

function DeletedCenterParticle(block)
{
	this.x = (block.x * kTileSize) + (kTileSizeD2) + block.ox
	this.y = (block.y * kTileSize) + (kTileSizeD2) + block.oy	
	
	this.deleteTime = 0;
	
	this.rgb = new RGB(255, 255, 255);
	
	this.Update = function()
	{
		if(this.deleteTime >= gDeletedCenterParticleTime)
		{
			return true;
		}
		else
		{
			this.deleteTime++;
		}
		
		return false;
	}
	
	this.Draw = function()
	{
		gDraw.particles.beginPath();
		var fraction = (this.deleteTime/gDeletedCenterParticleTime);
		var alpha = (fraction * 0.5) + 0.3;
		var size = kTileSize * fraction * 0.5;
		gDraw.particles.fillStyle = gStyles.GetColorAlpha(this.rgb, alpha);
		
		gDraw.particles.arc(this.x, this.y, size, 0, Math.PI * 2, false);
		gDraw.particles.fill();
	}
}

gParticleSystem.Register("DeletedCenterParticle", DeletedCenterParticle);

function ScoreParticle(data)
{
	this.x = (data.block.x * kTileSize) + kTileSizeD2 + data.block.ox;
	this.y = (data.block.y * kTileSize) + data.block.oy;
	
	this.text = "+" + data.score;
	this.time = 0;
	this.kLifetime = gBlockDeleteTime/2;
	this.rgb = gStyles.GetBlockRGB(data.block.type, data.block.value, false);
	if(data.chain > 0)
	{
		this.chainText = data.chain + " Chain";
	}
	else
	{
		this.chainText = null;
	}
	
	this.Update = function()
	{
		if(this.time == this.kLifetime - 1)
		{
			return true;
		}
		else
		{
			this.y--;
			this.time++;
			
			return false;
		}
	}

	this.Draw = function()
	{
		gDraw.particles.font = GetFont(GetFontScale(22));
		gDraw.particles.textAlign	 = 'center';
		gDraw.particles.textBaseline	 = 'middle';
		
		var alpha = Math.cos((this.time/this.kLifetime) * Math.PI * 0.5);
		var fillStyle = gStyles.GetColorAlpha(this.rgb, alpha);
		gDraw.particles.fillTextShadow(this.text, this.x, this.y, fillStyle, "rgb(0, 0, 0)");
		
		if(this.chainText)
		{
			gDraw.particles.font = GetFont(GetFontScale(12));
			gDraw.particles.fillTextShadow(this.chainText, this.x, this.y - 22, fillStyle, "rgb(0, 0, 0)");
		}
	}
}

gParticleSystem.Register("ScoreParticle", ScoreParticle);

