Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/hover.cc @ 6806

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

trunk: hover-rotors

File size: 11.3 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 ()
[6805]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");
78  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
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
[6799]101  PRINTF(4)("HOVER INIT\n");
[6443]102
[6799]103  this->loadModel("models/ships/hover_#.obj", 1.0);
[6443]104
105  EventHandler::getInstance()->grabEvents(true);
106
[6805]107  bForward = bBackward = bLeft = bRight = bAscend = bDescend = false;
[6443]108  xMouse = yMouse = 0;
[6637]109  mouseSensitivity = 0.05;
[6724]110  controlVelocityX = 100;
111  controlVelocityY = 100;
[6443]112
[6806]113  this->rotorSpeed = 1000.0f;
114  this->rotorCycle = 0.0f;
[6799]115
[6806]116  // camera - issue
[6800]117  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6806]118  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6724]119  this->cameraNode.setParent(this);
[6799]120
[6724]121  // rotors
[6803]122  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT );
123  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6801]124  this->wingNodeLeft.setParent(this);
125  this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0);
[6800]126  this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6801]127  this->rotorNodeLeft.setParent(&this->wingNodeLeft);
128  this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3);
[6799]129
[6801]130  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6803]131  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6800]132  this->wingNodeRight.setParent(this);
[6801]133  this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0);
134  this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6800]135  this->rotorNodeRight.setParent(&this->wingNodeRight);
[6801]136  this->rotorNodeRight.setRelCoor(0, 1.0, 2.3);
[6443]137
[6803]138  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
139
[6800]140  this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3);
141  this->loadModel("models/ships/hoverglider_rotor.obj", 1.0f, 4);
[6806]142  this->loadModel("models/ships/rotor.obj", .45f, 5);
[6800]143
[6443]144  //add events to the eventlist
[6637]145  registerEvent(KeyMapper::PEV_UP);
146  registerEvent(KeyMapper::PEV_DOWN);
147  registerEvent(KeyMapper::PEV_LEFT);
148  registerEvent(KeyMapper::PEV_RIGHT);
149  //registerEvent(SDLK_q);
[6443]150  registerEvent(SDLK_e);
151  registerEvent(SDLK_c);
152  registerEvent(KeyMapper::PEV_FIRE1);
153  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
154  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
[6637]155  //registerEvent(SDLK_PAGEUP);
156  //registerEvent(SDLK_PAGEDOWN);
[6443]157  registerEvent(EV_MOUSE_MOTION);
158
159
[6803]160  // WEAPON_MANAGER configuration
161  this->getWeaponManager()->setSlotCount(5);
162
163  this->getWeaponManager()->setSlotPosition(0, Vector(-0.28, 1.186, -2.750), &this->wingNodeLeft);
[6443]164  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
165
[6803]166  this->getWeaponManager()->setSlotPosition(1, Vector(-0.28, 1.186, 2.750), &this->wingNodeRight);
[6443]167  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
168
[6803]169  this->getWeaponManager()->setSlotPosition(2, Vector(-1.63, .809, -.003));
170  this->getWeaponManager()->setSlotCapability(2, WTYPE_HEAVY);
[6443]171
[6803]172  /// TODO: THESE ARE TOO MUCH
173  this->getWeaponManager()->setSlotPosition(3, Vector(-1.63, .678, -.652));
174  this->getWeaponManager()->setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0)));
[6443]175
[6803]176  this->getWeaponManager()->setSlotPosition(4, Vector(-1.63, .678, .652));
177  this->getWeaponManager()->setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0)));
[6443]178
[6724]179  this->getWeaponManager()->getFixedTarget()->setParent(&(this->cameraNode));
[6443]180}
181
182/**
[6799]183 * loads the Settings of a Hover from an XML-element.
[6443]184 * @param root the XML-element to load the Spaceship's properties from
185 */
[6799]186void Hover::loadParams(const TiXmlElement* root)
[6443]187{
[6512]188  WorldEntity::loadParams(root);
[6443]189}
190
[6799]191void  Hover::attachCamera()
[6724]192{
[6803]193  State::getCamera()->setParentSoft(&this->cameraNode);
[6724]194  State::getCameraTarget()->setParentSoft(this->getWeaponManager()->getFixedTarget());
[6443]195
[6724]196}
[6443]197
198
[6799]199void Hover::enter()
[6443]200{
201  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
202  this->attachCamera();
203
204
205}
206
[6799]207void Hover::leave()
[6443]208{
209  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
210  this->detachCamera();
211
212}
213
214
215/**
[6799]216 *  effect that occurs after the Hover is spawned
[6443]217*/
[6799]218void Hover::postSpawn ()
[6443]219{
220  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
221}
222
223/**
[6799]224 *  the action occuring if the hover left the game
[6443]225*/
[6799]226void Hover::leftWorld ()
[6443]227{}
228
229/**
230 *  this function is called, when two entities collide
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 */
[6799]235void Hover::collidesWith(WorldEntity* entity, const Vector& location)
[6443]236{
237}
238
239
240
241/**
242 *  the function called for each passing timeSnap
243 * @param time The timespan passed since last update
244*/
[6804]245void Hover::tick (float dt)
[6443]246{
[6804]247  Playable::tick(dt);
248
[6724]249  if( xMouse != 0 || yMouse != 0)
[6805]250  {
[6724]251    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
252    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
253    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
254    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
255  }
[6799]256
[6443]257  // spaceship controlled movement
[6805]258  this->movement(dt);
[6806]259  this->rotorCycle += this->rotorSpeed * dt;
[6443]260
[6804]261  Vector move = (velocity)*dt;
[6443]262
263  // this is the air friction (necessary for a smooth control)
264  if(velocity.len() != 0) velocity -= velocity*0.1;
[6805]265    this->shiftCoor (move);
[6443]266
267}
268
269/**
270 *  calculate the velocity
271 * @param time the timeslice since the last frame
272*/
[6806]273void Hover::movement (float dt)
[6443]274{
275  Vector accel(0.0, 0.0, 0.0);
[6805]276  float rotVal = .3;
[6443]277
[6805]278  if( this->bForward )  {
279    accel += Vector(rotVal, 0, 0);
[6443]280  }
281
[6805]282  if( this->bBackward ) {
283      accel -= Vector(rotVal,0,0);
[6443]284  }
[6805]285  if( this->bLeft) {
286    accel -= Vector(0,0,rotVal);
[6443]287  }
[6805]288  if( this->bRight) {
289    accel += Vector(0,0,rotVal);
[6443]290  }
291
[6805]292  if (this->bAscend ) {
293    accel += Vector(0,rotVal,0);
[6443]294  }
295  if (this->bDescend )
[6805]296    accel -= Vector(0,rotVal,0);
[6443]297
[6805]298  velocity += accel * 3.0;
[6443]299
[6805]300  this->setRelDirSoft(this->direction * Quaternion(-accel.x, Vector(0,0,1)) * Quaternion(accel.z, Vector(1,0,0)), 5);
301
302  this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z, Vector(1,0,0)), 10);
303  this->rotorNodeLeft.setRelDirSoft(Quaternion(-2.0*accel.x, Vector(0,0,1)), 10);
304
305  this->wingNodeRight.setRelDirSoft(Quaternion(accel.z, Vector(1,0,0)), 10);
306  this->rotorNodeRight.setRelDirSoft(Quaternion(-2.0*accel.x, Vector(0,0,1)), 10);
[6443]307}
308
309
[6799]310void Hover::draw() const
[6443]311{
[6801]312  Vector tmpRot;
[6443]313  WorldEntity::draw();
[6799]314
[6801]315  glPushMatrix();
316  /// LEFT SIDE
317  glTranslatef (this->wingNodeLeft.getAbsCoor ().x,
318                this->wingNodeLeft.getAbsCoor ().y,
319                this->wingNodeLeft.getAbsCoor ().z);
320  tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis();
321  glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
322  this->getModel(3)->draw();
323  glPopMatrix ();
[6443]324
[6801]325  glPushMatrix();
326  glTranslatef (this->rotorNodeLeft.getAbsCoor ().x,
327                this->rotorNodeLeft.getAbsCoor ().y,
328                this->rotorNodeLeft.getAbsCoor ().z);
329  tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis();
330  glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
331  this->getModel(4)->draw();
[6806]332  glTranslatef(0,-1,0);
333  glRotatef(this->rotorCycle, 0,1,0);
334  this->getModel(5)->draw();
[6801]335  glPopMatrix ();
[6724]336
[6801]337  /// RIGHT SIDE
338  glPushMatrix();
339  glTranslatef (this->wingNodeRight.getAbsCoor ().x,
340                this->wingNodeRight.getAbsCoor ().y,
341                this->wingNodeRight.getAbsCoor ().z);
342  tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis();
343  glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
344  glScalef(1,1,-1);
345  this->getModel(3)->draw();
346  glPopMatrix ();
[6724]347
[6801]348  glPushMatrix();
349  glTranslatef (this->rotorNodeRight.getAbsCoor ().x,
350                this->rotorNodeRight.getAbsCoor ().y,
351                this->rotorNodeRight.getAbsCoor ().z);
352  tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis();
353  glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
354  glScalef(1,1,-1);
355  this->getModel(4)->draw();
[6806]356  glTranslatef(0,-1,0);
357  glRotatef(this->rotorCycle, 0,1,0);
358  this->getModel(5)->draw();
[6801]359  glPopMatrix ();
[6443]360}
361
362/**
363 * @todo switch statement ??
364 */
[6799]365void Hover::process(const Event &event)
[6443]366{
[6804]367  Playable::process(event);
[6443]368
[6637]369  if( event.type == KeyMapper::PEV_LEFT)
[6805]370    this->bLeft = event.bPressed;
[6637]371  else if( event.type == KeyMapper::PEV_RIGHT)
[6805]372    this->bRight = event.bPressed;
[6443]373  else if( event.type == SDLK_e)
374    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
375  else if( event.type == SDLK_c)
376    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
[6637]377  else if( event.type == KeyMapper::PEV_UP)
[6805]378    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
[6637]379  else if( event.type == KeyMapper::PEV_DOWN)
[6805]380    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
[6443]381  else if( event.type == EV_MOUSE_MOTION)
382  {
383    this->xMouse = event.xRel*mouseSensitivity;
384    this->yMouse = event.yRel*mouseSensitivity;
385
[6805]386    // rotate the Player around the y-axis
387    this->direction *= Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0));
[6799]388
[6805]389    // rotate the Camera around the z-axis
[6724]390    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
391    if ((this->cameraNode.getAbsDirY()).y < 0.5)
392    {
[6805]393      if((this->cameraNode.getAbsDirX()).y > 0)
394      {
[6724]395        if(yMouse > 0) this->cameraNode.shiftDir(yDir);
[6805]396      }
397      else
398      {
399        if(yMouse < 0) this->cameraNode.shiftDir(yDir);
400      }
[6724]401    }
[6805]402    else this->cameraNode.shiftDir(yDir);
403  }
[6443]404}
Note: See TracBrowser for help on using the repository browser.