Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/christmas_branche/src/world_entities/creatures/md2_creature.cc @ 6166

Last change on this file since 6166 was 6166, checked in by patrick, 18 years ago

christmas: added some new model class

File size: 12.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: Patrick Boenzli
13   co-programmer:
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "md2_creature.h"
21
22#include "objModel.h"
23#include "resource_manager.h"
24
25#include "weapons/weapon_manager.h"
26#include "weapons/test_gun.h"
27#include "weapons/turret.h"
28#include "weapons/cannon.h"
29
30#include "factory.h"
31#include "key_mapper.h"
32#include "event_handler.h"
33
34#include "graphics_engine.h"
35
36using namespace std;
37
38CREATE_FACTORY(MD2Creature, CL_SPACE_SHIP);
39
40/**
41 *  creates the controlable MD2Creature
42 */
43MD2Creature::MD2Creature()
44{
45  this->init();
46}
47
48/**
49 *  destructs the MD2Creature, deletes alocated memory
50 */
51MD2Creature::~MD2Creature ()
52{
53}
54
55/**
56 * loads a MD2Creatures information from a specified file.
57 * @param fileName the name of the File to load the MD2Creature from (absolute path)
58 */
59MD2Creature::MD2Creature(const char* fileName)
60{
61  this->init();
62  TiXmlDocument doc(fileName);
63
64  if(!doc.LoadFile())
65  {
66    PRINTF(2)("Loading file %s failed for md2 creature.\n", fileName);
67    return;
68  }
69
70  this->loadParams(doc.RootElement());
71}
72
73/**
74 *  creates a new MD2Creature from Xml Data
75 * @param root the xml element containing MD2Creature data
76
77   @todo add more parameters to load
78*/
79MD2Creature::MD2Creature(const TiXmlElement* root)
80{
81  this->init();
82  if (root != NULL)
83    this->loadParams(root);
84
85  //weapons:
86  Weapon* wpRight = new TestGun(0);
87  wpRight->setName("testGun Right");
88  Weapon* wpLeft = new TestGun(1);
89  wpLeft->setName("testGun Left");
90  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
91
92  cannon->setName("BFG");
93
94  //this->getWeaponManager()->addWeapon(wpLeft, 1, 0);
95  //this->getWeaponManager()->addWeapon(wpRight,1 ,1);
96  //this->getWeaponManager()->addWeapon(cannon, 0, 6);
97
98  //this->getWeaponManager()->addWeapon(turret, 3, 0);
99
100  this->getWeaponManager()->changeWeaponConfig(1);
101}
102
103
104/**
105 * initializes a MD2Creature
106 */
107void MD2Creature::init()
108{
109//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
110  this->setClassID(CL_MD2_CREATURE, "MD2Creature");
111
112  PRINTF(4)("SPACESHIP INIT\n");
113
114  EventHandler::getInstance()->grabEvents(true);
115
116  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
117  bFire = false;
118  xMouse = yMouse = 0;
119  mouseSensitivity = 0.001;
120  airViscosity = 1.0;
121  cycle = 0.0;
122
123
124  travelSpeed = 15.0;
125  this->velocity = Vector(0.0,0.0,0.0);
126  this->mouseDir = this->getAbsDir();
127
128//   GLGuiButton* button = new GLGuiPushButton();
129//   button->show();
130//   button->setLabel("orxonox");
131//   button->setBindNode(this);
132
133  //add events to the eventlist
134  this->registerEvent(SDLK_w);
135  this->registerEvent(SDLK_s);
136  this->registerEvent(SDLK_a);
137  this->registerEvent(SDLK_d);
138  this->registerEvent(SDLK_q);
139  this->registerEvent(SDLK_e);
140  this->registerEvent(KeyMapper::PEV_FIRE1);
141  this->registerEvent(KeyMapper::PEV_NEXT_WEAPON);
142  this->registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
143  this->registerEvent(SDLK_PAGEUP);
144  this->registerEvent(SDLK_PAGEDOWN);
145  this->registerEvent(EV_MOUSE_MOTION);
146
147
148
149  this->getWeaponManager()->setSlotCount(7);
150
151  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
152  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
153
154  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
155  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
156
157  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
158  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
159
160  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
161  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
162
163  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
164  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
165
166  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
167  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
168//
169   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
170   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
171   //
172
173  this->getWeaponManager()->getFixedTarget()->setParent(this);
174  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
175
176}
177
178/**
179 * loads the Settings of a MD2Creature from an XML-element.
180 * @param root the XML-element to load the MD2Creature's properties from
181 */
182void MD2Creature::loadParams(const TiXmlElement* root)
183{
184  static_cast<WorldEntity*>(this)->loadParams(root);
185}
186
187
188/**
189 * adds a weapon to the weapon list of the MD2Creature
190 * @param weapon to add
191*/
192void MD2Creature::addWeapon(Weapon* weapon)
193{
194  this->getWeaponManager()->addWeapon(weapon);
195}
196
197
198/**
199 *  removes a weapon from the MD2Creature
200 * @param weapon to remove
201*/
202void MD2Creature::removeWeapon(Weapon* weapon)
203{
204  this->getWeaponManager()->removeWeapon(weapon);
205}
206
207/**
208 *  effect that occurs after the MD2Creature is spawned
209*/
210void MD2Creature::postSpawn ()
211{
212  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
213}
214
215/**
216 *  the action occuring if the MD2Creature left the game
217*/
218void MD2Creature::leftWorld ()
219{}
220
221WorldEntity* ref = NULL;
222/**
223 *  this function is called, when two entities collide
224 * @param entity: the world entity with whom it collides
225 *
226 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
227 */
228void MD2Creature::collidesWith(WorldEntity* entity, const Vector& location)
229{
230  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
231  {
232    this->ADDWEAPON();
233    ref = entity;
234    }
235//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
236}
237
238/**
239 *  draws the MD2Creature after transforming it.
240*/
241void MD2Creature::draw () const
242{
243  this->drawLODsafe();
244
245  this->getWeaponManager()->draw();
246
247  //this->debug(0);
248}
249
250/**
251 *  the function called for each passing timeSnap
252 * @param time The timespan passed since last update
253*/
254void MD2Creature::tick (float time)
255{
256
257  // MD2Creature controlled movement
258  this->calculateVelocity(time);
259
260  Vector move = (velocity)*time;
261
262  //orient the velocity in the direction of the MD2Creature.
263  travelSpeed = velocity.len();
264  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
265  velocity = (velocity.getNormalized())*travelSpeed;
266
267  //orient the MD2Creature in direction of the mouse
268   rotQuat = Quaternion::quatSlerp( this->getAbsDir(),mouseDir,fabsf(time)*3);
269   if (this->getAbsDir().distance(rotQuat) > 0.001)
270    this->setAbsDir( rotQuat);
271   //this->setAbsDirSoft(mouseDir,5);
272
273  // this is the air friction (necessary for a smooth control)
274  if(velocity.len() != 0) velocity -= velocity*0.01;
275
276  //hoover effect
277  cycle += time;
278  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
279
280  //readjust
281 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
282  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
283
284  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
285
286  this->shiftCoor (move);
287
288  this->getWeaponManager()->tick(time);
289  // weapon system manipulation
290  this->weaponAction();
291}
292
293/**
294 *  calculate the velocity
295 * @param time the timeslice since the last frame
296*/
297void MD2Creature::calculateVelocity (float time)
298{
299  Vector accel(0.0, 0.0, 0.0);
300  /*
301  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
302  */
303  //float rotVal = 0.0;
304  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
305  /* calculate the direction in which the craft is heading  */
306
307  if( this->bUp )
308   {
309     //this->shiftCoor(this->getAbsDirX());
310      accel += (this->getAbsDirX())*2;
311
312      /* Heli-Steuerung
313         accel += (this->getAbsDirX()*2;
314         if(
315      */
316   }
317
318  if( this->bDown )
319   {
320     //this->shiftCoor((this->getAbsDirX())*-1);
321     accel -= (this->getAbsDirX())*2;
322   }
323
324  if( this->bLeft/* > -this->getRelCoor().z*2*/)
325  {
326    this->shiftDir(Quaternion(time, Vector(0,1,0)));
327//    accel -= rightDirection;
328    //velocityDir.normalize();
329    //rot +=Vector(1,0,0);
330    //rotVal -= .4;
331  }
332  if( this->bRight /* > this->getRelCoor().z*2*/)
333  {
334    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
335
336    //    accel += rightDirection;
337    //velocityDir.normalize();
338    //rot += Vector(1,0,0);
339    //rotVal += .4;
340  }
341
342
343  if( this->bRollL /* > -this->getRelCoor().z*2*/)
344  {
345    mouseDir *= Quaternion(-time, Vector(1,0,0));
346//    accel -= rightDirection;
347    //velocityDir.normalize();
348    //rot +=Vector(1,0,0);
349    //rotVal -= .4;
350  }
351  if( this->bRollR /* > this->getRelCoor().z*2*/)
352  {
353    mouseDir *= Quaternion(time, Vector(1,0,0));
354
355    //    accel += rightDirection;
356    //velocityDir.normalize();
357    //rot += Vector(1,0,0);
358    //rotVal += .4;
359  }
360  if (this->bAscend )
361  {
362    this->shiftDir(Quaternion(time, Vector(0,0,1)));
363
364//    accel += upDirection;
365    //velocityDir.normalize();
366    //rot += Vector(0,0,1);
367    //rotVal += .4;
368  }
369  if (this->bDescend )
370  {
371    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
372
373    //    accel -= upDirection;
374    //velocityDir.normalize();
375    //rot += Vector(0,0,1);
376    //rotVal -= .4;
377  }
378
379  velocity += accel;
380  //rot.normalize();
381  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
382}
383
384/**
385 * weapon manipulation by the player
386*/
387void MD2Creature::weaponAction()
388{
389  if( this->bFire)
390    {
391      this->getWeaponManager()->fire();
392    }
393}
394
395/**
396 * @todo switch statement ??
397 */
398void MD2Creature::process(const Event &event)
399{
400
401
402  if( event.type == SDLK_a)
403      this->bRollL = event.bPressed;
404  else if( event.type == SDLK_d)
405      this->bRollR = event.bPressed;
406  else if( event.type == KeyMapper::PEV_FIRE1)
407      this->bFire = event.bPressed;
408  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
409    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
410  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
411    this->getWeaponManager()->previousWeaponConfig();
412
413  else if( event.type == SDLK_w)
414    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
415  else if( event.type == SDLK_s)
416    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
417  else if( event.type == EV_MOUSE_MOTION)
418  {
419    this->xMouse = event.xRel;
420    this->yMouse = event.yRel;
421    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
422    if( xMouse*xMouse + yMouse*yMouse < 0.9)
423     this->setAbsDir(mouseDir);
424  }
425}
426
427#include "weapons/aiming_turret.h"
428// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
429void MD2Creature::ADDWEAPON()
430{
431  Weapon* turret = NULL;
432
433  if ((float)rand()/RAND_MAX < .1)
434  {
435    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
436    {
437      turret = new Turret();
438      this->getWeaponManager()->addWeapon(turret, 2);
439      this->getWeaponManager()->changeWeaponConfig(2);
440    }
441  }
442  else
443  {
444    //if (this->getWeaponManager()->hasFreeSlot(3))
445    {
446      turret = new AimingTurret();
447      this->getWeaponManager()->addWeapon(turret, 3);
448
449      this->getWeaponManager()->changeWeaponConfig(3);
450    }
451  }
452
453  if(turret != NULL)
454  {
455    turret->setName("Turret");
456    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
457  }
458}
Note: See TracBrowser for help on using the repository browser.