//	PoliceBehaviour.cpp - The behaviour of police officers.
//  ----------------------------------------------------------------------------
//	This file is part of 'The Future Will Only Be Dystopian if Capitalism
//	Remains Undefeated', a game.
//	Copyright (C) 2009  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "PoliceBehaviour.h"
#include "WorkerBehaviour.h"
#include "states/GamePlay.h"
#include "sound/Sounds.h"

#include <cmath>

//------------------------------------------------------------------------------
int PoliceBehaviour::numPolice = 0;

//------------------------------------------------------------------------------
PoliceBehaviour::PoliceBehaviour():
lightVal(0.0f)
{
	++numPolice;
}

//------------------------------------------------------------------------------
PoliceBehaviour::~PoliceBehaviour()
{
	--numPolice;
}

//------------------------------------------------------------------------------
void PoliceBehaviour::act(Prole *p, GamePlay *gp)
{
	int i;
	float dist;
	float pX, pY;
	float blueVal;
	float x = p->getX();
	float y = p->getY();
	float xDir = p->getDirX();
	float yDir = p->getDirY();
	Prole *proles = gp->getProles();

	//Police Proles are weaker than normal Proles.
	p->setStrength(0.25f);

	//Flash the Prole blue like a police light.
	lightVal += 0.025f;
	if(lightVal > 1.0f)
		lightVal -= 1.0f;
	blueVal = (sinf(lightVal * 6.283185307179586476925286766559f)*0.25f) + 0.75f;
	p->setColour(0.0f, 0.0f, blueVal);
	p->setLifeFade(false);
	p->setTrail(true);

	//Move the Prole in the direction it's heading.
	x += xDir * 4.0f;
	y += yDir * 4.0f;

	if(x < 0.0f)
	{
		x = 0.0f;
		xDir = -xDir;
	}
	else if(x > (1024.0f-32.0f))
	{
		x = 1024.0f-32.0f;
		xDir = -xDir;
	}
	if(y < 0.0f)
	{
		y = 0.0f;
		yDir = -yDir;
	}
	else if(y > (768.0f-32.0f))
	{
		y = 768.0f-32.0f;
		yDir = -yDir;
	}

	p->setPosition(x, y);
	p->setDirection(xDir, yDir);

	//Now check if we've collided with any Revolutionaries, un-revol them.
	for(i=0;i<GamePlay::NumProles;++i)
	{
		if(&(proles[i]) != p)
		{
			pX = proles[i].getX() - x;
			pY = proles[i].getY() - y;
			dist = (pX*pX) + (pY*pY);
			if(dist < 256.0f) //256 = 16*16
			{
				if(i == 0) //0 is always the player.
				{
					gp->killPlayer();

					Sounds::getInstance().playSound(Sounds::PoliceCollision);
				}
				else if(proles[i].getRevolutionary())
				{
					WorkerBehaviour *b = new WorkerBehaviour();

					if(proles[i].getLife() > 0.2f)
						proles[i].reduceLife(0.2f);
					proles[i].backToWorker(gp);

					Sounds::getInstance().playSound(Sounds::PoliceCollision);
				}
			}
		}
	}
}
