| 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 | #include "fps_player.h" |
|---|
| 18 | |
|---|
| 19 | #include "interactive_model.h" |
|---|
| 20 | #include "state.h" |
|---|
| 21 | |
|---|
| 22 | #include "src/lib/util/loading/factory.h" |
|---|
| 23 | |
|---|
| 24 | #include "md2/md2Model.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 | #include "weapons/fps_sniper_rifle.h" |
|---|
| 31 | |
|---|
| 32 | #include "aabb.h" |
|---|
| 33 | |
|---|
| 34 | #include "key_mapper.h" |
|---|
| 35 | |
|---|
| 36 | #include "debug.h" |
|---|
| 37 | |
|---|
| 38 | #include "shared_network_data.h" |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER); |
|---|
| 43 | |
|---|
| 44 | #include "script_class.h" |
|---|
| 45 | CREATE_SCRIPTABLE_CLASS(FPSPlayer, CL_FPS_PLAYER, |
|---|
| 46 | addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor)) |
|---|
| 47 | ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX)) |
|---|
| 48 | ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY)) |
|---|
| 49 | ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ)) |
|---|
| 50 | ); |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * destructs the FPSPlayer, deletes alocated memory |
|---|
| 55 | */ |
|---|
| 56 | FPSPlayer::~FPSPlayer () |
|---|
| 57 | { |
|---|
| 58 | this->setPlayer(NULL); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * creates a new FPSPlayer from Xml Data |
|---|
| 64 | * @param root the xml element containing FPSPlayer data |
|---|
| 65 | * |
|---|
| 66 | */ |
|---|
| 67 | FPSPlayer::FPSPlayer(const TiXmlElement* root) |
|---|
| 68 | { |
|---|
| 69 | this->init(); |
|---|
| 70 | |
|---|
| 71 | if (root != NULL) |
|---|
| 72 | this->loadParams(root); |
|---|
| 73 | |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * initializes a FPSPlayer |
|---|
| 79 | */ |
|---|
| 80 | void FPSPlayer::init() |
|---|
| 81 | { |
|---|
| 82 | this->setClassID(CL_FPS_PLAYER, "FPSPlayer"); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | this->bLeft = false; |
|---|
| 86 | this->bRight = false; |
|---|
| 87 | this->bForward = false; |
|---|
| 88 | this->bBackward = false; |
|---|
| 89 | this->bJump = false; |
|---|
| 90 | this->bPosBut = false; |
|---|
| 91 | |
|---|
| 92 | this->xMouse = 0.0f; |
|---|
| 93 | this->yMouse = 0.0f; |
|---|
| 94 | |
|---|
| 95 | this->setHealthMax(100); |
|---|
| 96 | this->setHealth(80); |
|---|
| 97 | |
|---|
| 98 | this->fallVelocity = 0.0f; |
|---|
| 99 | this->jumpForce = 0.0f; |
|---|
| 100 | |
|---|
| 101 | this->cameraNode.setParent(this); |
|---|
| 102 | |
|---|
| 103 | this->attitude = this->getAbsDir().getAttitude(); |
|---|
| 104 | this->heading = this->getAbsDir().getHeading(); |
|---|
| 105 | |
|---|
| 106 | //add events to the eventlist |
|---|
| 107 | registerEvent(KeyMapper::PEV_FORWARD); |
|---|
| 108 | registerEvent(KeyMapper::PEV_BACKWARD); |
|---|
| 109 | registerEvent(KeyMapper::PEV_LEFT); |
|---|
| 110 | registerEvent(KeyMapper::PEV_RIGHT); |
|---|
| 111 | registerEvent(KeyMapper::PEV_FIRE1); |
|---|
| 112 | registerEvent(KeyMapper::PEV_JUMP); |
|---|
| 113 | registerEvent(EV_MOUSE_MOTION); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | // weapon manager for the fps |
|---|
| 118 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
|---|
| 119 | |
|---|
| 120 | Weapon* wpRight = new FPSSniperRifle(0); |
|---|
| 121 | wpRight->setName("testGun Right"); |
|---|
| 122 | /* Weapon* wpLeft = new TestGun(1);*/ |
|---|
| 123 | // Weapon* wpLeft = new Turret(); |
|---|
| 124 | // wpLeft->setName("testGun Left"); |
|---|
| 125 | |
|---|
| 126 | // this->addWeapon(wpLeft, 1, 0); |
|---|
| 127 | this->addWeapon(wpRight,1, 0); |
|---|
| 128 | this->getWeaponManager().changeWeaponConfig(1); |
|---|
| 129 | |
|---|
| 130 | this->getWeaponManager().setSlotCount(2); |
|---|
| 131 | // this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_2, Vector(0,1,0))); |
|---|
| 132 | this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
|---|
| 133 | this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
|---|
| 134 | this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1)); |
|---|
| 135 | this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0)); |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | this->getWeaponManager().setParentNode(&this->cameraNode); |
|---|
| 139 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
|---|
| 140 | |
|---|
| 141 | this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode); |
|---|
| 142 | this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | // network registration |
|---|
| 146 | registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); |
|---|
| 147 | registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); |
|---|
| 148 | registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) ); |
|---|
| 149 | registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) ); |
|---|
| 150 | registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) ); |
|---|
| 151 | registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) ); |
|---|
| 152 | registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) ); |
|---|
| 153 | |
|---|
| 154 | //subscribe to collision reaction |
|---|
| 155 | this->subscribeReaction(CREngine::CR_PHYSICS_FULL_WALK, CL_BSP_ENTITY); |
|---|
| 156 | |
|---|
| 157 | this->initWeapon = false; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * loads the Settings of a FPSPlayer from an XML-element. |
|---|
| 163 | * @param root the XML-element to load the Spaceship's properties from |
|---|
| 164 | */ |
|---|
| 165 | void FPSPlayer::loadParams(const TiXmlElement* root) |
|---|
| 166 | { |
|---|
| 167 | Playable::loadParams(root); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed) |
|---|
| 171 | { |
|---|
| 172 | this->attitude = this->getAbsDir().getAttitude(); |
|---|
| 173 | this->heading = this->getAbsDir().getHeading(); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | void FPSPlayer::reset() |
|---|
| 178 | { |
|---|
| 179 | this->bLeft = false; |
|---|
| 180 | this->bRight = false; |
|---|
| 181 | this->bForward = false; |
|---|
| 182 | this->bBackward = false; |
|---|
| 183 | this->xMouse = 0.0f; |
|---|
| 184 | this->yMouse = 0.0f; |
|---|
| 185 | |
|---|
| 186 | this->setHealth(80); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | void FPSPlayer::enter() |
|---|
| 191 | { |
|---|
| 192 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true ); |
|---|
| 193 | |
|---|
| 194 | State::getCameraNode()->setParentSoft(&this->cameraNode); |
|---|
| 195 | State::getCameraTargetNode()->setParentSoft(&this->cameraNode); |
|---|
| 196 | |
|---|
| 197 | this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode()); |
|---|
| 198 | this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0); |
|---|
| 199 | |
|---|
| 200 | if ( !State::isOnline() ) |
|---|
| 201 | { |
|---|
| 202 | this->respawn(); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | void FPSPlayer::leave() |
|---|
| 207 | { |
|---|
| 208 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
|---|
| 209 | this->detachCamera(); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | /** |
|---|
| 215 | * the function called for each passing timeSnap |
|---|
| 216 | * @param time The timespan passed since last update |
|---|
| 217 | */ |
|---|
| 218 | void FPSPlayer::tick (float time) |
|---|
| 219 | { |
|---|
| 220 | if ( !this->initWeapon ) |
|---|
| 221 | { |
|---|
| 222 | this->initWeapon = true; |
|---|
| 223 | |
|---|
| 224 | AABB* box = this->getModelAABB(); |
|---|
| 225 | if( box != NULL) |
|---|
| 226 | { |
|---|
| 227 | float f = 1.3f; |
|---|
| 228 | State::getCameraNode()->setRelCoor(0, box->halfLength[1] * f, 0); |
|---|
| 229 | State::getCameraTargetNode()->setRelCoor(10, box->halfLength[1] * f, 0); |
|---|
| 230 | |
|---|
| 231 | this->getWeaponManager().setSlotPosition(0, Vector(1.5, box->halfLength[1] * f - 0.7, 1.1)); |
|---|
| 232 | this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * f, 0.0)); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | if( this->bJump) |
|---|
| 237 | { |
|---|
| 238 | printf("mechanic2:walkTo( %f, mtheight, %f)\n",this->getAbsCoorX(),this->getAbsCoorZ()); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | Playable::tick( time ); |
|---|
| 242 | |
|---|
| 243 | if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) ) |
|---|
| 244 | { |
|---|
| 245 | xMouse *= time ; |
|---|
| 246 | yMouse *= time ; |
|---|
| 247 | |
|---|
| 248 | heading -= xMouse; |
|---|
| 249 | attitude-= yMouse; |
|---|
| 250 | |
|---|
| 251 | if ( attitude > 2.05 ) |
|---|
| 252 | attitude = 2.05; |
|---|
| 253 | |
|---|
| 254 | else if ( attitude < -1.15 ) |
|---|
| 255 | attitude = -1.15; |
|---|
| 256 | |
|---|
| 257 | xMouse = yMouse = 0; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | this->setAbsDir(Quaternion(heading, Vector(0,1,0))); |
|---|
| 261 | this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) )); |
|---|
| 262 | |
|---|
| 263 | Vector velocity; |
|---|
| 264 | |
|---|
| 265 | if ( this->bForward ) |
|---|
| 266 | { |
|---|
| 267 | velocity += this->getAbsDirX(); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | if ( this->bBackward ) |
|---|
| 271 | { |
|---|
| 272 | velocity -= this->getAbsDirX(); |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | if ( this->bRight ) |
|---|
| 276 | { |
|---|
| 277 | velocity += this->getAbsDirZ(); |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | if ( this->bLeft ) |
|---|
| 281 | { |
|---|
| 282 | velocity -= this->getAbsDirZ(); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | velocity *= 100; |
|---|
| 287 | |
|---|
| 288 | if( this->bJump && likely(this->getModel(0) != NULL)) |
|---|
| 289 | { |
|---|
| 290 | if( this->jumpForce < 1.0f) |
|---|
| 291 | { |
|---|
| 292 | this->jumpForce = 300.0f; |
|---|
| 293 | |
|---|
| 294 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP) |
|---|
| 295 | ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP); |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | else if(velocity.len() != 0.0f) |
|---|
| 299 | { |
|---|
| 300 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN) |
|---|
| 301 | ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); |
|---|
| 302 | } |
|---|
| 303 | else |
|---|
| 304 | { |
|---|
| 305 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND) |
|---|
| 306 | ((InteractiveModel*)this->getModel(0))->setAnimation(STAND); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | velocity.y += this->jumpForce; |
|---|
| 311 | if( this->jumpForce > 1.0f) |
|---|
| 312 | this->jumpForce *= 0.9f; |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | // physical falling of the player |
|---|
| 316 | if( !this->isOnGround()) |
|---|
| 317 | { |
|---|
| 318 | this->fallVelocity += 300.0f * time; |
|---|
| 319 | velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity; |
|---|
| 320 | } |
|---|
| 321 | else |
|---|
| 322 | { |
|---|
| 323 | this->fallVelocity = 0.0f; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | this->shiftCoor( velocity*time ); |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL)) |
|---|
| 336 | { |
|---|
| 337 | ((InteractiveModel*)this->getModel(0))->tick(time); |
|---|
| 338 | |
|---|
| 339 | // handle animations differently |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | // else if( this->bFire && likely(this->getModel(0) != NULL)) |
|---|
| 346 | // { |
|---|
| 347 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK) |
|---|
| 348 | // ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK); |
|---|
| 349 | // } |
|---|
| 350 | // else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL)) |
|---|
| 351 | // { |
|---|
| 352 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN) |
|---|
| 353 | // ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); |
|---|
| 354 | // } |
|---|
| 355 | // else if (likely(this->getModel(0) != NULL)) |
|---|
| 356 | // { |
|---|
| 357 | // if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND) |
|---|
| 358 | // ((InteractiveModel*)this->getModel(0))->setAnimation(STAND); |
|---|
| 359 | // } |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | /** |
|---|
| 367 | * draws the MD2Creature after transforming it. |
|---|
| 368 | */ |
|---|
| 369 | void FPSPlayer::draw () const |
|---|
| 370 | { |
|---|
| 371 | // only draw if this entity is not the player since the player nevers sees himself |
|---|
| 372 | if( this->getCurrentPlayer() == NULL) |
|---|
| 373 | WorldEntity::draw(); |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | /** |
|---|
| 379 | * process |
|---|
| 380 | */ |
|---|
| 381 | void FPSPlayer::process(const Event &event) |
|---|
| 382 | { |
|---|
| 383 | Playable::process(event); |
|---|
| 384 | |
|---|
| 385 | if( event.type == KeyMapper::PEV_LEFT) |
|---|
| 386 | this->bLeft = event.bPressed; |
|---|
| 387 | else if( event.type == KeyMapper::PEV_RIGHT) |
|---|
| 388 | this->bRight = event.bPressed; |
|---|
| 389 | else if( event.type == KeyMapper::PEV_FORWARD) |
|---|
| 390 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
|---|
| 391 | else if( event.type == KeyMapper::PEV_BACKWARD) |
|---|
| 392 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
|---|
| 393 | else if( event.type == EV_MOUSE_MOTION) |
|---|
| 394 | { |
|---|
| 395 | this->xMouse += event.xRel; |
|---|
| 396 | this->yMouse += event.yRel; |
|---|
| 397 | } |
|---|
| 398 | else if( event.type == KeyMapper::PEV_JUMP) |
|---|
| 399 | this->bJump = event.bPressed; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | /* |
|---|
| 403 | void FPSPlayer::respawn( ) |
|---|
| 404 | { |
|---|
| 405 | Playable::respawn(); |
|---|
| 406 | |
|---|
| 407 | AABB* box = this->getModelAABB(); |
|---|
| 408 | if( box != NULL) |
|---|
| 409 | { |
|---|
| 410 | float f = 1.3f; |
|---|
| 411 | State::getCameraNode()->setRelCoor(0, box->halfLength[1] * f, 0); |
|---|
| 412 | State::getCameraTargetNode()->setRelCoor(10, box->halfLength[1] * f, 0); |
|---|
| 413 | |
|---|
| 414 | this->getWeaponManager().setSlotPosition(0, Vector(1.5, box->halfLength[1] * f - 0.7, 1.1)); |
|---|
| 415 | this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * f, 0.0)); |
|---|
| 416 | } |
|---|
| 417 | } |
|---|
| 418 | */ |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|