[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 | * destructs the MD2Creature, deletes alocated memory |
---|
| 44 | */ |
---|
| 45 | MD2Creature::~MD2Creature () |
---|
| 46 | { |
---|
[6986] | 47 | this->setPlayer(NULL); |
---|
[6166] | 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 | */ |
---|
| 54 | MD2Creature::MD2Creature(const char* fileName) |
---|
| 55 | { |
---|
| 56 | this->init(); |
---|
| 57 | TiXmlDocument doc(fileName); |
---|
| 58 | |
---|
| 59 | if(!doc.LoadFile()) |
---|
| 60 | { |
---|
| 61 | PRINTF(2)("Loading file %s failed for md2 creature.\n", fileName); |
---|
| 62 | return; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | this->loadParams(doc.RootElement()); |
---|
[7069] | 66 | this->toList(OM_GROUP_03); |
---|
[6166] | 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * creates a new MD2Creature from Xml Data |
---|
| 71 | * @param root the xml element containing MD2Creature data |
---|
| 72 | |
---|
| 73 | @todo add more parameters to load |
---|
| 74 | */ |
---|
| 75 | MD2Creature::MD2Creature(const TiXmlElement* root) |
---|
| 76 | { |
---|
| 77 | this->init(); |
---|
| 78 | if (root != NULL) |
---|
| 79 | this->loadParams(root); |
---|
| 80 | |
---|
| 81 | //weapons: |
---|
| 82 | Weapon* wpRight = new TestGun(0); |
---|
| 83 | wpRight->setName("testGun Right"); |
---|
| 84 | Weapon* wpLeft = new TestGun(1); |
---|
| 85 | wpLeft->setName("testGun Left"); |
---|
| 86 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON)); |
---|
| 87 | |
---|
| 88 | cannon->setName("BFG"); |
---|
| 89 | |
---|
[6205] | 90 | this->getWeaponManager()->addWeapon(wpLeft, 1, 0); |
---|
| 91 | this->getWeaponManager()->addWeapon(wpRight,1 ,1); |
---|
[6181] | 92 | // this->getWeaponManager()->addWeapon(cannon, 0, 6); |
---|
[6166] | 93 | |
---|
| 94 | //this->getWeaponManager()->addWeapon(turret, 3, 0); |
---|
| 95 | |
---|
[6206] | 96 | this->getWeaponManager()->changeWeaponConfig(0); |
---|
[6166] | 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * initializes a MD2Creature |
---|
| 102 | */ |
---|
| 103 | void MD2Creature::init() |
---|
| 104 | { |
---|
| 105 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
| 106 | this->setClassID(CL_MD2_CREATURE, "MD2Creature"); |
---|
| 107 | |
---|
[6181] | 108 | PRINTF(4)("MD2CREATURE INIT\n"); |
---|
[6166] | 109 | |
---|
[6210] | 110 | this->mouseDirX *= Quaternion( M_PI * 0.75f, Vector(0,1,0)); |
---|
| 111 | |
---|
[6204] | 112 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bStrafeL = bStrafeR = bJump = false; |
---|
[6166] | 113 | bFire = false; |
---|
| 114 | xMouse = yMouse = 0; |
---|
[6195] | 115 | mouseSensitivity = 0.003; |
---|
| 116 | airViscosity = 0.0; |
---|
[6166] | 117 | cycle = 0.0; |
---|
| 118 | |
---|
[6208] | 119 | this->cameraConnNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 120 | this->cameraConnNode.setName("CameraConnectorNode"); |
---|
| 121 | this->addChild(&this->cameraConnNode); |
---|
[7014] | 122 | this->cameraConnNode.addChild(State::getCameraTargetNode()); |
---|
| 123 | this->cameraConnNode.addChild(State::getCameraNode()); |
---|
| 124 | State::getCameraTargetNode()->setRelCoor(10,0,0); |
---|
[6166] | 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); |
---|
[6198] | 139 | this->registerEvent(SDLK_SPACE); |
---|
[6166] | 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 | |
---|
[6205] | 151 | this->getWeaponManager()->setSlotPosition(0, Vector(-0.5, .2, -1.9)); |
---|
[6166] | 152 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 153 | |
---|
[6205] | 154 | this->getWeaponManager()->setSlotPosition(1, Vector(-0.5, .2, 1.9)); |
---|
[6166] | 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 | |
---|
[6212] | 173 | this->cameraConnNode.addChild(this->getWeaponManager()->getFixedTarget()); |
---|
[7057] | 174 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 175 | |
---|
[6212] | 176 | this->getWeaponManager()->getFixedTarget()->setRelCoor(10,0,0); |
---|
[6166] | 177 | |
---|
| 178 | } |
---|
| 179 | |
---|
[6218] | 180 | |
---|
| 181 | void MD2Creature::enter() |
---|
| 182 | { |
---|
| 183 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
[7053] | 184 | this->attachCamera(); |
---|
[6218] | 185 | |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void MD2Creature::leave() |
---|
| 189 | { |
---|
| 190 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | } |
---|
| 194 | |
---|
[6166] | 195 | /** |
---|
| 196 | * loads the Settings of a MD2Creature from an XML-element. |
---|
| 197 | * @param root the XML-element to load the MD2Creature's properties from |
---|
| 198 | */ |
---|
| 199 | void MD2Creature::loadParams(const TiXmlElement* root) |
---|
| 200 | { |
---|
[6512] | 201 | WorldEntity::loadParams(root); |
---|
[6166] | 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * effect that occurs after the MD2Creature is spawned |
---|
| 207 | */ |
---|
| 208 | void MD2Creature::postSpawn () |
---|
| 209 | { |
---|
| 210 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 211 | } |
---|
| 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 | { |
---|
[7069] | 227 | PRINTF(0)("Collided with the md2 model\n"); |
---|
[6166] | 228 | } |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | * draws the MD2Creature after transforming it. |
---|
| 232 | */ |
---|
| 233 | void MD2Creature::draw () const |
---|
| 234 | { |
---|
[7053] | 235 | if (this->getCurrentPlayer() != NULL) |
---|
| 236 | WorldEntity::draw(); |
---|
[6209] | 237 | // this->cameraConnNode.debugDraw(0); |
---|
[6166] | 238 | } |
---|
| 239 | |
---|
[6195] | 240 | |
---|
[6166] | 241 | /** |
---|
| 242 | * the function called for each passing timeSnap |
---|
| 243 | * @param time The timespan passed since last update |
---|
| 244 | */ |
---|
| 245 | void MD2Creature::tick (float time) |
---|
| 246 | { |
---|
[6804] | 247 | Playable::tick(time); |
---|
[6176] | 248 | if( likely(this->getModel(0) != NULL)) |
---|
| 249 | ((MD2Model*)this->getModel(0))->tick(time); |
---|
[6170] | 250 | |
---|
| 251 | |
---|
[6180] | 252 | // MD2Creature controlled movement |
---|
| 253 | this->calculateVelocity(time); |
---|
[6195] | 254 | Vector move = this->velocity * time; |
---|
| 255 | this->shiftCoor (move); |
---|
[6180] | 256 | |
---|
[6198] | 257 | if( this->bJump) |
---|
| 258 | { |
---|
[6203] | 259 | ((MD2Model*)this->getModel(0))->setAnim(JUMP); |
---|
[6198] | 260 | } |
---|
[6205] | 261 | else if( this->bFire) |
---|
| 262 | { |
---|
| 263 | if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK) ((MD2Model*)this->getModel(0))->setAnim(ATTACK); |
---|
| 264 | } |
---|
[6198] | 265 | else if( fabs(move.len()) > 0.0f) |
---|
| 266 | { |
---|
| 267 | if( ((MD2Model*)this->getModel(0))->getAnim() != RUN) ((MD2Model*)this->getModel(0))->setAnim(RUN); |
---|
| 268 | } |
---|
| 269 | else |
---|
| 270 | { |
---|
| 271 | if( ((MD2Model*)this->getModel(0))->getAnim() != STAND) ((MD2Model*)this->getModel(0))->setAnim(STAND); |
---|
| 272 | } |
---|
| 273 | |
---|
[6208] | 274 | |
---|
[6180] | 275 | //orient the MD2Creature in direction of the mouse |
---|
[6210] | 276 | this->setAbsDir(mouseDirX); |
---|
[6253] | 277 | this->cameraConnNode.setRelDir(mouseDirY); |
---|
[6166] | 278 | } |
---|
| 279 | |
---|
[6181] | 280 | |
---|
[6166] | 281 | /** |
---|
| 282 | * calculate the velocity |
---|
| 283 | * @param time the timeslice since the last frame |
---|
| 284 | */ |
---|
| 285 | void MD2Creature::calculateVelocity (float time) |
---|
| 286 | { |
---|
| 287 | Vector accel(0.0, 0.0, 0.0); |
---|
| 288 | /* |
---|
| 289 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
| 290 | */ |
---|
| 291 | //float rotVal = 0.0; |
---|
| 292 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
| 293 | /* calculate the direction in which the craft is heading */ |
---|
| 294 | |
---|
| 295 | if( this->bUp ) |
---|
| 296 | { |
---|
[6203] | 297 | accel += (this->getAbsDirX())*2; |
---|
[6166] | 298 | } |
---|
| 299 | |
---|
| 300 | if( this->bDown ) |
---|
| 301 | { |
---|
| 302 | accel -= (this->getAbsDirX())*2; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
| 306 | { |
---|
| 307 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
| 308 | } |
---|
| 309 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
| 310 | { |
---|
| 311 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | |
---|
[6195] | 315 | if( this->bStrafeL /* > -this->getRelCoor().z*2*/) |
---|
[6166] | 316 | { |
---|
[6195] | 317 | accel -= this->getAbsDirZ() * 2.0f; |
---|
[6166] | 318 | } |
---|
[6195] | 319 | if( this->bStrafeR /* > this->getRelCoor().z*2*/) |
---|
[6166] | 320 | { |
---|
[6195] | 321 | accel += this->getAbsDirZ() * 2.0f; |
---|
[6166] | 322 | } |
---|
| 323 | if (this->bAscend ) |
---|
| 324 | { |
---|
| 325 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
| 326 | } |
---|
| 327 | if (this->bDescend ) |
---|
| 328 | { |
---|
| 329 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
| 330 | } |
---|
| 331 | |
---|
[6211] | 332 | velocity = accel * 20.0f; |
---|
[6166] | 333 | //rot.normalize(); |
---|
| 334 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
| 335 | } |
---|
| 336 | |
---|
[6195] | 337 | |
---|
[6166] | 338 | |
---|
| 339 | /** |
---|
| 340 | * @todo switch statement ?? |
---|
| 341 | */ |
---|
| 342 | void MD2Creature::process(const Event &event) |
---|
| 343 | { |
---|
[6804] | 344 | Playable::process(event); |
---|
[6166] | 345 | if( event.type == SDLK_a) |
---|
[6195] | 346 | this->bStrafeL = event.bPressed; |
---|
[6166] | 347 | else if( event.type == SDLK_d) |
---|
[6195] | 348 | this->bStrafeR = event.bPressed; |
---|
[6166] | 349 | else if( event.type == SDLK_w) |
---|
| 350 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
| 351 | else if( event.type == SDLK_s) |
---|
| 352 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
[6198] | 353 | else if( event.type == SDLK_SPACE) |
---|
| 354 | this->bJump = event.bPressed; |
---|
[6166] | 355 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 356 | { |
---|
| 357 | this->xMouse = event.xRel; |
---|
[6203] | 358 | this->yMouse = event.yRel; |
---|
[6209] | 359 | this->mouseDirX *= Quaternion(-M_PI/4*this->xMouse*mouseSensitivity, Vector(0,1,0)); |
---|
| 360 | this->mouseDirY *= Quaternion(-M_PI/4*this->yMouse*mouseSensitivity, Vector(0,0,1)); |
---|
[6195] | 361 | // if( xMouse*xMouse < 0.9) |
---|
| 362 | // this->setAbsDir(mouseDir); |
---|
[6166] | 363 | } |
---|
| 364 | } |
---|