Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5973 was 5915, checked in by bensch, 19 years ago

orxonox/trunk: merged branches/spaceshipControll back to the trunk:
merged with command:
svn merge -r5835:HEAD branches/spaceshipcontroll trunk
conflicts resolved with dynamic indentation methodology algorithms

File size: 10.6 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
261  this->shiftCoor (move);
262
263  this->getWeaponManager()->tick(time);
264  // weapon system manipulation
265  this->weaponAction();
266}
267
268/**
269 *  calculate the velocity
270 * @param time the timeslice since the last frame
271*/
272void SpaceShip::calculateVelocity (float time)
273{
274  Vector accel(0.0, 0.0, 0.0);
275  //Vector rot(0.0, 0.0, 0.0);
276  //float rotVal = 0.0;
277  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
278  /* calculate the direction in which the craft is heading  */
279
280  if( this->bUp )
281   {
282     this->shiftCoor(this->getAbsDirX());
283      //if(travelSpeed <= 5000)  travelSpeed += 10;
284   }
285
286  if( this->bDown )
287   {
288     this->shiftCoor(Vector()-this->getAbsDirX());
289     //if(travelSpeed >= 10) travelSpeed -= 10;
290   }
291
292  if( this->bLeft /* > -this->getRelCoor().z*2*/)
293  {
294    this->shiftDir(Quaternion(time, Vector(0,1,0)));
295//    accel -= rightDirection;
296    //velocityDir.normalize();
297    //rot +=Vector(1,0,0);
298    //rotVal -= .4;
299  }
300  if( this->bRight /* > this->getRelCoor().z*2*/)
301  {
302    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
303
304    //    accel += rightDirection;
305    //velocityDir.normalize();
306    //rot += Vector(1,0,0);
307    //rotVal += .4;
308  }
309  if (this->bAscend )
310  {
311    this->shiftDir(Quaternion(time, Vector(0,0,1)));
312
313//    accel += upDirection;
314    //velocityDir.normalize();
315    //rot += Vector(0,0,1);
316    //rotVal += .4;
317  }
318  if (this->bDescend )
319  {
320    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
321
322    //    accel -= upDirection;
323    //velocityDir.normalize();
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.