Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/creatures/md2_creature.cc @ 6995

Last change on this file since 6995 was 6804, checked in by bensch, 19 years ago

trunk: remastered Playables a bit

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