Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/space_ship.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.6 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 "executor/executor.h"
20#include "space_ship.h"
21
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(SpaceShip, CL_SPACE_SHIP);
38
39/**
40 *  creates the controlable Spaceship
41 */
42SpaceShip::SpaceShip()
43{
44  this->init();
45}
46
47/**
48 *  destructs the spaceship, deletes alocated memory
49 */
50SpaceShip::~SpaceShip ()
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 */
58SpaceShip::SpaceShip(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 spaceship.\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*/
78SpaceShip::SpaceShip(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 SpaceShip::init()
107{
108//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
109  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
110
111  PRINTF(4)("SPACESHIP INIT\n");
112
113  EventHandler::getInstance()->grabEvents(true);
114
115  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
116  bFire = false;
117  xMouse = yMouse = 0;
118  mouseSensitivity = 0.001;
119
120  cycle = 0.0;
121
122
123  travelSpeed = 15.0;
124  this->velocity = Vector(0.0,0.0,0.0);
125  this->velocityDir = Vector(1.0,0.0,0.0);
126
127//   GLGuiButton* button = new GLGuiPushButton();
128//   button->show();
129//   button->setLabel("orxonox");
130//   button->setBindNode(this);
131
132  //add events to the eventlist
133  registerEvent(SDLK_w);
134  registerEvent(SDLK_s);
135  registerEvent(SDLK_a);
136  registerEvent(SDLK_d);
137  registerEvent(SDLK_q);
138  registerEvent(SDLK_e);
139  registerEvent(KeyMapper::PEV_FIRE1);
140  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
141  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
142  registerEvent(SDLK_PAGEUP);
143  registerEvent(SDLK_PAGEDOWN);
144  registerEvent(EV_MOUSE_MOTION);
145
146  this->getWeaponManager()->setSlotCount(7);
147
148  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
149  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
150
151  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
152  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
153
154  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
155  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
156
157  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
158  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
159
160  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
161  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
162
163  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
164  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
165//
166   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
167   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
168   //
169//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
170//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
171//
172//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
173//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
174
175}
176
177/**
178 * loads the Settings of a SpaceShip from an XML-element.
179 * @param root the XML-element to load the Spaceship's properties from
180 */
181void SpaceShip::loadParams(const TiXmlElement* root)
182{
183  static_cast<WorldEntity*>(this)->loadParams(root);
184}
185
186
187/**
188 * adds a weapon to the weapon list of the spaceship
189 * @param weapon to add
190*/
191void SpaceShip::addWeapon(Weapon* weapon)
192{
193  this->getWeaponManager()->addWeapon(weapon);
194}
195
196
197/**
198 *  removes a weapon from the spaceship
199 * @param weapon to remove
200*/
201void SpaceShip::removeWeapon(Weapon* weapon)
202{
203  this->getWeaponManager()->removeWeapon(weapon);
204}
205
206/**
207 *  effect that occurs after the SpaceShip is spawned
208*/
209void SpaceShip::postSpawn ()
210{
211  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
212}
213
214/**
215 *  the action occuring if the spaceship left the game
216*/
217void SpaceShip::leftWorld ()
218{}
219
220WorldEntity* ref = NULL;
221/**
222 *  this function is called, when two entities collide
223 * @param entity: the world entity with whom it collides
224 *
225 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
226 */
227void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
228{
229  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
230  {
231    this->ADDWEAPON();
232    ref = entity;
233    }
234//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
235}
236
237/**
238 *  draws the spaceship after transforming it.
239*/
240void SpaceShip::draw () const
241{
242  glMatrixMode(GL_MODELVIEW);
243  glPushMatrix();
244  /* translate */
245  glTranslatef (this->getAbsCoor ().x,
246                this->getAbsCoor ().y,
247                this->getAbsCoor ().z);
248  /* rotate */
249  Vector tmpRot = this->getAbsDir().getSpacialAxis();
250  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
251  this->getModel()->draw();
252  glPopMatrix();
253
254  this->getWeaponManager()->draw();
255
256  //this->debug(0);
257}
258
259/**
260 *  the function called for each passing timeSnap
261 * @param time The timespan passed since last update
262*/
263void SpaceShip::tick (float time)
264{
265     cycle += time;
266  // spaceship controlled movement
267  this->calculateVelocity(time);
268
269  Vector move = (velocity)*time;
270
271  //orient the spaceship model in the direction of movement.
272
273  // this is the air friction (necessary for a smooth control)
274  if(velocity.len() != 0) velocity -= velocity*0.01;
275
276  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
277
278  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
279
280  this->shiftCoor (move);
281
282  this->getWeaponManager()->tick(time);
283  // weapon system manipulation
284  this->weaponAction();
285}
286
287/**
288 *  calculate the velocity
289 * @param time the timeslice since the last frame
290*/
291void SpaceShip::calculateVelocity (float time)
292{
293  Vector accel(0.0, 0.0, 0.0);
294  //Vector rot(0.0, 0.0, 0.0);
295  //float rotVal = 0.0;
296  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
297  /* calculate the direction in which the craft is heading  */
298
299  if( this->bUp )
300   {
301     //this->shiftCoor(this->getAbsDirX());
302      accel += (this->getAbsDirX())*2;
303   }
304
305  if( this->bDown )
306   {
307     //this->shiftCoor((this->getAbsDirX())*-1);
308     accel -= (this->getAbsDirX())*2;
309   }
310
311  if( this->bLeft /* > -this->getRelCoor().z*2*/)
312  {
313    this->shiftDir(Quaternion(time, Vector(0,1,0)));
314//    accel -= rightDirection;
315    //velocityDir.normalize();
316    //rot +=Vector(1,0,0);
317    //rotVal -= .4;
318  }
319  if( this->bRight /* > this->getRelCoor().z*2*/)
320  {
321    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
322
323    //    accel += rightDirection;
324    //velocityDir.normalize();
325    //rot += Vector(1,0,0);
326    //rotVal += .4;
327  }
328
329  if( this->bRollL /* > -this->getRelCoor().z*2*/)
330  {
331    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
332//    accel -= rightDirection;
333    //velocityDir.normalize();
334    //rot +=Vector(1,0,0);
335    //rotVal -= .4;
336  }
337  if( this->bRollR /* > this->getRelCoor().z*2*/)
338  {
339    this->shiftDir(Quaternion(time, Vector(1,0,0)));
340
341    //    accel += rightDirection;
342    //velocityDir.normalize();
343    //rot += Vector(1,0,0);
344    //rotVal += .4;
345  }
346  if (this->bAscend )
347  {
348    this->shiftDir(Quaternion(time, Vector(0,0,1)));
349
350//    accel += upDirection;
351    //velocityDir.normalize();
352    //rot += Vector(0,0,1);
353    //rotVal += .4;
354  }
355  if (this->bDescend )
356  {
357    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
358
359    //    accel -= upDirection;
360    //velocityDir.normalize();
361    //rot += Vector(0,0,1);
362    //rotVal -= .4;
363  }
364
365  velocity += accel;
366  //rot.normalize();
367  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
368}
369
370/**
371 * weapon manipulation by the player
372*/
373void SpaceShip::weaponAction()
374{
375  if( this->bFire)
376    {
377      this->getWeaponManager()->fire();
378    }
379}
380
381/**
382 * @todo switch statement ??
383 */
384void SpaceShip::process(const Event &event)
385{
386
387
388  if( event.type == SDLK_a)
389      this->bRollL = event.bPressed;
390  else if( event.type == SDLK_d)
391      this->bRollR = event.bPressed;
392  else if( event.type == KeyMapper::PEV_FIRE1)
393      this->bFire = event.bPressed;
394  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
395    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
396  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
397    this->getWeaponManager()->previousWeaponConfig();
398
399  else if( event.type == SDLK_w)
400    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
401  else if( event.type == SDLK_s)
402    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
403  else if( event.type == EV_MOUSE_MOTION)
404  {
405    this->xMouse = event.xRel;
406    this->yMouse = event.yRel;
407    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
408  }
409}
410
411#include "weapons/aiming_turret.h"
412// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
413void SpaceShip::ADDWEAPON()
414{
415  Weapon* turret = NULL;
416
417  if ((float)rand()/RAND_MAX < .1)
418  {
419    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
420    {
421      turret = new Turret();
422      this->getWeaponManager()->addWeapon(turret, 2);
423      this->getWeaponManager()->changeWeaponConfig(2);
424    }
425  }
426  else
427  {
428    //if (this->getWeaponManager()->hasFreeSlot(3))
429    {
430      turret = new AimingTurret();
431      this->getWeaponManager()->addWeapon(turret, 3);
432
433      this->getWeaponManager()->changeWeaponConfig(3);
434    }
435  }
436
437  if(turret != NULL)
438  {
439    turret->setName("Turret");
440    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
441  }
442}
Note: See TracBrowser for help on using the repository browser.