Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/avi_play/src/world_entities/space_ships/hover.cc @ 6951

Last change on this file since 6951 was 6951, checked in by hdavid, 18 years ago

branches/avi_play

File size: 11.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
[6799]19#include "hover.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
26#include "factory.h"
27#include "key_mapper.h"
28#include "event_handler.h"
29#include "state.h"
30
31#include "graphics_engine.h"
32
33using namespace std;
34
[6799]35CREATE_FACTORY(Hover, CL_HOVER);
[6443]36
37/**
[6799]38 *  destructs the hover, deletes alocated memory
[6443]39 */
[6799]40Hover::~Hover ()
[6879]41{}
[6443]42
43/**
[6799]44 * loads a Hover information from a specified file.
45 * @param fileName the name of the File to load the hover from (absolute path)
[6443]46 */
[6799]47Hover::Hover(const char* fileName)
[6443]48{
49  this->init();
50  TiXmlDocument doc(fileName);
51
52  if(!doc.LoadFile())
53  {
[6799]54    PRINTF(2)("Loading file %s failed for Hover.\n", fileName);
[6443]55    return;
56  }
57
58  this->loadParams(doc.RootElement());
59}
60
61/**
62 *  creates a new Spaceship from Xml Data
63 * @param root the xml element containing spaceship data
64
65   @todo add more parameters to load
66*/
[6799]67Hover::Hover(const TiXmlElement* root)
[6443]68{
69  this->init();
70  if (root != NULL)
71    this->loadParams(root);
72
73  //weapons:
74  Weapon* wpRight = new TestGun(0);
75  wpRight->setName("testGun Right");
76  Weapon* wpLeft = new TestGun(1);
77  wpLeft->setName("testGun Left");
[6811]78  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_HYPERBLASTER));
[6443]79
80  cannon->setName("BFG");
81
82  this->addWeapon(wpLeft, 1, 0);
83  this->addWeapon(wpRight,1 ,1);
[6803]84  this->addWeapon(cannon, 0, 2);
[6443]85
86  this->getWeaponManager()->changeWeaponConfig(1);
87  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
[6800]88
89  this->loadModel("models/ships/hoverglider_mainbody.obj");
[6443]90}
91
92
93/**
[6799]94 * initializes a Hover
[6443]95 */
[6799]96void Hover::init()
[6443]97{
[6805]98  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
[6799]99  this->setClassID(CL_HOVER, "Hover");
[6443]100
[6807]101  this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3);
102  this->loadModel("models/ships/hoverglider_rotor.obj", 1.0f, 4);
103  this->loadModel("models/ships/rotor.obj", .45f, 5);
[6443]104
[6805]105  bForward = bBackward = bLeft = bRight = bAscend = bDescend = false;
[6807]106  mouseSensitivity = 0.005;
[6443]107
[6806]108  this->rotorSpeed = 1000.0f;
109  this->rotorCycle = 0.0f;
[6807]110  this->cameraLook = 0.0f;
111  this->rotation = 0.0f;
[6879]112  this->acceleration = 1.0;
[6799]113
[6806]114  // camera - issue
[6800]115  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6806]116  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6880]117  //this->cameraNode.setParentMode(PNODE_ROTATE_MOVEMENT);
[6724]118  this->cameraNode.setParent(this);
[6799]119
[6724]120  // rotors
[6803]121  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT );
122  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6801]123  this->wingNodeLeft.setParent(this);
124  this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0);
[6800]125  this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6801]126  this->rotorNodeLeft.setParent(&this->wingNodeLeft);
127  this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3);
[6799]128
[6801]129  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6803]130  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6800]131  this->wingNodeRight.setParent(this);
[6801]132  this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0);
133  this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6800]134  this->rotorNodeRight.setParent(&this->wingNodeRight);
[6801]135  this->rotorNodeRight.setRelCoor(0, 1.0, 2.3);
[6443]136
[6803]137  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
138
[6800]139
[6443]140  //add events to the eventlist
[6637]141  registerEvent(KeyMapper::PEV_UP);
142  registerEvent(KeyMapper::PEV_DOWN);
143  registerEvent(KeyMapper::PEV_LEFT);
144  registerEvent(KeyMapper::PEV_RIGHT);
[6880]145  registerEvent(SDLK_e); /// FIXME
146  registerEvent(SDLK_c); /// FIXME
[6443]147  registerEvent(KeyMapper::PEV_FIRE1);
148  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
149  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
150  registerEvent(EV_MOUSE_MOTION);
151
152
[6803]153  // WEAPON_MANAGER configuration
154  this->getWeaponManager()->setSlotCount(5);
155
156  this->getWeaponManager()->setSlotPosition(0, Vector(-0.28, 1.186, -2.750), &this->wingNodeLeft);
[6443]157  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
158
[6803]159  this->getWeaponManager()->setSlotPosition(1, Vector(-0.28, 1.186, 2.750), &this->wingNodeRight);
[6443]160  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
161
[6803]162  this->getWeaponManager()->setSlotPosition(2, Vector(-1.63, .809, -.003));
163  this->getWeaponManager()->setSlotCapability(2, WTYPE_HEAVY);
[6443]164
[6803]165  /// TODO: THESE ARE TOO MUCH
166  this->getWeaponManager()->setSlotPosition(3, Vector(-1.63, .678, -.652));
167  this->getWeaponManager()->setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0)));
[6443]168
[6803]169  this->getWeaponManager()->setSlotPosition(4, Vector(-1.63, .678, .652));
170  this->getWeaponManager()->setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0)));
[6443]171
[6807]172  this->cameraNode.setRelCoor(1,5,0);
173  this->getWeaponManager()->getFixedTarget()->setParent(&this->cameraNode);
174  this->getWeaponManager()->getFixedTarget()->setRelCoor(1000,0,0);
[6443]175}
176
177/**
[6799]178 * loads the Settings of a Hover from an XML-element.
[6443]179 * @param root the XML-element to load the Spaceship's properties from
180 */
[6799]181void Hover::loadParams(const TiXmlElement* root)
[6443]182{
[6512]183  WorldEntity::loadParams(root);
[6443]184}
185
186
[6799]187void Hover::enter()
[6443]188{
189  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
190
[6871]191  State::getCamera()->setParentSoft(&this->cameraNode);
192  State::getCamera()->setRelCoorSoft(-10, 0,0);
193  State::getCameraTarget()->setParentSoft(&this->cameraNode);
[6443]194}
195
[6799]196void Hover::leave()
[6443]197{
198  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
199  this->detachCamera();
200
201}
202
203
204/**
[6799]205 *  effect that occurs after the Hover is spawned
[6443]206*/
[6799]207void Hover::postSpawn ()
[6443]208{
209  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
210}
211
212/**
[6799]213 *  the action occuring if the hover left the game
[6443]214*/
[6799]215void Hover::leftWorld ()
[6443]216{}
217
218/**
219 *  this function is called, when two entities collide
220 * @param entity: the world entity with whom it collides
221 *
222 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
223 */
[6799]224void Hover::collidesWith(WorldEntity* entity, const Vector& location)
[6807]225{}
[6443]226
227
228
229/**
230 *  the function called for each passing timeSnap
231 * @param time The timespan passed since last update
232*/
[6804]233void Hover::tick (float dt)
[6443]234{
[6804]235  Playable::tick(dt);
236
[6951]237  this->debugNode();
[6443]238  // spaceship controlled movement
[6805]239  this->movement(dt);
[6806]240  this->rotorCycle += this->rotorSpeed * dt;
[6443]241}
242
243/**
244 *  calculate the velocity
245 * @param time the timeslice since the last frame
246*/
[6806]247void Hover::movement (float dt)
[6443]248{
249  Vector accel(0.0, 0.0, 0.0);
[6879]250  float rotSpeed = .3;
[6443]251
[6814]252  if( this->bForward )
253  {
[6879]254    accel += Vector(this->acceleration, 0, 0);
[6443]255  }
256
[6814]257  if( this->bBackward )
258  {
[6879]259    accel -= Vector(this->acceleration, 0, 0);
[6443]260  }
[6814]261  if( this->bLeft)
262  {
[6879]263    accel -= Vector(0, 0, this->acceleration);
[6443]264  }
[6807]265
[6814]266  if( this->bRight)
267  {
[6879]268    accel += Vector(0, 0, this->acceleration);
[6443]269  }
270
[6814]271  if (this->bAscend )
272  {
[6879]273    accel += Vector(0, this->acceleration, 0);
[6443]274  }
[6814]275  if (this->bDescend )
276  {
[6879]277    accel -= Vector(0, this->acceleration, 0);
[6807]278  }
[6882]279  Vector tmp = this->getAbsDir().apply(accel * 200.0);
[6880]280  //tmp.y = accel.y * 500.0 ;
[6879]281  velocity += tmp * dt;
[6443]282
[6882]283  accel -= this->getAbsDir().inverse().apply(velocity * .005);
[6814]284
[6879]285  // this is the air friction (necessary for a smooth control)
[6882]286  this->velocity *= 0.99;
[6879]287  this->shiftCoor (velocity * dt);
288  this->rotation = 0.0f;
289
[6880]290  this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
[6805]291
[6879]292  this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z+this->rotation, Vector(1,0,0)), 5);
[6880]293  this->rotorNodeLeft.setRelDirSoft(Quaternion(-2.0*accel.x+this->rotation + cameraLook, Vector(0,0,1)), 5);
[6805]294
[6879]295  this->wingNodeRight.setRelDirSoft(Quaternion(accel.z+this->rotation, Vector(1,0,0)), 5);
[6880]296  this->rotorNodeRight.setRelDirSoft(Quaternion(-2.0*accel.x-this->rotation + cameraLook, Vector(0,0,1)), 5);
[6443]297}
298
299
[6799]300void Hover::draw() const
[6443]301{
[6801]302  Vector tmpRot;
[6443]303  WorldEntity::draw();
[6799]304
[6801]305  glPushMatrix();
306  /// LEFT SIDE
307  glTranslatef (this->wingNodeLeft.getAbsCoor ().x,
308                this->wingNodeLeft.getAbsCoor ().y,
309                this->wingNodeLeft.getAbsCoor ().z);
310  tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis();
311  glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
312  this->getModel(3)->draw();
313  glPopMatrix ();
[6443]314
[6801]315  glPushMatrix();
316  glTranslatef (this->rotorNodeLeft.getAbsCoor ().x,
317                this->rotorNodeLeft.getAbsCoor ().y,
318                this->rotorNodeLeft.getAbsCoor ().z);
319  tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis();
320  glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
321  this->getModel(4)->draw();
[6806]322  glTranslatef(0,-1,0);
323  glRotatef(this->rotorCycle, 0,1,0);
324  this->getModel(5)->draw();
[6801]325  glPopMatrix ();
[6724]326
[6801]327  /// RIGHT SIDE
328  glPushMatrix();
329  glTranslatef (this->wingNodeRight.getAbsCoor ().x,
330                this->wingNodeRight.getAbsCoor ().y,
331                this->wingNodeRight.getAbsCoor ().z);
332  tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis();
333  glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
334  glScalef(1,1,-1);
335  this->getModel(3)->draw();
336  glPopMatrix ();
[6724]337
[6801]338  glPushMatrix();
339  glTranslatef (this->rotorNodeRight.getAbsCoor ().x,
340                this->rotorNodeRight.getAbsCoor ().y,
341                this->rotorNodeRight.getAbsCoor ().z);
342  tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis();
343  glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
344  glScalef(1,1,-1);
345  this->getModel(4)->draw();
[6806]346  glTranslatef(0,-1,0);
347  glRotatef(this->rotorCycle, 0,1,0);
348  this->getModel(5)->draw();
[6801]349  glPopMatrix ();
[6443]350}
351
352/**
353 * @todo switch statement ??
354 */
[6799]355void Hover::process(const Event &event)
[6443]356{
[6804]357  Playable::process(event);
[6443]358
[6637]359  if( event.type == KeyMapper::PEV_LEFT)
[6805]360    this->bLeft = event.bPressed;
[6637]361  else if( event.type == KeyMapper::PEV_RIGHT)
[6805]362    this->bRight = event.bPressed;
[6443]363  else if( event.type == SDLK_e)
364    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
365  else if( event.type == SDLK_c)
366    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
[6637]367  else if( event.type == KeyMapper::PEV_UP)
[6805]368    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
[6637]369  else if( event.type == KeyMapper::PEV_DOWN)
[6805]370    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
[6443]371  else if( event.type == EV_MOUSE_MOTION)
372  {
[6807]373    float xMouse, yMouse;
374    xMouse = event.xRel*mouseSensitivity;
375    yMouse = event.yRel*mouseSensitivity;
[6443]376
[6805]377    // rotate the Player around the y-axis
[6807]378    this->direction *= Quaternion(-M_PI/4.0*xMouse, Vector(0,1,0));
379    this->rotation += xMouse;
[6799]380
[6807]381    this->cameraLook += yMouse;
[6805]382    // rotate the Camera around the z-axis
[6807]383    if (cameraLook > M_PI_4)
384      cameraLook = M_PI_4;
[6880]385    else if (cameraLook < -M_PI_4)
386      cameraLook = -M_PI_4;
387    //this->cameraNode.setRelDirSoft(this->direction,10);
[6805]388  }
[6443]389}
Note: See TracBrowser for help on using the repository browser.