//	Pickup2xSpeed.cpp - 2x speed 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 "../TonesSounder.h"
#include "drawing/Drawer.h"
#include "Pickup2xSpeed.h"
#include "DebugHeaders.h"
#include "GameCounter.h"
#include "../Tone.h"

#include <sstream>

using namespace std;

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Pickup2xSpeed::Pickup2xSpeed():
Pickup(L"Double Speed")
{

}

//------------------------------------------------------------------------------
Pickup2xSpeed::~Pickup2xSpeed()
{

}

//------------------------------------------------------------------------------
shared_ptr<OverrideBehaviour> Pickup2xSpeed::getBehaviour() const
{
	return shared_ptr<OverrideBehaviour>(NiallNew Behaviour2xSpeed(L"pickup-2xSpeed"));
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Behaviour2xSpeed::Behaviour2xSpeed(const wstring& icon):
OverrideBehaviour(icon),
pickupIcon(icon)
{

}

//------------------------------------------------------------------------------
Behaviour2xSpeed::~Behaviour2xSpeed()
{

}

//------------------------------------------------------------------------------
bool Behaviour2xSpeed::moveTone(Tone& tone,
								std::vector<TonePtr>& allTones,
								float globalSpeed)
{
	bool retval = false;

	int i;
	float tempf;
	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(!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;

		//Update dot position.
		tone.position.x -= tone.speed * 2.0f * globalSpeed;
		if((((tone.speed > 0.0f) && (tone.position.x < 0.0f)) ||
			((tone.speed < 0.0f) && (tone.position.x > 1280.0f))) &&
			!tone.noteOff)
		{
			wstringstream tempstr;

			tone.getSounder()->noteOff(tone.synthIndex, tone.collided);
			tone.noteOff = true;

			tempstr << tone.getName() << L" Survived";
			tone.getGameCounter()->incrementCount(tempstr.str(),
												  (int)((float)tone.getScore() * 2.0f * (0.5f+globalSpeed)));
		}

		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 Behaviour2xSpeed::drawExtraInformation(DrawerPtr& d,
											float timestepInterp,
											const Tone& tone)
{
	//Draw the icon.
	d->drawImage(pickupIcon, tone.position-64.0f);
}
