Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/world_entities/creatures/md2_creature.cc @ 8504

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

bsp: md2 creature repared, no segfaults anymore. better entity handling now. but no good control

File size: 9.7 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 "md2/md2Model.h"
24#include "util/loading/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 "util/loading/factory.h"
33#include "key_mapper.h"
34
35#include "graphics_engine.h"
36
37#include "debug.h"
38
39CREATE_FACTORY(MD2Creature, CL_MD2_CREATURE);
40
41/**
42 *  destructs the MD2Creature, deletes alocated memory
43 */
44MD2Creature::~MD2Creature ()
45{
46  this->setPlayer(NULL);
47}
48
49/**
50 * loads a MD2Creatures information from a specified file.
51 * @param fileName the name of the File to load the MD2Creature from (absolute path)
52 */
53MD2Creature::MD2Creature(const std::string& fileName)
54{
55  this->init();
56  TiXmlDocument doc(fileName);
57
58  if(!doc.LoadFile())
59  {
60    PRINTF(2)("Loading file %s failed for md2 creature.\n", fileName.c_str());
61    return;
62  }
63
64  this->loadParams(doc.RootElement());
65}
66
67/**
68 *  creates a new MD2Creature from Xml Data
69 * @param root the xml element containing MD2Creature data
70
71   @todo add more parameters to load
72*/
73MD2Creature::MD2Creature(const TiXmlElement* root)
74{
75  this->init();
76  if (root != NULL)
77    this->loadParams(root);
78}
79
80
81/**
82 * initializes a MD2Creature
83 */
84void MD2Creature::init()
85{
86  PRINTF(4)("MD2CREATURE INIT\n");
87  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
88  this->setClassID(CL_MD2_CREATURE, "MD2Creature");
89
90  this->toList(OM_GROUP_01);
91
92  //weapons:
93  Weapon* wpRight = new TestGun(0);
94  wpRight->setName("testGun Right");
95  Weapon* wpLeft = new TestGun(1);
96  wpLeft->setName("testGun Left");
97  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
98
99  cannon->setName("BFG");
100
101  this->addWeapon(wpLeft, 1, 0);
102  this->addWeapon(wpRight,1 ,1);
103  this->getWeaponManager().changeWeaponConfig(0);
104
105
106  // pnode camera issue
107  this->cameraConnNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
108  this->cameraConnNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
109  this->cameraConnNode.setName("CameraConnectorNode");
110  this->addChild(&this->cameraConnNode);
111  this->cameraConnNode.addChild(State::getCameraTargetNode());
112  this->cameraConnNode.addChild(State::getCameraNode());
113  State::getCameraTargetNode()->setRelCoor(10,0,0);
114
115
116  // control initialisation
117  this->mouseDirX *= Quaternion( M_PI * 0.75f, Vector(0,1,0));
118
119  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bStrafeL = bStrafeR = bJump = false;
120  bFire = false;
121  xMouse = yMouse = 0;
122  mouseSensitivity = 0.003;
123  airViscosity = 0.0;
124  cycle = 0.0;
125
126  travelSpeed = 15.0;
127  this->velocity = Vector(0.0,0.0,0.0);
128
129
130  //add events to the eventlist
131  //add events to the eventlist
132  registerEvent(KeyMapper::PEV_FORWARD);
133  registerEvent(KeyMapper::PEV_BACKWARD);
134  registerEvent(KeyMapper::PEV_LEFT);
135  registerEvent(KeyMapper::PEV_RIGHT);
136  registerEvent(KeyMapper::PEV_UP);
137  registerEvent(KeyMapper::PEV_DOWN);
138  registerEvent(KeyMapper::PEV_FIRE1);
139  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
140  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
141  registerEvent(EV_MOUSE_MOTION);
142  this->registerEvent(SDLK_SPACE);
143
144
145
146  this->getWeaponManager().setSlotCount(7);
147
148  this->getWeaponManager().setSlotPosition(0, Vector(-0.5, .2, -1.9));
149  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
150
151  this->getWeaponManager().setSlotPosition(1, Vector(-0.5, .2, 1.9));
152  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
153
154  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
155  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
156
157  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
158  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
159
160  this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
161  this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
162
163  this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
164  this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
165  //
166  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
167  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
168  //
169
170  this->cameraConnNode.addChild(this->getWeaponManager().getFixedTarget());
171  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
172
173  this->getWeaponManager().getFixedTarget()->setRelCoor(10,0,0);
174
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  WorldEntity::loadParams(root);
185}
186
187
188void MD2Creature::enter()
189{
190  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
191
192  State::getCameraNode()->setParentSoft(&this->cameraConnNode);
193  State::getCameraNode()->setRelCoorSoft(-10, 0,0);
194  State::getCameraTargetNode()->setParentSoft(&this->cameraConnNode);
195
196}
197
198void MD2Creature::leave()
199{
200  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
201  this->detachCamera();
202}
203
204
205/**
206 *  effect that occurs after the MD2Creature is spawned
207*/
208void MD2Creature::postSpawn ()
209{}
210
211/**
212 *  the action occuring if the MD2Creature left the game
213*/
214void MD2Creature::leftWorld ()
215{}
216
217/**
218 *  this function is called, when two entities collide
219 * @param entity: the world entity with whom it collides
220 *
221 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
222 */
223void MD2Creature::collidesWith(WorldEntity* entity, const Vector& location)
224{
225  PRINTF(0)("Collided with the md2 model\n");
226}
227
228/**
229 *  draws the MD2Creature after transforming it.
230*/
231void MD2Creature::draw () const
232{
233  if (this->getCurrentPlayer() != NULL)
234    WorldEntity::draw();
235
236  this->cameraConnNode.debugDraw(0);
237}
238
239
240/**
241 *  the function called for each passing timeSnap
242 * @param time The timespan passed since last update
243*/
244void MD2Creature::tick (float time)
245{
246  Playable::tick(time);
247  if( likely(this->getModel(0) != NULL))
248    ((MD2Model*)this->getModel(0))->tick(time);
249
250
251  // MD2Creature controlled movement
252  this->calculateVelocity(time);
253  Vector move = this->velocity * time;
254  this->shiftCoor (move);
255
256
257  // handle animations differently
258  if( this->bJump && likely(this->getModel(0) != NULL))
259  {
260    ((MD2Model*)this->getModel(0))->setAnimation(JUMP);
261  }
262  else if( this->bFire && likely(this->getModel(0) != NULL))
263  {
264    if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK)
265      ((MD2Model*)this->getModel(0))->setAnimation(ATTACK);
266  }
267  else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
268  {
269    if( ((MD2Model*)this->getModel(0))->getAnim() != RUN)
270      ((MD2Model*)this->getModel(0))->setAnimation(RUN);
271  }
272  else if (likely(this->getModel(0) != NULL))
273  {
274    if( ((MD2Model*)this->getModel(0))->getAnim() != STAND)
275      ((MD2Model*)this->getModel(0))->setAnimation(STAND);
276  }
277
278
279  //orient the MD2Creature in direction of the mouse
280//   this->setAbsDir(mouseDirX);
281//    this->cameraConnNode.setRelDir(mouseDirY);
282
283  this->setAbsDir(this->mouseDirX);
284}
285
286
287/**
288 *  calculate the velocity
289 * @param time the timeslice since the last frame
290*/
291void MD2Creature::calculateVelocity (float time)
292{
293  Vector accel(0.0, 0.0, 0.0);
294  /*
295  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
296  */
297  //float rotVal = 0.0;
298  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
299  /* calculate the direction in which the craft is heading  */
300
301  if( bMouseMotion)
302  {
303    PRINTF(0)("motion\n");
304    this->mouseDirX *= Quaternion(-M_PI / 4.0f * this->xMouse*mouseSensitivity, Vector(0,1,0));
305    this->mouseDirY *= Quaternion(-M_PI / 4.0f * this->yMouse*mouseSensitivity, Vector(0,0,1));
306    this->bMouseMotion = false;
307  }
308
309  if( this->bUp )
310  {
311    accel += (this->getAbsDirX())*2;
312  }
313
314  if( this->bDown )
315  {
316    accel -= (this->getAbsDirX())*2;
317  }
318
319  if( this->bLeft/* > -this->getRelCoor().z*2*/)
320  {
321    this->shiftDir(Quaternion(time, Vector(0,1,0)));
322  }
323  if( this->bRight /* > this->getRelCoor().z*2*/)
324  {
325    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
326  }
327
328
329  if( this->bStrafeL /* > -this->getRelCoor().z*2*/)
330  {
331    accel -= this->getAbsDirZ() * 2.0f;
332  }
333  if( this->bStrafeR /* > this->getRelCoor().z*2*/)
334  {
335    accel += this->getAbsDirZ() * 2.0f;
336  }
337  if (this->bAscend )
338  {
339    this->shiftDir(Quaternion(time, Vector(0,0,1)));
340  }
341  if (this->bDescend )
342  {
343    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
344  }
345
346  velocity = accel * 20.0f;
347  //rot.normalize();
348  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
349}
350
351
352/**
353 * @todo switch statement ??
354 */
355void MD2Creature::process(const Event &event)
356{
357  Playable::process(event);
358  if( event.type == KeyMapper::PEV_LEFT)
359    this->bStrafeL = event.bPressed;
360  else if( event.type == KeyMapper::PEV_RIGHT)
361    this->bStrafeR = event.bPressed;
362  else if( event.type == KeyMapper::PEV_FORWARD)
363    this->bUp = event.bPressed;
364  else if( event.type == KeyMapper::PEV_BACKWARD)
365    this->bDown = event.bPressed;
366  else if( event.type == SDLK_SPACE)
367    this->bJump = event.bPressed;
368  else if( event.type == EV_MOUSE_MOTION)
369  {
370    this->bMouseMotion = true;
371    this->xMouse = event.xRel;
372    this->yMouse = event.yRel;
373  }
374}
Note: See TracBrowser for help on using the repository browser.