Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5904 was 5904, checked in by snellen, 18 years ago

space_ship.cc updated

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