Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6026 was 6005, checked in by bensch, 19 years ago

orxonox/trunk: automatic LOD-loading

File size: 11.2 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 Grauer
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
30#include "graphics_engine.h"
31
32using namespace std;
33
34CREATE_FACTORY(Helicopter, CL_HELICOPTER);
35
36/**
37 *  creates the controlable Spaceship
38 */
39Helicopter::Helicopter()
40{
41  this->init();
42}
43
44/**
45 *  destructs the spaceship, deletes alocated memory
46 */
47Helicopter::~Helicopter ()
48{
49}
50
51/**
52 * loads a Spaceships information from a specified file.
53 * @param fileName the name of the File to load the spaceship from (absolute path)
54 */
55Helicopter::Helicopter(const char* fileName)
56{
57  this->init();
58  TiXmlDocument doc(fileName);
59
60  if(!doc.LoadFile())
61  {
62    PRINTF(2)("Loading file %s failed for Helicopter.\n", fileName);
63    return;
64  }
65
66  this->loadParams(doc.RootElement());
67}
68
69/**
70 *  creates a new Spaceship from Xml Data
71 * @param root the xml element containing spaceship data
72
73   @todo add more parameters to load
74*/
75Helicopter::Helicopter(const TiXmlElement* root)
76{
77  this->init();
78  if (root != NULL)
79    this->loadParams(root);
80
81  //weapons:
82  Weapon* wpRight = new TestGun(0);
83  wpRight->setName("testGun Right");
84  Weapon* wpLeft = new TestGun(1);
85  wpLeft->setName("testGun Left");
86  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
87
88  cannon->setName("BFG");
89
90  this->getWeaponManager()->addWeapon(wpLeft, 1, 0);
91  this->getWeaponManager()->addWeapon(wpRight,1 ,1);
92  this->getWeaponManager()->addWeapon(cannon, 0, 6);
93
94  //this->getWeaponManager()->addWeapon(turret, 3, 0);
95
96  this->getWeaponManager()->changeWeaponConfig(1);
97}
98
99
100/**
101 * initializes a Spaceship
102 */
103void Helicopter::init()
104{
105//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
106  this->setClassID(CL_HELICOPTER, "Helicopter");
107
108  PRINTF(4)("SPACESHIP 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.001;
118
119  cycle = 0.0;
120
121
122  travelSpeed = 15.0;
123  this->velocity = Vector(0.0,0.0,0.0);
124  this->velocityDir = Vector(1.0,0.0,0.0);
125
126//   GLGuiButton* button = new GLGuiPushButton();
127//   button->show();
128//   button->setLabel("orxonox");
129//   button->setBindNode(this);
130
131  //add events to the eventlist
132  registerEvent(SDLK_w);
133  registerEvent(SDLK_s);
134  registerEvent(SDLK_a);
135  registerEvent(SDLK_d);
136  registerEvent(SDLK_q);
137  registerEvent(SDLK_e);
138  registerEvent(KeyMapper::PEV_FIRE1);
139  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
140  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
141  registerEvent(SDLK_PAGEUP);
142  registerEvent(SDLK_PAGEDOWN);
143  registerEvent(EV_MOUSE_MOTION);
144
145  this->getWeaponManager()->setSlotCount(7);
146
147  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
148  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
149
150  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
151  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
152
153  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
154  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
155
156  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
157  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
158
159  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
160  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
161
162  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
163  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
164//
165   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
166   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
167   //
168//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
169//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
170//
171//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
172//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
173
174}
175
176/**
177 * loads the Settings of a Helicopter from an XML-element.
178 * @param root the XML-element to load the Spaceship's properties from
179 */
180void Helicopter::loadParams(const TiXmlElement* root)
181{
182  static_cast<WorldEntity*>(this)->loadParams(root);
183}
184
185
186/**
187 * adds a weapon to the weapon list of the spaceship
188 * @param weapon to add
189*/
190void Helicopter::addWeapon(Weapon* weapon)
191{
192  this->getWeaponManager()->addWeapon(weapon);
193}
194
195
196/**
197 *  removes a weapon from the spaceship
198 * @param weapon to remove
199*/
200void Helicopter::removeWeapon(Weapon* weapon)
201{
202  this->getWeaponManager()->removeWeapon(weapon);
203}
204
205/**
206 *  effect that occurs after the Helicopter is spawned
207*/
208void Helicopter::postSpawn ()
209{
210  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
211}
212
213/**
214 *  the action occuring if the spaceship left the game
215*/
216void Helicopter::leftWorld ()
217{}
218
219/**
220 *  this function is called, when two entities collide
221 * @param entity: the world entity with whom it collides
222 *
223 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
224 */
225void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
226{
227  if (entity->isA(CL_TURRET_POWER_UP))
228  {
229    this->ADDWEAPON();
230    }
231//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
232}
233
234
235
236/**
237 *  the function called for each passing timeSnap
238 * @param time The timespan passed since last update
239*/
240void Helicopter::tick (float time)
241{
242     cycle += time;
243  // spaceship controlled movement
244  this->calculateVelocity(time);
245
246  Vector move = (velocity)*time;
247
248  //orient the spaceship model in the direction of movement.
249
250  // this is the air friction (necessary for a smooth control)
251  if(velocity.len() != 0) velocity -= velocity*0.01;
252
253  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
254
255  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
256
257  this->shiftCoor (move);
258
259  this->getWeaponManager()->tick(time);
260  // weapon system manipulation
261  this->weaponAction();
262}
263
264/**
265 *  calculate the velocity
266 * @param time the timeslice since the last frame
267*/
268void Helicopter::calculateVelocity (float time)
269{
270  Vector accel(0.0, 0.0, 0.0);
271  //Vector rot(0.0, 0.0, 0.0);
272  //float rotVal = 0.0;
273  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
274  /* calculate the direction in which the craft is heading  */
275
276  if( this->bUp )
277   {
278     //this->shiftCoor(this->getAbsDirX());
279      accel += (this->getAbsDirX())*2;
280   }
281
282  if( this->bDown )
283   {
284     //this->shiftCoor((this->getAbsDirX())*-1);
285     accel -= (this->getAbsDirX())*2;
286   }
287
288  if( this->bLeft /* > -this->getRelCoor().z*2*/)
289  {
290    this->shiftDir(Quaternion(time, Vector(0,1,0)));
291//    accel -= rightDirection;
292    //velocityDir.normalize();
293    //rot +=Vector(1,0,0);
294    //rotVal -= .4;
295  }
296  if( this->bRight /* > this->getRelCoor().z*2*/)
297  {
298    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
299
300    //    accel += rightDirection;
301    //velocityDir.normalize();
302    //rot += Vector(1,0,0);
303    //rotVal += .4;
304  }
305
306  if( this->bRollL /* > -this->getRelCoor().z*2*/)
307  {
308    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
309//    accel -= rightDirection;
310    //velocityDir.normalize();
311    //rot +=Vector(1,0,0);
312    //rotVal -= .4;
313  }
314  if( this->bRollR /* > this->getRelCoor().z*2*/)
315  {
316    this->shiftDir(Quaternion(time, Vector(1,0,0)));
317
318    //    accel += rightDirection;
319    //velocityDir.normalize();
320    //rot += Vector(1,0,0);
321    //rotVal += .4;
322  }
323  if (this->bAscend )
324  {
325    this->shiftDir(Quaternion(time, Vector(0,0,1)));
326
327//    accel += upDirection;
328    //velocityDir.normalize();
329    //rot += Vector(0,0,1);
330    //rotVal += .4;
331  }
332  if (this->bDescend )
333  {
334    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
335
336    //    accel -= upDirection;
337    //velocityDir.normalize();
338    //rot += Vector(0,0,1);
339    //rotVal -= .4;
340  }
341
342  velocity += accel;
343  //rot.normalize();
344  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
345}
346
347
348void Helicopter::draw() const
349{
350  this->drawLODsafe();
351
352  this->getWeaponManager()->draw();
353}
354
355
356/**
357 * weapon manipulation by the player
358*/
359void Helicopter::weaponAction()
360{
361  if( this->bFire)
362    {
363      this->getWeaponManager()->fire();
364    }
365}
366
367/**
368 * @todo switch statement ??
369 */
370void Helicopter::process(const Event &event)
371{
372
373
374  if( event.type == SDLK_a)
375      this->bRollL = event.bPressed;
376  else if( event.type == SDLK_d)
377      this->bRollR = event.bPressed;
378  else if( event.type == KeyMapper::PEV_FIRE1)
379      this->bFire = event.bPressed;
380  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
381    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
382  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
383    this->getWeaponManager()->previousWeaponConfig();
384
385  else if( event.type == SDLK_w)
386    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
387  else if( event.type == SDLK_s)
388    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
389  else if( event.type == EV_MOUSE_MOTION)
390  {
391    this->xMouse = event.xRel;
392    this->yMouse = event.yRel;
393    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
394  }
395}
396
397#include "weapons/aiming_turret.h"
398// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
399void Helicopter::ADDWEAPON()
400{
401  Weapon* turret = NULL;
402
403  if ((float)rand()/RAND_MAX < .1)
404  {
405    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
406    {
407      turret = new Turret();
408      this->getWeaponManager()->addWeapon(turret, 2);
409      this->getWeaponManager()->changeWeaponConfig(2);
410    }
411  }
412  else
413  {
414    //if (this->getWeaponManager()->hasFreeSlot(3))
415    {
416      turret = new AimingTurret();
417      this->getWeaponManager()->addWeapon(turret, 3);
418
419      this->getWeaponManager()->changeWeaponConfig(3);
420    }
421  }
422
423  if(turret != NULL)
424  {
425    turret->setName("Turret");
426    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
427  }
428}
Note: See TracBrowser for help on using the repository browser.