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
Line 
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
19#include "hover.h"
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
35CREATE_FACTORY(Hover, CL_HOVER);
36
37/**
38 *  destructs the hover, deletes alocated memory
39 */
40Hover::~Hover ()
41{}
42
43/**
44 * loads a Hover information from a specified file.
45 * @param fileName the name of the File to load the hover from (absolute path)
46 */
47Hover::Hover(const char* fileName)
48{
49  this->init();
50  TiXmlDocument doc(fileName);
51
52  if(!doc.LoadFile())
53  {
54    PRINTF(2)("Loading file %s failed for Hover.\n", fileName);
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*/
67Hover::Hover(const TiXmlElement* root)
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);
84  this->addWeapon(cannon, 0, 2);
85
86  this->getWeaponManager()->changeWeaponConfig(1);
87  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
88
89  this->loadModel("models/ships/hoverglider_mainbody.obj");
90}
91
92
93/**
94 * initializes a Hover
95 */
96void Hover::init()
97{
98  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
99  this->setClassID(CL_HOVER, "Hover");
100
101  PRINTF(4)("HOVER INIT\n");
102
103  this->loadModel("models/ships/hover_#.obj", 1.0);
104
105  EventHandler::getInstance()->grabEvents(true);
106
107  bForward = bBackward = bLeft = bRight = bAscend = bDescend = false;
108  xMouse = yMouse = 0;
109  mouseSensitivity = 0.05;
110  controlVelocityX = 100;
111  controlVelocityY = 100;
112
113  this->rotorSpeed = 1000.0f;
114  this->rotorCycle = 0.0f;
115
116  // camera - issue
117  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
118  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
119  this->cameraNode.setParent(this);
120
121  // rotors
122  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT );
123  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
124  this->wingNodeLeft.setParent(this);
125  this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0);
126  this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
127  this->rotorNodeLeft.setParent(&this->wingNodeLeft);
128  this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3);
129
130  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
131  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
132  this->wingNodeRight.setParent(this);
133  this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0);
134  this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
135  this->rotorNodeRight.setParent(&this->wingNodeRight);
136  this->rotorNodeRight.setRelCoor(0, 1.0, 2.3);
137
138  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
139
140  this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3);
141  this->loadModel("models/ships/hoverglider_rotor.obj", 1.0f, 4);
142  this->loadModel("models/ships/rotor.obj", .45f, 5);
143
144  //add events to the eventlist
145  registerEvent(KeyMapper::PEV_UP);
146  registerEvent(KeyMapper::PEV_DOWN);
147  registerEvent(KeyMapper::PEV_LEFT);
148  registerEvent(KeyMapper::PEV_RIGHT);
149  //registerEvent(SDLK_q);
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);
155  //registerEvent(SDLK_PAGEUP);
156  //registerEvent(SDLK_PAGEDOWN);
157  registerEvent(EV_MOUSE_MOTION);
158
159
160  // WEAPON_MANAGER configuration
161  this->getWeaponManager()->setSlotCount(5);
162
163  this->getWeaponManager()->setSlotPosition(0, Vector(-0.28, 1.186, -2.750), &this->wingNodeLeft);
164  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
165
166  this->getWeaponManager()->setSlotPosition(1, Vector(-0.28, 1.186, 2.750), &this->wingNodeRight);
167  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
168
169  this->getWeaponManager()->setSlotPosition(2, Vector(-1.63, .809, -.003));
170  this->getWeaponManager()->setSlotCapability(2, WTYPE_HEAVY);
171
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)));
175
176  this->getWeaponManager()->setSlotPosition(4, Vector(-1.63, .678, .652));
177  this->getWeaponManager()->setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0)));
178
179  this->getWeaponManager()->getFixedTarget()->setParent(&(this->cameraNode));
180}
181
182/**
183 * loads the Settings of a Hover from an XML-element.
184 * @param root the XML-element to load the Spaceship's properties from
185 */
186void Hover::loadParams(const TiXmlElement* root)
187{
188  WorldEntity::loadParams(root);
189}
190
191void  Hover::attachCamera()
192{
193  State::getCamera()->setParentSoft(&this->cameraNode);
194  State::getCameraTarget()->setParentSoft(this->getWeaponManager()->getFixedTarget());
195
196}
197
198
199void Hover::enter()
200{
201  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
202  this->attachCamera();
203
204
205}
206
207void Hover::leave()
208{
209  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
210  this->detachCamera();
211
212}
213
214
215/**
216 *  effect that occurs after the Hover is spawned
217*/
218void Hover::postSpawn ()
219{
220  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
221}
222
223/**
224 *  the action occuring if the hover left the game
225*/
226void Hover::leftWorld ()
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 */
235void Hover::collidesWith(WorldEntity* entity, const Vector& location)
236{
237}
238
239
240
241/**
242 *  the function called for each passing timeSnap
243 * @param time The timespan passed since last update
244*/
245void Hover::tick (float dt)
246{
247  Playable::tick(dt);
248
249  if( xMouse != 0 || yMouse != 0)
250  {
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  }
256
257  // spaceship controlled movement
258  this->movement(dt);
259  this->rotorCycle += this->rotorSpeed * dt;
260
261  Vector move = (velocity)*dt;
262
263  // this is the air friction (necessary for a smooth control)
264  if(velocity.len() != 0) velocity -= velocity*0.1;
265    this->shiftCoor (move);
266
267}
268
269/**
270 *  calculate the velocity
271 * @param time the timeslice since the last frame
272*/
273void Hover::movement (float dt)
274{
275  Vector accel(0.0, 0.0, 0.0);
276  float rotVal = .3;
277
278  if( this->bForward )  {
279    accel += Vector(rotVal, 0, 0);
280  }
281
282  if( this->bBackward ) {
283      accel -= Vector(rotVal,0,0);
284  }
285  if( this->bLeft) {
286    accel -= Vector(0,0,rotVal);
287  }
288  if( this->bRight) {
289    accel += Vector(0,0,rotVal);
290  }
291
292  if (this->bAscend ) {
293    accel += Vector(0,rotVal,0);
294  }
295  if (this->bDescend )
296    accel -= Vector(0,rotVal,0);
297
298  velocity += accel * 3.0;
299
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);
307}
308
309
310void Hover::draw() const
311{
312  Vector tmpRot;
313  WorldEntity::draw();
314
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 ();
324
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();
332  glTranslatef(0,-1,0);
333  glRotatef(this->rotorCycle, 0,1,0);
334  this->getModel(5)->draw();
335  glPopMatrix ();
336
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 ();
347
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();
356  glTranslatef(0,-1,0);
357  glRotatef(this->rotorCycle, 0,1,0);
358  this->getModel(5)->draw();
359  glPopMatrix ();
360}
361
362/**
363 * @todo switch statement ??
364 */
365void Hover::process(const Event &event)
366{
367  Playable::process(event);
368
369  if( event.type == KeyMapper::PEV_LEFT)
370    this->bLeft = event.bPressed;
371  else if( event.type == KeyMapper::PEV_RIGHT)
372    this->bRight = event.bPressed;
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);
377  else if( event.type == KeyMapper::PEV_UP)
378    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
379  else if( event.type == KeyMapper::PEV_DOWN)
380    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
381  else if( event.type == EV_MOUSE_MOTION)
382  {
383    this->xMouse = event.xRel*mouseSensitivity;
384    this->yMouse = event.yRel*mouseSensitivity;
385
386    // rotate the Player around the y-axis
387    this->direction *= Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0));
388
389    // rotate the Camera around the z-axis
390    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
391    if ((this->cameraNode.getAbsDirY()).y < 0.5)
392    {
393      if((this->cameraNode.getAbsDirX()).y > 0)
394      {
395        if(yMouse > 0) this->cameraNode.shiftDir(yDir);
396      }
397      else
398      {
399        if(yMouse < 0) this->cameraNode.shiftDir(yDir);
400      }
401    }
402    else this->cameraNode.shiftDir(yDir);
403  }
404}
Note: See TracBrowser for help on using the repository browser.