Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/space_ships/spacecraft_2d.cc @ 9129

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

limited controls

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