[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 | |
---|
[6204] | 118 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bStrafeL = bStrafeR = bJump = false; |
---|
[6166] | 119 | bFire = false; |
---|
| 120 | xMouse = yMouse = 0; |
---|
[6195] | 121 | mouseSensitivity = 0.003; |
---|
| 122 | airViscosity = 0.0; |
---|
[6166] | 123 | cycle = 0.0; |
---|
| 124 | |
---|
[6208] | 125 | this->cameraConnNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 126 | this->cameraConnNode.setName("CameraConnectorNode"); |
---|
| 127 | this->addChild(&this->cameraConnNode); |
---|
| 128 | this->cameraConnNode.addChild(State::getCameraTarget()); |
---|
| 129 | State::getCameraTarget()->setRelCoor(10,0,0); |
---|
[6166] | 130 | |
---|
| 131 | travelSpeed = 15.0; |
---|
| 132 | this->velocity = Vector(0.0,0.0,0.0); |
---|
| 133 | |
---|
| 134 | // GLGuiButton* button = new GLGuiPushButton(); |
---|
| 135 | // button->show(); |
---|
| 136 | // button->setLabel("orxonox"); |
---|
| 137 | // button->setBindNode(this); |
---|
| 138 | |
---|
| 139 | //add events to the eventlist |
---|
| 140 | this->registerEvent(SDLK_w); |
---|
| 141 | this->registerEvent(SDLK_s); |
---|
| 142 | this->registerEvent(SDLK_a); |
---|
| 143 | this->registerEvent(SDLK_d); |
---|
[6198] | 144 | this->registerEvent(SDLK_SPACE); |
---|
[6166] | 145 | this->registerEvent(SDLK_q); |
---|
| 146 | this->registerEvent(SDLK_e); |
---|
| 147 | this->registerEvent(KeyMapper::PEV_FIRE1); |
---|
| 148 | this->registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
| 149 | this->registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
| 150 | this->registerEvent(EV_MOUSE_MOTION); |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | this->getWeaponManager()->setSlotCount(7); |
---|
| 155 | |
---|
[6205] | 156 | this->getWeaponManager()->setSlotPosition(0, Vector(-0.5, .2, -1.9)); |
---|
[6166] | 157 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 158 | |
---|
[6205] | 159 | this->getWeaponManager()->setSlotPosition(1, Vector(-0.5, .2, 1.9)); |
---|
[6166] | 160 | this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 161 | |
---|
| 162 | this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 163 | this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
| 164 | |
---|
| 165 | this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 166 | this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
| 167 | |
---|
| 168 | this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
| 169 | this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
| 170 | |
---|
| 171 | this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
| 172 | this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
| 173 | // |
---|
| 174 | this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
| 175 | this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 176 | // |
---|
| 177 | |
---|
| 178 | this->getWeaponManager()->getFixedTarget()->setParent(this); |
---|
| 179 | this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0); |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * loads the Settings of a MD2Creature from an XML-element. |
---|
| 185 | * @param root the XML-element to load the MD2Creature's properties from |
---|
| 186 | */ |
---|
| 187 | void MD2Creature::loadParams(const TiXmlElement* root) |
---|
| 188 | { |
---|
| 189 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * adds a weapon to the weapon list of the MD2Creature |
---|
| 195 | * @param weapon to add |
---|
| 196 | */ |
---|
| 197 | void MD2Creature::addWeapon(Weapon* weapon) |
---|
| 198 | { |
---|
| 199 | this->getWeaponManager()->addWeapon(weapon); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * removes a weapon from the MD2Creature |
---|
| 205 | * @param weapon to remove |
---|
| 206 | */ |
---|
| 207 | void MD2Creature::removeWeapon(Weapon* weapon) |
---|
| 208 | { |
---|
| 209 | this->getWeaponManager()->removeWeapon(weapon); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * effect that occurs after the MD2Creature is spawned |
---|
| 214 | */ |
---|
| 215 | void MD2Creature::postSpawn () |
---|
| 216 | { |
---|
| 217 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | /** |
---|
| 221 | * the action occuring if the MD2Creature left the game |
---|
| 222 | */ |
---|
| 223 | void MD2Creature::leftWorld () |
---|
| 224 | {} |
---|
| 225 | |
---|
| 226 | /** |
---|
| 227 | * this function is called, when two entities collide |
---|
| 228 | * @param entity: the world entity with whom it collides |
---|
| 229 | * |
---|
| 230 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 231 | */ |
---|
| 232 | void MD2Creature::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 233 | { |
---|
[6171] | 234 | if (entity->isA(CL_TURRET_POWER_UP) ) |
---|
[6166] | 235 | { |
---|
| 236 | this->ADDWEAPON(); |
---|
| 237 | } |
---|
| 238 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | /** |
---|
| 242 | * draws the MD2Creature after transforming it. |
---|
| 243 | */ |
---|
| 244 | void MD2Creature::draw () const |
---|
| 245 | { |
---|
[6176] | 246 | this->drawLODsafe(); |
---|
[6208] | 247 | |
---|
[6209] | 248 | // this->cameraConnNode.debugDraw(0); |
---|
[6166] | 249 | } |
---|
| 250 | |
---|
[6195] | 251 | |
---|
[6166] | 252 | /** |
---|
| 253 | * the function called for each passing timeSnap |
---|
| 254 | * @param time The timespan passed since last update |
---|
| 255 | */ |
---|
| 256 | void MD2Creature::tick (float time) |
---|
| 257 | { |
---|
[6176] | 258 | if( likely(this->getModel(0) != NULL)) |
---|
| 259 | ((MD2Model*)this->getModel(0))->tick(time); |
---|
[6170] | 260 | |
---|
| 261 | |
---|
[6180] | 262 | // MD2Creature controlled movement |
---|
| 263 | this->calculateVelocity(time); |
---|
[6195] | 264 | Vector move = this->velocity * time; |
---|
| 265 | this->shiftCoor (move); |
---|
[6180] | 266 | |
---|
[6198] | 267 | if( this->bJump) |
---|
| 268 | { |
---|
[6203] | 269 | ((MD2Model*)this->getModel(0))->setAnim(JUMP); |
---|
[6198] | 270 | } |
---|
[6205] | 271 | else if( this->bFire) |
---|
| 272 | { |
---|
| 273 | if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK) ((MD2Model*)this->getModel(0))->setAnim(ATTACK); |
---|
| 274 | } |
---|
[6198] | 275 | else if( fabs(move.len()) > 0.0f) |
---|
| 276 | { |
---|
| 277 | if( ((MD2Model*)this->getModel(0))->getAnim() != RUN) ((MD2Model*)this->getModel(0))->setAnim(RUN); |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | if( ((MD2Model*)this->getModel(0))->getAnim() != STAND) ((MD2Model*)this->getModel(0))->setAnim(STAND); |
---|
| 282 | } |
---|
| 283 | |
---|
[6208] | 284 | |
---|
[6180] | 285 | //orient the MD2Creature in direction of the mouse |
---|
[6206] | 286 | this->setAbsDir( /*rotQuat*/ mouseDirX); |
---|
[6209] | 287 | this->cameraConnNode.setAbsDirSoft(mouseDirX*mouseDirY, 10); |
---|
[6180] | 288 | |
---|
| 289 | this->getWeaponManager()->tick(time); |
---|
| 290 | // weapon system manipulation |
---|
| 291 | this->weaponAction(); |
---|
[6166] | 292 | } |
---|
| 293 | |
---|
[6181] | 294 | |
---|
[6166] | 295 | /** |
---|
| 296 | * calculate the velocity |
---|
| 297 | * @param time the timeslice since the last frame |
---|
| 298 | */ |
---|
| 299 | void MD2Creature::calculateVelocity (float time) |
---|
| 300 | { |
---|
| 301 | Vector accel(0.0, 0.0, 0.0); |
---|
| 302 | /* |
---|
| 303 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
| 304 | */ |
---|
| 305 | //float rotVal = 0.0; |
---|
| 306 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
| 307 | /* calculate the direction in which the craft is heading */ |
---|
| 308 | |
---|
| 309 | if( this->bUp ) |
---|
| 310 | { |
---|
[6203] | 311 | accel += (this->getAbsDirX())*2; |
---|
[6166] | 312 | } |
---|
| 313 | |
---|
| 314 | if( this->bDown ) |
---|
| 315 | { |
---|
| 316 | accel -= (this->getAbsDirX())*2; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
| 320 | { |
---|
| 321 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
| 322 | } |
---|
| 323 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
| 324 | { |
---|
| 325 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | |
---|
[6195] | 329 | if( this->bStrafeL /* > -this->getRelCoor().z*2*/) |
---|
[6166] | 330 | { |
---|
[6195] | 331 | accel -= this->getAbsDirZ() * 2.0f; |
---|
[6166] | 332 | } |
---|
[6195] | 333 | if( this->bStrafeR /* > this->getRelCoor().z*2*/) |
---|
[6166] | 334 | { |
---|
[6195] | 335 | accel += this->getAbsDirZ() * 2.0f; |
---|
[6166] | 336 | } |
---|
| 337 | if (this->bAscend ) |
---|
| 338 | { |
---|
| 339 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
| 340 | } |
---|
| 341 | if (this->bDescend ) |
---|
| 342 | { |
---|
| 343 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
| 344 | } |
---|
| 345 | |
---|
[6195] | 346 | velocity = accel * 30.0f; |
---|
[6166] | 347 | //rot.normalize(); |
---|
| 348 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
| 349 | } |
---|
| 350 | |
---|
[6195] | 351 | |
---|
[6166] | 352 | /** |
---|
| 353 | * weapon manipulation by the player |
---|
| 354 | */ |
---|
| 355 | void MD2Creature::weaponAction() |
---|
| 356 | { |
---|
| 357 | if( this->bFire) |
---|
| 358 | { |
---|
| 359 | this->getWeaponManager()->fire(); |
---|
| 360 | } |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | /** |
---|
| 364 | * @todo switch statement ?? |
---|
| 365 | */ |
---|
| 366 | void MD2Creature::process(const Event &event) |
---|
| 367 | { |
---|
| 368 | if( event.type == SDLK_a) |
---|
[6195] | 369 | this->bStrafeL = event.bPressed; |
---|
[6166] | 370 | else if( event.type == SDLK_d) |
---|
[6195] | 371 | this->bStrafeR = event.bPressed; |
---|
[6166] | 372 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
| 373 | this->bFire = event.bPressed; |
---|
| 374 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
| 375 | this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
| 376 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
| 377 | this->getWeaponManager()->previousWeaponConfig(); |
---|
| 378 | else if( event.type == SDLK_w) |
---|
| 379 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
| 380 | else if( event.type == SDLK_s) |
---|
| 381 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
[6198] | 382 | else if( event.type == SDLK_SPACE) |
---|
| 383 | this->bJump = event.bPressed; |
---|
[6166] | 384 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 385 | { |
---|
| 386 | this->xMouse = event.xRel; |
---|
[6203] | 387 | this->yMouse = event.yRel; |
---|
[6209] | 388 | this->mouseDirX *= Quaternion(-M_PI/4*this->xMouse*mouseSensitivity, Vector(0,1,0)); |
---|
| 389 | this->mouseDirY *= Quaternion(-M_PI/4*this->yMouse*mouseSensitivity, Vector(0,0,1)); |
---|
[6195] | 390 | // if( xMouse*xMouse < 0.9) |
---|
| 391 | // this->setAbsDir(mouseDir); |
---|
[6166] | 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | #include "weapons/aiming_turret.h" |
---|
| 396 | // FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG |
---|
| 397 | void MD2Creature::ADDWEAPON() |
---|
| 398 | { |
---|
| 399 | Weapon* turret = NULL; |
---|
| 400 | |
---|
| 401 | if ((float)rand()/RAND_MAX < .1) |
---|
| 402 | { |
---|
| 403 | //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET)) |
---|
| 404 | { |
---|
| 405 | turret = new Turret(); |
---|
| 406 | this->getWeaponManager()->addWeapon(turret, 2); |
---|
| 407 | this->getWeaponManager()->changeWeaponConfig(2); |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | else |
---|
| 411 | { |
---|
| 412 | //if (this->getWeaponManager()->hasFreeSlot(3)) |
---|
| 413 | { |
---|
| 414 | turret = new AimingTurret(); |
---|
| 415 | this->getWeaponManager()->addWeapon(turret, 3); |
---|
| 416 | |
---|
| 417 | this->getWeaponManager()->changeWeaponConfig(3); |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | if(turret != NULL) |
---|
| 422 | { |
---|
| 423 | turret->setName("Turret"); |
---|
| 424 | turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1); |
---|
| 425 | } |
---|
| 426 | } |
---|