Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

space_ship.cc and world.cc updated

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