//	WorkerBehaviour.cpp - The default behaviour for the Prole class.
//  ----------------------------------------------------------------------------
//	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 "WorkerBehaviour.h"
#include "Random.h"

#include <cmath>

//------------------------------------------------------------------------------
WorkerBehaviour::WorkerBehaviour():
ProleBehaviour()
{
	xDir = Random::getGlobal().getRandomFloat()-0.5f;
	yDir = Random::getGlobal().getRandomFloat()-0.5f;
	xIntendedDir = Random::getGlobal().getRandomFloat()-0.5f;
	yIntendedDir = Random::getGlobal().getRandomFloat()-0.5f;

	changeCount = (int)(Random::getGlobal().getRandomFloat() * 32.0f);
}

//------------------------------------------------------------------------------
WorkerBehaviour::~WorkerBehaviour()
{
	
}

//------------------------------------------------------------------------------
void WorkerBehaviour::act(Prole *p, GamePlay *gp)
{
	float x;
	float y;
	float dist;

	//Make sure the Prole is white.
	p->setColour(1.0f, 1.0f, 1.0f);
	p->setLifeFade(true);
	p->setTrail(false);

	x = p->getX();
	y = p->getY();

	//Change direction if changeCount says so.
	if(!changeCount)
	{
		xIntendedDir = Random::getGlobal().getRandomFloat()-0.5f;
		yIntendedDir = Random::getGlobal().getRandomFloat()-0.5f;
		dist = 1.0f/sqrtf((xIntendedDir*xIntendedDir)+(yIntendedDir*yIntendedDir));
		xIntendedDir *= dist;
		yIntendedDir *= dist;

		changeCount = (int)(Random::getGlobal().getRandomFloat() * 32.0f);
	}
	else
		--changeCount;

	//Update current direction if necessary.
	if((xDir != xIntendedDir) || (yDir != yIntendedDir))
	{
		xDir += (xIntendedDir-xDir)*(1.0f/128.0f);
		yDir += (yIntendedDir-yDir)*(1.0f/128.0f);

		//No need to update when we're close enough.
		if(fabsf(xIntendedDir-xDir) < 0.01f)
			xDir = xIntendedDir;
		if(fabsf(yIntendedDir-yDir) < 0.01f)
			yDir = yIntendedDir;

		dist = 1.0f/sqrtf((xDir*xDir)+(yDir*yDir));
		xDir *= dist;
		yDir *= dist;
	}

	//Update position.
	x += xDir * p->getLife();
	y += yDir * p->getLife();

	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);
}
