Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

better ship, shoots

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