Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.older/src/world_entities/creatures/md2_creature.cc @ 10619

Last change on this file since 10619 was 8724, checked in by bensch, 19 years ago

merged the bsp-model-stuff back here

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
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
117  // control initialisation
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  travelSpeed =300.0;
128  this->velocity = Vector(0.0,0.0,0.0);
129
130
131  //add events to the eventlist
132  //add events to the eventlist
133  registerEvent(KeyMapper::PEV_FORWARD);
134  registerEvent(KeyMapper::PEV_BACKWARD);
135  registerEvent(KeyMapper::PEV_LEFT);
136  registerEvent(KeyMapper::PEV_RIGHT);
137  registerEvent(KeyMapper::PEV_UP);
138  registerEvent(KeyMapper::PEV_DOWN);
139  registerEvent(KeyMapper::PEV_FIRE1);
140  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
141  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
142  registerEvent(EV_MOUSE_MOTION);
143  this->registerEvent(SDLK_SPACE);
144
145
146
147  this->getWeaponManager().setSlotCount(7);
148
149  this->getWeaponManager().setSlotPosition(0, Vector(-0.5, .2, -1.9));
150  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
151
152  this->getWeaponManager().setSlotPosition(1, Vector(-0.5, .2, 1.9));
153  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
154
155  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
156  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
157
158  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
159  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
160
161  this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
162  this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
163
164  this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
165  this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
166  //
167  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
168  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
169  //
170
171  this->cameraConnNode.addChild(this->getWeaponManager().getFixedTarget());
172  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
173
174  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
175
176}
177
178
179/**
180 * loads the Settings of a MD2Creature from an XML-element.
181 * @param root the XML-element to load the MD2Creature's properties from
182 */
183void MD2Creature::loadParams(const TiXmlElement* root)
184{
185  WorldEntity::loadParams(root);
186}
187
188
189void MD2Creature::enter()
190{
191  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
192
193  State::getCameraNode()->setParent(&this->cameraConnNode);
194  State::getCameraNode()->setRelCoor(0, 0,0);
195  State::getCameraTargetNode()->setParentSoft(&this->cameraConnNode);
196
197}
198
199void MD2Creature::leave()
200{
201  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
202  this->detachCamera();
203}
204
205
206/**
207 *  effect that occurs after the MD2Creature is spawned
208*/
209void MD2Creature::postSpawn ()
210{}
211
212/**
213 *  the action occuring if the MD2Creature left the game
214*/
215void MD2Creature::leftWorld ()
216{}
217
218/**
219 *  this function is called, when two entities collide
220 * @param entity: the world entity with whom it collides
221 *
222 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
223 */
224void MD2Creature::collidesWith(WorldEntity* entity, const Vector& location)
225{
226  PRINTF(0)("Collided with the md2 model\n");
227}
228
229/**
230 *  draws the MD2Creature after transforming it.
231*/
232void MD2Creature::draw () const
233{
234  if (this->getCurrentPlayer() != NULL)
235    WorldEntity::draw();
236
237  this->cameraConnNode.debugDraw(0);
238}
239
240
241/**
242 *  the function called for each passing timeSnap
243 * @param time The timespan passed since last update
244*/
245void MD2Creature::tick (float time)
246{
247  Playable::tick(time);
248  if( likely(this->getModel(0) != NULL))
249    ((MD2Model*)this->getModel(0))->tick(time);
250
251
252  // MD2Creature controlled movement
253  this->calculateVelocity(time);
254  Vector move = this->velocity * time;
255  this->shiftCoor (move);
256
257
258  // handle animations differently
259  if( this->bJump && likely(this->getModel(0) != NULL))
260  {
261    ((MD2Model*)this->getModel(0))->setAnimation(JUMP);
262  }
263  else if( this->bFire && likely(this->getModel(0) != NULL))
264  {
265    if( ((MD2Model*)this->getModel(0))->getAnimation() != ATTACK)
266      ((MD2Model*)this->getModel(0))->setAnimation(ATTACK);
267  }
268  else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
269  {
270    if( ((MD2Model*)this->getModel(0))->getAnimation() != RUN)
271      ((MD2Model*)this->getModel(0))->setAnimation(RUN);
272  }
273  else if (likely(this->getModel(0) != NULL))
274  {
275    if( ((MD2Model*)this->getModel(0))->getAnimation() != STAND)
276      ((MD2Model*)this->getModel(0))->setAnimation(STAND);
277  }
278
279
280  //orient the MD2Creature in direction of the mouse
281//   this->setAbsDir(mouseDirX);
282//    this->cameraConnNode.setRelDir(mouseDirY);
283
284  this->cameraConnNode.setRelDir(mouseDirY);
285  this->setAbsDir(this->mouseDirX);
286}
287
288
289/**
290 *  calculate the velocity
291 * @param time the timeslice since the last frame
292*/
293void MD2Creature::calculateVelocity (float time)
294{
295  Vector accel(0.0, 0.0, 0.0);
296  /*
297  Vector rot(0.0, 0.0, 0.0); // wird ben�igt fr Helicopter
298  */
299  //float rotVal = 0.0;
300  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
301  /* calculate the direction in which the craft is heading  */
302
303  if( bMouseMotion)
304  {
305    this->mouseDirX *= Quaternion(-M_PI / 4.0f * this->xMouse*mouseSensitivity, Vector(0,1,0));
306    this->mouseDirY *= Quaternion(-M_PI / 4.0f * this->yMouse*mouseSensitivity, Vector(0,0,1));
307    this->bMouseMotion = false;
308  }
309
310  if( this->bUp )
311  {
312    accel += (this->getAbsDirX())*2;
313  }
314
315  if( this->bDown )
316  {
317    accel -= (this->getAbsDirX())*2;
318  }
319
320  if( this->bLeft/* > -this->getRelCoor().z*2*/)
321  {
322    this->shiftDir(Quaternion(time, Vector(0,1,0)));
323  }
324  if( this->bRight /* > this->getRelCoor().z*2*/)
325  {
326    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
327  }
328
329
330  if( this->bStrafeL /* > -this->getRelCoor().z*2*/)
331  {
332    accel -= this->getAbsDirZ() * 2.0f;
333  }
334  if( this->bStrafeR /* > this->getRelCoor().z*2*/)
335  {
336    accel += this->getAbsDirZ() * 2.0f;
337  }
338  if (this->bAscend )
339  {
340    this->shiftDir(Quaternion(time, Vector(0,0,1)));
341  }
342  if (this->bDescend )
343  {
344    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
345  }
346
347  velocity = accel * 40.0f;
348  //rot.normalize();
349  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
350}
351
352
353/**
354 * @todo switch statement ??
355 */
356void MD2Creature::process(const Event &event)
357{
358  Playable::process(event);
359  if( event.type == KeyMapper::PEV_LEFT)
360    this->bStrafeL = event.bPressed;
361  else if( event.type == KeyMapper::PEV_RIGHT)
362    this->bStrafeR = event.bPressed;
363  else if( event.type == KeyMapper::PEV_FORWARD)
364    this->bUp = event.bPressed;
365  else if( event.type == KeyMapper::PEV_BACKWARD)
366    this->bDown = event.bPressed;
367  else if( event.type == SDLK_SPACE)
368    this->bJump = event.bPressed;
369  else if( event.type == EV_MOUSE_MOTION)
370  {
371    this->bMouseMotion = true;
372    this->xMouse = event.xRel;
373    this->yMouse = event.yRel;
374  }
375}
Note: See TracBrowser for help on using the repository browser.