Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/creatures/md2_creature.cc @ 9716

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

more renamings

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