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