Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cd/src/world_entities/space_ships/hover.cc @ 7520

Last change on this file since 7520 was 7337, checked in by bensch, 19 years ago

orxonox/trunk: simple useless stuff

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