//	SpiralBullet.cpp - Class which alters a Bullet's direction in a spiral
//					   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 "SpiralBullet.h"
#include "MiscHelper.h"

#include <cmath>

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

//------------------------------------------------------------------------------
SpiralBullet::SpiralBullet():
strength(0.0f),
strengthInc(0.0f)
{
	if(!tableReady)
	{
		int i;

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

		tableReady = true;
	}
}

//------------------------------------------------------------------------------
SpiralBullet::~SpiralBullet()
{

}

//------------------------------------------------------------------------------
void SpiralBullet::alterDirection(TwoFloats& dir,
								  Bullet& bullet,
								  GamePlayState& state)
{
	float tempf;
	TwoFloats tempDir;
	const TwoFloats origin;

	tempf = origin.getAngle(dir);
	tempf += strength;
	dir.x = sinf(tempf);
	dir.y = cosf(tempf);

	strength += strengthInc;

	if(strengthInc < 0.04f)
		strengthInc += 0.001f;
}

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