Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/presentaion enter right playmode

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