Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: new algorithm for the Camera-Distance

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