Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/space_ships/hover.cc @ 6943

Last change on this file since 6943 was 6882, checked in by bensch, 19 years ago

trunk: smooth

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