Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5878 was 5878, checked in by bknecht, 18 years ago

implemented space_ship control

File size: 9.5 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
31using namespace std;
32
33CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP);
34
35/**
36 *  creates the controlable Spaceship
37 */
38SpaceShip::SpaceShip()
39{
40        this->init();
41}
42
43/**
44 *  destructs the spaceship, deletes alocated memory
45 */
46SpaceShip::~SpaceShip ()
47{
48  /* do not delete the weapons, they are contained in the pnode tree
49  and will be deleted there.
50  this only frees the memory allocated to save the list.
51  */
52  delete this->weaponMan;
53}
54
55/**
56 * loads a Spaceships information from a specified file.
57 * @param fileName the name of the File to load the spaceship from (absolute path)
58 */
59SpaceShip::SpaceShip(const char* fileName)
60{
61  this->init();
62  TiXmlDocument doc(fileName);
63
64  if(!doc.LoadFile())
65  {
66    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName);
67    return;
68  }
69
70  this->loadParams(doc.RootElement());
71}
72
73/**
74 *  creates a new Spaceship from Xml Data
75 * @param root the xml element containing spaceship data
76
77   @todo add more parameters to load
78*/
79SpaceShip::SpaceShip(const TiXmlElement* root)
80{
81  this->init();
82  if (root != NULL)
83    this->loadParams(root);
84
85  //weapons:
86  Weapon* wpRight = new TestGun(0);
87  wpRight->setName("testGun Right");
88  Weapon* wpLeft = new TestGun(1);
89  wpLeft->setName("testGun Left");
90  Weapon* cannon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(CL_CANNON));
91
92  cannon->setName("BFG");
93
94  this->weaponMan->addWeapon(wpLeft, 1, 0);
95  this->weaponMan->addWeapon(wpRight,1 ,1);
96  this->weaponMan->addWeapon(cannon, 0, 6);
97
98  //this->weaponMan->addWeapon(turret, 3, 0);
99
100  this->weaponMan->changeWeaponConfig(1);
101}
102
103
104/**
105 * initializes a Spaceship
106 */
107void SpaceShip::init()
108{
109//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
110  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
111
112  PRINTF(4)("SPACESHIP INIT\n");
113  travelSpeed = 15.0;
114  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
115  bFire = false;
116  acceleration = 10.0;
117
118//   GLGuiButton* button = new GLGuiPushButton();
119//   button->show();
120//   button->setLabel("orxonox");
121//   button->setBindNode(this);
122
123  this->weaponMan = new WeaponManager(this);
124  this->weaponMan->setSlotCount(7);
125
126  this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0));
127  this->weaponMan->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
128
129  this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0));
130  this->weaponMan->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
131
132  this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5));
133  this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
134
135  this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5));
136  this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
137
138  this->weaponMan->setSlotPosition(4, Vector(-1.5, -.5, .5));
139  this->weaponMan->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
140
141  this->weaponMan->setSlotPosition(5, Vector(-1.5, -.5, -.5));
142  this->weaponMan->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
143//
144   this->weaponMan->setSlotPosition(6, Vector(-1, 0.0, 0));
145   this->weaponMan->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
146   //
147//   this->weaponMan->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
148//   this->weaponMan->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
149//
150//   this->weaponMan->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
151//   this->weaponMan->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
152
153}
154
155/**
156 * loads the Settings of a SpaceShip from an XML-element.
157 * @param root the XML-element to load the Spaceship's properties from
158 */
159void SpaceShip::loadParams(const TiXmlElement* root)
160{
161  static_cast<WorldEntity*>(this)->loadParams(root);
162}
163
164
165/**
166 * adds a weapon to the weapon list of the spaceship
167 * @param weapon to add
168*/
169void SpaceShip::addWeapon(Weapon* weapon)
170{
171  this->weaponMan->addWeapon(weapon);
172}
173
174
175/**
176 *  removes a weapon from the spaceship
177 * @param weapon to remove
178*/
179void SpaceShip::removeWeapon(Weapon* weapon)
180{
181  this->weaponMan->removeWeapon(weapon);
182}
183
184/**
185 *  effect that occurs after the SpaceShip is spawned
186*/
187void SpaceShip::postSpawn ()
188{
189  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
190}
191
192/**
193 *  the action occuring if the spaceship left the game
194*/
195void SpaceShip::leftWorld ()
196{}
197
198WorldEntity* ref = NULL;
199/**
200 *  this function is called, when two entities collide
201 * @param entity: the world entity with whom it collides
202 *
203 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
204 */
205void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
206{
207  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
208  {
209    this->ADDWEAPON();
210    ref = entity;
211    }
212//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
213}
214
215/**
216 *  draws the spaceship after transforming it.
217*/
218void SpaceShip::draw () const
219{
220  glMatrixMode(GL_MODELVIEW);
221  glPushMatrix();
222  /* translate */
223  glTranslatef (this->getAbsCoor ().x,
224                this->getAbsCoor ().y,
225                this->getAbsCoor ().z);
226  /* rotate */
227  Vector tmpRot = this->getAbsDir().getSpacialAxis();
228  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
229  this->model->draw();
230  glPopMatrix();
231
232  this->weaponMan->draw();
233
234  //this->debug(0);
235}
236
237/**
238 *  the function called for each passing timeSnap
239 * @param time The timespan passed since last update
240*/
241void SpaceShip::tick (float time)
242{
243  // spaceship controlled movement
244  this->move(time);
245
246  this->weaponMan->tick(time);
247  // weapon system manipulation
248  this->weaponAction();
249}
250
251/**
252 *  action if spaceship moves
253 * @param time the timeslice since the last frame
254*/
255void SpaceShip::move (float time)
256{
257  Vector accel(0.0, 0.0, 0.0);
258  Vector rot(0.0, 0.0, 0.0);
259  float rotVal = 0.0;
260  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
261  /* calculate the direction in which the craft is heading  */
262  Vector direction (1.0, 0.0, 0.0);
263  //direction = this->absDirection.apply (direction);
264  Vector orthDirection (0.0, 0.0, 1.0);
265  //orthDirection = orthDirection.cross (direction);
266
267  if( this->bUp && this->getRelCoor().x < 20)
268    accel += direction;
269  if( this->bDown && this->getRelCoor().x > -5)
270    accel -= direction;
271
272  if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
273  {
274    accel -=(orthDirection);
275    rot +=Vector(1,0,0);
276    rotVal -= .4;
277  }
278  if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
279  {
280    accel += orthDirection;
281    rot += Vector(1,0,0);
282    rotVal += .4;
283  }
284  if (this->bAscend )
285  {
286    accel += Vector(0,1,0);
287    rot += Vector(0,0,1);
288    rotVal += .4;
289  }
290  if (this->bDescend )
291  {
292    accel -= Vector(0,1,0);
293    rot += Vector(0,0,1);
294    rotVal -= .4;
295  }
296
297  Vector move = accel * time *acceleration;
298
299/*  if (accel.z < 0)
300    this->setRelDirSoft(Quaternion(-.4, accel), 5);
301  else if (accel.z > 0)
302    this->setRelDirSoft(Quaternion(.4, accel), 5);
303  else*/
304  rot.normalize();
305  this->setRelDirSoft(Quaternion(rotVal, rot), 5);
306  this->shiftCoor (move);
307}
308
309/**
310 * weapon manipulation by the player
311*/
312void SpaceShip::weaponAction()
313{
314  if( this->bFire)
315    {
316      this->weaponMan->fire();
317    }
318}
319
320/**
321 * @todo switch statement ??
322 */
323void SpaceShip::process(const Event &event)
324{
325  if( event.type == KeyMapper::PEV_UP)
326      this->bUp = event.bPressed;
327  else if( event.type == KeyMapper::PEV_DOWN)
328      this->bDown = event.bPressed;
329  else if( event.type == KeyMapper::PEV_RIGHT)
330      this->bRight= event.bPressed;
331  else if( event.type == KeyMapper::PEV_LEFT)
332      this->bLeft = event.bPressed;
333  else if( event.type == KeyMapper::PEV_FIRE1)
334      this->bFire = event.bPressed;
335  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
336    this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
337  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
338    this->weaponMan->previousWeaponConfig();
339
340  else if( event.type == SDLK_PAGEUP)
341    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
342  else if( event.type == SDLK_PAGEDOWN)
343    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
344}
345
346#include "weapons/aiming_turret.h"
347// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
348void SpaceShip::ADDWEAPON()
349{
350  Weapon* turret = NULL;
351
352  if ((float)rand()/RAND_MAX < .1)
353  {
354    //if (this->weaponMan->hasFreeSlot(2, WTYPE_TURRET))
355    {
356      turret = new Turret();
357      this->weaponMan->addWeapon(turret, 2);
358      this->weaponMan->changeWeaponConfig(2);
359    }
360  }
361  else
362  {
363    //if (this->weaponMan->hasFreeSlot(3))
364    {
365      turret = new AimingTurret();
366      this->weaponMan->addWeapon(turret, 3);
367
368      this->weaponMan->changeWeaponConfig(3);
369    }
370  }
371
372  if(turret != NULL)
373  {
374    turret->setName("Turret");
375    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
376  }
377}
Note: See TracBrowser for help on using the repository browser.