[6166] | 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" |
---|
[6170] | 23 | #include "md2Model.h" |
---|
[6166] | 24 | #include "resource_manager.h" |
---|
[6208] | 25 | #include "state.h" |
---|
[6166] | 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 | |
---|
| 38 | using namespace std; |
---|
| 39 | |
---|
[6167] | 40 | CREATE_FACTORY(MD2Creature, CL_MD2_CREATURE); |
---|
[6166] | 41 | |
---|
| 42 | /** |
---|
| 43 | * creates the controlable MD2Creature |
---|
| 44 | */ |
---|
| 45 | MD2Creature::MD2Creature() |
---|
| 46 | { |
---|
| 47 | this->init(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * destructs the MD2Creature, deletes alocated memory |
---|
| 52 | */ |
---|
| 53 | MD2Creature::~MD2Creature () |
---|
| 54 | { |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * loads a MD2Creatures information from a specified file. |
---|
| 59 | * @param fileName the name of the File to load the MD2Creature from (absolute path) |
---|
| 60 | */ |
---|
| 61 | MD2Creature::MD2Creature(const char* fileName) |
---|
| 62 | { |
---|
| 63 | this->init(); |
---|
| 64 | TiXmlDocument doc(fileName); |
---|
| 65 | |
---|
| 66 | if(!doc.LoadFile()) |
---|
| 67 | { |
---|
| 68 | PRINTF(2)("Loading file %s failed for md2 creature.\n", fileName); |
---|
| 69 | return; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | this->loadParams(doc.RootElement()); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * creates a new MD2Creature from Xml Data |
---|
| 77 | * @param root the xml element containing MD2Creature data |
---|
| 78 | |
---|
| 79 | @todo add more parameters to load |
---|
| 80 | */ |
---|
| 81 | MD2Creature::MD2Creature(const TiXmlElement* root) |
---|
| 82 | { |
---|
| 83 | this->init(); |
---|
| 84 | if (root != NULL) |
---|
| 85 | this->loadParams(root); |
---|
| 86 | |
---|
| 87 | //weapons: |
---|
| 88 | Weapon* wpRight = new TestGun(0); |
---|
| 89 | wpRight->setName("testGun Right"); |
---|
| 90 | Weapon* wpLeft = new TestGun(1); |
---|
| 91 | wpLeft->setName("testGun Left"); |
---|
| 92 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON)); |
---|
| 93 | |
---|
| 94 | cannon->setName("BFG"); |
---|
| 95 | |
---|
[6205] | 96 | this->getWeaponManager()->addWeapon(wpLeft, 1, 0); |
---|
| 97 | this->getWeaponManager()->addWeapon(wpRight,1 ,1); |
---|
[6181] | 98 | // this->getWeaponManager()->addWeapon(cannon, 0, 6); |
---|
[6166] | 99 | |
---|
| 100 | //this->getWeaponManager()->addWeapon(turret, 3, 0); |
---|
| 101 | |
---|
[6206] | 102 | this->getWeaponManager()->changeWeaponConfig(0); |
---|
[6166] | 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * initializes a MD2Creature |
---|
| 108 | */ |
---|
| 109 | void MD2Creature::init() |
---|
| 110 | { |
---|
| 111 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
| 112 | this->setClassID(CL_MD2_CREATURE, "MD2Creature"); |
---|
| 113 | |
---|
[6181] | 114 | PRINTF(4)("MD2CREATURE INIT\n"); |
---|
[6166] | 115 | |
---|
[6172] | 116 | EventHandler::getInstance()->grabEvents(true); |
---|
[6166] | 117 | |
---|
[6210] | 118 | this->mouseDirX *= Quaternion( M_PI * 0.75f, Vector(0,1,0)); |
---|
| 119 | |
---|
[6204] | 120 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bStrafeL = bStrafeR = bJump = false; |
---|
[6166] | 121 | bFire = false; |
---|
| 122 | xMouse = yMouse = 0; |
---|
[6195] | 123 | mouseSensitivity = 0.003; |
---|
| 124 | airViscosity = 0.0; |
---|
[6166] | 125 | cycle = 0.0; |
---|
| 126 | |
---|
[6208] | 127 | this->cameraConnNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 128 | this->cameraConnNode.setName("CameraConnectorNode"); |
---|
| 129 | this->addChild(&this->cameraConnNode); |
---|
| 130 | this->cameraConnNode.addChild(State::getCameraTarget()); |
---|
[6212] | 131 | this->cameraConnNode.addChild(State::getCamera()); |
---|
[6208] | 132 | State::getCameraTarget()->setRelCoor(10,0,0); |
---|
[6166] | 133 | |
---|
| 134 | travelSpeed = 15.0; |
---|
| 135 | this->velocity = Vector(0.0,0.0,0.0); |
---|
| 136 | |
---|
| 137 | // GLGuiButton* button = new GLGuiPushButton(); |
---|
| 138 | // button->show(); |
---|
| 139 | // button->setLabel("orxonox"); |
---|
| 140 | // button->setBindNode(this); |
---|
| 141 | |
---|
| 142 | //add events to the eventlist |
---|
| 143 | this->registerEvent(SDLK_w); |
---|
| 144 | this->registerEvent(SDLK_s); |
---|
| 145 | this->registerEvent(SDLK_a); |
---|
| 146 | this->registerEvent(SDLK_d); |
---|
[6198] | 147 | this->registerEvent(SDLK_SPACE); |
---|
[6166] | 148 | this->registerEvent(SDLK_q); |
---|
| 149 | this->registerEvent(SDLK_e); |
---|
| 150 | this->registerEvent(KeyMapper::PEV_FIRE1); |
---|
| 151 | this->registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
| 152 | this->registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
| 153 | this->registerEvent(EV_MOUSE_MOTION); |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | this->getWeaponManager()->setSlotCount(7); |
---|
| 158 | |
---|
[6205] | 159 | this->getWeaponManager()->setSlotPosition(0, Vector(-0.5, .2, -1.9)); |
---|
[6166] | 160 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 161 | |
---|
[6205] | 162 | this->getWeaponManager()->setSlotPosition(1, Vector(-0.5, .2, 1.9)); |
---|
[6166] | 163 | this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 164 | |
---|
| 165 | this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 166 | this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
| 167 | |
---|
| 168 | this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 169 | this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
| 170 | |
---|
| 171 | this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
| 172 | this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
| 173 | |
---|
| 174 | this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
| 175 | this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
| 176 | // |
---|
| 177 | this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
| 178 | this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 179 | // |
---|
| 180 | |
---|
[6212] | 181 | this->cameraConnNode.addChild(this->getWeaponManager()->getFixedTarget()); |
---|
| 182 | this->getWeaponManager()->getFixedTarget()->setRelCoor(10,0,0); |
---|
[6166] | 183 | |
---|
| 184 | } |
---|
| 185 | |
---|
[6218] | 186 | |
---|
| 187 | void MD2Creature::enter() |
---|
| 188 | { |
---|
| 189 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | void MD2Creature::leave() |
---|
| 195 | { |
---|
| 196 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | } |
---|
| 200 | |
---|
[6166] | 201 | /** |
---|
| 202 | * loads the Settings of a MD2Creature from an XML-element. |
---|
| 203 | * @param root the XML-element to load the MD2Creature's properties from |
---|
| 204 | */ |
---|
| 205 | void MD2Creature::loadParams(const TiXmlElement* root) |
---|
| 206 | { |
---|
| 207 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * adds a weapon to the weapon list of the MD2Creature |
---|
| 213 | * @param weapon to add |
---|
| 214 | */ |
---|
| 215 | void MD2Creature::addWeapon(Weapon* weapon) |
---|
| 216 | { |
---|
| 217 | this->getWeaponManager()->addWeapon(weapon); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | * removes a weapon from the MD2Creature |
---|
| 223 | * @param weapon to remove |
---|
| 224 | */ |
---|
| 225 | void MD2Creature::removeWeapon(Weapon* weapon) |
---|
| 226 | { |
---|
| 227 | this->getWeaponManager()->removeWeapon(weapon); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | * effect that occurs after the MD2Creature is spawned |
---|
| 232 | */ |
---|
| 233 | void MD2Creature::postSpawn () |
---|
| 234 | { |
---|
| 235 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * the action occuring if the MD2Creature left the game |
---|
| 240 | */ |
---|
| 241 | void MD2Creature::leftWorld () |
---|
| 242 | {} |
---|
| 243 | |
---|
| 244 | /** |
---|
| 245 | * this function is called, when two entities collide |
---|
| 246 | * @param entity: the world entity with whom it collides |
---|
| 247 | * |
---|
| 248 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 249 | */ |
---|
| 250 | void MD2Creature::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 251 | { |
---|
[6171] | 252 | if (entity->isA(CL_TURRET_POWER_UP) ) |
---|
[6166] | 253 | { |
---|
| 254 | this->ADDWEAPON(); |
---|
| 255 | } |
---|
| 256 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | * draws the MD2Creature after transforming it. |
---|
| 261 | */ |
---|
| 262 | void MD2Creature::draw () const |
---|
| 263 | { |
---|
[6176] | 264 | this->drawLODsafe(); |
---|
[6208] | 265 | |
---|
[6209] | 266 | // this->cameraConnNode.debugDraw(0); |
---|
[6166] | 267 | } |
---|
| 268 | |
---|
[6195] | 269 | |
---|
[6166] | 270 | /** |
---|
| 271 | * the function called for each passing timeSnap |
---|
| 272 | * @param time The timespan passed since last update |
---|
| 273 | */ |
---|
| 274 | void MD2Creature::tick (float time) |
---|
| 275 | { |
---|
[6176] | 276 | if( likely(this->getModel(0) != NULL)) |
---|
| 277 | ((MD2Model*)this->getModel(0))->tick(time); |
---|
[6170] | 278 | |
---|
| 279 | |
---|
[6180] | 280 | // MD2Creature controlled movement |
---|
| 281 | this->calculateVelocity(time); |
---|
[6195] | 282 | Vector move = this->velocity * time; |
---|
| 283 | this->shiftCoor (move); |
---|
[6180] | 284 | |
---|
[6198] | 285 | if( this->bJump) |
---|
| 286 | { |
---|
[6203] | 287 | ((MD2Model*)this->getModel(0))->setAnim(JUMP); |
---|
[6198] | 288 | } |
---|
[6205] | 289 | else if( this->bFire) |
---|
| 290 | { |
---|
| 291 | if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK) ((MD2Model*)this->getModel(0))->setAnim(ATTACK); |
---|
| 292 | } |
---|
[6198] | 293 | else if( fabs(move.len()) > 0.0f) |
---|
| 294 | { |
---|
| 295 | if( ((MD2Model*)this->getModel(0))->getAnim() != RUN) ((MD2Model*)this->getModel(0))->setAnim(RUN); |
---|
| 296 | } |
---|
| 297 | else |
---|
| 298 | { |
---|
| 299 | if( ((MD2Model*)this->getModel(0))->getAnim() != STAND) ((MD2Model*)this->getModel(0))->setAnim(STAND); |
---|
| 300 | } |
---|
| 301 | |
---|
[6208] | 302 | |
---|
[6180] | 303 | //orient the MD2Creature in direction of the mouse |
---|
[6210] | 304 | this->setAbsDir(mouseDirX); |
---|
[6209] | 305 | this->cameraConnNode.setAbsDirSoft(mouseDirX*mouseDirY, 10); |
---|
[6180] | 306 | |
---|
| 307 | this->getWeaponManager()->tick(time); |
---|
| 308 | // weapon system manipulation |
---|
| 309 | this->weaponAction(); |
---|
[6166] | 310 | } |
---|
| 311 | |
---|
[6181] | 312 | |
---|
[6166] | 313 | /** |
---|
| 314 | * calculate the velocity |
---|
| 315 | * @param time the timeslice since the last frame |
---|
| 316 | */ |
---|
| 317 | void MD2Creature::calculateVelocity (float time) |
---|
| 318 | { |
---|
| 319 | Vector accel(0.0, 0.0, 0.0); |
---|
| 320 | /* |
---|
| 321 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
| 322 | */ |
---|
| 323 | //float rotVal = 0.0; |
---|
| 324 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
| 325 | /* calculate the direction in which the craft is heading */ |
---|
| 326 | |
---|
| 327 | if( this->bUp ) |
---|
| 328 | { |
---|
[6203] | 329 | accel += (this->getAbsDirX())*2; |
---|
[6166] | 330 | } |
---|
| 331 | |
---|
| 332 | if( this->bDown ) |
---|
| 333 | { |
---|
| 334 | accel -= (this->getAbsDirX())*2; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
| 338 | { |
---|
| 339 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
| 340 | } |
---|
| 341 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
| 342 | { |
---|
| 343 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | |
---|
[6195] | 347 | if( this->bStrafeL /* > -this->getRelCoor().z*2*/) |
---|
[6166] | 348 | { |
---|
[6195] | 349 | accel -= this->getAbsDirZ() * 2.0f; |
---|
[6166] | 350 | } |
---|
[6195] | 351 | if( this->bStrafeR /* > this->getRelCoor().z*2*/) |
---|
[6166] | 352 | { |
---|
[6195] | 353 | accel += this->getAbsDirZ() * 2.0f; |
---|
[6166] | 354 | } |
---|
| 355 | if (this->bAscend ) |
---|
| 356 | { |
---|
| 357 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
| 358 | } |
---|
| 359 | if (this->bDescend ) |
---|
| 360 | { |
---|
| 361 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
| 362 | } |
---|
| 363 | |
---|
[6211] | 364 | velocity = accel * 20.0f; |
---|
[6166] | 365 | //rot.normalize(); |
---|
| 366 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
| 367 | } |
---|
| 368 | |
---|
[6195] | 369 | |
---|
[6166] | 370 | /** |
---|
| 371 | * weapon manipulation by the player |
---|
| 372 | */ |
---|
| 373 | void MD2Creature::weaponAction() |
---|
| 374 | { |
---|
| 375 | if( this->bFire) |
---|
| 376 | { |
---|
| 377 | this->getWeaponManager()->fire(); |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | /** |
---|
| 382 | * @todo switch statement ?? |
---|
| 383 | */ |
---|
| 384 | void MD2Creature::process(const Event &event) |
---|
| 385 | { |
---|
| 386 | if( event.type == SDLK_a) |
---|
[6195] | 387 | this->bStrafeL = event.bPressed; |
---|
[6166] | 388 | else if( event.type == SDLK_d) |
---|
[6195] | 389 | this->bStrafeR = event.bPressed; |
---|
[6166] | 390 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
| 391 | this->bFire = event.bPressed; |
---|
| 392 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
| 393 | this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
| 394 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
| 395 | this->getWeaponManager()->previousWeaponConfig(); |
---|
| 396 | else if( event.type == SDLK_w) |
---|
| 397 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
| 398 | else if( event.type == SDLK_s) |
---|
| 399 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
[6198] | 400 | else if( event.type == SDLK_SPACE) |
---|
| 401 | this->bJump = event.bPressed; |
---|
[6166] | 402 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 403 | { |
---|
| 404 | this->xMouse = event.xRel; |
---|
[6203] | 405 | this->yMouse = event.yRel; |
---|
[6209] | 406 | this->mouseDirX *= Quaternion(-M_PI/4*this->xMouse*mouseSensitivity, Vector(0,1,0)); |
---|
| 407 | this->mouseDirY *= Quaternion(-M_PI/4*this->yMouse*mouseSensitivity, Vector(0,0,1)); |
---|
[6195] | 408 | // if( xMouse*xMouse < 0.9) |
---|
| 409 | // this->setAbsDir(mouseDir); |
---|
[6166] | 410 | } |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | #include "weapons/aiming_turret.h" |
---|
| 414 | // FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG |
---|
| 415 | void MD2Creature::ADDWEAPON() |
---|
| 416 | { |
---|
| 417 | Weapon* turret = NULL; |
---|
| 418 | |
---|
| 419 | if ((float)rand()/RAND_MAX < .1) |
---|
| 420 | { |
---|
| 421 | //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET)) |
---|
| 422 | { |
---|
| 423 | turret = new Turret(); |
---|
| 424 | this->getWeaponManager()->addWeapon(turret, 2); |
---|
| 425 | this->getWeaponManager()->changeWeaponConfig(2); |
---|
| 426 | } |
---|
| 427 | } |
---|
| 428 | else |
---|
| 429 | { |
---|
| 430 | //if (this->getWeaponManager()->hasFreeSlot(3)) |
---|
| 431 | { |
---|
| 432 | turret = new AimingTurret(); |
---|
| 433 | this->getWeaponManager()->addWeapon(turret, 3); |
---|
| 434 | |
---|
| 435 | this->getWeaponManager()->changeWeaponConfig(3); |
---|
| 436 | } |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | if(turret != NULL) |
---|
| 440 | { |
---|
| 441 | turret->setName("Turret"); |
---|
| 442 | turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1); |
---|
| 443 | } |
---|
| 444 | } |
---|