Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented the helicopter

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