Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: target strafes with mouse-movement

File size: 11.4 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/**
187 * loads the Settings of a MD2Creature from an XML-element.
188 * @param root the XML-element to load the MD2Creature's properties from
189 */
190void MD2Creature::loadParams(const TiXmlElement* root)
191{
192  static_cast<WorldEntity*>(this)->loadParams(root);
193}
194
195
196/**
197 * adds a weapon to the weapon list of the MD2Creature
198 * @param weapon to add
199*/
200void MD2Creature::addWeapon(Weapon* weapon)
201{
202  this->getWeaponManager()->addWeapon(weapon);
203}
204
205
206/**
207 *  removes a weapon from the MD2Creature
208 * @param weapon to remove
209*/
210void MD2Creature::removeWeapon(Weapon* weapon)
211{
212  this->getWeaponManager()->removeWeapon(weapon);
213}
214
215/**
216 *  effect that occurs after the MD2Creature is spawned
217*/
218void MD2Creature::postSpawn ()
219{
220  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
221}
222
223/**
224 *  the action occuring if the MD2Creature left the game
225*/
226void MD2Creature::leftWorld ()
227{}
228
229/**
230 *  this function is called, when two entities collide
231 * @param entity: the world entity with whom it collides
232 *
233 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
234 */
235void MD2Creature::collidesWith(WorldEntity* entity, const Vector& location)
236{
237  if (entity->isA(CL_TURRET_POWER_UP) )
238  {
239    this->ADDWEAPON();
240    }
241//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
242}
243
244/**
245 *  draws the MD2Creature after transforming it.
246*/
247void MD2Creature::draw () const
248{
249  this->drawLODsafe();
250
251//  this->cameraConnNode.debugDraw(0);
252}
253
254
255/**
256 *  the function called for each passing timeSnap
257 * @param time The timespan passed since last update
258*/
259void MD2Creature::tick (float time)
260{
261  if( likely(this->getModel(0) != NULL))
262    ((MD2Model*)this->getModel(0))->tick(time);
263
264
265  // MD2Creature controlled movement
266  this->calculateVelocity(time);
267  Vector move = this->velocity * time;
268  this->shiftCoor (move);
269
270  if( this->bJump)
271  {
272    ((MD2Model*)this->getModel(0))->setAnim(JUMP);
273  }
274  else if( this->bFire)
275  {
276    if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK) ((MD2Model*)this->getModel(0))->setAnim(ATTACK);
277  }
278  else if( fabs(move.len()) > 0.0f)
279  {
280    if( ((MD2Model*)this->getModel(0))->getAnim() != RUN) ((MD2Model*)this->getModel(0))->setAnim(RUN);
281  }
282  else
283  {
284    if( ((MD2Model*)this->getModel(0))->getAnim() != STAND) ((MD2Model*)this->getModel(0))->setAnim(STAND);
285  }
286
287
288  //orient the MD2Creature in direction of the mouse
289  this->setAbsDir(mouseDirX);
290  this->cameraConnNode.setAbsDirSoft(mouseDirX*mouseDirY, 10);
291
292  this->getWeaponManager()->tick(time);
293  // weapon system manipulation
294  this->weaponAction();
295}
296
297
298/**
299 *  calculate the velocity
300 * @param time the timeslice since the last frame
301*/
302void MD2Creature::calculateVelocity (float time)
303{
304  Vector accel(0.0, 0.0, 0.0);
305  /*
306  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
307  */
308  //float rotVal = 0.0;
309  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
310  /* calculate the direction in which the craft is heading  */
311
312  if( this->bUp )
313   {
314     accel += (this->getAbsDirX())*2;
315   }
316
317  if( this->bDown )
318   {
319     accel -= (this->getAbsDirX())*2;
320   }
321
322  if( this->bLeft/* > -this->getRelCoor().z*2*/)
323  {
324    this->shiftDir(Quaternion(time, Vector(0,1,0)));
325  }
326  if( this->bRight /* > this->getRelCoor().z*2*/)
327  {
328    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
329  }
330
331
332  if( this->bStrafeL /* > -this->getRelCoor().z*2*/)
333  {
334    accel -= this->getAbsDirZ() * 2.0f;
335  }
336  if( this->bStrafeR /* > this->getRelCoor().z*2*/)
337  {
338    accel += this->getAbsDirZ() * 2.0f;
339  }
340  if (this->bAscend )
341  {
342    this->shiftDir(Quaternion(time, Vector(0,0,1)));
343  }
344  if (this->bDescend )
345  {
346    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
347  }
348
349  velocity = accel * 20.0f;
350  //rot.normalize();
351  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
352}
353
354
355/**
356 * weapon manipulation by the player
357*/
358void MD2Creature::weaponAction()
359{
360  if( this->bFire)
361    {
362      this->getWeaponManager()->fire();
363    }
364}
365
366/**
367 * @todo switch statement ??
368 */
369void MD2Creature::process(const Event &event)
370{
371  if( event.type == SDLK_a)
372      this->bStrafeL = event.bPressed;
373  else if( event.type == SDLK_d)
374      this->bStrafeR = event.bPressed;
375  else if( event.type == KeyMapper::PEV_FIRE1)
376      this->bFire = event.bPressed;
377  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
378    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
379  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
380    this->getWeaponManager()->previousWeaponConfig();
381  else if( event.type == SDLK_w)
382    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
383  else if( event.type == SDLK_s)
384    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
385  else if( event.type == SDLK_SPACE)
386    this->bJump = event.bPressed;
387  else if( event.type == EV_MOUSE_MOTION)
388  {
389    this->xMouse = event.xRel;
390    this->yMouse = event.yRel;
391    this->mouseDirX *= Quaternion(-M_PI/4*this->xMouse*mouseSensitivity, Vector(0,1,0));
392    this->mouseDirY *= Quaternion(-M_PI/4*this->yMouse*mouseSensitivity, Vector(0,0,1));
393//     if( xMouse*xMouse < 0.9)
394//      this->setAbsDir(mouseDir);
395  }
396}
397
398#include "weapons/aiming_turret.h"
399// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
400void MD2Creature::ADDWEAPON()
401{
402  Weapon* turret = NULL;
403
404  if ((float)rand()/RAND_MAX < .1)
405  {
406    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
407    {
408      turret = new Turret();
409      this->getWeaponManager()->addWeapon(turret, 2);
410      this->getWeaponManager()->changeWeaponConfig(2);
411    }
412  }
413  else
414  {
415    //if (this->getWeaponManager()->hasFreeSlot(3))
416    {
417      turret = new AimingTurret();
418      this->getWeaponManager()->addWeapon(turret, 3);
419
420      this->getWeaponManager()->changeWeaponConfig(3);
421    }
422  }
423
424  if(turret != NULL)
425  {
426    turret->setName("Turret");
427    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
428  }
429}
Note: See TracBrowser for help on using the repository browser.