/*
	Lan Wars.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef SPRITE_H
#define SPRITE_H

//=====================================================================================================================
//=====================================================================================================================

#include "Graphics/Texture.h"

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{	
	//=================================================================================================================
	/// @brief		Represents a sprite in the game world. Sprites are drawn in batches.
	//=================================================================================================================

	class Sprite
	{
		public:

			//=========================================================================================================
			// Functions
			//=========================================================================================================

				/// Default constructor: creates an invalid sprite

				Sprite(){}

				/// Creates the sprite from the given texture, size and position
				
				Sprite( const Texture & texture , const Vec3f & position , const Vec2f & size )
				{
					m_texture			= texture;
					m_position			= position;
					m_size				= size;
					m_horizontal_flip	= false;
					m_additive_blend	= false;
					m_color				= Vec4f::One();
				}
				
				/// Copy constructor:

				Sprite( const Sprite & other )
				{
					m_texture			= other.m_texture;
					m_position			= other.m_position;
					m_size				= other.m_size;
					m_horizontal_flip	= other.m_horizontal_flip;
					m_additive_blend	= other.m_additive_blend;
					m_color				= other.m_color;
				}
				
				/// Assignment operator:

				Sprite & operator = ( const Sprite & other )
				{
					m_texture			= other.m_texture;
					m_position			= other.m_position;
					m_size				= other.m_size;
					m_horizontal_flip	= other.m_horizontal_flip;
					m_additive_blend	= other.m_additive_blend;
					m_color				= other.m_color;
					
					return * this;
				}

				/// Tells if the resources for the sprite are valid

				inline bool IsValid() const { return m_texture.IsValid(); }

				/// Returns the positon of the sprite
				
				const Vec3f & GetPosition() const { return m_position; }
				
				/// Returns the size of the sprite
				
				const Vec2f & GetSize() const { return m_size; }
				
				/// Returns the texture of the sprite
				
				const Texture & GetTexture() const { return m_texture; }
				
				/// Sets the position of the sprite
				
				void SetPosition( const Vec3f & position ){ m_position = position; }
				
				/// Sets the size of the sprite
				
				void SetSize( const Vec2f & size ){ m_size = size; }
				
				/// Sets the color of the sprite
				
				void SetColor( const Vec4f & color ){ m_color = color; }
				
				/// Gets the color of the sprite
				
				const Vec4f & GetColor() const { return m_color; }
				
				/// Sets the texture for the sprite
				
				void SetTexture( const Texture & texture ){ m_texture = texture; }
				
				/// Sets whether additive blending is used for the sprite
				
				void SetAdditivelyBlended( bool additive ){ m_additive_blend = additive; }
				
				/// Gets whether additive blending is used for the sprite
				
				bool IsAdditivelyBlended() const { return m_additive_blend; }
				
				/// Tells if the sprite is horizontally flipped
				
				inline bool IsHorizontallyFlipped() const { return m_horizontal_flip; }
				
				/// Sets if the sprite is to be horizontally flipped
				
				inline void SetHorizontallyFlipped( bool flip ){ m_horizontal_flip = flip; }
				
				/// Comparison operator. Compares two sprites for equality based on texture and additive blending.
				
				bool operator == ( const Sprite & other ) const 
				{ 
					return m_texture == other.m_texture && m_additive_blend == other.m_additive_blend; 
				}

				/// Comparison operator. Compares two sprites for equality based on texture and additive blending.
				
				bool operator != ( const Sprite & other ) const 
				{ 
					return m_texture != other.m_texture && m_additive_blend == other.m_additive_blend; 
				}
				
				/// Comparison operator. Compares two sprites for equality based on texture.
				
				bool operator >= ( const Sprite & other ) const 
				{ 
					if ( m_additive_blend )
					{
						if ( other.m_additive_blend == false )
						{					
							return false;
						}
						else
						{
							return m_texture >= other.m_texture;
						}
					}
					else
					{
						if ( other.m_additive_blend == true )
						{
							return true;
						}
						else
						{
							return m_texture >= other.m_texture; 
						}
					}
				}
				
				/// Comparison operator. Compares two sprites for equality based on texture.
				
				bool operator <= ( const Sprite & other ) const 
				{ 
					if ( m_additive_blend )
					{
						if ( other.m_additive_blend == false )
						{					
							return true;
						}
						else
						{
							return m_texture <= other.m_texture;
						}
					}
					else
					{
						if ( other.m_additive_blend == true )
						{
							return false;
						}
						else
						{
							return m_texture <= other.m_texture; 
						}
					}
				}
				
				/// Comparison operator. Compares two sprites for equality based on texture.
				
				bool operator > ( const Sprite & other ) const 
				{ 
					if ( m_additive_blend )
					{
						if ( other.m_additive_blend == false )
						{					
							return false;
						}
						else
						{
							return m_texture > other.m_texture; 
						}
					}
					else
					{
						if ( other.m_additive_blend == true )
						{
							return true;
						}
						else
						{
							return m_texture > other.m_texture;
						}
					}
				}
				
				/// Comparison operator. Compares two sprites for equality based on texture.
				
				bool operator < ( const Sprite & other ) const 
				{ 
					if ( m_additive_blend )
					{
						if ( other.m_additive_blend == false )
						{					
							return true;
						}
						else
						{
							return m_texture < other.m_texture;
						}
					}
					else
					{
						if ( other.m_additive_blend == true )
						{
							return false;
						}
						else
						{
							return m_texture < other.m_texture;
						}
					}
				}
				
		private:			
		
			//=========================================================================================================
			// Variables
			//=========================================================================================================

				/// This is the texture used by the sprite
				
				Texture m_texture;
				
				/// This is the position of the sprite
				
				Vec3f m_position;
				
				/// This is the size of the sprite
				
				Vec2f m_size;
				
				/// This is the color of the sprite
				
				Vec4f m_color;
				
				/// If true then the sprite is flipped horizontally
				
				bool m_horizontal_flip;
				
				/// If true then the sprite is additively blended
				
				bool m_additive_blend;
	};
};

#endif
