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: cPlayer.cpp
19 // Project: Shooting Star
20 // Author:
21 // Copyrights (c) 2003 2ndPoint ry (www.2ndpoint.fi)
22 //------------------------------------------------------------------------------
23 // Revision history
24 //==============================================================================
25 
26 //==============================================================================
27 // Includes
28 #include "cPlayer.hpp"
29 #include <GL/gl.h>
30 #include "Actions.hpp"
31 #include "Debug.hpp"
32 #include "cPistol.hpp"
33 #include "cWorld.hpp"
34 #include "cBurningEffect.hpp"
35 //------------------------------------------------------------------------------
36 // Namespaces
37 using namespace ShootingStar;
38 //==============================================================================
39 
40 
41 //! Constructor
cPlayer(string animationPrefix)42 cPlayer::cPlayer (string animationPrefix):
43 cSoldier (animationPrefix)
44 {
45 	// Starting weapon
46 	cWeapon *pWeapon = new cPistol ();
47 	cWorld::GetInstance ().SpawnObject (pWeapon);
48 	AddWeapon (pWeapon);
49 };
50 
51 //! Destructor
~cPlayer(void)52 cPlayer::~cPlayer (void)
53 {
54 	// Empty
55 };
56 
57 //! Rendering interface
58 void
Render(Uint32 deltaTime)59 cPlayer::Render (Uint32 deltaTime)
60 {
61 	// Call base Render
62 	cSoldier::Render (deltaTime);
63 
64 	// Render health bar
65 	glPushMatrix ();
66 	glPushAttrib (GL_ENABLE_BIT|GL_CURRENT_BIT);
67 	glDisable (GL_TEXTURE_2D);
68 	glRotatef (-GetRotation () - 90.0f, 0.0f, 0.0f, 1.0f);
69 	glTranslatef (-32.0f, 40.0f, 0.0f);
70 	float health = float (GetHealth ()) / float (GetMaxHealth ());
71 	glBegin (GL_QUADS);
72 		if ( GetMaxArmor () != 0 )
73 		{
74 			float armor = float (GetArmor ()) / float (GetMaxArmor ());
75 			glColor4f (0.0f, 1.0f, 0.0f, 0.5f);
76 			glVertex2f (0.0f,-5.0f);
77 			glVertex2f (0.0f,-1.0f);
78 			glColor4f (0.0f, 1.0f - armor, armor, 0.5f);
79 			glVertex2f (armor * 64.0f,-1.0f);
80 			glVertex2f (armor * 64.0f,-5.0f);
81 		}
82 
83 		glColor4f (1.0f, 0.0f, 0.0f, 0.5f);
84 		glVertex2f (0.0f, 0.0f);
85 		glVertex2f (0.0f, 4.0f);
86 		glColor4f (1.0f - health, health, 0.0f, 0.5f);
87 		glVertex2f (health * 64.0f, 4.0f);
88 		glVertex2f (health * 64.0f, 0.0f);
89 
90 
91 		cWeapon *pCurrentWeapon = GetCurrentWeapon ();
92 		if ( pCurrentWeapon != NULL )
93 		{
94 			float status = 0.0f;
95 			if ( pCurrentWeapon->GetState () == cWeapon::State_Reloading )
96 			{
97 				status = pCurrentWeapon->GetReloadStatus ();
98 				glColor4f (0.0f, 0.0f, 1.0f, 0.5f);
99 			}
100 			else
101 			{
102 				status = pCurrentWeapon->GetClipStatus ();
103 				glColor4f (1.0f, 1.0f, 0.0f, 0.5f);
104 			}
105 			glVertex2f (0.0f, 5.0f);
106 			glVertex2f (0.0f, 9.0f);
107 			glVertex2f (status * 64.0f, 9.0f);
108 			glVertex2f (status * 64.0f, 5.0f);
109 		}
110 	glEnd ();
111 	glPopAttrib ();
112 	glPopMatrix ();
113 
114 	glPopMatrix ();
115 	glPushMatrix ();
116 
117 	glDisable (GL_TEXTURE_2D);
118 
119 	cVector2f begin = GetEmittingPosition ();
120 	cVector2f end = begin + GetDirection (0) * 200.0f;
121 
122 	// Aim line
123 	/*glLineWidth (3.0f);
124 	glColor4f (1.0f, 0.0f, 0.0f, 0.25f);
125 	glBegin (GL_LINES);
126 		glVertex2fv ((float *)&begin);
127 		glVertex2fv ((float *)&end);
128 	glEnd ();
129 	glLineWidth (1.0f);*/
130 
131 	// Crosshair
132 	glColor4f (1.0f, 0.0f, 0.0f, 1.0f);
133 	glPointSize (3.0f);
134 	glBegin (GL_POINTS);
135 		glVertex2fv ((float *)&end);
136 	glEnd ();
137 	glPointSize (1.0f);
138 
139 	glEnable (GL_TEXTURE_2D);
140 }
141 
142 void
HandleAction(int action)143 cPlayer::HandleAction (int action)
144 {
145 	switch ( action )
146 	{
147 		case PlayerAction_PullTrigger:
148 			PullTrigger ();
149 			break;
150 		case PlayerAction_ReleaseTrigger:
151 			ReleaseTrigger ();
152 			break;
153 		case PlayerAction_NextWeapon:
154 			NextWeapon ();
155 			break;
156 		case PlayerAction_PreviousWeapon:
157 			PreviousWeapon ();
158 			break;
159 		case PlayerAction_EnableFineAim:
160 			EnableFineAim ();
161 			break;
162 		case PlayerAction_DisableFineAim:
163 			DisableFineAim ();
164 			break;
165 		case PlayerAction_EnableStrafe:
166 			EnableStrafe ();
167 			break;
168 		case PlayerAction_DisableStrafe:
169 			DisableStrafe ();
170 			break;
171 		default:
172 			break;
173 	}
174 };
175 
176 void
OnLevelChanged()177 cPlayer::OnLevelChanged ()
178 {
179 	// This is called when level changes
180 
181 	// Call base
182 	cSoldier::OnLevelChanged ();
183 
184 	// Reset healt & armor
185 	SetHealth (GetMaxHealth ());
186 	SetArmor (GetMaxArmor ());
187 
188 	StopWalking ();
189 	StopTurning ();
190 	ReleaseTrigger ();
191 	DisableFineAim ();
192 	DisableStrafe ();
193 
194 	// We have to respawn all weapons because all objects are killed
195 	// when level changes
196 	for ( unsigned int i = 0; i < mWeapons.size (); i++ )
197 	{
198 		mWeapons[i]->SetParent (this);
199 		mWeapons[i]->Reset ();
200 		if ( !mWeapons[i]->IsAlive () )
201 			cWorld::GetInstance ().SpawnObject (mWeapons[i]);
202 	}
203 
204 	cWeapon *pCurrentWeapon = GetCurrentWeapon ();
205 	if ( pCurrentWeapon != NULL )
206 		pCurrentWeapon->SetActive (true);
207 }
208 
209 void
SaveWeapons(void)210 cPlayer::SaveWeapons (void)
211 {
212 	mStoredWeapons.clear ();
213 	for ( unsigned int i = 0; i < mWeapons.size (); i++ )
214 	{
215 		tWeaponInfo weapon;
216 		weapon.name = mWeapons[i]->GetName ();
217 		mWeapons[i]->GetAmmo (weapon.ammo, weapon.clips);
218 		mStoredWeapons.push_back (weapon);
219 	}
220 	//dbgInfo () << mStoredWeapons.size () << " weapons saved\n";
221 }
222 
223 void
RestoreWeapons(void)224 cPlayer::RestoreWeapons (void)
225 {
226 	mCurrentWeapon = -1;
227 
228 	vector<cPointer<cWeapon> > newWeapons;
229 
230 	//dbgInfo () << "Size of weapons " << mWeapons.size ();
231 
232 	bool remove;
233 	vector<cPointer<cWeapon> >::iterator weapon = mWeapons.begin ();
234 	while ( weapon != mWeapons.end () )
235 	{
236 		for ( unsigned int i = 0; i < mStoredWeapons.size (); i++ )
237 		{
238 			if ( (*weapon)->GetName () == mStoredWeapons[i].name )
239 			{
240 				//dbgInfo () << "Restoring " << (*weapon)->GetName () << '\n';
241 				(*weapon)->SetAmmo (mStoredWeapons[i].ammo, mStoredWeapons[i].clips);
242 				newWeapons.push_back (*weapon);
243 			}
244 		}
245 		weapon++;
246 	}
247 
248 	mWeapons.clear ();
249 	mWeapons = newWeapons;
250 
251 	//dbgInfo () << mWeapons.size () << " weapons restored\n";
252 
253 	dbg::assertion (DBG_ASSERTION (mWeapons.size () == mStoredWeapons.size ()));
254 	NextWeapon ();
255 }
256 
257 //==============================================================================
258 // EOF
259 //==============================================================================
260