Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/space_ships/hover.cc @ 9230

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