Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/world_entities/space_ships/cruizer.cc @ 9046

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

orxonox/branches/single_player_map: added Spacecraft_2d

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