1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: Christian Meyer |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PLAYER |
---|
17 | |
---|
18 | #include "player.h" |
---|
19 | |
---|
20 | #include "track_manager.h" |
---|
21 | #include "objModel.h" |
---|
22 | #include "resource_manager.h" |
---|
23 | |
---|
24 | #include "weapon_manager.h" |
---|
25 | #include "test_gun.h" |
---|
26 | #include "turret.h" |
---|
27 | #include "world.h" |
---|
28 | |
---|
29 | #include "list.h" |
---|
30 | |
---|
31 | #include "event_handler.h" |
---|
32 | |
---|
33 | #include "event.h" |
---|
34 | |
---|
35 | |
---|
36 | using namespace std; |
---|
37 | |
---|
38 | CREATE_FACTORY(Player); |
---|
39 | |
---|
40 | /** |
---|
41 | * creates a new Player |
---|
42 | * @param isFree if the player is free |
---|
43 | */ |
---|
44 | Player::Player() |
---|
45 | { |
---|
46 | this->init(); |
---|
47 | |
---|
48 | //weapons: |
---|
49 | Weapon* wpRight = new TestGun(this->weaponMan, 0); |
---|
50 | Weapon* wpLeft = new TestGun(this->weaponMan, 1); |
---|
51 | |
---|
52 | this->weaponMan->addWeapon(wpRight); |
---|
53 | // this->weaponMan->addWeapon(wpLeft, WM_CONFIG1, WM_SLOT1); |
---|
54 | // this->weaponMan->addWeapon(wpRight, WM_CONFIG2); |
---|
55 | // this->weaponMan->addWeapon(wpLeft, WM_CONFIG2); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * loads a Players information from a specified file. |
---|
60 | * @param fileName the name of the File to load the player from (absolute path) |
---|
61 | */ |
---|
62 | Player::Player(const char* fileName) |
---|
63 | { |
---|
64 | this->init(); |
---|
65 | TiXmlDocument doc(fileName); |
---|
66 | |
---|
67 | if(!doc.LoadFile()) |
---|
68 | { |
---|
69 | PRINTF(2)("Loading file %s failed for player.\n", fileName); |
---|
70 | return; |
---|
71 | } |
---|
72 | |
---|
73 | this->loadParams(doc.RootElement()); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * creates a new Player from Xml Data |
---|
78 | * @param root the xml element containing player data |
---|
79 | |
---|
80 | @todo add more parameters to load |
---|
81 | */ |
---|
82 | Player::Player(const TiXmlElement* root) |
---|
83 | { |
---|
84 | this->init(); |
---|
85 | this->loadParams(root); |
---|
86 | |
---|
87 | //weapons: |
---|
88 | Weapon* wpRight = new TestGun(this->weaponMan, 0); |
---|
89 | wpRight->setName("testGun Right"); |
---|
90 | Weapon* wpLeft = new TestGun(this->weaponMan, 1); |
---|
91 | wpLeft->setName("testGun Left"); |
---|
92 | |
---|
93 | Weapon* turret = new Turret(this->weaponMan); |
---|
94 | turret->setName("main-Turret"); |
---|
95 | Weapon* turret2 = new Turret(this->weaponMan); |
---|
96 | this->weaponMan->addWeapon(wpLeft, 1, 0); |
---|
97 | this->weaponMan->addWeapon(wpRight,1 ,1); |
---|
98 | this->weaponMan->addWeapon(turret, 2, 2); |
---|
99 | this->weaponMan->addWeapon(turret2, 2, 3); |
---|
100 | this->weaponMan->addWeapon(wpLeft, 3, 0); |
---|
101 | this->weaponMan->addWeapon(wpRight,3 ,1); |
---|
102 | |
---|
103 | this->weaponMan->changeWeaponConfig(0); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * destructs the player, deletes alocated memory |
---|
108 | */ |
---|
109 | Player::~Player () |
---|
110 | { |
---|
111 | /* do not delete the weapons, they are contained in the pnode tree |
---|
112 | and will be deleted there. |
---|
113 | this only frees the memory allocated to save the list. |
---|
114 | */ |
---|
115 | delete this->weaponMan; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * initializes a Player |
---|
120 | */ |
---|
121 | void Player::init() |
---|
122 | { |
---|
123 | this->setClassID(CL_PLAYER, "Player"); |
---|
124 | |
---|
125 | // this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN); |
---|
126 | travelSpeed = 15.0; |
---|
127 | velocity = new Vector(); |
---|
128 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
129 | bFire = false; |
---|
130 | acceleration = 10.0; |
---|
131 | |
---|
132 | |
---|
133 | this->weaponMan = new WeaponManager(this); |
---|
134 | this->weaponMan->setSlotCount(4); |
---|
135 | this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
136 | this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
137 | this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
138 | this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
139 | this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
140 | this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * loads the Settings of a Player from an XML-element. |
---|
146 | * @param root the XML-element to load the Player's properties from |
---|
147 | */ |
---|
148 | void Player::loadParams(const TiXmlElement* root) |
---|
149 | { |
---|
150 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /** |
---|
158 | * adds a weapon to the weapon list of player |
---|
159 | * @param weapon to add |
---|
160 | */ |
---|
161 | void Player::addWeapon(Weapon* weapon) |
---|
162 | { |
---|
163 | this->weaponMan->addWeapon(weapon); |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | /** |
---|
168 | * removes a weapon from the player |
---|
169 | * @param weapon to remove |
---|
170 | */ |
---|
171 | void Player::removeWeapon(Weapon* weapon) |
---|
172 | { |
---|
173 | this->weaponMan->removeWeapon(weapon); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | /** |
---|
178 | * effect that occurs after the player is spawned |
---|
179 | */ |
---|
180 | void Player::postSpawn () |
---|
181 | { |
---|
182 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | /** |
---|
187 | * the action occuring if the player left the game |
---|
188 | */ |
---|
189 | void Player::leftWorld () |
---|
190 | {} |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | /** |
---|
195 | * if the player is hit, call this function |
---|
196 | * @param weapon hit by this weapon |
---|
197 | * @param loc ?? |
---|
198 | */ |
---|
199 | void Player::hit (WorldEntity* weapon, Vector* loc) |
---|
200 | { |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | /** |
---|
205 | * Collision with another Entity has this effect |
---|
206 | * @param other the other colider |
---|
207 | * @param ownhitflags ?? |
---|
208 | * @param otherhitflags ?? |
---|
209 | */ |
---|
210 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
211 | { |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | /** |
---|
216 | * draws the player after transforming him. |
---|
217 | */ |
---|
218 | void Player::draw () |
---|
219 | { |
---|
220 | glMatrixMode(GL_MODELVIEW); |
---|
221 | glPushMatrix(); |
---|
222 | float matrix[4][4]; |
---|
223 | |
---|
224 | /* translate */ |
---|
225 | glTranslatef (this->getAbsCoor ().x, |
---|
226 | this->getAbsCoor ().y, |
---|
227 | this->getAbsCoor ().z); |
---|
228 | /* rotate */ |
---|
229 | this->getAbsDir ().matrix (matrix); |
---|
230 | glMultMatrixf((float*)matrix); |
---|
231 | |
---|
232 | this->model->draw(); |
---|
233 | glPopMatrix(); |
---|
234 | |
---|
235 | this->weaponMan->draw(); |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | /** |
---|
240 | * the function called for each passing timeSnap |
---|
241 | * @param time The timespan passed since last update |
---|
242 | */ |
---|
243 | void Player::tick (float time) |
---|
244 | { |
---|
245 | // player controlled movement |
---|
246 | this->move(time); |
---|
247 | |
---|
248 | this->weaponMan->tick(time); |
---|
249 | // weapon system manipulation |
---|
250 | this->weaponAction(); |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | /** |
---|
255 | * action if player moves |
---|
256 | * @param time the timeslice since the last frame |
---|
257 | */ |
---|
258 | void Player::move (float time) |
---|
259 | { |
---|
260 | Vector accel(0.0, 0.0, 0.0); |
---|
261 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
262 | /* calculate the direction in which the craft is heading */ |
---|
263 | Vector direction (1.0, 0.0, 0.0); |
---|
264 | //direction = this->absDirection.apply (direction); |
---|
265 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
266 | //orthDirection = orthDirection.cross (direction); |
---|
267 | |
---|
268 | if( this->bUp && this->getRelCoor().x < 20) |
---|
269 | accel = accel+(direction*acceleration); |
---|
270 | if( this->bDown && this->getRelCoor().x > -5) |
---|
271 | accel = accel -(direction*acceleration); |
---|
272 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) |
---|
273 | accel = accel - (orthDirection*acceleration); |
---|
274 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) |
---|
275 | accel = accel + (orthDirection*acceleration); |
---|
276 | if( this->bAscend ) { /* FIXME */ } |
---|
277 | if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */ |
---|
278 | |
---|
279 | Vector move = accel * time; |
---|
280 | this->shiftCoor (move); |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | /** |
---|
285 | * weapon manipulation by the player |
---|
286 | */ |
---|
287 | void Player::weaponAction() |
---|
288 | { |
---|
289 | if( this->bFire) |
---|
290 | { |
---|
291 | this->weaponMan->fire(); |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * @todo switch statement ?? |
---|
297 | */ |
---|
298 | void Player::process(const Event &event) |
---|
299 | { |
---|
300 | if( event.type == KeyMapper::PEV_UP) |
---|
301 | this->bUp = event.bPressed; |
---|
302 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
303 | this->bDown = event.bPressed; |
---|
304 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
305 | this->bRight= event.bPressed; |
---|
306 | else if( event.type == KeyMapper::PEV_LEFT) |
---|
307 | this->bLeft = event.bPressed; |
---|
308 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
309 | this->bFire = event.bPressed; |
---|
310 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
311 | this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
312 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
313 | this->weaponMan->previousWeaponConfig(); |
---|
314 | } |
---|