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 | WorldEntity::draw(); |
---|
267 | this->getWeaponManager()->draw(); |
---|
268 | |
---|
269 | //this->debug(0); |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * the function called for each passing timeSnap |
---|
274 | * @param time The timespan passed since last update |
---|
275 | */ |
---|
276 | void SpaceShip::tick (float time) |
---|
277 | { |
---|
278 | |
---|
279 | // spaceship controlled movement |
---|
280 | this->calculateVelocity(time); |
---|
281 | |
---|
282 | Vector move = (velocity)*time; |
---|
283 | |
---|
284 | //orient the velocity in the direction of the spaceship. |
---|
285 | travelSpeed = velocity.len(); |
---|
286 | velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity; |
---|
287 | velocity = (velocity.getNormalized())*travelSpeed; |
---|
288 | |
---|
289 | //orient the spaceship in direction of the mouse |
---|
290 | rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*3.0); |
---|
291 | if (this->getAbsDir().distance(rotQuat) > 0.00000000000001) |
---|
292 | this->setAbsDir( rotQuat); |
---|
293 | //this->setAbsDirSoft(mouseDir,5); |
---|
294 | |
---|
295 | // this is the air friction (necessary for a smooth control) |
---|
296 | if(velocity.len() != 0) velocity -= velocity*0.01; |
---|
297 | |
---|
298 | //hoover effect |
---|
299 | cycle += time; |
---|
300 | this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); |
---|
301 | |
---|
302 | //readjust |
---|
303 | // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); |
---|
304 | //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); |
---|
305 | |
---|
306 | //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); |
---|
307 | |
---|
308 | this->shiftCoor (move); |
---|
309 | |
---|
310 | this->getWeaponManager()->tick(time); |
---|
311 | // weapon system manipulation |
---|
312 | this->weaponAction(); |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * calculate the velocity |
---|
317 | * @param time the timeslice since the last frame |
---|
318 | */ |
---|
319 | void SpaceShip::calculateVelocity (float time) |
---|
320 | { |
---|
321 | Vector accel(0.0, 0.0, 0.0); |
---|
322 | /* |
---|
323 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
324 | */ |
---|
325 | //float rotVal = 0.0; |
---|
326 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
327 | /* calculate the direction in which the craft is heading */ |
---|
328 | |
---|
329 | Plane plane(Vector(0,1,0), Vector(0,0,0)); |
---|
330 | |
---|
331 | if( this->bUp ) |
---|
332 | { |
---|
333 | //this->shiftCoor(this->getAbsDirX()); |
---|
334 | accel += (this->getAbsDirX())*2; |
---|
335 | |
---|
336 | /* Heli-Steuerung |
---|
337 | accel += (this->getAbsDirX()*2; |
---|
338 | if( |
---|
339 | */ |
---|
340 | } |
---|
341 | |
---|
342 | if( this->bDown ) |
---|
343 | { |
---|
344 | //this->shiftCoor((this->getAbsDirX())*-1); |
---|
345 | accel -= (this->getAbsDirX())*2; |
---|
346 | } |
---|
347 | |
---|
348 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
349 | { |
---|
350 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
351 | // accel -= rightDirection; |
---|
352 | //velocityDir.normalize(); |
---|
353 | //rot +=Vector(1,0,0); |
---|
354 | //rotVal -= .4; |
---|
355 | } |
---|
356 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
357 | { |
---|
358 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
359 | |
---|
360 | // accel += rightDirection; |
---|
361 | //velocityDir.normalize(); |
---|
362 | //rot += Vector(1,0,0); |
---|
363 | //rotVal += .4; |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | if( this->bRollL /* > -this->getRelCoor().z*2*/) |
---|
368 | { |
---|
369 | mouseDir *= Quaternion(-time, Vector(1,0,0)); |
---|
370 | // accel -= rightDirection; |
---|
371 | //velocityDir.normalize(); |
---|
372 | //rot +=Vector(1,0,0); |
---|
373 | //rotVal -= .4; |
---|
374 | } |
---|
375 | if( this->bRollR /* > this->getRelCoor().z*2*/) |
---|
376 | { |
---|
377 | mouseDir *= Quaternion(time, Vector(1,0,0)); |
---|
378 | |
---|
379 | // accel += rightDirection; |
---|
380 | //velocityDir.normalize(); |
---|
381 | //rot += Vector(1,0,0); |
---|
382 | //rotVal += .4; |
---|
383 | } |
---|
384 | if (this->bAscend ) |
---|
385 | { |
---|
386 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
387 | |
---|
388 | // accel += upDirection; |
---|
389 | //velocityDir.normalize(); |
---|
390 | //rot += Vector(0,0,1); |
---|
391 | //rotVal += .4; |
---|
392 | } |
---|
393 | if (this->bDescend ) |
---|
394 | { |
---|
395 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
396 | |
---|
397 | // accel -= upDirection; |
---|
398 | //velocityDir.normalize(); |
---|
399 | //rot += Vector(0,0,1); |
---|
400 | //rotVal -= .4; |
---|
401 | } |
---|
402 | |
---|
403 | velocity += accel; |
---|
404 | //rot.normalize(); |
---|
405 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | * weapon manipulation by the player |
---|
410 | */ |
---|
411 | void SpaceShip::weaponAction() |
---|
412 | { |
---|
413 | if( this->bFire) |
---|
414 | { |
---|
415 | this->getWeaponManager()->fire(); |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | /** |
---|
420 | * @todo switch statement ?? |
---|
421 | */ |
---|
422 | void SpaceShip::process(const Event &event) |
---|
423 | { |
---|
424 | |
---|
425 | |
---|
426 | if( event.type == SDLK_a) |
---|
427 | this->bRollL = event.bPressed; |
---|
428 | else if( event.type == SDLK_d) |
---|
429 | this->bRollR = event.bPressed; |
---|
430 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
431 | this->bFire = event.bPressed; |
---|
432 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
433 | this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
434 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
435 | this->getWeaponManager()->previousWeaponConfig(); |
---|
436 | else if( event.type == SDLK_w) |
---|
437 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
438 | else if( event.type == SDLK_s) |
---|
439 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
440 | else if( event.type == EV_MOUSE_MOTION) |
---|
441 | { |
---|
442 | this->xMouse = event.xRel; |
---|
443 | this->yMouse = event.yRel; |
---|
444 | mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1))); |
---|
445 | // if( xMouse*xMouse + yMouse*yMouse < 0.9) |
---|
446 | //this->setAbsDir(mouseDir); |
---|
447 | } |
---|
448 | } |
---|
449 | |
---|
450 | /** |
---|
451 | * |
---|
452 | */ |
---|
453 | bool SpaceShip::pickup(PowerUp* powerUp) |
---|
454 | { |
---|
455 | if(powerUp->isA(CL_WEAPON_POWER_UP)) { |
---|
456 | Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon(); |
---|
457 | WeaponManager* manager = this->getWeaponManager(); |
---|
458 | int slot = manager->getNextFreeSlot(0, weapon->getCapability()); |
---|
459 | if(slot >= 0) { |
---|
460 | manager->addWeapon(weapon, 0, slot); |
---|
461 | return true; |
---|
462 | } |
---|
463 | } |
---|
464 | else if(powerUp->isA(CL_PARAM_POWER_UP)) { |
---|
465 | ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); |
---|
466 | switch(ppu->getType()) { |
---|
467 | case PARAM_SHIELD: |
---|
468 | break; |
---|
469 | } |
---|
470 | } |
---|
471 | return false; |
---|
472 | } |
---|
473 | |
---|
474 | #include "weapons/aiming_turret.h" |
---|
475 | // FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG |
---|
476 | void SpaceShip::ADDWEAPON() |
---|
477 | { |
---|
478 | Weapon* turret = NULL; |
---|
479 | |
---|
480 | if ((float)rand()/RAND_MAX < .1) |
---|
481 | { |
---|
482 | //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET)) |
---|
483 | { |
---|
484 | turret = new Turret(); |
---|
485 | this->getWeaponManager()->addWeapon(turret, 2); |
---|
486 | this->getWeaponManager()->changeWeaponConfig(2); |
---|
487 | } |
---|
488 | } |
---|
489 | else |
---|
490 | { |
---|
491 | //if (this->getWeaponManager()->hasFreeSlot(3)) |
---|
492 | { |
---|
493 | turret = new AimingTurret(); |
---|
494 | this->getWeaponManager()->addWeapon(turret, 3); |
---|
495 | |
---|
496 | this->getWeaponManager()->changeWeaponConfig(3); |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | if(turret != NULL) |
---|
501 | { |
---|
502 | turret->setName("Turret"); |
---|
503 | turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1); |
---|
504 | } |
---|
505 | } |
---|