Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/world_entities/space_ships/space_ship.cc @ 5965

Last change on this file since 5965 was 5965, checked in by manuel, 18 years ago

many changes:
created generic weapon_power_up that can contain any kind of weapon
space_ship is now extendable and can pickup weapon_power_ups

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