Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/space_ship.cc @ 6028

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

orxonox/trunk: automatic LOD-loading

File size: 11.3 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  this->drawLODsafe();
243
244  this->getWeaponManager()->draw();
245
246  //this->debug(0);
247}
248
249/**
250 *  the function called for each passing timeSnap
251 * @param time The timespan passed since last update
252*/
253void SpaceShip::tick (float time)
254{
255     cycle += time;
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.01;
265
266  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
267
268  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
269
270  this->shiftCoor (move);
271
272  this->getWeaponManager()->tick(time);
273  // weapon system manipulation
274  this->weaponAction();
275}
276
277/**
278 *  calculate the velocity
279 * @param time the timeslice since the last frame
280*/
281void SpaceShip::calculateVelocity (float time)
282{
283  Vector accel(0.0, 0.0, 0.0);
284  //Vector rot(0.0, 0.0, 0.0);
285  //float rotVal = 0.0;
286  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
287  /* calculate the direction in which the craft is heading  */
288
289  if( this->bUp )
290   {
291     //this->shiftCoor(this->getAbsDirX());
292      accel += (this->getAbsDirX())*2;
293   }
294
295  if( this->bDown )
296   {
297     //this->shiftCoor((this->getAbsDirX())*-1);
298     accel -= (this->getAbsDirX())*2;
299   }
300
301  if( this->bLeft /* > -this->getRelCoor().z*2*/)
302  {
303    this->shiftDir(Quaternion(time, Vector(0,1,0)));
304//    accel -= rightDirection;
305    //velocityDir.normalize();
306    //rot +=Vector(1,0,0);
307    //rotVal -= .4;
308  }
309  if( this->bRight /* > this->getRelCoor().z*2*/)
310  {
311    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
312
313    //    accel += rightDirection;
314    //velocityDir.normalize();
315    //rot += Vector(1,0,0);
316    //rotVal += .4;
317  }
318
319  if( this->bRollL /* > -this->getRelCoor().z*2*/)
320  {
321    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
322//    accel -= rightDirection;
323    //velocityDir.normalize();
324    //rot +=Vector(1,0,0);
325    //rotVal -= .4;
326  }
327  if( this->bRollR /* > this->getRelCoor().z*2*/)
328  {
329    this->shiftDir(Quaternion(time, Vector(1,0,0)));
330
331    //    accel += rightDirection;
332    //velocityDir.normalize();
333    //rot += Vector(1,0,0);
334    //rotVal += .4;
335  }
336  if (this->bAscend )
337  {
338    this->shiftDir(Quaternion(time, Vector(0,0,1)));
339
340//    accel += upDirection;
341    //velocityDir.normalize();
342    //rot += Vector(0,0,1);
343    //rotVal += .4;
344  }
345  if (this->bDescend )
346  {
347    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
348
349    //    accel -= upDirection;
350    //velocityDir.normalize();
351    //rot += Vector(0,0,1);
352    //rotVal -= .4;
353  }
354
355  velocity += accel;
356  //rot.normalize();
357  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
358}
359
360/**
361 * weapon manipulation by the player
362*/
363void SpaceShip::weaponAction()
364{
365  if( this->bFire)
366    {
367      this->getWeaponManager()->fire();
368    }
369}
370
371/**
372 * @todo switch statement ??
373 */
374void SpaceShip::process(const Event &event)
375{
376
377
378  if( event.type == SDLK_a)
379      this->bRollL = event.bPressed;
380  else if( event.type == SDLK_d)
381      this->bRollR = event.bPressed;
382  else if( event.type == KeyMapper::PEV_FIRE1)
383      this->bFire = event.bPressed;
384  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
385    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
386  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
387    this->getWeaponManager()->previousWeaponConfig();
388
389  else if( event.type == SDLK_w)
390    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
391  else if( event.type == SDLK_s)
392    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
393  else if( event.type == EV_MOUSE_MOTION)
394  {
395    this->xMouse = event.xRel;
396    this->yMouse = event.yRel;
397    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
398  }
399}
400
401#include "weapons/aiming_turret.h"
402// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
403void SpaceShip::ADDWEAPON()
404{
405  Weapon* turret = NULL;
406
407  if ((float)rand()/RAND_MAX < .1)
408  {
409    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
410    {
411      turret = new Turret();
412      this->getWeaponManager()->addWeapon(turret, 2);
413      this->getWeaponManager()->changeWeaponConfig(2);
414    }
415  }
416  else
417  {
418    //if (this->getWeaponManager()->hasFreeSlot(3))
419    {
420      turret = new AimingTurret();
421      this->getWeaponManager()->addWeapon(turret, 3);
422
423      this->getWeaponManager()->changeWeaponConfig(3);
424    }
425  }
426
427  if(turret != NULL)
428  {
429    turret->setName("Turret");
430    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
431  }
432}
Note: See TracBrowser for help on using the repository browser.