| [7193] | 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 |  | 
|---|
| [10033] | 22 | #include "obj/objModel.h" | 
|---|
| [8490] | 23 | #include "md2/md2Model.h" | 
|---|
| [7193] | 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 |  | 
|---|
| [8362] | 36 | #include "debug.h" | 
|---|
| [7193] | 37 |  | 
|---|
| [10114] | 38 |  | 
|---|
 | 39 | ObjectListDefinition(MD2Creature); | 
|---|
| [9869] | 40 | CREATE_FACTORY(MD2Creature); | 
|---|
| [7193] | 41 |  | 
|---|
 | 42 | /** | 
|---|
 | 43 |  *  destructs the MD2Creature, deletes alocated memory | 
|---|
 | 44 |  */ | 
|---|
 | 45 | MD2Creature::~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 |  */ | 
|---|
| [7221] | 54 | MD2Creature::MD2Creature(const std::string& fileName) | 
|---|
| [7193] | 55 | { | 
|---|
 | 56 |   this->init(); | 
|---|
 | 57 |   TiXmlDocument doc(fileName); | 
|---|
 | 58 |  | 
|---|
 | 59 |   if(!doc.LoadFile()) | 
|---|
 | 60 |   { | 
|---|
| [7221] | 61 |     PRINTF(2)("Loading file %s failed for md2 creature.\n", fileName.c_str()); | 
|---|
| [7193] | 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 | */ | 
|---|
 | 74 | MD2Creature::MD2Creature(const TiXmlElement* root) | 
|---|
 | 75 | { | 
|---|
 | 76 |   this->init(); | 
|---|
 | 77 |   if (root != NULL) | 
|---|
 | 78 |     this->loadParams(root); | 
|---|
| [8724] | 79 | } | 
|---|
| [7193] | 80 |  | 
|---|
| [8724] | 81 |  | 
|---|
 | 82 | /** | 
|---|
 | 83 |  * initializes a MD2Creature | 
|---|
 | 84 |  */ | 
|---|
 | 85 | void MD2Creature::init() | 
|---|
 | 86 | { | 
|---|
 | 87 |   PRINTF(4)("MD2CREATURE INIT\n"); | 
|---|
 | 88 |   //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); | 
|---|
| [9869] | 89 |   this->registerObject(this, MD2Creature::_objectList); | 
|---|
| [8724] | 90 |  | 
|---|
 | 91 |   this->toList(OM_GROUP_01); | 
|---|
 | 92 |  | 
|---|
| [7193] | 93 |   //weapons: | 
|---|
 | 94 |   Weapon* wpRight = new TestGun(0); | 
|---|
 | 95 |   wpRight->setName("testGun Right"); | 
|---|
 | 96 |   Weapon* wpLeft = new TestGun(1); | 
|---|
 | 97 |   wpLeft->setName("testGun Left"); | 
|---|
| [9869] | 98 |   Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate("Cannon")); | 
|---|
| [7193] | 99 |  | 
|---|
 | 100 |   cannon->setName("BFG"); | 
|---|
 | 101 |  | 
|---|
| [8724] | 102 |   this->addWeapon(wpLeft, 1, 0); | 
|---|
 | 103 |   this->addWeapon(wpRight,1 ,1); | 
|---|
| [7337] | 104 |   this->getWeaponManager().changeWeaponConfig(0); | 
|---|
| [7193] | 105 |  | 
|---|
 | 106 |  | 
|---|
| [8724] | 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); | 
|---|
| [7193] | 115 |  | 
|---|
 | 116 |  | 
|---|
| [9869] | 117 |  | 
|---|
| [8724] | 118 |   // control initialisation | 
|---|
| [7193] | 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 |  | 
|---|
| [8724] | 128 |   travelSpeed =300.0; | 
|---|
| [7193] | 129 |   this->velocity = Vector(0.0,0.0,0.0); | 
|---|
 | 130 |  | 
|---|
 | 131 |  | 
|---|
 | 132 |   //add events to the eventlist | 
|---|
| [8724] | 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); | 
|---|
| [7193] | 144 |   this->registerEvent(SDLK_SPACE); | 
|---|
 | 145 |  | 
|---|
 | 146 |  | 
|---|
 | 147 |  | 
|---|
| [7337] | 148 |   this->getWeaponManager().setSlotCount(7); | 
|---|
| [7193] | 149 |  | 
|---|
| [7337] | 150 |   this->getWeaponManager().setSlotPosition(0, Vector(-0.5, .2, -1.9)); | 
|---|
 | 151 |   this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); | 
|---|
| [7193] | 152 |  | 
|---|
| [7337] | 153 |   this->getWeaponManager().setSlotPosition(1, Vector(-0.5, .2, 1.9)); | 
|---|
 | 154 |   this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); | 
|---|
| [7193] | 155 |  | 
|---|
| [7337] | 156 |   this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5)); | 
|---|
 | 157 |   this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); | 
|---|
| [7193] | 158 |  | 
|---|
| [7337] | 159 |   this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5)); | 
|---|
 | 160 |   this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); | 
|---|
| [7193] | 161 |  | 
|---|
| [7337] | 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))); | 
|---|
| [7193] | 164 |  | 
|---|
| [7337] | 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))); | 
|---|
| [8724] | 167 |   // | 
|---|
 | 168 |   this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0)); | 
|---|
 | 169 |   this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); | 
|---|
 | 170 |   // | 
|---|
| [7193] | 171 |  | 
|---|
| [8724] | 172 |   this->cameraConnNode.addChild(this->getWeaponManager().getFixedTarget()); | 
|---|
| [7337] | 173 |   dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); | 
|---|
| [7193] | 174 |  | 
|---|
| [8724] | 175 |   this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0); | 
|---|
| [7193] | 176 |  | 
|---|
 | 177 | } | 
|---|
 | 178 |  | 
|---|
 | 179 |  | 
|---|
| [8724] | 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 |  */ | 
|---|
 | 184 | void MD2Creature::loadParams(const TiXmlElement* root) | 
|---|
 | 185 | { | 
|---|
 | 186 |   WorldEntity::loadParams(root); | 
|---|
 | 187 | } | 
|---|
 | 188 |  | 
|---|
 | 189 |  | 
|---|
| [7193] | 190 | void MD2Creature::enter() | 
|---|
 | 191 | { | 
|---|
| [7337] | 192 |   dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true); | 
|---|
| [7193] | 193 |  | 
|---|
| [8724] | 194 |   State::getCameraNode()->setParent(&this->cameraConnNode); | 
|---|
 | 195 |   State::getCameraNode()->setRelCoor(0, 0,0); | 
|---|
 | 196 |   State::getCameraTargetNode()->setParentSoft(&this->cameraConnNode); | 
|---|
 | 197 |  | 
|---|
| [7193] | 198 | } | 
|---|
 | 199 |  | 
|---|
 | 200 | void MD2Creature::leave() | 
|---|
 | 201 | { | 
|---|
| [7337] | 202 |   dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); | 
|---|
| [8724] | 203 |   this->detachCamera(); | 
|---|
| [7193] | 204 | } | 
|---|
 | 205 |  | 
|---|
 | 206 |  | 
|---|
 | 207 | /** | 
|---|
 | 208 |  *  effect that occurs after the MD2Creature is spawned | 
|---|
 | 209 | */ | 
|---|
 | 210 | void MD2Creature::postSpawn () | 
|---|
| [8724] | 211 | {} | 
|---|
| [7193] | 212 |  | 
|---|
 | 213 | /** | 
|---|
 | 214 |  *  the action occuring if the MD2Creature left the game | 
|---|
 | 215 | */ | 
|---|
 | 216 | void 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 |  */ | 
|---|
 | 225 | void 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 | */ | 
|---|
 | 233 | void MD2Creature::draw () const | 
|---|
 | 234 | { | 
|---|
 | 235 |   if (this->getCurrentPlayer() != NULL) | 
|---|
 | 236 |     WorldEntity::draw(); | 
|---|
| [8724] | 237 |  | 
|---|
 | 238 |   this->cameraConnNode.debugDraw(0); | 
|---|
| [7193] | 239 | } | 
|---|
 | 240 |  | 
|---|
 | 241 |  | 
|---|
 | 242 | /** | 
|---|
 | 243 |  *  the function called for each passing timeSnap | 
|---|
 | 244 |  * @param time The timespan passed since last update | 
|---|
 | 245 | */ | 
|---|
 | 246 | void 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 |  | 
|---|
| [8724] | 258 |  | 
|---|
 | 259 |   // handle animations differently | 
|---|
| [7221] | 260 |   if( this->bJump && likely(this->getModel(0) != NULL)) | 
|---|
| [7193] | 261 |   { | 
|---|
| [8490] | 262 |     ((MD2Model*)this->getModel(0))->setAnimation(JUMP); | 
|---|
| [7193] | 263 |   } | 
|---|
| [7221] | 264 |   else if( this->bFire && likely(this->getModel(0) != NULL)) | 
|---|
| [7193] | 265 |   { | 
|---|
| [8724] | 266 |     if( ((MD2Model*)this->getModel(0))->getAnimation() != ATTACK) | 
|---|
 | 267 |       ((MD2Model*)this->getModel(0))->setAnimation(ATTACK); | 
|---|
| [7193] | 268 |   } | 
|---|
| [7221] | 269 |   else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL)) | 
|---|
| [7193] | 270 |   { | 
|---|
| [8724] | 271 |     if( ((MD2Model*)this->getModel(0))->getAnimation() != RUN) | 
|---|
 | 272 |       ((MD2Model*)this->getModel(0))->setAnimation(RUN); | 
|---|
| [7193] | 273 |   } | 
|---|
| [7221] | 274 |   else if (likely(this->getModel(0) != NULL)) | 
|---|
| [7193] | 275 |   { | 
|---|
| [8724] | 276 |     if( ((MD2Model*)this->getModel(0))->getAnimation() != STAND) | 
|---|
 | 277 |       ((MD2Model*)this->getModel(0))->setAnimation(STAND); | 
|---|
| [7193] | 278 |   } | 
|---|
 | 279 |  | 
|---|
 | 280 |  | 
|---|
 | 281 |   //orient the MD2Creature in direction of the mouse | 
|---|
| [8724] | 282 | //   this->setAbsDir(mouseDirX); | 
|---|
 | 283 | //    this->cameraConnNode.setRelDir(mouseDirY); | 
|---|
 | 284 |  | 
|---|
| [7193] | 285 |   this->cameraConnNode.setRelDir(mouseDirY); | 
|---|
| [8724] | 286 |   this->setAbsDir(this->mouseDirX); | 
|---|
| [7193] | 287 | } | 
|---|
 | 288 |  | 
|---|
 | 289 |  | 
|---|
 | 290 | /** | 
|---|
 | 291 |  *  calculate the velocity | 
|---|
 | 292 |  * @param time the timeslice since the last frame | 
|---|
 | 293 | */ | 
|---|
 | 294 | void MD2Creature::calculateVelocity (float time) | 
|---|
 | 295 | { | 
|---|
 | 296 |   Vector accel(0.0, 0.0, 0.0); | 
|---|
 | 297 |   /* | 
|---|
| [8724] | 298 |   Vector rot(0.0, 0.0, 0.0); // wird ben�igt fr Helicopter | 
|---|
| [7193] | 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 |  | 
|---|
| [8724] | 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 |  | 
|---|
| [7193] | 311 |   if( this->bUp ) | 
|---|
| [8724] | 312 |   { | 
|---|
 | 313 |     accel += (this->getAbsDirX())*2; | 
|---|
 | 314 |   } | 
|---|
| [7193] | 315 |  | 
|---|
 | 316 |   if( this->bDown ) | 
|---|
| [8724] | 317 |   { | 
|---|
 | 318 |     accel -= (this->getAbsDirX())*2; | 
|---|
 | 319 |   } | 
|---|
| [7193] | 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 |  | 
|---|
| [8724] | 348 |   velocity = accel * 40.0f; | 
|---|
| [7193] | 349 |   //rot.normalize(); | 
|---|
 | 350 |   //this->setRelDirSoft(Quaternion(rotVal, rot), 5); | 
|---|
 | 351 | } | 
|---|
 | 352 |  | 
|---|
 | 353 |  | 
|---|
 | 354 | /** | 
|---|
 | 355 |  * @todo switch statement ?? | 
|---|
 | 356 |  */ | 
|---|
 | 357 | void MD2Creature::process(const Event &event) | 
|---|
 | 358 | { | 
|---|
 | 359 |   Playable::process(event); | 
|---|
| [8724] | 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; | 
|---|
| [7193] | 368 |   else if( event.type == SDLK_SPACE) | 
|---|
 | 369 |     this->bJump = event.bPressed; | 
|---|
 | 370 |   else if( event.type == EV_MOUSE_MOTION) | 
|---|
 | 371 |   { | 
|---|
| [8724] | 372 |     this->bMouseMotion = true; | 
|---|
| [7193] | 373 |     this->xMouse = event.xRel; | 
|---|
 | 374 |     this->yMouse = event.yRel; | 
|---|
 | 375 |   } | 
|---|
 | 376 | } | 
|---|