Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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