Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/world_entities/space_ships/spacecraft_2d.cc @ 9679

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

orxonox/trunk: merged the Presentation back

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