Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10669 was 10368, checked in by patrick, 18 years ago

merged the branche playability into the trunk

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