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