Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the controll back

File size: 11.7 KB
RevLine 
[6443]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:
[6947]12   main-programmer: Benjamin Knecht
[6443]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/**
[6724]38 *  creates the controlable Helicopter
[6443]39 */
40Helicopter::Helicopter()
41{
42  this->init();
43}
44
45/**
[6724]46 *  destructs the helicopter, deletes alocated memory
[6443]47 */
48Helicopter::~Helicopter ()
49{
50}
51
52/**
[6724]53 * loads a Helicopter information from a specified file.
54 * @param fileName the name of the File to load the helicopter from (absolute path)
[6443]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
[6724]107  PRINTF(4)("HELICOPTER INIT\n");
[6443]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;
[6637]116  mouseSensitivity = 0.05;
[6724]117  controlVelocityX = 100;
118  controlVelocityY = 100;
[6443]119
[6804]120
[6947]121  // initialization of cameraNode
[6724]122  this->cameraNode.setParent(this);
123  this->cameraNode.setParentMode(PNODE_ALL);
[6947]124  this->cameraNode.setRelCoor(Vector(0,1,0));
125 
[6724]126  // rotors
127  this->topRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
128  this->tailRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6804]129
[6724]130  this->topRotor.setParent(this);
131  this->tailRotor.setParent(this);
[6804]132
[6724]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)));
[6804]136
[6724]137  this->loadModel("models/ships/rotor.obj",1.0,3);
138  this->loadModel("models/ships/rotor.obj",0.2,4);
[6443]139
140
141  this->velocity = Vector(0.0,0.0,0.0);
142  this->velocityDir = Vector(1.0,0.0,0.0);
[6947]143 
144  // very, very old stuff
145  //  GLGuiButton* button = new GLGuiPushButton();
146  //  button->show();
147  //  button->setLabel("orxonox");
148  //  button->setBindNode(this);
[6443]149
150  //add events to the eventlist
[6637]151  registerEvent(KeyMapper::PEV_UP);
152  registerEvent(KeyMapper::PEV_DOWN);
153  registerEvent(KeyMapper::PEV_LEFT);
154  registerEvent(KeyMapper::PEV_RIGHT);
[6443]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
[6947]182  this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
183  this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
184
[6724]185  this->getWeaponManager()->getFixedTarget()->setParent(&(this->cameraNode));
186  this->getWeaponManager()->getFixedTarget()->setRelCoor(0,0,0);
[6443]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{
[6512]198  WorldEntity::loadParams(root);
[6443]199}
200
[6871]201void Helicopter::enter()
[6724]202{
[6871]203  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
[6724]204  State::getCamera()->setParentSoft(this->getWeaponManager()->getFixedTarget());
205  State::getCameraTarget()->setParentSoft(this->getWeaponManager()->getFixedTarget());
206}
[6443]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/**
[6724]225 *  the action occuring if the helicopter left the game
[6443]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{
[6804]248  Playable::tick(time);
249
[6724]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  }
[6804]257
[6724]258  // rotorrotation
259  this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
260  this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
[6804]261
[6443]262  // spaceship controlled movement
263  this->calculateVelocity(time);
264
[6637]265  Vector move = (velocity)*time;
[6443]266
267  // this is the air friction (necessary for a smooth control)
268  if(velocity.len() != 0) velocity -= velocity*0.1;
[6804]269
[6443]270
271  //readjust
[6947]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)));
[6443]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);
[6637]286  float rotValX = 0.0;
287  float rotValZ = 0.0;
[6443]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();
[6804]295
[6637]296     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
297     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
[6443]298   }
299   else
300   {
[6637]301       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1)));
[6443]302   }
303
304  if( this->bDown )
305   {
306     //this->shiftCoor((this->getAbsDirX())*-1);
307     //accel -= this->getAbsDirY();
[6804]308
[6637]309     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
310     rotValZ += time;
[6443]311   }
312   else
313   {
[6637]314         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
[6443]315   }
316
[6947]317  if( this->bLeft )
[6443]318  {
319    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
320    //accel -= this->getAbsDirY();
321    //velocityDir.normalize();
[6804]322
[6637]323    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
324    rotValX -= time;
[6443]325  }
326  else
327   {
[6637]328         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
[6443]329   }
330
[6947]331  if( this->bRight )
[6443]332  {
333    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
[6637]334    //accel += this->getAbsDirY();
[6443]335    //velocityDir.normalize();
[6804]336
[6637]337    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
338    rotValX += time;
[6443]339  }
340  else
341   {
[6637]342         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
[6443]343   }
344
[6947]345  if( this->bRollL )
[6443]346  {
347    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
348  }
[6947]349  if( this->bRollR )
[6443]350  {
351    this->shiftDir(Quaternion(time, Vector(1,0,0)));
352  }
353  if (this->bAscend )
354  {
[6637]355    accel += this->getAbsDirY();
[6443]356  }
357
358  if (this->bDescend )
359  {
[6637]360    accel -= this->getAbsDirY();
[6443]361  }
362
[6637]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)));
[6443]366}
367
368
369void Helicopter::draw() const
370{
371  WorldEntity::draw();
[6804]372
[6724]373  glMatrixMode(GL_MODELVIEW);
374    glPushMatrix();
[6443]375
[6724]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 ();
[6443]396}
397
398/**
399 * @todo switch statement ??
400 */
401void Helicopter::process(const Event &event)
402{
[6804]403  Playable::process(event);
[6443]404
[6637]405  if( event.type == KeyMapper::PEV_LEFT)
[6443]406      this->bLeft = event.bPressed;
[6637]407  else if( event.type == KeyMapper::PEV_RIGHT)
[6443]408      this->bRight = event.bPressed;
409  else if( event.type == SDLK_e)
[6947]410    this->bAscend = event.bPressed;
[6443]411  else if( event.type == SDLK_c)
[6947]412    this->bDescend = event.bPressed;
[6637]413  else if( event.type == KeyMapper::PEV_UP)
[6947]414    this->bUp = event.bPressed;
[6637]415  else if( event.type == KeyMapper::PEV_DOWN)
[6947]416    this->bDown = event.bPressed;
[6443]417  else if( event.type == EV_MOUSE_MOTION)
418  {
419    this->xMouse = event.xRel*mouseSensitivity;
420    this->yMouse = event.yRel*mouseSensitivity;
421
[6724]422    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0)));
[6804]423
[6724]424    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
425
[6804]426
[6724]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    }
[6443]440}
Note: See TracBrowser for help on using the repository browser.