Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5881 was 5881, checked in by bensch, 18 years ago

orxonox/branches/spaceshipcontroll: simple world-interactivity

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