//	Capitalist.cpp - The capitalist 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 "Capitalist.h"
#include "Random.h"
#include "Images.h"
#include "states/GamePlay.h"
#include "NiallsDrawingLib/Drawer.h"
#include "NiallsDrawingLib/ImageLoader.h"

#include <cmath>

//------------------------------------------------------------------------------
uint32_t Capitalist::image = 0;

//------------------------------------------------------------------------------
Capitalist::Capitalist():
xPos(0.0f),
yPos(0.0f),
xDir(0.0f),
yDir(1.0f),
inertia(0.0f),
life(1.0f),
gamePlay(0)
{
	waveCount = (int)(Random::getGlobal().getRandomFloat() * 300);
}

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

//------------------------------------------------------------------------------
void Capitalist::move()
{
	int i;

	//Capitalists' life gradually regenerates.
	if((life > 0.0f) && (life < 1.0f))
	{
		life += 0.0002f;
		if(life > 1.0f)
			life = 1.0f;
	}

	if((inertia > 0.0f) && (life > 0.0f))
	{
		float x, y;
		float dist;
		Capitalist *capitalists = gamePlay->getCapitalists();

		xPos += (xDir * inertia);
		yPos += (yDir * inertia);
		inertia -= 0.05f;

		if(xPos < 0.0f)
			xPos = 0.0f;
		else if(xPos > (1024.0f-64.0f))
			xPos = 1024.0f-64.0f;
		if(yPos < 0.0f)
			yPos = 0.0f;
		else if(yPos > (768.0f-64.0f))
			yPos = 768.0f-64.0f;

		//Make sure we don't collide with other capitalists.
		for(i=0;i<GamePlay::NumCapitalists;++i)
		{
			if(&(capitalists[i]) != this)
			{
				x = capitalists[i].getX() - xPos;
				y = capitalists[i].getY() - yPos;

				if(((x*x)+(y*y)) < 1024.0f) //1024 = 32x32
				{
					dist = 1.0f/sqrtf((x*x)+(y*y));
					x *= dist;
					y *= dist;

					xDir = x;
					yDir = y;
					inertia = 2.0f;
				}
			}
		}
	}
}

//------------------------------------------------------------------------------
void Capitalist::draw(Drawer *d)
{
	if(!image)
	{
		ImageLoader imLoad;
		ImageData data;

		data = imLoad.loadImage(Images::capitalist_png, Images::capitalist_pngSize);
		image = d->loadImage(data.data, data.width, data.height);
	}

	if((xPos == -128) && (yPos == -128))
		return;

	d->drawImage(image, xPos, yPos, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, life);

	//Add a debilitation wave to the world.
	if(life > 0.0f)
	{
		--waveCount;
		if(waveCount <= 0)
		{
			waveCount = 300;
			if(gamePlay)
				gamePlay->addWave(xPos-224, yPos-224, life); //Centre on this image.
		}
	}
}

//------------------------------------------------------------------------------
void Capitalist::setPosition(int x, int y)
{
	xPos = x;
	yPos = y;
}

//------------------------------------------------------------------------------
bool Capitalist::reduceLife(float val, float x, float y)
{
	bool retval = false;

	xDir = x;
	yDir = y;
	inertia = 35.0f * val; //val is in the range 0->0.02: x50 scales it to 0->1.

	life -= val;
	if(life <= 0.0f)
	{
		xPos = -128;
		yPos = -128;
		retval = true;
	}

	return retval;
}

//------------------------------------------------------------------------------
void Capitalist::regenerate()
{
	if((xPos > 0.0f) && (yPos > 0.0f))
		life = 1.0f;
}

//------------------------------------------------------------------------------
void Capitalist::setGamePlay(GamePlay *gp)
{
	gamePlay = gp;
}
