Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/helicopter.cc @ 6952

Last change on this file since 6952 was 6947, checked in by bensch, 19 years ago

orxonox/trunk: merged the controll back

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