//	Bullet.h - Basic bullet class.
//  ----------------------------------------------------------------------------
//	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/>.
//	----------------------------------------------------------------------------

#ifndef BULLET_H_
#define BULLET_H_

#include "ThreeFloats.h"
#include "Collidable.h"
#include "c0xHeaders.h"

#include <vector>

class BulletCollisionModifier;
class BulletDirectionModifier;
class BulletDeathModifier;
class GamePlayState;
class Drawer;

///	Basic bullet class.
class Bullet : public Collidable
{
  public:
	///	Constructor.
	Bullet(const TwoFloats& pos,
		   const TwoFloats& dir,
		   const ThreeFloats& col,
		   const std::wstring& owner);
	///	Destructor.
	~Bullet();

	///	Moves the bullet.
	void update(const TwoFloats& arenaTopLeft,
			    const TwoFloats& arenaBottomRight,
				GamePlayState& state);
	///	Draws the bullet's trail.
	void drawTrail(shared_ptr<Drawer>& d, float timestepInterp);
	///	Draws the bullet.
	void draw(shared_ptr<Drawer>& d, float timestepInterp);

	///	Called before the bullet is deleted.
	void deathAction(GamePlayState& state);
	///	Used to resurrect a bullet before it's deleted.
	void resurrect();
	///	Returns true if the bullet should be deleted.
	bool getDead() const {return dead;};
	///	Returns how long this bullet has been alive.
	float getLifespan() const {return lifespan;};

	///	Kills the bullet.
	void setDead();

	///	Sets the bullet's position.
	void setPosition(const TwoFloats& pos);
	///	Sets the bullet's direction.
	void setDirection(const TwoFloats& dir);
	///	Multiplies the bullet's global speed by the passed-in value.
	/*!
		This is used to speed up/slow down bullets when moving through
		speed/slow field puddles. It is reset at the end of each update() call.
	 */
	void multiplyGlobalSpeed(float val);

	///	Returns the bullet's parent.
	const std::wstring& getParent() const {return parent;};
	///	Returns true if the bullet collided with a zombie.
	bool getCollidedWithZombie() const {return collidedWithZombie;};

	///	Returns the bullet's direction.
	const TwoFloats& getDirection() const {return direction;};
	///	Returns the bullet's colour.
	const ThreeFloats& getColour() const {return colour;};

	///	Adds a collision modifier to the bullet.
	void addCollisionModifier(shared_ptr<BulletCollisionModifier>& mod);
	///	Adds a death modifier to the bullet.
	void addDeathModifier(shared_ptr<BulletDeathModifier>& mod);
	///	Adds a direction modifier to the bullet.
	void addDirectionModifier(shared_ptr<BulletDirectionModifier>& mod);

	///	Returns true if this entity wants to know when it collides with the named entity type.
	bool interestedInCollisionsWith(const std::wstring& entity) const;
	///	Called when this entity collides with another.
	bool collide(const shared_ptr<Collidable>& other,
				 GamePlayState& state);

	///	Returns the entity's position.
	const TwoFloats& getPosition() const {return (const TwoFloats&)position;};
	///	Returns the entity's radius.
	float getRadius() const {return 5.0f;};
	///	Returns true if other entities can pass through this one.
	//bool getIncorporeal() const {return (invinciCount!=0);};
	bool getIncorporeal() const {return false;};
  private:
	///	The size of the bullet trail we draw.
	enum
	{
		TrailSize = 12
	};

	///	The bullet's position.
	TwoFloats position;
	///	The bullet's direction.
	TwoFloats direction;
	///	The bullet's colour.
	ThreeFloats colour;
	///	The entity which fired this bullet.
	std::wstring parent;

	///	The bullet's global speed (for speed/slow puddles).
	float globalSpeed;

	///	True if the bullet should be deleted.
	bool dead;
	///	If true, no death action is carried out when the bullet dies.
	bool noDeathAction;
	///	How long this bullet has been alive.
	float lifespan;

	///	The bullet's actual direction after modifiers.
	TwoFloats actualDir;

	///	For the bullet's trail.
	std::vector<TwoFloats> trailPos;
	///	For the bullet's trail.
	std::vector<float> trailSize;
	///	For the bullet's trail.
	std::vector<ThreeFloats> trailCol;
	///	For the bullet's trail.
	std::vector<float> trailAlpha;

	///	Used to ensure it doesn't immediately collide with the tank which fired it.
	int invinciCount;

	///	True if the bullet collided with a zombie.
	bool collidedWithZombie;

	///	Any modifiers for when the bullet hits a wall.
	std::vector<shared_ptr<BulletCollisionModifier> > collisionModifiers;
	///	Any modifiers for when the bullet dies.
	std::vector<shared_ptr<BulletDeathModifier> > deathModifiers;
	///	Any modifiers which alter the bullet's direction.
	std::vector<shared_ptr<BulletDirectionModifier> > directionModifiers;
};

#endif
