Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/world_entities/space_ships/hover.cc @ 9558

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

should link

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