Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/test/src/world_entities/space_ships/hover.cc @ 10727

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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
36#include "class_id_DEPRECATED.h"
37ObjectListDefinitionID(Hover, CL_HOVER);
38CREATE_FACTORY(Hover);
39
40#include "script_class.h"
41CREATE_SCRIPTABLE_CLASS(Hover,
42                        addMethod("hasPlayer", Executor0ret<Playable, lua_State*,bool>(&Playable::hasPlayer))
43                        //Coordinates
44                            ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
45                            ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
46                            ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
47                            ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
48                       );
49
50/**
51 *  destructs the hover, deletes alocated memory
52 */
53Hover::~Hover ()
54{
55  this->setPlayer(NULL);
56
57  //if (this->hoverBuffer != NULL)
58  //  ResourceManager::getInstance()->unload(this->hoverBuffer);
59}
60
61/**
62 * loads a Hover information from a specified file.
63 * @param fileName the name of the File to load the hover from (absolute path)
64 */
65Hover::Hover(const std::string& fileName)
66{
67  this->init();
68  TiXmlDocument doc(fileName);
69
70  if(!doc.LoadFile())
71  {
72    PRINTF(2)("Loading file %s failed for Hover.\n", fileName.c_str());
73    return;
74  }
75
76  this->loadParams(doc.RootElement());
77
78  //load sound
79  //if (this->hoverBuffer != NULL)
80  //  ResourceManager::getInstance()->unload(this->hoverBuffer);
81  //this->hoverBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/engine/hover.wav", WAV);
82
83}
84
85/**
86 *  creates a new Spaceship from Xml Data
87 * @param root the xml element containing spaceship data
88
89   @todo add more parameters to load
90*/
91Hover::Hover(const TiXmlElement* root)
92{
93  this->init();
94  if (root != NULL)
95    this->loadParams(root);
96
97  //weapons:
98  Weapon* wpRight = new TestGun(0);
99  wpRight->setName("testGun Right");
100  Weapon* wpLeft = new TestGun(1);
101  wpLeft->setName("testGun Left");
102  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate("Hyperblaster"));
103
104  cannon->setName("BFG");
105
106  this->addWeapon(wpLeft, 1, 0);
107  this->addWeapon(wpRight,1 ,1);
108  this->addWeapon(cannon, 0, 2);
109
110  this->getWeaponManager().changeWeaponConfig(1);
111  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
112
113  this->loadModel("models/ships/hoverglider_mainbody.obj");
114}
115
116
117/**
118 * initializes a Hover
119 */
120void Hover::init()
121{
122
123  //this->hitEntity = NULL;
124  //this->hoverBuffer = NULL;
125
126  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
127  this->registerObject(this, Hover::_objectList);
128  this->toReflectionList();
129
130  this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3);
131  this->loadModel("models/ships/hoverglider_rotor.obj", 1.0f, 4);
132  this->loadModel("models/ships/rotor.obj", .45f, 5);
133
134  bForward = bBackward = bLeft = bRight = bAscend = bDescend = false;
135  mouseSensitivity = 0.005;
136
137  this->rotorSpeed = 1000.0f;
138  this->rotorCycle = 0.0f;
139  this->cameraLook = 0.0f;
140  this->rotation = 0.0f;
141  this->acceleration = 15.0f;
142  this->airFriction = 3.0f;
143
144  // camera - issue
145  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
146  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
147  //this->cameraNode.setParentMode(PNODE_ROTATE_MOVEMENT);
148  this->cameraNode.setParent(this);
149
150  // rotors
151  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT );
152  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
153  this->wingNodeLeft.setParent(this);
154  this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0);
155  this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
156  this->rotorNodeLeft.setParent(&this->wingNodeLeft);
157  this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3);
158
159  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
160  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
161  this->wingNodeRight.setParent(this);
162  this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0);
163  this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
164  this->rotorNodeRight.setParent(&this->wingNodeRight);
165  this->rotorNodeRight.setRelCoor(0, 1.0, 2.3);
166
167  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
168
169
170  //add events to the eventlist
171  registerEvent(KeyMapper::PEV_FORWARD);
172  registerEvent(KeyMapper::PEV_BACKWARD);
173  registerEvent(KeyMapper::PEV_LEFT);
174  registerEvent(KeyMapper::PEV_RIGHT);
175  registerEvent(KeyMapper::PEV_UP);
176  registerEvent(KeyMapper::PEV_DOWN);
177  registerEvent(KeyMapper::PEV_FIRE1);
178  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
179  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
180  registerEvent(EV_MOUSE_MOTION);
181
182
183  // WEAPON_MANAGER configuration
184  this->getWeaponManager().setSlotCount(5);
185
186  this->getWeaponManager().setSlotPosition(0, Vector(-0.28, 1.186, -2.750), &this->wingNodeLeft);
187  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
188
189  this->getWeaponManager().setSlotPosition(1, Vector(-0.28, 1.186, 2.750), &this->wingNodeRight);
190  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
191
192  this->getWeaponManager().setSlotPosition(2, Vector(-1.63, .809, -.003));
193  this->getWeaponManager().setSlotCapability(2, WTYPE_HEAVY);
194
195  /// TODO: THESE ARE TOO MUCH
196  this->getWeaponManager().setSlotPosition(3, Vector(-1.63, .678, -.652));
197  this->getWeaponManager().setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0)));
198
199  this->getWeaponManager().setSlotPosition(4, Vector(-1.63, .678, .652));
200  this->getWeaponManager().setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0)));
201
202  this->cameraNode.setRelCoor(1,5,0);
203  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
204  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
205
206  // NETWORK THINGS
207
208  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
209  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
210  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
211  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
212  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
213  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
214
215  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
216}
217
218/**
219 * loads the Settings of a Hover from an XML-element.
220 * @param root the XML-element to load the Spaceship's properties from
221 */
222void Hover::loadParams(const TiXmlElement* root)
223{
224  WorldEntity::loadParams(root);
225}
226
227
228void Hover::enter()
229{
230  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
231
232  State::getCameraNode()->setParentSoft(&this->cameraNode);
233  State::getCameraNode()->setRelCoorSoft(-10, 0,0);
234  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
235
236  //this->soundSource.play(this->hoverBuffer, 0.3f, true);
237
238}
239
240void Hover::leave()
241{
242  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
243  this->detachCamera();
244
245}
246
247
248/**
249 *  effect that occurs after the Hover is spawned
250*/
251void Hover::postSpawn ()
252{
253  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
254}
255
256/**
257 *  the action occuring if the hover left the game
258*/
259void Hover::leftWorld ()
260{}
261
262/**
263 *  this function is called, when two entities collide
264 * @param entity: the world entity with whom it collides
265 *
266 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
267 */
268void Hover::collidesWith(WorldEntity* entity, const Vector& location)
269{
270  Playable::collidesWith(entity, location);
271}
272
273
274
275/**
276 *  the function called for each passing timeSnap
277 * @param time The timespan passed since last update
278*/
279void Hover::tick (float dt)
280{
281  Playable::tick(dt);
282
283  // spaceship controlled movement
284  this->movement(dt);
285  this->rotorCycle += this->rotorSpeed * dt;
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.