Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged network back to trunk

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