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: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
18 | |
---|
19 | #include "helicopter.h" |
---|
20 | |
---|
21 | #include "weapons/weapon_manager.h" |
---|
22 | #include "weapons/test_gun.h" |
---|
23 | #include "weapons/turret.h" |
---|
24 | #include "weapons/cannon.h" |
---|
25 | |
---|
26 | #include "factory.h" |
---|
27 | #include "key_mapper.h" |
---|
28 | #include "event_handler.h" |
---|
29 | |
---|
30 | #include "graphics_engine.h" |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | CREATE_FACTORY(Helicopter, CL_HELICOPTER); |
---|
35 | |
---|
36 | /** |
---|
37 | * creates the controlable Spaceship |
---|
38 | */ |
---|
39 | Helicopter::Helicopter() |
---|
40 | { |
---|
41 | this->init(); |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * destructs the spaceship, deletes alocated memory |
---|
46 | */ |
---|
47 | Helicopter::~Helicopter () |
---|
48 | { |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * loads a Spaceships information from a specified file. |
---|
53 | * @param fileName the name of the File to load the spaceship from (absolute path) |
---|
54 | */ |
---|
55 | Helicopter::Helicopter(const char* fileName) |
---|
56 | { |
---|
57 | this->init(); |
---|
58 | TiXmlDocument doc(fileName); |
---|
59 | |
---|
60 | if(!doc.LoadFile()) |
---|
61 | { |
---|
62 | PRINTF(2)("Loading file %s failed for Helicopter.\n", fileName); |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | this->loadParams(doc.RootElement()); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * creates a new Spaceship from Xml Data |
---|
71 | * @param root the xml element containing spaceship data |
---|
72 | |
---|
73 | @todo add more parameters to load |
---|
74 | */ |
---|
75 | Helicopter::Helicopter(const TiXmlElement* root) |
---|
76 | { |
---|
77 | this->init(); |
---|
78 | if (root != NULL) |
---|
79 | this->loadParams(root); |
---|
80 | |
---|
81 | //weapons: |
---|
82 | Weapon* wpRight = new TestGun(0); |
---|
83 | wpRight->setName("testGun Right"); |
---|
84 | Weapon* wpLeft = new TestGun(1); |
---|
85 | wpLeft->setName("testGun Left"); |
---|
86 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON)); |
---|
87 | |
---|
88 | cannon->setName("BFG"); |
---|
89 | |
---|
90 | this->getWeaponManager()->addWeapon(wpLeft, 1, 0); |
---|
91 | this->getWeaponManager()->addWeapon(wpRight,1 ,1); |
---|
92 | this->getWeaponManager()->addWeapon(cannon, 0, 6); |
---|
93 | |
---|
94 | //this->getWeaponManager()->addWeapon(turret, 3, 0); |
---|
95 | |
---|
96 | this->getWeaponManager()->changeWeaponConfig(1); |
---|
97 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | * initializes a Helicopter |
---|
103 | */ |
---|
104 | void Helicopter::init() |
---|
105 | { |
---|
106 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
107 | this->setClassID(CL_HELICOPTER, "Helicopter"); |
---|
108 | |
---|
109 | PRINTF(4)("SPACESHIP INIT\n"); |
---|
110 | |
---|
111 | this->loadModel("models/ships/helicopter_#.obj", 1.0); |
---|
112 | |
---|
113 | EventHandler::getInstance()->grabEvents(true); |
---|
114 | |
---|
115 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false; |
---|
116 | bFire = false; |
---|
117 | xMouse = yMouse = 0; |
---|
118 | mouseSensitivity = 0.001; |
---|
119 | |
---|
120 | cycle = 0.0; |
---|
121 | |
---|
122 | |
---|
123 | travelSpeed = 15.0; |
---|
124 | this->velocity = Vector(0.0,0.0,0.0); |
---|
125 | this->velocityDir = Vector(1.0,0.0,0.0); |
---|
126 | |
---|
127 | // GLGuiButton* button = new GLGuiPushButton(); |
---|
128 | // button->show(); |
---|
129 | // button->setLabel("orxonox"); |
---|
130 | // button->setBindNode(this); |
---|
131 | |
---|
132 | //add events to the eventlist |
---|
133 | registerEvent(SDLK_w); |
---|
134 | registerEvent(SDLK_s); |
---|
135 | registerEvent(SDLK_a); |
---|
136 | registerEvent(SDLK_d); |
---|
137 | registerEvent(SDLK_q); |
---|
138 | registerEvent(SDLK_e); |
---|
139 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
140 | registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
141 | registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
142 | registerEvent(SDLK_PAGEUP); |
---|
143 | registerEvent(SDLK_PAGEDOWN); |
---|
144 | registerEvent(EV_MOUSE_MOTION); |
---|
145 | |
---|
146 | this->getWeaponManager()->setSlotCount(7); |
---|
147 | |
---|
148 | this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
149 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
150 | |
---|
151 | this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
152 | this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
153 | |
---|
154 | this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
155 | this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
156 | |
---|
157 | this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
158 | this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
159 | |
---|
160 | this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
161 | this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
162 | |
---|
163 | this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
164 | this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
165 | // |
---|
166 | this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
167 | this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
168 | // |
---|
169 | // this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0)); |
---|
170 | // this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0))); |
---|
171 | // |
---|
172 | // this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0)); |
---|
173 | // this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));: |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * loads the Settings of a Helicopter from an XML-element. |
---|
179 | * @param root the XML-element to load the Spaceship's properties from |
---|
180 | */ |
---|
181 | void Helicopter::loadParams(const TiXmlElement* root) |
---|
182 | { |
---|
183 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | void Helicopter::enter() |
---|
190 | { |
---|
191 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
192 | |
---|
193 | |
---|
194 | } |
---|
195 | |
---|
196 | void Helicopter::leave() |
---|
197 | { |
---|
198 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
199 | |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | /** |
---|
205 | * adds a weapon to the weapon list of the spaceship |
---|
206 | * @param weapon to add |
---|
207 | */ |
---|
208 | void Helicopter::addWeapon(Weapon* weapon) |
---|
209 | { |
---|
210 | this->getWeaponManager()->addWeapon(weapon); |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | /** |
---|
215 | * removes a weapon from the spaceship |
---|
216 | * @param weapon to remove |
---|
217 | */ |
---|
218 | void Helicopter::removeWeapon(Weapon* weapon) |
---|
219 | { |
---|
220 | this->getWeaponManager()->removeWeapon(weapon); |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * effect that occurs after the Helicopter is spawned |
---|
225 | */ |
---|
226 | void Helicopter::postSpawn () |
---|
227 | { |
---|
228 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * the action occuring if the spaceship left the game |
---|
233 | */ |
---|
234 | void Helicopter::leftWorld () |
---|
235 | {} |
---|
236 | |
---|
237 | /** |
---|
238 | * this function is called, when two entities collide |
---|
239 | * @param entity: the world entity with whom it collides |
---|
240 | * |
---|
241 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
242 | */ |
---|
243 | void Helicopter::collidesWith(WorldEntity* entity, const Vector& location) |
---|
244 | { |
---|
245 | if (entity->isA(CL_TURRET_POWER_UP)) |
---|
246 | { |
---|
247 | this->ADDWEAPON(); |
---|
248 | } |
---|
249 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | /** |
---|
255 | * the function called for each passing timeSnap |
---|
256 | * @param time The timespan passed since last update |
---|
257 | */ |
---|
258 | void Helicopter::tick (float time) |
---|
259 | { |
---|
260 | // spaceship controlled movement |
---|
261 | this->calculateVelocity(time); |
---|
262 | |
---|
263 | Vector move = (velocity)*time; |
---|
264 | |
---|
265 | // this is the air friction (necessary for a smooth control) |
---|
266 | if(velocity.len() != 0) velocity -= velocity*0.1; |
---|
267 | |
---|
268 | |
---|
269 | //hoover effect |
---|
270 | cycle += time; |
---|
271 | this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); |
---|
272 | |
---|
273 | //readjust |
---|
274 | // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); |
---|
275 | //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); |
---|
276 | |
---|
277 | //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); |
---|
278 | |
---|
279 | this->shiftCoor (move); |
---|
280 | |
---|
281 | this->getWeaponManager()->tick(time); |
---|
282 | // weapon system manipulation |
---|
283 | this->weaponAction(); |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * calculate the velocity |
---|
288 | * @param time the timeslice since the last frame |
---|
289 | */ |
---|
290 | void Helicopter::calculateVelocity (float time) |
---|
291 | { |
---|
292 | Vector accel(0.0, 0.0, 0.0); |
---|
293 | Vector rot(0.0, 0.0, 0.0); |
---|
294 | float rotVal = 0.0; |
---|
295 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
296 | /* calculate the direction in which the craft is heading */ |
---|
297 | |
---|
298 | if( this->bUp ) |
---|
299 | { |
---|
300 | //this->shiftCoor(this->getAbsDirX()); |
---|
301 | |
---|
302 | accel += Vector(this->getAbsDirX().x,0,this->getAbsDirX().z).getNormalized()*2; |
---|
303 | if((this->getAbsDirX()).y > -.2) |
---|
304 | { |
---|
305 | rot += Vector (0,0,1); |
---|
306 | rotVal -= time/5; |
---|
307 | } |
---|
308 | } |
---|
309 | else |
---|
310 | { |
---|
311 | if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time/5, Vector(0,0,1))); |
---|
312 | } |
---|
313 | |
---|
314 | if( this->bDown ) |
---|
315 | { |
---|
316 | //this->shiftCoor((this->getAbsDirX())*-1); |
---|
317 | accel -= Vector(this->getAbsDirX().x,0,this->getAbsDirX().z).getNormalized()*2; |
---|
318 | if((this->getAbsDirX()).y < .2) |
---|
319 | { |
---|
320 | rot += Vector (0,0,1); |
---|
321 | rotVal += time/5; |
---|
322 | } |
---|
323 | } |
---|
324 | else |
---|
325 | { |
---|
326 | if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(-time/5, Vector(0,0,1))); |
---|
327 | } |
---|
328 | |
---|
329 | if( this->bLeft /* > -this->getRelCoor().z*2*/) |
---|
330 | { |
---|
331 | //this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
332 | accel -= Vector(this->getAbsDirZ().x,0,this->getAbsDirZ().z).getNormalized()*2; |
---|
333 | //velocityDir.normalize(); |
---|
334 | if((this->getAbsDirZ()).y < .2) |
---|
335 | { |
---|
336 | rot +=Vector(1,0,0); |
---|
337 | rotVal -= time/5; |
---|
338 | } |
---|
339 | } |
---|
340 | else |
---|
341 | { |
---|
342 | if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time/5, Vector(1,0,0))); |
---|
343 | } |
---|
344 | |
---|
345 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
346 | { |
---|
347 | //this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
348 | |
---|
349 | accel += Vector(this->getAbsDirZ().x,0,this->getAbsDirZ().z).getNormalized()*2; |
---|
350 | //velocityDir.normalize(); |
---|
351 | if((this->getAbsDirZ()).y > -.2) |
---|
352 | { |
---|
353 | rot += Vector(1,0,0); |
---|
354 | rotVal += time/5; |
---|
355 | } |
---|
356 | } |
---|
357 | else |
---|
358 | { |
---|
359 | if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time/5, Vector(1,0,0))); |
---|
360 | } |
---|
361 | |
---|
362 | if( this->bRollL /* > -this->getRelCoor().z*2*/) |
---|
363 | { |
---|
364 | this->shiftDir(Quaternion(-time, Vector(1,0,0))); |
---|
365 | //accel -= rightDirection; |
---|
366 | //velocityDir.normalize(); |
---|
367 | //rot +=Vector(1,0,0); |
---|
368 | //rotVal -= .4; |
---|
369 | } |
---|
370 | if( this->bRollR /* > this->getRelCoor().z*2*/) |
---|
371 | { |
---|
372 | this->shiftDir(Quaternion(time, Vector(1,0,0))); |
---|
373 | |
---|
374 | //accel += rightDirection; |
---|
375 | //velocityDir.normalize(); |
---|
376 | //rot += Vector(1,0,0); |
---|
377 | //rotVal += .4; |
---|
378 | } |
---|
379 | if (this->bAscend ) |
---|
380 | { |
---|
381 | //this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
382 | |
---|
383 | accel += (this->getAbsDirY())*2; |
---|
384 | //velocityDir.normalize(); |
---|
385 | //rot += Vector(0,0,1); |
---|
386 | //rotVal += .4; |
---|
387 | } |
---|
388 | if (this->bDescend ) |
---|
389 | { |
---|
390 | //this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
391 | |
---|
392 | accel -= (this->getAbsDirY())*2; |
---|
393 | //velocityDir.normalize(); |
---|
394 | //rot += Vector(0,0,1); |
---|
395 | //rotVal -= .4; |
---|
396 | } |
---|
397 | |
---|
398 | velocity += accel; |
---|
399 | rot.normalize(); |
---|
400 | this->shiftDir(Quaternion(rotVal, rot)); |
---|
401 | } |
---|
402 | |
---|
403 | |
---|
404 | void Helicopter::draw() const |
---|
405 | { |
---|
406 | WorldEntity::draw(); |
---|
407 | |
---|
408 | this->getWeaponManager()->draw(); |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | /** |
---|
413 | * weapon manipulation by the player |
---|
414 | */ |
---|
415 | void Helicopter::weaponAction() |
---|
416 | { |
---|
417 | if( this->bFire) |
---|
418 | { |
---|
419 | this->getWeaponManager()->fire(); |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | /** |
---|
424 | * @todo switch statement ?? |
---|
425 | */ |
---|
426 | void Helicopter::process(const Event &event) |
---|
427 | { |
---|
428 | |
---|
429 | |
---|
430 | if( event.type == SDLK_a) |
---|
431 | this->bLeft = event.bPressed; |
---|
432 | else if( event.type == SDLK_d) |
---|
433 | this->bRight = event.bPressed; |
---|
434 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
435 | this->bFire = event.bPressed; |
---|
436 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
437 | this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
438 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
439 | this->getWeaponManager()->previousWeaponConfig(); |
---|
440 | else if( event.type == SDLK_e) |
---|
441 | this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
442 | else if( event.type == SDLK_c) |
---|
443 | this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
444 | else if( event.type == SDLK_w) |
---|
445 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
446 | else if( event.type == SDLK_s) |
---|
447 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
448 | else if( event.type == EV_MOUSE_MOTION) |
---|
449 | { |
---|
450 | this->xMouse = event.xRel; |
---|
451 | this->yMouse = event.yRel; |
---|
452 | if(((this->getAbsDirX().y) <= .2 && yMouse > 0) || ((this->getAbsDirX().y) >= -.2 && yMouse < 0)) yMouse = 0; |
---|
453 | this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1))); |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | #include "weapons/aiming_turret.h" |
---|
458 | // FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG |
---|
459 | void Helicopter::ADDWEAPON() |
---|
460 | { |
---|
461 | Weapon* turret = NULL; |
---|
462 | |
---|
463 | if ((float)rand()/RAND_MAX < .1) |
---|
464 | { |
---|
465 | //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET)) |
---|
466 | { |
---|
467 | turret = new Turret(); |
---|
468 | this->getWeaponManager()->addWeapon(turret, 2); |
---|
469 | this->getWeaponManager()->changeWeaponConfig(2); |
---|
470 | } |
---|
471 | } |
---|
472 | else |
---|
473 | { |
---|
474 | //if (this->getWeaponManager()->hasFreeSlot(3)) |
---|
475 | { |
---|
476 | turret = new AimingTurret(); |
---|
477 | this->getWeaponManager()->addWeapon(turret, 3); |
---|
478 | |
---|
479 | this->getWeaponManager()->changeWeaponConfig(3); |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | if(turret != NULL) |
---|
484 | { |
---|
485 | turret->setName("Turret"); |
---|
486 | turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1); |
---|
487 | } |
---|
488 | } |
---|