Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: brainfuck: interface to Playable much bea now

File size: 9.6 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 *  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 char* 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);
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  //weapons:
81  Weapon* wpRight = new TestGun(0);
82  wpRight->setName("testGun Right");
83  Weapon* wpLeft = new TestGun(1);
84  wpLeft->setName("testGun Left");
85  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
86
87  cannon->setName("BFG");
88
89   this->getWeaponManager()->addWeapon(wpLeft, 1, 0);
90   this->getWeaponManager()->addWeapon(wpRight,1 ,1);
91//   this->getWeaponManager()->addWeapon(cannon, 0, 6);
92
93  //this->getWeaponManager()->addWeapon(turret, 3, 0);
94
95  this->getWeaponManager()->changeWeaponConfig(0);
96}
97
98
99/**
100 * initializes a MD2Creature
101 */
102void MD2Creature::init()
103{
104//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
105  this->setClassID(CL_MD2_CREATURE, "MD2Creature");
106
107  PRINTF(4)("MD2CREATURE INIT\n");
108
109  EventHandler::getInstance()->grabEvents(true);
110
111  this->mouseDirX *= Quaternion( M_PI * 0.75f, Vector(0,1,0));
112
113  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bStrafeL = bStrafeR = bJump = false;
114  bFire = false;
115  xMouse = yMouse = 0;
116  mouseSensitivity = 0.003;
117  airViscosity = 0.0;
118  cycle = 0.0;
119
120  this->cameraConnNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
121  this->cameraConnNode.setName("CameraConnectorNode");
122  this->addChild(&this->cameraConnNode);
123  this->cameraConnNode.addChild(State::getCameraTarget());
124  this->cameraConnNode.addChild(State::getCamera());
125  State::getCameraTarget()->setRelCoor(10,0,0);
126
127  travelSpeed = 15.0;
128  this->velocity = Vector(0.0,0.0,0.0);
129
130//   GLGuiButton* button = new GLGuiPushButton();
131//   button->show();
132//   button->setLabel("orxonox");
133//   button->setBindNode(this);
134
135  //add events to the eventlist
136  this->registerEvent(SDLK_w);
137  this->registerEvent(SDLK_s);
138  this->registerEvent(SDLK_a);
139  this->registerEvent(SDLK_d);
140  this->registerEvent(SDLK_SPACE);
141  this->registerEvent(SDLK_q);
142  this->registerEvent(SDLK_e);
143  this->registerEvent(KeyMapper::PEV_FIRE1);
144  this->registerEvent(KeyMapper::PEV_NEXT_WEAPON);
145  this->registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
146  this->registerEvent(EV_MOUSE_MOTION);
147
148
149
150  this->getWeaponManager()->setSlotCount(7);
151
152  this->getWeaponManager()->setSlotPosition(0, Vector(-0.5, .2, -1.9));
153  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
154
155  this->getWeaponManager()->setSlotPosition(1, Vector(-0.5, .2, 1.9));
156  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
157
158  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
159  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
160
161  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
162  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
163
164  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
165  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
166
167  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
168  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
169//
170   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
171   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
172   //
173
174   this->cameraConnNode.addChild(this->getWeaponManager()->getFixedTarget());
175  this->getWeaponManager()->getFixedTarget()->setRelCoor(10,0,0);
176
177}
178
179
180void MD2Creature::enter()
181{
182  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
183
184
185}
186
187void MD2Creature::leave()
188{
189  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
190
191
192}
193
194/**
195 * loads the Settings of a MD2Creature from an XML-element.
196 * @param root the XML-element to load the MD2Creature's properties from
197 */
198void MD2Creature::loadParams(const TiXmlElement* root)
199{
200  WorldEntity::loadParams(root);
201}
202
203
204/**
205 *  effect that occurs after the MD2Creature is spawned
206*/
207void MD2Creature::postSpawn ()
208{
209  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
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
227}
228
229/**
230 *  draws the MD2Creature after transforming it.
231*/
232void MD2Creature::draw () const
233{
234  WorldEntity::draw();
235//  this->cameraConnNode.debugDraw(0);
236}
237
238
239/**
240 *  the function called for each passing timeSnap
241 * @param time The timespan passed since last update
242*/
243void MD2Creature::tick (float time)
244{
245  Playable::tick(time);
246  if( likely(this->getModel(0) != NULL))
247    ((MD2Model*)this->getModel(0))->tick(time);
248
249
250  // MD2Creature controlled movement
251  this->calculateVelocity(time);
252  Vector move = this->velocity * time;
253  this->shiftCoor (move);
254
255  if( this->bJump)
256  {
257    ((MD2Model*)this->getModel(0))->setAnim(JUMP);
258  }
259  else if( this->bFire)
260  {
261    if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK) ((MD2Model*)this->getModel(0))->setAnim(ATTACK);
262  }
263  else if( fabs(move.len()) > 0.0f)
264  {
265    if( ((MD2Model*)this->getModel(0))->getAnim() != RUN) ((MD2Model*)this->getModel(0))->setAnim(RUN);
266  }
267  else
268  {
269    if( ((MD2Model*)this->getModel(0))->getAnim() != STAND) ((MD2Model*)this->getModel(0))->setAnim(STAND);
270  }
271
272
273  //orient the MD2Creature in direction of the mouse
274  this->setAbsDir(mouseDirX);
275  this->cameraConnNode.setRelDir(mouseDirY);
276}
277
278
279/**
280 *  calculate the velocity
281 * @param time the timeslice since the last frame
282*/
283void MD2Creature::calculateVelocity (float time)
284{
285  Vector accel(0.0, 0.0, 0.0);
286  /*
287  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
288  */
289  //float rotVal = 0.0;
290  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
291  /* calculate the direction in which the craft is heading  */
292
293  if( this->bUp )
294   {
295     accel += (this->getAbsDirX())*2;
296   }
297
298  if( this->bDown )
299   {
300     accel -= (this->getAbsDirX())*2;
301   }
302
303  if( this->bLeft/* > -this->getRelCoor().z*2*/)
304  {
305    this->shiftDir(Quaternion(time, Vector(0,1,0)));
306  }
307  if( this->bRight /* > this->getRelCoor().z*2*/)
308  {
309    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
310  }
311
312
313  if( this->bStrafeL /* > -this->getRelCoor().z*2*/)
314  {
315    accel -= this->getAbsDirZ() * 2.0f;
316  }
317  if( this->bStrafeR /* > this->getRelCoor().z*2*/)
318  {
319    accel += this->getAbsDirZ() * 2.0f;
320  }
321  if (this->bAscend )
322  {
323    this->shiftDir(Quaternion(time, Vector(0,0,1)));
324  }
325  if (this->bDescend )
326  {
327    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
328  }
329
330  velocity = accel * 20.0f;
331  //rot.normalize();
332  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
333}
334
335
336
337/**
338 * @todo switch statement ??
339 */
340void MD2Creature::process(const Event &event)
341{
342  Playable::process(event);
343  if( event.type == SDLK_a)
344      this->bStrafeL = event.bPressed;
345  else if( event.type == SDLK_d)
346      this->bStrafeR = event.bPressed;
347  else if( event.type == SDLK_w)
348    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
349  else if( event.type == SDLK_s)
350    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
351  else if( event.type == SDLK_SPACE)
352    this->bJump = event.bPressed;
353  else if( event.type == EV_MOUSE_MOTION)
354  {
355    this->xMouse = event.xRel;
356    this->yMouse = event.yRel;
357    this->mouseDirX *= Quaternion(-M_PI/4*this->xMouse*mouseSensitivity, Vector(0,1,0));
358    this->mouseDirY *= Quaternion(-M_PI/4*this->yMouse*mouseSensitivity, Vector(0,0,1));
359//     if( xMouse*xMouse < 0.9)
360//      this->setAbsDir(mouseDir);
361  }
362}
Note: See TracBrowser for help on using the repository browser.