//	Snowflakes.cpp - Class which draws snowflakes if the game is played during
//				     December.
//  ----------------------------------------------------------------------------
//	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 "drawing/Drawer.h"
#include "Snowflakes.h"
#include "MiscHelper.h"
#include "Random.h"

#include <ctime>

//------------------------------------------------------------------------------
Snowflakes::Snowflakes():
numSnowflakes(256),
december(false)
{
	int i;
	time_t tim;
	std::tm *timeStruct;

	tim = time(0);
	timeStruct = gmtime(&tim);

	if(timeStruct->tm_mon == 11)
		december = true;

	images[0] = L"Snowflake0";
	images[1] = L"Snowflake1";
	images[2] = L"Snowflake2";
	imagePivots[0] = TwoFloats(3.5f, 4.0f);
	imagePivots[1] = TwoFloats(5.0f, 4.5f);
	imagePivots[2] = TwoFloats(3.5f, 4.0f);

	for(i=0;i<NumSnowflakes;++i)
	{
		snowflakes[i].position.x = Random::getGlobal().getRandomFloat();
		snowflakes[i].position.y = Random::getGlobal().getRandomFloat();
		snowflakes[i].image = Random::getGlobal().getRandomInt(3);
		snowflakes[i].rotation = Random::getGlobal().getRandomFloat();
		snowflakes[i].sinIndex = Random::getGlobal().getRandomFloat();
		snowflakes[i].sinMag = 2.0f + (Random::getGlobal().getRandomFloat() * 30.0f);
		snowflakes[i].fallInc = 0.0005f + (Random::getGlobal().getRandomFloat() * 0.0015f);
		if(Random::getGlobal().getRandomFloat() > 0.5f)
			snowflakes[i].rotSpeed = -0.001f - (Random::getGlobal().getRandomFloat() * 0.004f);
		else
			snowflakes[i].rotSpeed = 0.001f + (Random::getGlobal().getRandomFloat() * 0.004f);
		snowflakes[i].sinInc = 0.005f + (Random::getGlobal().getRandomFloat() * 0.005f);
	}
}

//------------------------------------------------------------------------------
Snowflakes::~Snowflakes()
{

}

//------------------------------------------------------------------------------
void Snowflakes::update()
{
	int i;

	for(i=0;i<numSnowflakes;++i)
	{
		snowflakes[i].position.y += snowflakes[i].fallInc;
		if(snowflakes[i].position.y > 1.0f)
		{
			snowflakes[i].position.x = Random::getGlobal().getRandomFloat();
			snowflakes[i].position.y = 0.0f;
		}

		snowflakes[i].rotation += snowflakes[i].rotSpeed;
		if(snowflakes[i].rotation > 1.0f)
			snowflakes[i].rotation -= 1.0f;

		snowflakes[i].sinIndex += snowflakes[i].sinInc;
		if(snowflakes[i].sinIndex > 1.0f)
			snowflakes[i].sinIndex -= 1.0f;
	}
}

//------------------------------------------------------------------------------
void Snowflakes::draw(shared_ptr<Drawer>& d)
{
	int i;
	TwoFloats tempPos;
	const TwoFloats drawSize(d->getDrawingAreaSize() + 20.0f);

	for(i=0;i<numSnowflakes;++i)
	{
		tempPos = -10.0f;
		tempPos += snowflakes[i].position * drawSize;
		tempPos.x += sinf(snowflakes[i].sinIndex * 2.0f * NIALL_PI) * snowflakes[i].sinMag;
		tempPos -= imagePivots[snowflakes[i].image];

		d->drawRotImage(images[snowflakes[i].image],
						tempPos,
						snowflakes[i].rotation * 2.0f * NIALL_PI,
						imagePivots[snowflakes[i].image],
						1.0f,
						1.0f,
						0.75f);
	}
}

//------------------------------------------------------------------------------
void Snowflakes::setNumSnowflakes(float val)
{
	numSnowflakes = (int)(val * 2048.0f);
}
