Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/spacecraft_2d.cc @ 9109

Last change on this file since 9109 was 9061, checked in by patrick, 18 years ago

merged the single_player branche to trunk

File size: 11.4 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 Grauer
13   co-programmer: ...
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "spacecraft_2d.h"
20
21#include "weapons/weapon_manager.h"
22#include "weapons/test_gun.h"
23#include "weapons/turret.h"
24#include "weapons/cannon.h"
25
26#include "util/loading/factory.h"
27#include "key_mapper.h"
28#include "state.h"
29
30#include "graphics_engine.h"
31#include "dot_emitter.h"
32#include "sprite_particles.h"
33
34#include "debug.h"
35
36CREATE_FACTORY(Spacecraft2D, CL_SPACECRAFT_2D);
37
38
39/**
40 * @brief loads a Spacecraft2D information from a specified file.
41 * @param fileName the name of the File to load the spacecraft_2d from (absolute path)
42 */
43Spacecraft2D::Spacecraft2D(const std::string& fileName)
44{
45  this->init();
46  TiXmlDocument doc(fileName);
47
48  if(!doc.LoadFile())
49  {
50    PRINTF(2)("Loading file %s failed for Spacecraft2D.\n", fileName.c_str());
51    return;
52  }
53
54  this->loadParams(doc.RootElement());
55}
56
57/**
58 * @brief creates a new Spaceship from Xml Data
59 * @param root the xml element containing spaceship data
60
61   @todo add more parameters to load
62*/
63Spacecraft2D::Spacecraft2D(const TiXmlElement* root)
64{
65  this->init();
66  if (root != NULL)
67    this->loadParams(root);
68
69  //weapons:
70  Weapon* wpRight = new TestGun(0);
71  wpRight->setName("testGun Right");
72  Weapon* wpLeft = new TestGun(1);
73  wpLeft->setName("testGun Left");
74  //Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_HYPERBLASTER));
75
76  //  cannon->setName("BFG");
77
78  this->addWeapon(wpLeft, 1, 0);
79  this->addWeapon(wpRight,1 ,1);
80  //this->addWeapon(cannon, 0, 2);
81
82  this->getWeaponManager().changeWeaponConfig(1);
83  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
84}
85
86
87/**
88 *  destructs the spacecraft_2d, deletes alocated memory
89 */
90Spacecraft2D::~Spacecraft2D ()
91{
92  this->setPlayer(NULL);
93}
94
95/**
96 * @brief initializes a Spacecraft2D
97 */
98void Spacecraft2D::init()
99{
100  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
101  this->setClassID(CL_SPACECRAFT_2D, "Spacecraft2D");
102
103  this->setSupportedPlaymodes(Playable::Full3D | Playable::Horizontal);
104
105
106  bForward = bBackward = bLeft = bRight = false;
107  mouseSensitivity = 0.005;
108
109  this->cameraLook = 0.0f;
110  this->rotation = 0.0f;
111  this->acceleration = 10.0f;
112  this->altitude = 0.0f;
113
114  this->setHealthMax(100);
115  this->setHealth(100);
116
117
118  // camera - issue
119  this->travelNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
120  //this->cameraNode.setParentMode(PNODE_ROTATE_MOVEMENT);
121  //this->cameraNode.setParent(this);
122
123  // rotors
124  // PARTICLES
125  this->burstEmitter = new DotEmitter(200, 5.0, .01);
126  this->burstEmitter->setParent(this);
127  this->burstEmitter->setRelCoor(0, -0.7, 0);
128  this->burstEmitter->setRelDir(Quaternion(-M_PI_2, Vector(0,0,1)));
129  this->burstEmitter->setName("Spacecraft2D_Burst_emitter_Left");
130
131
132  this->burstSystem = new SpriteParticles(1000);
133  this->burstSystem->addEmitter(this->burstEmitter);
134  this->burstSystem->setName("SpaceShip_Burst_System");
135  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
136  this->burstSystem->setLifeSpan(1.0, .3);
137  this->burstSystem->setRadius(0.0, 1.5);
138  this->burstSystem->setRadius(0.05, 1.8);
139  this->burstSystem->setRadius(.5, .8);
140  this->burstSystem->setRadius(1.0, 0);
141  this->burstSystem->setColor(0.0, .7,.7,1,.5);
142  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
143  this->burstSystem->setColor(0.5, .5,.5,.8,.3);
144  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
145
146
147  //add events to the eventlist of the Playable
148  this->registerEvent(KeyMapper::PEV_FORWARD);
149  this->registerEvent(KeyMapper::PEV_BACKWARD);
150  this->registerEvent(KeyMapper::PEV_LEFT);
151  this->registerEvent(KeyMapper::PEV_RIGHT);
152  this->registerEvent(KeyMapper::PEV_UP);
153  this->registerEvent(KeyMapper::PEV_DOWN);
154  this->registerEvent(KeyMapper::PEV_FIRE1);
155  this->registerEvent(KeyMapper::PEV_NEXT_WEAPON);
156  this->registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
157  this->registerEvent(EV_MOUSE_MOTION);
158
159  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
160
161  // WEAPON_MANAGER configuration
162  this->getWeaponManager().setSlotCount(5);
163
164  this->getWeaponManager().setSlotPosition(0, Vector(-0.28, 1.186, -2.750));
165  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
166
167  this->getWeaponManager().setSlotPosition(1, Vector(-0.28, 1.186, 2.750));
168  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
169
170  this->getWeaponManager().setSlotPosition(2, Vector(-1.63, .809, -.003));
171  this->getWeaponManager().setSlotCapability(2, WTYPE_HEAVY);
172
173  /// TODO: THESE ARE TOO MUCH
174  this->getWeaponManager().setSlotPosition(3, Vector(-1.63, .678, -.652));
175  this->getWeaponManager().setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0)));
176
177  this->getWeaponManager().setSlotPosition(4, Vector(-1.63, .678, .652));
178  this->getWeaponManager().setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0)));
179
180  this->travelNode.setRelCoor(0,0,0);
181  //this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
182  //this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
183
184  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
185  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
186  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
187  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
188  //registerVar( new SynchronizeableQuaternion( &direction, &direction, "direction", PERMISSION_OWNER ) );
189  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
190  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
191}
192
193/**
194 * @brief loads the Settings of a Spacecraft2D from an XML-element.
195 * @param root the XML-element to load the Spaceship's properties from
196 */
197void Spacecraft2D::loadParams(const TiXmlElement* root)
198{
199  Playable::loadParams(root);
200}
201
202void Spacecraft2D::setPlayDirection(const Quaternion& rot, float speed)
203{
204  this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
205}
206
207void Spacecraft2D::setTravelDirecton(const Quaternion& rot, float speed)
208{
209  this->setPlayDirection(rot, speed);
210}
211
212void Spacecraft2D::setTravelSpeed(float travelSpeed)
213{
214  this->travelSpeed = travelSpeed;
215}
216
217
218
219void Spacecraft2D::enter()
220{
221  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
222
223  if (State::getCameraNode != NULL)
224  {
225    State::getCameraNode()->setParentSoft(&this->travelNode);
226    State::getCameraNode()->setRelCoorSoft(-10, 50,0);
227    State::getCameraTargetNode()->setParentSoft(&this->travelNode);
228  }
229}
230
231void Spacecraft2D::leave()
232{
233  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
234  this->detachCamera();
235
236}
237
238
239/**
240 * @brief effect that occurs after the Spacecraft2D is spawned
241*/
242void Spacecraft2D::postSpawn ()
243{
244  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
245}
246
247/**
248 * @brief the action occuring if the spacecraft_2d left the game
249*/
250void Spacecraft2D::leftWorld ()
251{}
252
253/**
254 * @brief this function is called, when two entities collide
255 * @param entity: the world entity with whom it collides
256 *
257 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
258 */
259void Spacecraft2D::collidesWith(WorldEntity* entity, const Vector& location)
260{
261  Playable::collidesWith(entity, location);
262}
263
264
265
266/**
267 * @brief the function called for each passing timeSnap
268 * @param time The timespan passed since last update
269*/
270void Spacecraft2D::tick (float dt)
271{
272  //  this->debugNode(1);
273  Playable::tick(dt);
274
275  // spaceship controlled movement
276  this->movement(dt);
277
278  // TRYING TO FIX PNode.
279  //this->travelNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
280  //this->travelNode.setRelDirSoft(this->getAbsDir(), 30.0f);
281}
282
283/**
284 * @brief calculate the velocity
285 * @param time the timeslice since the last frame
286*/
287void Spacecraft2D::movement (float dt)
288{
289  Vector accel(0.0, 0.0, 0.0);
290
291  if( this->bForward )
292  {
293    accel += Vector(this->acceleration, 0, 0);
294  }
295
296  if( this->bBackward )
297  {
298    accel -= Vector(this->acceleration, 0, 0);
299  }
300  if( this->bLeft)
301  {
302    accel -= Vector(0, 0, this->acceleration);
303  }
304
305  if( this->bRight)
306  {
307    accel += Vector(0, 0, this->acceleration);
308  }
309
310
311  switch(this->getPlaymode())
312  {
313    case Playable::Full3D:
314      {
315        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
316
317        // this is the air friction (necessary for a smooth control)
318
319        this->velocity += (accelerationDir)* dt;
320        this->shiftCoor (this->velocity * dt);
321
322        // limit the maximum rotation speed.
323        if (this->rotation != 0.0f)
324        {
325          float maxRot = 10.0 * dt;
326          if (unlikely(this->rotation > maxRot)) this->rotation = maxRot;
327          if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot;
328          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
329
330          this->rotation = 0.0f;
331        }
332
333        this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
334
335      }
336      break;
337
338    case Playable::Horizontal:
339      {
340        accel.y = 0.0;
341        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
342        accelerationDir.y = 0.0;
343
344        // this is the air friction (necessary for a smooth control)
345
346
347        this->velocity += (accelerationDir )* dt;
348        this->shiftCoor (this->velocity * dt);
349
350        // limit the maximum rotation speed.
351        if (this->rotation != 0.0f)
352        {
353          float maxRot = 10.0 * dt;
354          if (unlikely(this->rotation > 0.01 || this->rotation < 0.01)) this->rotation /=1.5;
355          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
356
357          this->rotation = 0.0f;
358        }
359
360        this->setRelDirSoft(this->direction, 5);
361      }
362      break;
363
364    default:
365      PRINTF(2)("Playmode %s Not Implemented\n", Playable::playmodeToString(this->getPlaymode()).c_str());
366  }
367}
368
369
370void Spacecraft2D::draw() const
371{
372  WorldEntity::draw();
373}
374
375/**
376 * @todo switch statement ??
377 */
378void Spacecraft2D::process(const Event &event)
379{
380  Playable::process(event);
381
382  if( event.type == KeyMapper::PEV_LEFT)
383    this->bLeft = event.bPressed;
384  else if( event.type == KeyMapper::PEV_RIGHT)
385    this->bRight = event.bPressed;
386  else if( event.type == KeyMapper::PEV_FORWARD)
387    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
388  else if( event.type == KeyMapper::PEV_BACKWARD)
389    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
390  else if( event.type == EV_MOUSE_MOTION)
391  {
392    float xMouse, yMouse;
393    xMouse = event.xRel*mouseSensitivity;
394    yMouse = event.yRel*mouseSensitivity;
395
396    // rotate the Player around the y-axis
397    if (this->getPlaymode() == Playable::Full3D)
398      this->rotation += xMouse;
399
400    this->cameraLook += yMouse;
401    // rotate the Camera around the z-axis
402    if (cameraLook > M_PI_4)
403      cameraLook = M_PI_4;
404    else if (cameraLook < -M_PI_4)
405      cameraLook = -M_PI_4;
406    //this->cameraNode.setRelDirSoft(this->direction,10);
407  }
408}
Note: See TracBrowser for help on using the repository browser.