//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 CompletedLevel(data)
{
	this.timer = 0;
	this.spawnedCount = 0;
	this.completeStars = data.completeStars;
	this.kGapBetweenStars = 10;
	
	this.Update = function()
	{
		if(this.timer == 0)
		{
			var firstPlayer = gPlayerSystem.players[0];
			gParticleSystem.Spawn("CompleteParticle", {player:firstPlayer, starIndex:this.spawnedCount + 1});
			this.spawnedCount++;
			if(this.spawnedCount == this.completeStars)
			{
				return true;
			}
			else
			{
				this.timer = this.kGapBetweenStars;
			}
		}
		else
		{
			this.timer--;
		}
		return false;
	}
	
	this.Draw = function()
	{
	
	}
}

gParticleSystem.Register("CompletedLevel", CompletedLevel);

function CompleteParticle(data)
{
	this.index = data.starIndex;
	
	var angleIndex = gRulesSystem.kStarCount + 1 - data.starIndex;
	this.angle = (Math.PI * (angleIndex/gRulesSystem.kStarCount)) - (Math.PI);
	
	var x = (data.player.x * kTileSize) + kTileSizeD2 + (Math.sin(this.angle) * kTileSizeD2);
	var y = (data.player.y * kTileSize) + kTileSizeD2 - (Math.cos(this.angle) * kTileSizeD2);
	this.p1 = new Vec2(x, y);
	

	var tx = gObjectivesOverlay.GetStarX(data.starIndex) - gDraw.horizontalCenterOffset;
	var ty = gObjectivesOverlay.GetStarY(data.starIndex) - gDraw.verticalCenterOffset;
	this.p2 = new Vec2(tx, ty);

	switch(this.index)
	{
		case 1:
			this.cp1 = new Vec2(this.p1.x, this.p2.y);
			break;
		case 2:
			this.cp1 = new Vec2((this.p1.x + this.p2.x) * 0.5, (this.p1.y + this.p2.y) * 0.5);
			break;
		case 3:
			this.cp1 = new Vec2(this.p2.x, this.p1.y);
			break;
	}

	this.cp2 = new Vec2(tx, ty);
	
	this.pos = new Vec2();
	
	this.rotation = this.angle;
	this.time = 0;
	this.kTotalTime = 45;
	
	this.Update = function()
	{
		var f = Math.pow(this.time/this.kTotalTime, 3)
		var pos = this.GetPositionOnBezier(this.p1.x, this.p1.y, this.cp1.x, this.cp1.y, this.cp2.x, this.cp2.y, this.p2.x, this.p2.y, f)
		this.pos.x = pos.x
		this.pos.y = pos.y
		this.rotation += Math.PI * 0.05;
		gParticleSystem.Spawn("CompleteParticleTrail", this);
		this.time++;
		if (this.time == this.kTotalTime)
		{
			gObjectivesOverlay.StarArrived(this.index);
			gCompleteOverlay.StarArrived();
			return true;
		}
		
		return false;
	}

	this.Draw = function()
	{

		/*
		//DEBUG
		gDraw.foreparticles.beginPath();
		gDraw.foreparticles.strokeStyle = "rgb(255, 255, 255)"
		gDraw.foreparticles.moveTo(this.p1.x, this.p1.y);
		
		gDraw.foreparticles.bezierCurveTo(this.cp1.x, this.cp1.y, this.cp2.x, this.cp2.y, this.p2.x, this.p2.y)
		gDraw.foreparticles.lineWidth = 0.1;
		gDraw.foreparticles.stroke();
		//DEBUG
		*/
	}
	
	this.GetPositionOnBezier = function(x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2, f)
	{
		var B0 = Math.pow((1-f), 3)
		var B1 = 3 * f * Math.pow((1-f),2)
		var B2 = 3 * Math.pow(f,2) * (1-f)
		var B3 = Math.pow(f,3)
		
		var pos = new Vec2();
		pos.x = (B0 * x1) + (B1 * cp1x) + (B2 * cp2x) + (B3 * x2)
		pos.y = (B0 * y1) + (B1 * cp1y) + (B2 * cp2y) + (B3 * y2)
		return pos;
	}
}

gParticleSystem.Register("CompleteParticle", CompleteParticle);

function CompleteParticleTrail(data)
{
	this.pos = new Vec2(data.pos.x, data.pos.y);
	this.rotation = data.rotation;
	
	this.timer = 0;
	this.kTime = 15;
	
	this.Update = function()
	{
		if(this.timer > this.kTime)
		{
			return true;
		}
		else
		{
			this.timer++;
			return false;
		}
	}

	this.Draw = function()
	{
		/*
		gDraw.foreparticles.beginPath();
		var alpha = 1 - (this.timer / this.kTime);
		gDraw.foreparticles.fillStyle = "rgba(255, 255, 255, " + alpha + ")"
		var radius = kTileSize * 0.2 * alpha;
		gDraw.foreparticles.fillStar(this.pos.x, this.pos.y, radius, this.rotation);
		*/
		gDraw.foreparticles.save();
		var render = gParticleSystem.PreRender("CompleteParticle");
		var alpha = Math.max(1 - (this.timer / this.kTime), 0);
		var x = this.pos.x;
		var y = this.pos.y;
		gDraw.foreparticles.globalAlpha = alpha;
		gDraw.foreparticles.translate(x, y);
		gDraw.foreparticles.rotate(this.rotation);
		gDraw.foreparticles.drawImage(render, -render.width/2, -render.height/2);
		gDraw.foreparticles.restore();
	}
}


function CompleteParticlePreRender()
{
	var radius = kTileSize * 0.2;
	var size = radius * 2.1;
	var canvas = document.createElement('canvas');
	canvas.width = size;
	canvas.height = size;
	var context = canvas.getContext('2d');
	
	context.fillStyle = "rgb(255, 255, 255)"
	context.beginPath();
	context.Star = Star;
	context.fillStar = fillStar;
	context.fillStar(radius, radius, radius, 0);

	gParticleSystem.AddPreRender("CompleteParticle", canvas);
}
gParticleSystem.AddPreRenderFunc(CompleteParticlePreRender);
gParticleSystem.Register("CompleteParticleTrail", CompleteParticleTrail);