Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/cruizer.cc

Last change on this file was 10317, checked in by patrick, 17 years ago

merged branche data-fix back to trunk. this breaks compatibility with the old data/trunk data repository! be sure to update your data trunk

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