Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/std/src/world_entities/space_ships/helicopter.cc @ 7214

Last change on this file since 7214 was 7207, checked in by bensch, 19 years ago

orxonox/trunk: evil was within

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