Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6790 was 6724, checked in by bensch, 19 years ago

merged the Spaceshipcontrol back to the trunk

File size: 12.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: ...
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/nimrod#.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  controlVelocityX = 100;
121  controlVelocityY = 100;
122
123
124  this->velocity = Vector(0.0,0.0,0.0);
125  this->velocityDir = Vector(1.0,0.0,0.0);
126
127//   GLGuiButton* button = new GLGuiPushButton();
128//   button->show();
129//   button->setLabel("orxonox");
130//   button->setBindNode(this);
131
132  //add events to the eventlist
133  registerEvent(KeyMapper::PEV_UP);
134  registerEvent(KeyMapper::PEV_DOWN);
135  registerEvent(KeyMapper::PEV_LEFT);
136  registerEvent(KeyMapper::PEV_RIGHT);
137  registerEvent(SDLK_e);
138  registerEvent(SDLK_c);
139  registerEvent(KeyMapper::PEV_FIRE1);
140  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
141  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
142  registerEvent(EV_MOUSE_MOTION);
143
144  this->getWeaponManager()->setSlotCount(7);
145
146  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
147  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
148
149  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
150  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
151
152  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
153  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
154
155  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
156  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
157
158  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
159  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
160
161  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
162  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
163//
164   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
165   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
166   //
167//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
168//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
169//
170//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
171//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
172
173  this->getWeaponManager()->getFixedTarget()->setParent(this);
174  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
175
176  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
177
178}
179
180/**
181 * loads the Settings of a Helicopter from an XML-element.
182 * @param root the XML-element to load the Spaceship's properties from
183 */
184void Hover::loadParams(const TiXmlElement* root)
185{
186  static_cast<WorldEntity*>(this)->loadParams(root);
187}
188
189
190
191
192void Hover::enter()
193{
194  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
195  this->attachCamera();
196
197
198}
199
200void Hover::leave()
201{
202  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
203  this->detachCamera();
204
205}
206
207
208/**
209 *  effect that occurs after the Helicopter is spawned
210*/
211void Hover::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 Hover::leftWorld ()
220{}
221
222/**
223 *  this function is called, when two entities collide
224 * @param entity: the world entity with whom it collides
225 *
226 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
227 */
228void Hover::collidesWith(WorldEntity* entity, const Vector& location)
229{
230  if (entity->isA(CL_TURRET_POWER_UP))
231  {
232    this->ADDWEAPON();
233    }
234//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
235}
236
237
238
239/**
240 *  the function called for each passing timeSnap
241 * @param time The timespan passed since last update
242*/
243void Hover::tick (float time)
244{
245  if( xMouse != 0 || yMouse != 0)
246   {
247    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
248    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
249    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
250    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
251   }
252   
253  Quaternion xDir = Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0));
254  Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
255   
256  if ((this->getAbsDirY()).y < 0.8)
257  {
258     if((this->getAbsDirX()).y > 0)
259     {
260        if(yMouse > 0)  this->shiftDir(xDir*yDir);
261        else this->shiftDir(xDir);
262     }
263     else
264     {
265         if(yMouse < 0) this->shiftDir(xDir*yDir);
266         else this->shiftDir(xDir);
267     }
268  }
269  else this->shiftDir(xDir*yDir);
270 
271  if((this->getAbsDirZ()).y > 0.05) this->shiftDir(Quaternion(0.02,Vector(1,0,0)));
272  else if((this->getAbsDirZ()).y < -0.05) this->shiftDir(Quaternion(-0.02,Vector(1,0,0)));
273 
274  // spaceship controlled movement
275  this->calculateVelocity(time);
276
277  Vector move = (velocity)*time;
278
279  // this is the air friction (necessary for a smooth control)
280  if(velocity.len() != 0) velocity -= velocity*0.1;
281
282  //readjust
283 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
284  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
285
286  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
287
288  this->shiftCoor (move);
289  //this->shiftDir(Quaternion(-M_PI/4*tailrotorspeed, Vector(0,1,0)));
290
291  this->getWeaponManager()->tick(time);
292  // weapon system manipulation
293  this->weaponAction();
294}
295
296/**
297 *  calculate the velocity
298 * @param time the timeslice since the last frame
299*/
300void Hover::calculateVelocity (float time)
301{
302  Vector accel(0.0, 0.0, 0.0);
303  float rotValX = 0.0;
304  float rotValZ = 0.0;
305  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
306  /* calculate the direction in which the craft is heading  */
307
308  if( this->bUp )
309   {
310     //this->shiftCoor(this->getAbsDirX());
311     //accel -= this->getAbsDirY();
312     
313     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
314     //if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
315   }
316
317  if( this->bDown )
318   {
319     //this->shiftCoor((this->getAbsDirX())*-1);
320     //accel -= this->getAbsDirY();
321     
322     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
323     //rotValZ += time;
324   }
325
326  if( this->bLeft /* > -this->getRelCoor().z*2*/)
327  {
328    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
329    //accel -= this->getAbsDirY();
330    //velocityDir.normalize();
331   
332    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
333    //rotValX -= time;
334  }
335
336  if( this->bRight /* > this->getRelCoor().z*2*/)
337  {
338    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
339    //accel += this->getAbsDirY();
340    //velocityDir.normalize();
341   
342    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
343    //rotValX += time;
344  }
345
346  if( this->bRollL /* > -this->getRelCoor().z*2*/)
347  {
348    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
349    //accel -= rightDirection;
350    //velocityDir.normalize();
351    //rot +=Vector(1,0,0);
352    //rotVal -= .4;
353  }
354  if( this->bRollR /* > this->getRelCoor().z*2*/)
355  {
356    this->shiftDir(Quaternion(time, Vector(1,0,0)));
357
358    //accel += rightDirection;
359    //velocityDir.normalize();
360    //rot += Vector(1,0,0);
361    //rotVal += .4;
362  }
363  if (this->bAscend )
364  {
365    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
366
367    accel += this->getAbsDirY();
368    //rotorspeed += 0.05;
369    //if (rotorspeed >= 2) rotorspeed = 2;
370    //velocityDir.normalize();
371    //rot += Vector(0,0,1);
372    //rotVal += .4;
373  }
374  else
375  {
376    //if(rotorspeed >= 1.05) rotorspeed -= 0.05;
377  }
378
379  if (this->bDescend )
380  {
381    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
382
383    accel -= this->getAbsDirY();
384    //rotorspeed -= 0.05;
385    //if (rotorspeed <= 0) rotorspeed = 0;
386    //velocityDir.normalize();
387    //rot += Vector(0,0,1);
388    //rotVal -= .4;
389  }
390  else
391  {
392    //if(rotorspeed <= 0.05) rotorspeed += 0.05;
393  }
394
395  velocity += accel*4;
396  //if((this->getAbsDirX()).y <= 0.3 && (this->getAbsDirX()).y >= -0.3) this->shiftDir(Quaternion(rotValZ, Vector(0,0,1)));
397  //if((this->getAbsDirZ()).y <= 0.3 && (this->getAbsDirZ()).y >= -0.3) this->shiftDir(Quaternion(rotValX, Vector(1,0,0)));
398}
399
400
401void Hover::draw() const
402{
403  WorldEntity::draw();
404
405  this->getWeaponManager()->draw();
406}
407
408
409/**
410 * weapon manipulation by the player
411*/
412void Hover::weaponAction()
413{
414  if( this->bFire)
415    {
416      this->getWeaponManager()->fire();
417    }
418}
419
420/**
421 * @todo switch statement ??
422 */
423void Hover::process(const Event &event)
424{
425
426
427  if( event.type == KeyMapper::PEV_LEFT)
428      this->bLeft = event.bPressed;
429  else if( event.type == KeyMapper::PEV_RIGHT)
430      this->bRight = event.bPressed;
431  else if( event.type == KeyMapper::PEV_FIRE1)
432      this->bFire = event.bPressed;
433  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
434    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
435  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
436    this->getWeaponManager()->previousWeaponConfig();
437  else if( event.type == SDLK_e)
438    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
439  else if( event.type == SDLK_c)
440    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
441  else if( event.type == KeyMapper::PEV_UP)
442    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
443  else if( event.type == KeyMapper::PEV_DOWN)
444    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
445  else if( event.type == EV_MOUSE_MOTION)
446  {
447    this->xMouse = event.xRel*mouseSensitivity;
448    this->yMouse = event.yRel*mouseSensitivity;
449  }
450}
451
452#include "weapons/aiming_turret.h"
453// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
454void Hover::ADDWEAPON()
455{
456  Weapon* turret = NULL;
457
458  if ((float)rand()/RAND_MAX < .1)
459  {
460    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
461    {
462      turret = new Turret();
463      this->addWeapon(turret, 2);
464      this->getWeaponManager()->changeWeaponConfig(2);
465    }
466  }
467  else
468  {
469    //if (this->getWeaponManager()->hasFreeSlot(3))
470    {
471      turret = new AimingTurret();
472      this->addWeapon(turret, 3);
473
474      this->getWeaponManager()->changeWeaponConfig(3);
475    }
476  }
477
478  if(turret != NULL)
479  {
480    turret->setName("Turret");
481    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
482  }
483}
Note: See TracBrowser for help on using the repository browser.