Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

spaceship updated

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