Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branch compiles again. factory problems solved

File size: 12.2 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
37#include "graphics_engine.h"
38
39using namespace std;
40
41CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP);
42
43/**
44 *  creates the controlable Spaceship
45 */
46SpaceShip::SpaceShip()
47{
48  this->init();
49}
50
51/**
52 *  destructs the spaceship, deletes alocated memory
53 */
54SpaceShip::~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 */
62SpaceShip::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*/
82SpaceShip::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 */
110void 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
124  cycle = 0.0;
125
126
127  travelSpeed = 15.0;
128  this->velocity = Vector(0.0,0.0,0.0);
129  this->velocityDir = Vector(1.0,0.0,0.0);
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}
180
181/**
182 * loads the Settings of a SpaceShip from an XML-element.
183 * @param root the XML-element to load the Spaceship's properties from
184 */
185void SpaceShip::loadParams(const TiXmlElement* root)
186{
187  static_cast<WorldEntity*>(this)->loadParams(root);
188}
189
190
191/**
192 * adds a weapon to the weapon list of the spaceship
193 * @param weapon to add
194*/
195void SpaceShip::addWeapon(Weapon* weapon)
196{
197  this->getWeaponManager()->addWeapon(weapon);
198}
199
200
201/**
202 *  removes a weapon from the spaceship
203 * @param weapon to remove
204*/
205void SpaceShip::removeWeapon(Weapon* weapon)
206{
207  this->getWeaponManager()->removeWeapon(weapon);
208}
209
210/**
211 *  effect that occurs after the SpaceShip is spawned
212*/
213void SpaceShip::postSpawn ()
214{
215  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
216}
217
218/**
219 *  the action occuring if the spaceship left the game
220*/
221void SpaceShip::leftWorld ()
222{}
223
224WorldEntity* ref = NULL;
225/**
226 *  this function is called, when two entities collide
227 * @param entity: the world entity with whom it collides
228 *
229 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
230 */
231void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
232{
233  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
234  {
235    this->ADDWEAPON();
236    ref = entity;
237    }
238//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
239}
240
241/**
242 *  draws the spaceship after transforming it.
243*/
244void SpaceShip::draw () const
245{
246  glMatrixMode(GL_MODELVIEW);
247  glPushMatrix();
248  /* translate */
249  glTranslatef (this->getAbsCoor ().x,
250                this->getAbsCoor ().y,
251                this->getAbsCoor ().z);
252  /* rotate */
253  Vector tmpRot = this->getAbsDir().getSpacialAxis();
254  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
255  this->model->draw();
256  glPopMatrix();
257
258  this->getWeaponManager()->draw();
259
260  //this->debug(0);
261}
262
263/**
264 *  the function called for each passing timeSnap
265 * @param time The timespan passed since last update
266*/
267void SpaceShip::tick (float time)
268{
269     cycle += time;
270  // spaceship controlled movement
271  this->calculateVelocity(time);
272
273  Vector move = (velocity)*time;
274
275  //orient the spaceship model in the direction of movement.
276
277  // this is the air friction (necessary for a smooth control)
278  if(velocity.len() != 0) velocity -= velocity*0.01;
279
280  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
281
282  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
283
284  this->shiftCoor (move);
285
286  this->getWeaponManager()->tick(time);
287  // weapon system manipulation
288  this->weaponAction();
289}
290
291/**
292 *  calculate the velocity
293 * @param time the timeslice since the last frame
294*/
295void SpaceShip::calculateVelocity (float time)
296{
297  Vector accel(0.0, 0.0, 0.0);
298  //Vector rot(0.0, 0.0, 0.0);
299  //float rotVal = 0.0;
300  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
301  /* calculate the direction in which the craft is heading  */
302
303  if( this->bUp )
304   {
305     //this->shiftCoor(this->getAbsDirX());
306      accel += (this->getAbsDirX())*2;
307   }
308
309  if( this->bDown )
310   {
311     //this->shiftCoor((this->getAbsDirX())*-1);
312     accel -= (this->getAbsDirX())*2;
313   }
314
315  if( this->bLeft /* > -this->getRelCoor().z*2*/)
316  {
317    this->shiftDir(Quaternion(time, Vector(0,1,0)));
318//    accel -= rightDirection;
319    //velocityDir.normalize();
320    //rot +=Vector(1,0,0);
321    //rotVal -= .4;
322  }
323  if( this->bRight /* > this->getRelCoor().z*2*/)
324  {
325    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
326
327    //    accel += rightDirection;
328    //velocityDir.normalize();
329    //rot += Vector(1,0,0);
330    //rotVal += .4;
331  }
332
333  if( this->bRollL /* > -this->getRelCoor().z*2*/)
334  {
335    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
336//    accel -= rightDirection;
337    //velocityDir.normalize();
338    //rot +=Vector(1,0,0);
339    //rotVal -= .4;
340  }
341  if( this->bRollR /* > this->getRelCoor().z*2*/)
342  {
343    this->shiftDir(Quaternion(time, Vector(1,0,0)));
344
345    //    accel += rightDirection;
346    //velocityDir.normalize();
347    //rot += Vector(1,0,0);
348    //rotVal += .4;
349  }
350  if (this->bAscend )
351  {
352    this->shiftDir(Quaternion(time, Vector(0,0,1)));
353
354//    accel += upDirection;
355    //velocityDir.normalize();
356    //rot += Vector(0,0,1);
357    //rotVal += .4;
358  }
359  if (this->bDescend )
360  {
361    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
362
363    //    accel -= upDirection;
364    //velocityDir.normalize();
365    //rot += Vector(0,0,1);
366    //rotVal -= .4;
367  }
368
369  velocity += accel;
370  //rot.normalize();
371  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
372}
373
374/**
375 * weapon manipulation by the player
376*/
377void SpaceShip::weaponAction()
378{
379  if( this->bFire)
380    {
381      this->getWeaponManager()->fire();
382    }
383}
384
385/**
386 * @todo switch statement ??
387 */
388void SpaceShip::process(const Event &event)
389{
390
391
392  if( event.type == SDLK_a)
393      this->bRollL = event.bPressed;
394  else if( event.type == SDLK_d)
395      this->bRollR = event.bPressed;
396  else if( event.type == KeyMapper::PEV_FIRE1)
397      this->bFire = event.bPressed;
398  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
399    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
400  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
401    this->getWeaponManager()->previousWeaponConfig();
402
403  else if( event.type == SDLK_w)
404    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
405  else if( event.type == SDLK_s)
406    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
407  else if( event.type == EV_MOUSE_MOTION)
408  {
409    this->xMouse = event.xRel;
410    this->yMouse = event.yRel;
411    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
412  }
413}
414
415/**
416 *
417 */
418bool SpaceShip::pickup(PowerUp* powerUp)
419{
420  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
421    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
422    WeaponManager* manager = this->getWeaponManager();
423    int slot = manager->getNextFreeSlot(0, weapon->getCapability());
424    if(slot >= 0) {
425      manager->addWeapon(weapon, 0, slot);
426      return true;
427    }
428  }
429  else if(powerUp->isA(CL_SHIELD_POWER_UP)) {
430
431  }
432  return false;
433}
434
435#include "weapons/aiming_turret.h"
436// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
437void SpaceShip::ADDWEAPON()
438{
439  Weapon* turret = NULL;
440
441  if ((float)rand()/RAND_MAX < .1)
442  {
443    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
444    {
445      turret = new Turret();
446      this->getWeaponManager()->addWeapon(turret, 2);
447      this->getWeaponManager()->changeWeaponConfig(2);
448    }
449  }
450  else
451  {
452    //if (this->getWeaponManager()->hasFreeSlot(3))
453    {
454      turret = new AimingTurret();
455      this->getWeaponManager()->addWeapon(turret, 3);
456
457      this->getWeaponManager()->changeWeaponConfig(3);
458    }
459  }
460
461  if(turret != NULL)
462  {
463    turret->setName("Turret");
464    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
465  }
466}
Note: See TracBrowser for help on using the repository browser.