Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

mouse control works fine, still need some improvements (next steps: mouse sensitivity, lock mouse in center)

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