Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

space_ship.cc updated

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