Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/world_entities/space_ships/space_ship.cc @ 6109

Last change on this file since 6109 was 6109, checked in by manuel, 18 years ago

some debugging

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