[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()); |
---|
| 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); |
---|
| 79 | |
---|
| 80 | //weapons: |
---|
| 81 | Weapon* wpRight = new TestGun(0); |
---|
| 82 | wpRight->setName("testGun Right"); |
---|
| 83 | Weapon* wpLeft = new TestGun(1); |
---|
| 84 | wpLeft->setName("testGun Left"); |
---|
| 85 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON)); |
---|
| 86 | |
---|
| 87 | cannon->setName("BFG"); |
---|
| 88 | |
---|
[6205] | 89 | this->getWeaponManager()->addWeapon(wpLeft, 1, 0); |
---|
| 90 | this->getWeaponManager()->addWeapon(wpRight,1 ,1); |
---|
[6181] | 91 | // this->getWeaponManager()->addWeapon(cannon, 0, 6); |
---|
[6166] | 92 | |
---|
| 93 | //this->getWeaponManager()->addWeapon(turret, 3, 0); |
---|
| 94 | |
---|
[6206] | 95 | this->getWeaponManager()->changeWeaponConfig(0); |
---|
[6166] | 96 | } |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * initializes a MD2Creature |
---|
| 101 | */ |
---|
| 102 | void MD2Creature::init() |
---|
| 103 | { |
---|
| 104 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
| 105 | this->setClassID(CL_MD2_CREATURE, "MD2Creature"); |
---|
| 106 | |
---|
[6181] | 107 | PRINTF(4)("MD2CREATURE INIT\n"); |
---|
[6166] | 108 | |
---|
[6210] | 109 | this->mouseDirX *= Quaternion( M_PI * 0.75f, Vector(0,1,0)); |
---|
| 110 | |
---|
[6204] | 111 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bStrafeL = bStrafeR = bJump = false; |
---|
[6166] | 112 | bFire = false; |
---|
| 113 | xMouse = yMouse = 0; |
---|
[6195] | 114 | mouseSensitivity = 0.003; |
---|
| 115 | airViscosity = 0.0; |
---|
[6166] | 116 | cycle = 0.0; |
---|
| 117 | |
---|
[6208] | 118 | this->cameraConnNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 119 | this->cameraConnNode.setName("CameraConnectorNode"); |
---|
| 120 | this->addChild(&this->cameraConnNode); |
---|
| 121 | this->cameraConnNode.addChild(State::getCameraTarget()); |
---|
[6212] | 122 | this->cameraConnNode.addChild(State::getCamera()); |
---|
[6208] | 123 | State::getCameraTarget()->setRelCoor(10,0,0); |
---|
[6166] | 124 | |
---|
| 125 | travelSpeed = 15.0; |
---|
| 126 | this->velocity = Vector(0.0,0.0,0.0); |
---|
| 127 | |
---|
| 128 | // GLGuiButton* button = new GLGuiPushButton(); |
---|
| 129 | // button->show(); |
---|
| 130 | // button->setLabel("orxonox"); |
---|
| 131 | // button->setBindNode(this); |
---|
| 132 | |
---|
| 133 | //add events to the eventlist |
---|
| 134 | this->registerEvent(SDLK_w); |
---|
| 135 | this->registerEvent(SDLK_s); |
---|
| 136 | this->registerEvent(SDLK_a); |
---|
| 137 | this->registerEvent(SDLK_d); |
---|
[6198] | 138 | this->registerEvent(SDLK_SPACE); |
---|
[6166] | 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 | |
---|
[6205] | 150 | this->getWeaponManager()->setSlotPosition(0, Vector(-0.5, .2, -1.9)); |
---|
[6166] | 151 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 152 | |
---|
[6205] | 153 | this->getWeaponManager()->setSlotPosition(1, Vector(-0.5, .2, 1.9)); |
---|
[6166] | 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 | |
---|
[6212] | 172 | this->cameraConnNode.addChild(this->getWeaponManager()->getFixedTarget()); |
---|
| 173 | this->getWeaponManager()->getFixedTarget()->setRelCoor(10,0,0); |
---|
[6166] | 174 | |
---|
| 175 | } |
---|
| 176 | |
---|
[6218] | 177 | |
---|
| 178 | void MD2Creature::enter() |
---|
| 179 | { |
---|
| 180 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void MD2Creature::leave() |
---|
| 186 | { |
---|
| 187 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | } |
---|
| 191 | |
---|
[6166] | 192 | /** |
---|
| 193 | * loads the Settings of a MD2Creature from an XML-element. |
---|
| 194 | * @param root the XML-element to load the MD2Creature's properties from |
---|
| 195 | */ |
---|
| 196 | void MD2Creature::loadParams(const TiXmlElement* root) |
---|
| 197 | { |
---|
[6512] | 198 | WorldEntity::loadParams(root); |
---|
[6166] | 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * effect that occurs after the MD2Creature is spawned |
---|
| 204 | */ |
---|
| 205 | void MD2Creature::postSpawn () |
---|
| 206 | { |
---|
| 207 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * the action occuring if the MD2Creature left the game |
---|
| 212 | */ |
---|
| 213 | void MD2Creature::leftWorld () |
---|
| 214 | {} |
---|
| 215 | |
---|
| 216 | /** |
---|
| 217 | * this function is called, when two entities collide |
---|
| 218 | * @param entity: the world entity with whom it collides |
---|
| 219 | * |
---|
| 220 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 221 | */ |
---|
| 222 | void MD2Creature::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 223 | { |
---|
[6804] | 224 | |
---|
[6166] | 225 | } |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * draws the MD2Creature after transforming it. |
---|
| 229 | */ |
---|
| 230 | void MD2Creature::draw () const |
---|
| 231 | { |
---|
[6281] | 232 | WorldEntity::draw(); |
---|
[6209] | 233 | // this->cameraConnNode.debugDraw(0); |
---|
[6166] | 234 | } |
---|
| 235 | |
---|
[6195] | 236 | |
---|
[6166] | 237 | /** |
---|
| 238 | * the function called for each passing timeSnap |
---|
| 239 | * @param time The timespan passed since last update |
---|
| 240 | */ |
---|
| 241 | void MD2Creature::tick (float time) |
---|
| 242 | { |
---|
[6804] | 243 | Playable::tick(time); |
---|
[6176] | 244 | if( likely(this->getModel(0) != NULL)) |
---|
| 245 | ((MD2Model*)this->getModel(0))->tick(time); |
---|
[6170] | 246 | |
---|
| 247 | |
---|
[6180] | 248 | // MD2Creature controlled movement |
---|
| 249 | this->calculateVelocity(time); |
---|
[6195] | 250 | Vector move = this->velocity * time; |
---|
| 251 | this->shiftCoor (move); |
---|
[6180] | 252 | |
---|
[6198] | 253 | if( this->bJump) |
---|
| 254 | { |
---|
[6203] | 255 | ((MD2Model*)this->getModel(0))->setAnim(JUMP); |
---|
[6198] | 256 | } |
---|
[6205] | 257 | else if( this->bFire) |
---|
| 258 | { |
---|
| 259 | if( ((MD2Model*)this->getModel(0))->getAnim() != ATTACK) ((MD2Model*)this->getModel(0))->setAnim(ATTACK); |
---|
| 260 | } |
---|
[6198] | 261 | else if( fabs(move.len()) > 0.0f) |
---|
| 262 | { |
---|
| 263 | if( ((MD2Model*)this->getModel(0))->getAnim() != RUN) ((MD2Model*)this->getModel(0))->setAnim(RUN); |
---|
| 264 | } |
---|
| 265 | else |
---|
| 266 | { |
---|
| 267 | if( ((MD2Model*)this->getModel(0))->getAnim() != STAND) ((MD2Model*)this->getModel(0))->setAnim(STAND); |
---|
| 268 | } |
---|
| 269 | |
---|
[6208] | 270 | |
---|
[6180] | 271 | //orient the MD2Creature in direction of the mouse |
---|
[6210] | 272 | this->setAbsDir(mouseDirX); |
---|
[6253] | 273 | this->cameraConnNode.setRelDir(mouseDirY); |
---|
[6166] | 274 | } |
---|
| 275 | |
---|
[6181] | 276 | |
---|
[6166] | 277 | /** |
---|
| 278 | * calculate the velocity |
---|
| 279 | * @param time the timeslice since the last frame |
---|
| 280 | */ |
---|
| 281 | void MD2Creature::calculateVelocity (float time) |
---|
| 282 | { |
---|
| 283 | Vector accel(0.0, 0.0, 0.0); |
---|
| 284 | /* |
---|
| 285 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
| 286 | */ |
---|
| 287 | //float rotVal = 0.0; |
---|
| 288 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
| 289 | /* calculate the direction in which the craft is heading */ |
---|
| 290 | |
---|
| 291 | if( this->bUp ) |
---|
| 292 | { |
---|
[6203] | 293 | accel += (this->getAbsDirX())*2; |
---|
[6166] | 294 | } |
---|
| 295 | |
---|
| 296 | if( this->bDown ) |
---|
| 297 | { |
---|
| 298 | accel -= (this->getAbsDirX())*2; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
| 302 | { |
---|
| 303 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
| 304 | } |
---|
| 305 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
| 306 | { |
---|
| 307 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | |
---|
[6195] | 311 | if( this->bStrafeL /* > -this->getRelCoor().z*2*/) |
---|
[6166] | 312 | { |
---|
[6195] | 313 | accel -= this->getAbsDirZ() * 2.0f; |
---|
[6166] | 314 | } |
---|
[6195] | 315 | if( this->bStrafeR /* > this->getRelCoor().z*2*/) |
---|
[6166] | 316 | { |
---|
[6195] | 317 | accel += this->getAbsDirZ() * 2.0f; |
---|
[6166] | 318 | } |
---|
| 319 | if (this->bAscend ) |
---|
| 320 | { |
---|
| 321 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
| 322 | } |
---|
| 323 | if (this->bDescend ) |
---|
| 324 | { |
---|
| 325 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
| 326 | } |
---|
| 327 | |
---|
[6211] | 328 | velocity = accel * 20.0f; |
---|
[6166] | 329 | //rot.normalize(); |
---|
| 330 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
| 331 | } |
---|
| 332 | |
---|
[6195] | 333 | |
---|
[6166] | 334 | |
---|
| 335 | /** |
---|
| 336 | * @todo switch statement ?? |
---|
| 337 | */ |
---|
| 338 | void MD2Creature::process(const Event &event) |
---|
| 339 | { |
---|
[6804] | 340 | Playable::process(event); |
---|
[6166] | 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 == SDLK_w) |
---|
| 346 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
| 347 | else if( event.type == SDLK_s) |
---|
| 348 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
[6198] | 349 | else if( event.type == SDLK_SPACE) |
---|
| 350 | this->bJump = event.bPressed; |
---|
[6166] | 351 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 352 | { |
---|
| 353 | this->xMouse = event.xRel; |
---|
[6203] | 354 | this->yMouse = event.yRel; |
---|
[6209] | 355 | this->mouseDirX *= Quaternion(-M_PI/4*this->xMouse*mouseSensitivity, Vector(0,1,0)); |
---|
| 356 | this->mouseDirY *= Quaternion(-M_PI/4*this->yMouse*mouseSensitivity, Vector(0,0,1)); |
---|
[6195] | 357 | // if( xMouse*xMouse < 0.9) |
---|
| 358 | // this->setAbsDir(mouseDir); |
---|
[6166] | 359 | } |
---|
| 360 | } |
---|