Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/space_ship.cc @ 6123

Last change on this file since 6123 was 6078, checked in by bensch, 19 years ago

orxonox/trunk: PNode compiles again
PNode is now handled with the erase function of stl, rather than remove (which is totally the wrong way when iterating through a list and delete elements)

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