Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5906 was 5906, 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  //orient the spaceship model in the direction of movement.
259  if( velocity.len() != 0 )
260   {
261     this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
262   }
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   Vector forwardDirection (1.0, 0.0, 0.0);
283   //direction = this->absDirection.apply (direction);
284   Vector rightDirection (0.0, 0.0, 1.0);
285   //orthDirection = orthDirection.cross (direction);
286   Vector upDirection (0.0, 1.0, 0.0);
287
288
289  if( this->bUp )
290   {
291    //if(velocity.len() == 0)
292     accel += forwardDirection;
293    //else
294      //travelSpeed++;
295   }
296
297  if( this->bDown )
298   {
299    //if(velocity.len() == 0)
300     accel -= forwardDirection;
301   // else
302     //travelSpeed--;
303   }
304
305  if( this->bLeft /* > -this->getRelCoor().z*2*/)
306  {
307    accel -= rightDirection;
308    //rot +=Vector(1,0,0);
309    //rotVal -= .4;
310  }
311  if( this->bRight /* > this->getRelCoor().z*2*/)
312  {
313    accel += rightDirection;
314    //rot += Vector(1,0,0);
315    //rotVal += .4;
316  }
317  if (this->bAscend )
318  {
319    accel += upDirection;
320    //rot += Vector(0,0,1);
321    //rotVal += .4;
322  }
323  if (this->bDescend )
324  {
325    accel -= upDirection;
326    //rot += Vector(0,0,1);
327    //rotVal -= .4;
328  }
329
330  velocity += accel*time;
331  //rot.normalize();
332  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
333}
334
335/**
336 * weapon manipulation by the player
337*/
338void SpaceShip::weaponAction()
339{
340  if( this->bFire)
341    {
342      this->getWeaponManager()->fire();
343    }
344}
345
346/**
347 * @todo switch statement ??
348 */
349void SpaceShip::process(const Event &event)
350{
351  if( event.type == KeyMapper::PEV_UP)
352      this->bUp = event.bPressed;
353  else if( event.type == KeyMapper::PEV_DOWN)
354      this->bDown = event.bPressed;
355  else if( event.type == KeyMapper::PEV_RIGHT)
356      this->bRight= event.bPressed;
357  else if( event.type == KeyMapper::PEV_LEFT)
358      this->bLeft = event.bPressed;
359  else if( event.type == KeyMapper::PEV_FIRE1)
360      this->bFire = event.bPressed;
361  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
362    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
363  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
364    this->getWeaponManager()->previousWeaponConfig();
365
366  else if( event.type == SDLK_PAGEUP)
367    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
368  else if( event.type == SDLK_PAGEDOWN)
369    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
370}
371
372#include "weapons/aiming_turret.h"
373// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
374void SpaceShip::ADDWEAPON()
375{
376  Weapon* turret = NULL;
377
378  if ((float)rand()/RAND_MAX < .1)
379  {
380    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
381    {
382      turret = new Turret();
383      this->getWeaponManager()->addWeapon(turret, 2);
384      this->getWeaponManager()->changeWeaponConfig(2);
385    }
386  }
387  else
388  {
389    //if (this->getWeaponManager()->hasFreeSlot(3))
390    {
391      turret = new AimingTurret();
392      this->getWeaponManager()->addWeapon(turret, 3);
393
394      this->getWeaponManager()->changeWeaponConfig(3);
395    }
396  }
397
398  if(turret != NULL)
399  {
400    turret->setName("Turret");
401    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
402  }
403}
Note: See TracBrowser for help on using the repository browser.