//	World5.cpp - One of the worlds.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  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 "World5.h"
#include "Random.h"

//------------------------------------------------------------------------------
World5::World5():
World(ThreeFloats(97.0f/255.0f, 154.0f/255.0f, 30.0f/255.0f),
	  ThreeFloats(3.0f/255.0f, 0.0f/255.0f, 51.0f/255.0f),
	  ThreeFloats(26.0f/255.0f, 53.0f/255.0f, 190.0f/255.0f),
	  ThreeFloats(186.0f/255.0f, 204.0f/255.0f, 227.0f/255.0f),
	  ThreeFloats(1.0f, 0.78f, 0.7f),
	  TwoFloats(4000.0f, -800.0f)),
calm(0.0f),
calmCount(0),
lastPos(640.0f, 360.0f),
jumpCount(0),
centrePos(640.0f, 360.0f),
done(false)
{
	calcJumpPos();
	currentPos = jumpPos;
}

//------------------------------------------------------------------------------
World5::~World5()
{

}

//------------------------------------------------------------------------------
void World5::mouseMove(const TwoFloats& pos)
{
	if(lastPos.getDistance(pos) < 4.0f)
	{
		calmCount = 30;

		calm += 0.005f;
		if(calm >= 1.0f)
		{
			done = true;
			setPosition(TwoFloats(4000.0f, -800.0f));
		}
	}

	lastPos = pos;
}

//------------------------------------------------------------------------------
void World5::subclassTick()
{
	float tempf;
	TwoFloats tempPos;

	if(done)
		return;

	if(!calmCount && (calm > 0.0f))
	{
		calm -= 0.005f;
		if(calm < 0.0f)
			calm = 0.0f;
	}

	if(currentPos != jumpPos)
	{
		currentPos += ((jumpPos-currentPos) * 0.1f);
		if(currentPos.getDistance(jumpPos) < 1.0f)
			currentPos = jumpPos;
	}

	tempf = 0.05f + (calm*0.61f);
	setOverallScale(tempf);

	tempPos = currentPos * (1.0f-calm);
	tempPos += centrePos * calm;
	tempPos += TwoFloats(4000.0f-640.0f, -800.0f-360.0f);
	setPosition(tempPos);

	--jumpCount;
	if(jumpCount <= 0)
	{
		jumpCount = 10 + Random::getGlobal().getRandomInt(30);
		calcJumpPos();
	}

	if(calmCount)
		--calmCount;
}

//------------------------------------------------------------------------------
void World5::calcJumpPos()
{
	jumpPos.x = Random::getGlobal().getRandomFloat() * 1280.0f;
	jumpPos.y = Random::getGlobal().getRandomFloat() * 720.0f;
	while(jumpPos.getDistance(centrePos) < 256.0f)
	{
		jumpPos.x = Random::getGlobal().getRandomFloat() * 1280.0f;
		jumpPos.y = Random::getGlobal().getRandomFloat() * 720.0f;
	}
}
