//	PickupPendulum.cpp - Pendulum Pickup.
//  ----------------------------------------------------------------------------
//	This file is part of 'GameTemplate', template code for a game.
//	Copyright (C) 2011  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 "drawing/Drawer.h"
#include "PickupPendulum.h"
#include "DebugHeaders.h"
#include "GameCounter.h"
#include "../Tone.h"

#include <sstream>

using namespace std;

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
PickupPendulum::PickupPendulum():
Pickup(L"Pendulum")
{

}

//------------------------------------------------------------------------------
PickupPendulum::~PickupPendulum()
{

}

//------------------------------------------------------------------------------
shared_ptr<OverrideBehaviour> PickupPendulum::getBehaviour() const
{
	return shared_ptr<OverrideBehaviour>(NiallNew BehaviourPendulum(L"pickup-pendulum"));
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BehaviourPendulum::BehaviourPendulum(const wstring& icon):
OverrideBehaviour(icon),
pickupIcon(icon),
index(0.0f),
sineX(640.0f),
periodsRemaining(3),
needsInit(true)
{

}

//------------------------------------------------------------------------------
BehaviourPendulum::~BehaviourPendulum()
{

}

//------------------------------------------------------------------------------
bool BehaviourPendulum::moveTone(Tone& tone,
								 std::vector<TonePtr>& allTones,
								 float globalSpeed)
{
	int i;
	float tempf;
	bool retval = false;
	const float halfWidth = (float)SineWidth/2.0f;
	const float oldRailDist = (592.0f/(float)(tone.prevNumRails-1));
	const float newRailDist = (592.0f/(float)(tone.numRails-1));
	const float railDist = (tone.railInterp * newRailDist) + ((1.0f-tone.railInterp) * oldRailDist);

	if(needsInit)
	{
		if((tone.position.x > halfWidth) &&
		   (tone.position.x < (1280.0f-halfWidth)))
		{
			index = 0.0f;
			sineX = tone.position.x;
		}
		else if(tone.position.x < halfWidth)
		{
			index = 1.570796f; //pi/2
			sineX = tone.position.x + halfWidth;
		}
		else if(tone.position.x > (1280.0f-halfWidth))
		{
			index = 4.71238898f;//pi + pi/2
			sineX = tone.position.x - halfWidth;
		}

		needsInit = false;
	}

	if(!tone.isPaused)
	{
		//Update dot trail.
		for(i=(tone.TrailSize-1);i>0;--i)
			tone.prevPos[i] = tone.prevPos[i-1];
		tone.prevPos[0] = tone.position;

		index += 0.01f * fabsf(tone.speed);
		if(index > 6.283185f)
		{
			index = 0.0f;
			--periodsRemaining;
			if(periodsRemaining <= 0)
				retval = true;
		}

		tone.position.x = sineX - (sinf(index) * halfWidth);

		if(tone.newRail != tone.rail)
		{
			tempf = (cosf(tone.movingRail)+1.0f)*0.5f; //1 -> 0

			if(tone.newRail < tone.rail)
			{
				tempf *= railDist;
				tone.position.y = 64 + (tone.newRail * railDist) + tempf;
			}
			else
			{
				tempf = 1.0f - tempf;
				tempf *= railDist;
				tone.position.y = 64 + (tone.rail * railDist) + tempf;
			}

			tone.movingRail += 0.0125f * fabsf(tone.speed);
			if(tone.movingRail > 3.14159265f)
			{
				tone.rail = tone.newRail;
				tone.position.y = 64 + (tone.rail * railDist);
			}
		}
		else if(tone.railInterp < 1.0f)
		{
			tone.railInterp += 0.02f;

			if(tone.railInterp >= 1.0f)
			{
				tone.railInterp = 1.0f;
				tone.prevNumRails = tone.numRails;
			}

			tone.position.y = 64 + (tone.rail * railDist);
		}
	}

	return retval;
}

//------------------------------------------------------------------------------
void BehaviourPendulum::drawExtraInformation(DrawerPtr& d,
											 float timestepInterp,
											 const Tone& tone)
{
	//Draw the icon.
	d->drawImage(pickupIcon, tone.position-64.0f);
}
