Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/space_ships/space_ship.cc @ 5964

Last change on this file since 5964 was 5964, checked in by bknecht, 18 years ago

Mousecontrol implemented! Mousesensitivity implemented! Mouse is grabbed! Control works fine

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