//	SineBullet.cpp - Class which alters a Bullet's direction in a sine motion.
//  ----------------------------------------------------------------------------
//	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 "GamePlayState.h"
#include "MiscHelper.h"
#include "SineBullet.h"
#include "Random.h"

#include <cmath>

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BulletDirectionCreator<SineBullet> SineBullet::creator;
float SineBullet::sineTable[TableSize];
bool SineBullet::tableReady = false;

//------------------------------------------------------------------------------
SineBullet::SineBullet():
index(Random::getGlobal().getRandomFloat())
{
	if(!tableReady)
	{
		int i;

		for(i=0;i<TableSize;++i)
			sineTable[i] = sinf(((float)i/(float)TableSize) * 2.0f * NIALL_PI);

		tableReady = true;
	}
}

//------------------------------------------------------------------------------
SineBullet::~SineBullet()
{

}

//------------------------------------------------------------------------------
void SineBullet::alterDirection(TwoFloats& dir,
								Bullet& bullet,
								GamePlayState& state)
{
	int i, j;
	float tempf;

	tempf = (index * (float)TableSize);

	i = ((int)tempf) % TableSize;
	j = (i+1) % TableSize;

	tempf -= (float)((int)tempf);

	tempf = sineTable[i] + (tempf * (sineTable[j] - sineTable[i]));

	dir += dir.perpendicular() * tempf * 0.66f;
	dir = dir.normal();

	index += 0.066f;
	if(index >= 1.0f)
		index -= 1.0f;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void SineBulletCreator::apply(GamePlayState& state)
{
	state.getBulletCreator().addDirectionModifier(&SineBullet::creator);
}
