Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: ballanced speed

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