1 //==============================================================================
2 // This program is free software; you can redistribute it and/or modify
3 // it under the terms of the GNU General Public License as published by
4 // the Free Software Foundation; either version 2 of the License, or
5 // (at your option) any later version.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU Library General Public License for more details.
11 //
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 //==============================================================================
16 
17 //==============================================================================
18 // File: cHurtable.hpp
19 // Project: Shooting Star
20 // Author: Jarmo Hekkanen <jarski@2ndpoint.fi>
21 // Copyrights (c) 2003 2ndPoint ry (www.2ndpoint.fi)
22 //------------------------------------------------------------------------------
23 // Revision history
24 //==============================================================================
25 #ifndef cHurtable_hpp
26 #define cHurtable_hpp
27 //==============================================================================
28 // Includes
29 #include "cCollidable.hpp"
30 //------------------------------------------------------------------------------
31 // Namespaces
32 using namespace std;
33 namespace ShootingStar {
34 //------------------------------------------------------------------------------
35 // Forward declarations
36 //==============================================================================
37 
38 //==============================================================================
39 //! Object that can be hurt by harmful objects
40 //------------------------------------------------------------------------------
41 class cHurtable : public cCollidable
42 {
43 	// Constructor & Destructor
44 	public:
45 		//! Constructor
46 		cHurtable (void);
47 		//! Destructor
48 		virtual ~cHurtable (void);
49 
50 	// Public methods
51 	public:
52 		// --- Armor ---
53 		//! Sets the maximal armor for this object
SetMaxArmor(int maxArmor)54 		void SetMaxArmor (int maxArmor)
55 		{
56 			mMaxArmor = maxArmor;
57 			LimitArmor ();
58 		};
59 		//! Returns the maximal armor of this object
GetMaxArmor(void) const60 		int GetMaxArmor (void) const { return mMaxArmor; };
61 		//! Sets the armor of this object
SetArmor(int armor)62 		void SetArmor (int armor)
63 		{
64 			mArmor = armor;
65 			LimitArmor ();
66 		};
67 		//! Returns the armor of this object
GetArmor(void) const68 		int GetArmor (void) const { return mArmor; };
69 		//! Increase the armor of this object
GainArmor(int amount)70 		void GainArmor (int amount)
71 		{
72 			mArmor += amount;
73 			LimitArmor ();
74 		};
75 
76 		// --- Health ---
77 		//! Sets the maximal health for this object
SetMaxHealth(int maxHealth)78 		void SetMaxHealth (int maxHealth)
79 		{
80 			mMaxHealth = maxHealth;
81 			LimitHealth ();
82 		};
83 		//! Returns the maximal health of this object
GetMaxHealth(void) const84 		int GetMaxHealth (void) const { return mMaxHealth; };
85 		//! Sets the health of this object
SetHealth(int health)86 		void SetHealth (int health)
87 		{
88 			mHealth = health;
89 			LimitHealth ();
90 		};
91 		//! Returns the health of this object
GetHealth(void) const92 		int GetHealth (void) const { return mHealth; };
93 		//! Increase the health of this object
GainHealth(int health)94 		void GainHealth (int health)
95 		{
96 			mHealth += health;
97 			LimitHealth ();
98 		};
99 
100 		// --- Damage ---
DoDamage(int amount,const cVector2f & direction,ObjectID attacker=0)101 		void DoDamage (int amount, const cVector2f &direction, ObjectID attacker = 0)
102 		{
103 			OnHurt (amount, direction, attacker);
104 
105 			mArmor -= amount;
106 			if ( mArmor < 0 )
107 			{
108 				mHealth += mArmor;
109 				mArmor = 0;
110 
111 				// Check if the object has died
112 				if ( mHealth < 0 )
113 					OnDeath ();
114 			}
115 		};
116 		//! Called when object is hurt
OnHurt(int amount,const cVector2f & direction,ObjectID attacker)117 		virtual void OnHurt (int amount, const cVector2f &direction, ObjectID attacker) { };
118 
119 		//! Called on death
OnDeath(void)120 		virtual void OnDeath (void) { Kill (); };
121 
122 	// Private methods
123 	private:
LimitArmor(void)124 		void LimitArmor (void)
125 		{
126 			if ( mArmor > mMaxArmor )
127 				mArmor = mMaxArmor;
128 		};
LimitHealth(void)129 		void LimitHealth (void)
130 		{
131 			if ( mHealth > mMaxHealth )
132 				mHealth = mMaxHealth;
133 		};
134 
135 	// Member variables
136 	private:
137 		int mMaxArmor;		//!< Maximal armor
138 		int mArmor;			//!< Current armor
139 		int mMaxHealth;		//!< Maximal health
140 		int mHealth;		//!< Current health
141 };
142 //==============================================================================
143 
144 //==============================================================================
145 }		// End of the ShootingStar namespace
146 #endif // cHurtable_hpp
147 //------------------------------------------------------------------------------
148 // EOF
149 //==============================================================================
150