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