Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6701 was 6561, checked in by bensch, 19 years ago

trunk: WeaponManager update

File size: 11.2 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  WorldEntity::loadParams(root);
208}
209
210
211/**
212 *  effect that occurs after the MD2Creature is spawned
213*/
214void MD2Creature::postSpawn ()
215{
216  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
217}
218
219/**
220 *  the action occuring if the MD2Creature left the game
221*/
222void MD2Creature::leftWorld ()
223{}
224
225/**
226 *  this function is called, when two entities collide
227 * @param entity: the world entity with whom it collides
228 *
229 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
230 */
231void MD2Creature::collidesWith(WorldEntity* entity, const Vector& location)
232{
233  if (entity->isA(CL_TURRET_POWER_UP) )
234  {
235    this->ADDWEAPON();
236    }
237//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
238}
239
240/**
241 *  draws the MD2Creature after transforming it.
242*/
243void MD2Creature::draw () const
244{
245  WorldEntity::draw();
246//  this->cameraConnNode.debugDraw(0);
247}
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  if( likely(this->getModel(0) != NULL))
257    ((MD2Model*)this->getModel(0))->tick(time);
258
259
260  // MD2Creature controlled movement
261  this->calculateVelocity(time);
262  Vector move = this->velocity * time;
263  this->shiftCoor (move);
264
265  if( this->bJump)
266  {
267    ((MD2Model*)this->getModel(0))->setAnim(JUMP);
268  }
269  else if( this->bFire)
270  {
271    if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK) ((MD2Model*)this->getModel(0))->setAnim(ATTACK);
272  }
273  else if( fabs(move.len()) > 0.0f)
274  {
275    if( ((MD2Model*)this->getModel(0))->getAnim() != RUN) ((MD2Model*)this->getModel(0))->setAnim(RUN);
276  }
277  else
278  {
279    if( ((MD2Model*)this->getModel(0))->getAnim() != STAND) ((MD2Model*)this->getModel(0))->setAnim(STAND);
280  }
281
282
283  //orient the MD2Creature in direction of the mouse
284  this->setAbsDir(mouseDirX);
285  this->cameraConnNode.setRelDir(mouseDirY);
286
287  this->getWeaponManager()->tick(time);
288  // weapon system manipulation
289  this->weaponAction();
290}
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     accel += (this->getAbsDirX())*2;
310   }
311
312  if( this->bDown )
313   {
314     accel -= (this->getAbsDirX())*2;
315   }
316
317  if( this->bLeft/* > -this->getRelCoor().z*2*/)
318  {
319    this->shiftDir(Quaternion(time, Vector(0,1,0)));
320  }
321  if( this->bRight /* > this->getRelCoor().z*2*/)
322  {
323    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
324  }
325
326
327  if( this->bStrafeL /* > -this->getRelCoor().z*2*/)
328  {
329    accel -= this->getAbsDirZ() * 2.0f;
330  }
331  if( this->bStrafeR /* > this->getRelCoor().z*2*/)
332  {
333    accel += this->getAbsDirZ() * 2.0f;
334  }
335  if (this->bAscend )
336  {
337    this->shiftDir(Quaternion(time, Vector(0,0,1)));
338  }
339  if (this->bDescend )
340  {
341    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
342  }
343
344  velocity = accel * 20.0f;
345  //rot.normalize();
346  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
347}
348
349
350/**
351 * weapon manipulation by the player
352*/
353void MD2Creature::weaponAction()
354{
355  if( this->bFire)
356    {
357      this->getWeaponManager()->fire();
358    }
359}
360
361/**
362 * @todo switch statement ??
363 */
364void MD2Creature::process(const Event &event)
365{
366  if( event.type == SDLK_a)
367      this->bStrafeL = event.bPressed;
368  else if( event.type == SDLK_d)
369      this->bStrafeR = event.bPressed;
370  else if( event.type == KeyMapper::PEV_FIRE1)
371      this->bFire = event.bPressed;
372  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
373    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
374  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
375    this->getWeaponManager()->previousWeaponConfig();
376  else if( event.type == SDLK_w)
377    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
378  else if( event.type == SDLK_s)
379    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
380  else if( event.type == SDLK_SPACE)
381    this->bJump = event.bPressed;
382  else if( event.type == EV_MOUSE_MOTION)
383  {
384    this->xMouse = event.xRel;
385    this->yMouse = event.yRel;
386    this->mouseDirX *= Quaternion(-M_PI/4*this->xMouse*mouseSensitivity, Vector(0,1,0));
387    this->mouseDirY *= Quaternion(-M_PI/4*this->yMouse*mouseSensitivity, Vector(0,0,1));
388//     if( xMouse*xMouse < 0.9)
389//      this->setAbsDir(mouseDir);
390  }
391}
392
393#include "weapons/aiming_turret.h"
394// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
395void MD2Creature::ADDWEAPON()
396{
397  Weapon* turret = NULL;
398
399  if ((float)rand()/RAND_MAX < .1)
400  {
401    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
402    {
403      turret = new Turret();
404      this->getWeaponManager()->addWeapon(turret, 2);
405      this->getWeaponManager()->changeWeaponConfig(2);
406    }
407  }
408  else
409  {
410    //if (this->getWeaponManager()->hasFreeSlot(3))
411    {
412      turret = new AimingTurret();
413      this->getWeaponManager()->addWeapon(turret, 3);
414
415      this->getWeaponManager()->changeWeaponConfig(3);
416    }
417  }
418
419  if(turret != NULL)
420  {
421    turret->setName("Turret");
422    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
423  }
424}
Note: See TracBrowser for help on using the repository browser.