Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: UP is FORWARD, DOWN is BACKWARD

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