Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/hover.cc @ 6658

Last change on this file since 6658 was 6637, checked in by bensch, 19 years ago

orxonox/trunk: merged the SpaceShipControl branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/spaceshipcontrol . -r6482:HEAD

minor conflicts, and added file hover.cc/h to the Makefile.

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