Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8591 was 8362, checked in by bensch, 19 years ago

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

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