Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/preferences/src/world_entities/creatures/md2_creature.cc @ 7186

Last change on this file since 7186 was 6281, checked in by bensch, 19 years ago

trunk: make usage of the Virtual-derivates

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