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