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