Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/creatures/md2_creature.cc @ 6222

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

orxonox/trunk: merged the christmas branche to the trunk
merged with command:
svn merge -r6165:HEAD christmas_branche/ ../trunk/
no conflicts

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  this->drawLODsafe();
265
266//  this->cameraConnNode.debugDraw(0);
267}
268
269
270/**
271 *  the function called for each passing timeSnap
272 * @param time The timespan passed since last update
273*/
274void MD2Creature::tick (float time)
275{
276  if( likely(this->getModel(0) != NULL))
277    ((MD2Model*)this->getModel(0))->tick(time);
278
279
280  // MD2Creature controlled movement
281  this->calculateVelocity(time);
282  Vector move = this->velocity * time;
283  this->shiftCoor (move);
284
285  if( this->bJump)
286  {
287    ((MD2Model*)this->getModel(0))->setAnim(JUMP);
288  }
289  else if( this->bFire)
290  {
291    if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK) ((MD2Model*)this->getModel(0))->setAnim(ATTACK);
292  }
293  else if( fabs(move.len()) > 0.0f)
294  {
295    if( ((MD2Model*)this->getModel(0))->getAnim() != RUN) ((MD2Model*)this->getModel(0))->setAnim(RUN);
296  }
297  else
298  {
299    if( ((MD2Model*)this->getModel(0))->getAnim() != STAND) ((MD2Model*)this->getModel(0))->setAnim(STAND);
300  }
301
302
303  //orient the MD2Creature in direction of the mouse
304  this->setAbsDir(mouseDirX);
305  this->cameraConnNode.setAbsDirSoft(mouseDirX*mouseDirY, 10);
306
307  this->getWeaponManager()->tick(time);
308  // weapon system manipulation
309  this->weaponAction();
310}
311
312
313/**
314 *  calculate the velocity
315 * @param time the timeslice since the last frame
316*/
317void MD2Creature::calculateVelocity (float time)
318{
319  Vector accel(0.0, 0.0, 0.0);
320  /*
321  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
322  */
323  //float rotVal = 0.0;
324  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
325  /* calculate the direction in which the craft is heading  */
326
327  if( this->bUp )
328   {
329     accel += (this->getAbsDirX())*2;
330   }
331
332  if( this->bDown )
333   {
334     accel -= (this->getAbsDirX())*2;
335   }
336
337  if( this->bLeft/* > -this->getRelCoor().z*2*/)
338  {
339    this->shiftDir(Quaternion(time, Vector(0,1,0)));
340  }
341  if( this->bRight /* > this->getRelCoor().z*2*/)
342  {
343    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
344  }
345
346
347  if( this->bStrafeL /* > -this->getRelCoor().z*2*/)
348  {
349    accel -= this->getAbsDirZ() * 2.0f;
350  }
351  if( this->bStrafeR /* > this->getRelCoor().z*2*/)
352  {
353    accel += this->getAbsDirZ() * 2.0f;
354  }
355  if (this->bAscend )
356  {
357    this->shiftDir(Quaternion(time, Vector(0,0,1)));
358  }
359  if (this->bDescend )
360  {
361    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
362  }
363
364  velocity = accel * 20.0f;
365  //rot.normalize();
366  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
367}
368
369
370/**
371 * weapon manipulation by the player
372*/
373void MD2Creature::weaponAction()
374{
375  if( this->bFire)
376    {
377      this->getWeaponManager()->fire();
378    }
379}
380
381/**
382 * @todo switch statement ??
383 */
384void MD2Creature::process(const Event &event)
385{
386  if( event.type == SDLK_a)
387      this->bStrafeL = event.bPressed;
388  else if( event.type == SDLK_d)
389      this->bStrafeR = event.bPressed;
390  else if( event.type == KeyMapper::PEV_FIRE1)
391      this->bFire = event.bPressed;
392  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
393    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
394  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
395    this->getWeaponManager()->previousWeaponConfig();
396  else if( event.type == SDLK_w)
397    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
398  else if( event.type == SDLK_s)
399    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
400  else if( event.type == SDLK_SPACE)
401    this->bJump = event.bPressed;
402  else if( event.type == EV_MOUSE_MOTION)
403  {
404    this->xMouse = event.xRel;
405    this->yMouse = event.yRel;
406    this->mouseDirX *= Quaternion(-M_PI/4*this->xMouse*mouseSensitivity, Vector(0,1,0));
407    this->mouseDirY *= Quaternion(-M_PI/4*this->yMouse*mouseSensitivity, Vector(0,0,1));
408//     if( xMouse*xMouse < 0.9)
409//      this->setAbsDir(mouseDir);
410  }
411}
412
413#include "weapons/aiming_turret.h"
414// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
415void MD2Creature::ADDWEAPON()
416{
417  Weapon* turret = NULL;
418
419  if ((float)rand()/RAND_MAX < .1)
420  {
421    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
422    {
423      turret = new Turret();
424      this->getWeaponManager()->addWeapon(turret, 2);
425      this->getWeaponManager()->changeWeaponConfig(2);
426    }
427  }
428  else
429  {
430    //if (this->getWeaponManager()->hasFreeSlot(3))
431    {
432      turret = new AimingTurret();
433      this->getWeaponManager()->addWeapon(turret, 3);
434
435      this->getWeaponManager()->changeWeaponConfig(3);
436    }
437  }
438
439  if(turret != NULL)
440  {
441    turret->setName("Turret");
442    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
443  }
444}
Note: See TracBrowser for help on using the repository browser.