//	World0.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 "MiscHelper.h"
#include "World0.h"
#include "Random.h"

//------------------------------------------------------------------------------
World0::World0():
World(ThreeFloats(115.0f/255.0f, 115.0f/255.0f, 115.0f/255.0f),
	  ThreeFloats(182.0f/255.0f, 182.0f/255.0f, 182.0f/255.0f),
	  ThreeFloats(111.0f/255.0f, 111.0f/255.0f, 111.0f/255.0f),
	  ThreeFloats(155.0f/255.0f, 155.0f/255.0f, 155.0f/255.0f),
	  ThreeFloats(1.0f, 0.75f, 0.65f),
	  TwoFloats(640.0f, 360.0f)),
mousePos(640.0f, 360.0f),
closeToCorrect(0.0f),
done(false)
{
	float index;
	float radius;

	desatCol[0] = ThreeFloats(182.0f/255.0f, 182.0f/255.0f, 182.0f/255.0f);
	desatCol[1] = ThreeFloats(111.0f/255.0f, 111.0f/255.0f, 111.0f/255.0f);
	desatCol[2] = ThreeFloats(155.0f/255.0f, 155.0f/255.0f, 155.0f/255.0f);

	correctCol[0] = ThreeFloats(255.0f/255.0f, 108.0f/255.0f, 108.0f/255.0f);
	correctCol[1] = ThreeFloats(0.0f/255.0f, 215.0f/255.0f, 221.0f/255.0f);
	correctCol[2] = ThreeFloats(255.0f/255.0f, 151.0f/255.0f, 53.0f/255.0f);

	desatBack = ThreeFloats(115.0f/255.0f, 115.0f/255.0f, 115.0f/255.0f);
	correctBack = ThreeFloats(0.8f, 0.15f, 0.1f);

	index = Random::getGlobal().getRandomFloat() * 2.0f * NIALL_PI;
	radius = 256.0f + (Random::getGlobal().getRandomFloat() * (360.0f-256.0f));
	correctPos.x = 640.0f + sinf(index) * radius;
	correctPos.y = 360.0f + cosf(index) * radius;
}

//------------------------------------------------------------------------------
World0::~World0()
{

}

//------------------------------------------------------------------------------
void World0::mouseMove(const TwoFloats& pos)
{
	float dist;

	mousePos = pos;

	dist = mousePos.getDistance(correctPos);
	if(dist < 192.0f)
	{
		ThreeFloats tempCol[3];
		ThreeFloats tempBack;

		if(dist < 4.0f)
		{
			mousePos = correctPos;
			closeToCorrect = 1.0f;

			setRingColours(correctCol);
			setBackgroundColour(correctBack);
		}
		else
		{
			dist /= 192.0f;
			closeToCorrect = dist;
			tempCol[0] = desatCol[0] * dist;
			tempCol[0] += correctCol[0] * (1.0f-dist);

			tempCol[1] = desatCol[1] * dist;
			tempCol[1] += correctCol[1] * (1.0f-dist);

			tempCol[2] = desatCol[2] * dist;
			tempCol[2] += correctCol[2] * (1.0f-dist);

			tempBack = desatBack * dist;
			tempBack += correctBack * (1.0f-dist);

			setRingColours(tempCol);
			setBackgroundColour(tempBack);
		}
	}
	else
	{
		closeToCorrect = 0.0f;
		setRingColours(desatCol);
		setBackgroundColour(desatBack);
	}
}

//------------------------------------------------------------------------------
void World0::subclassTick()
{
	if(closeToCorrect > 0.0f)
	{
		float tempf = getOverallScale();

		if(tempf < 0.66f)
		{
			tempf += 0.0025f * closeToCorrect;
			setOverallScale(tempf);

			if(mousePos != correctPos)
			{
				float dist;

				mousePos += (correctPos-mousePos)*0.05f;

				dist = mousePos.getDistance(correctPos);
				if(dist < 4.0f)
				{
					mousePos = correctPos;
					closeToCorrect = 1.0f;

					setRingColours(correctCol);
					setBackgroundColour(correctBack);
				}
				else
				{
					ThreeFloats tempBack;
					ThreeFloats tempCol[3];

					dist /= 192.0f;
					closeToCorrect = dist;
					tempCol[0] = desatCol[0] * dist;
					tempCol[0] += correctCol[0] * (1.0f-dist);

					tempCol[1] = desatCol[1] * dist;
					tempCol[1] += correctCol[1] * (1.0f-dist);

					tempCol[2] = desatCol[2] * dist;
					tempCol[2] += correctCol[2] * (1.0f-dist);

					tempBack = desatBack * dist;
					tempBack += correctBack * (1.0f-dist);

					setRingColours(tempCol);
					setBackgroundColour(tempBack);
				}
			}
		}
	}

	if(getOverallScale() >= 0.66f)
		done = true;
}
