Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: smoother control of the Hover

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