[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 | |
---|
[9103] | 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" |
---|
[8776] | 31 | |
---|
[9101] | 32 | #include "aabb.h" |
---|
| 33 | |
---|
[8685] | 34 | #include "key_mapper.h" |
---|
| 35 | |
---|
[8687] | 36 | #include "debug.h" |
---|
[8685] | 37 | |
---|
[9085] | 38 | #include "shared_network_data.h" |
---|
[8685] | 39 | |
---|
[8687] | 40 | |
---|
[8731] | 41 | |
---|
[8685] | 42 | CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER); |
---|
| 43 | |
---|
[8894] | 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 | ); |
---|
[8685] | 51 | |
---|
[8894] | 52 | |
---|
[8685] | 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; |
---|
[9003] | 90 | this->bPosBut = false; |
---|
[8731] | 91 | |
---|
[8685] | 92 | this->xMouse = 0.0f; |
---|
| 93 | this->yMouse = 0.0f; |
---|
| 94 | |
---|
| 95 | this->setHealthMax(100); |
---|
| 96 | this->setHealth(80); |
---|
| 97 | |
---|
[9003] | 98 | this->fallVelocity = 0.0f; |
---|
[9105] | 99 | this->jumpForce = 0.0f; |
---|
[8685] | 100 | |
---|
[8731] | 101 | this->cameraNode.setParent(this); |
---|
| 102 | |
---|
| 103 | this->attitude = this->getAbsDir().getAttitude(); |
---|
| 104 | this->heading = this->getAbsDir().getHeading(); |
---|
| 105 | |
---|
[8685] | 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); |
---|
[8687] | 112 | registerEvent(KeyMapper::PEV_JUMP); |
---|
[8685] | 113 | registerEvent(EV_MOUSE_MOTION); |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
[8776] | 117 | // weapon manager for the fps |
---|
[8685] | 118 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
| 119 | |
---|
[8894] | 120 | Weapon* wpRight = new FPSSniperRifle(0); |
---|
[8776] | 121 | wpRight->setName("testGun Right"); |
---|
[8894] | 122 | /* Weapon* wpLeft = new TestGun(1);*/ |
---|
[8776] | 123 | // Weapon* wpLeft = new Turret(); |
---|
[8894] | 124 | // wpLeft->setName("testGun Left"); |
---|
[8776] | 125 | |
---|
[8894] | 126 | // this->addWeapon(wpLeft, 1, 0); |
---|
| 127 | this->addWeapon(wpRight,1, 0); |
---|
[8776] | 128 | this->getWeaponManager().changeWeaponConfig(1); |
---|
| 129 | |
---|
| 130 | this->getWeaponManager().setSlotCount(2); |
---|
[9003] | 131 | // this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_2, Vector(0,1,0))); |
---|
[8776] | 132 | this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 133 | this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
[9102] | 134 | this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1)); |
---|
| 135 | this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0)); |
---|
[8776] | 136 | |
---|
[9101] | 137 | |
---|
[8844] | 138 | this->getWeaponManager().setParentNode(&this->cameraNode); |
---|
[8846] | 139 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
| 140 | |
---|
[8776] | 141 | this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode); |
---|
| 142 | this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0); |
---|
| 143 | |
---|
| 144 | |
---|
[8686] | 145 | // network registration |
---|
[8685] | 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 ) ); |
---|
[9085] | 150 | registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) ); |
---|
| 151 | registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) ); |
---|
[8685] | 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * loads the Settings of a FPSPlayer from an XML-element. |
---|
| 157 | * @param root the XML-element to load the Spaceship's properties from |
---|
| 158 | */ |
---|
| 159 | void FPSPlayer::loadParams(const TiXmlElement* root) |
---|
| 160 | { |
---|
| 161 | Playable::loadParams(root); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed) |
---|
| 165 | { |
---|
[8731] | 166 | this->attitude = this->getAbsDir().getAttitude(); |
---|
| 167 | this->heading = this->getAbsDir().getHeading(); |
---|
[8685] | 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | void FPSPlayer::reset() |
---|
| 172 | { |
---|
| 173 | this->bLeft = false; |
---|
| 174 | this->bRight = false; |
---|
| 175 | this->bForward = false; |
---|
| 176 | this->bBackward = false; |
---|
| 177 | this->xMouse = 0.0f; |
---|
| 178 | this->yMouse = 0.0f; |
---|
| 179 | |
---|
| 180 | this->setHealth(80); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | void FPSPlayer::enter() |
---|
| 185 | { |
---|
[8731] | 186 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true ); |
---|
| 187 | |
---|
| 188 | State::getCameraNode()->setParentSoft(&this->cameraNode); |
---|
| 189 | State::getCameraTargetNode()->setParentSoft(&this->cameraNode); |
---|
| 190 | |
---|
| 191 | this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode()); |
---|
| 192 | this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0); |
---|
| 193 | |
---|
| 194 | |
---|
[9101] | 195 | AABB* box = this->getModelAABB(); |
---|
| 196 | if( box != NULL) |
---|
| 197 | { |
---|
| 198 | State::getCameraNode()->setRelCoor(0, box->halfLength[1] * 2.0f, 0); |
---|
| 199 | State::getCameraTargetNode()->setRelCoor(10, box->halfLength[1] * 2.0f, 0); |
---|
[9102] | 200 | |
---|
| 201 | this->getWeaponManager().setSlotPosition(0, Vector(1.5, box->halfLength[1] * 2.0f - 0.7, 1.1)); |
---|
| 202 | this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * 2.0f, 0.0)); |
---|
[9101] | 203 | } |
---|
[8685] | 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 | { |
---|
[9101] | 220 | |
---|
[9003] | 221 | if( this->bPosBut) |
---|
| 222 | { |
---|
| 223 | this->bPosBut = false; |
---|
[9104] | 224 | printf("mechanic:walkTo( %f, %f, %f)\n",this->getAbsCoorX(),this->getAbsCoorY(),this->getAbsCoorZ()); |
---|
[9003] | 225 | } |
---|
[9101] | 226 | |
---|
[8685] | 227 | Playable::tick( time ); |
---|
| 228 | |
---|
[9085] | 229 | if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) ) |
---|
[8685] | 230 | { |
---|
[8687] | 231 | xMouse *= time ; |
---|
| 232 | yMouse *= time ; |
---|
[8685] | 233 | |
---|
[8731] | 234 | heading -= xMouse; |
---|
| 235 | attitude-= yMouse; |
---|
[8685] | 236 | |
---|
[8731] | 237 | if ( attitude > 2.05 ) |
---|
| 238 | attitude = 2.05; |
---|
[8685] | 239 | |
---|
[8731] | 240 | else if ( attitude < -1.15 ) |
---|
| 241 | attitude = -1.15; |
---|
[8685] | 242 | |
---|
| 243 | xMouse = yMouse = 0; |
---|
| 244 | } |
---|
| 245 | |
---|
[9085] | 246 | this->setAbsDir(Quaternion(heading, Vector(0,1,0))); |
---|
| 247 | this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) )); |
---|
[8685] | 248 | |
---|
| 249 | Vector velocity; |
---|
| 250 | |
---|
| 251 | if ( this->bForward ) |
---|
| 252 | { |
---|
| 253 | velocity += this->getAbsDirX(); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | if ( this->bBackward ) |
---|
| 257 | { |
---|
| 258 | velocity -= this->getAbsDirX(); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | if ( this->bRight ) |
---|
| 262 | { |
---|
| 263 | velocity += this->getAbsDirZ(); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | if ( this->bLeft ) |
---|
| 267 | { |
---|
| 268 | velocity -= this->getAbsDirZ(); |
---|
| 269 | } |
---|
| 270 | |
---|
[9003] | 271 | |
---|
[8685] | 272 | velocity *= 100; |
---|
| 273 | |
---|
[9104] | 274 | if( this->bJump && likely(this->getModel(0) != NULL)) |
---|
[9103] | 275 | { |
---|
[9105] | 276 | if( this->jumpForce < 1.0f) |
---|
| 277 | { |
---|
| 278 | this->jumpForce = 300.0f; |
---|
| 279 | |
---|
| 280 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP) |
---|
| 281 | ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP); |
---|
| 282 | } |
---|
[9104] | 283 | } |
---|
| 284 | else if(velocity.len() != 0.0f) |
---|
| 285 | { |
---|
[9103] | 286 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN) |
---|
| 287 | ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); |
---|
| 288 | } |
---|
| 289 | else |
---|
| 290 | { |
---|
| 291 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND) |
---|
| 292 | ((InteractiveModel*)this->getModel(0))->setAnimation(STAND); |
---|
| 293 | } |
---|
[9003] | 294 | |
---|
[9103] | 295 | |
---|
[9105] | 296 | velocity.y += this->jumpForce; |
---|
| 297 | if( this->jumpForce > 1.0f) |
---|
| 298 | this->jumpForce *= 0.9f; |
---|
[9103] | 299 | |
---|
[9105] | 300 | |
---|
[9003] | 301 | // physical falling of the player |
---|
| 302 | if( !this->isOnGround()) |
---|
| 303 | { |
---|
| 304 | this->fallVelocity += 300.0f * time; |
---|
| 305 | velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity; |
---|
| 306 | } |
---|
| 307 | else |
---|
| 308 | { |
---|
| 309 | this->fallVelocity = 0.0f; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | |
---|
[8685] | 313 | this->shiftCoor( velocity*time ); |
---|
| 314 | |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | |
---|
[9003] | 318 | |
---|
| 319 | |
---|
| 320 | |
---|
[8693] | 321 | if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL)) |
---|
| 322 | { |
---|
| 323 | ((InteractiveModel*)this->getModel(0))->tick(time); |
---|
[9103] | 324 | |
---|
| 325 | // handle animations differently |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | |
---|
[9104] | 330 | |
---|
[8685] | 331 | // else if( this->bFire && likely(this->getModel(0) != NULL)) |
---|
| 332 | // { |
---|
| 333 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK) |
---|
| 334 | // ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK); |
---|
| 335 | // } |
---|
| 336 | // else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL)) |
---|
| 337 | // { |
---|
| 338 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN) |
---|
| 339 | // ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); |
---|
| 340 | // } |
---|
[8687] | 341 | // else if (likely(this->getModel(0) != NULL)) |
---|
| 342 | // { |
---|
| 343 | // if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND) |
---|
| 344 | // ((InteractiveModel*)this->getModel(0))->setAnimation(STAND); |
---|
| 345 | // } |
---|
[8693] | 346 | } |
---|
[8685] | 347 | |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | |
---|
[8686] | 352 | /** |
---|
| 353 | * draws the MD2Creature after transforming it. |
---|
| 354 | */ |
---|
| 355 | void FPSPlayer::draw () const |
---|
| 356 | { |
---|
| 357 | // only draw if this entity is not the player since the player nevers sees himself |
---|
[9103] | 358 | // if( this->getCurrentPlayer() == NULL) |
---|
[8686] | 359 | WorldEntity::draw(); |
---|
| 360 | } |
---|
[8685] | 361 | |
---|
| 362 | |
---|
| 363 | |
---|
| 364 | /** |
---|
[8686] | 365 | * process |
---|
[8685] | 366 | */ |
---|
| 367 | void FPSPlayer::process(const Event &event) |
---|
| 368 | { |
---|
| 369 | Playable::process(event); |
---|
| 370 | |
---|
| 371 | if( event.type == KeyMapper::PEV_LEFT) |
---|
| 372 | this->bLeft = event.bPressed; |
---|
| 373 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
| 374 | this->bRight = event.bPressed; |
---|
| 375 | else if( event.type == KeyMapper::PEV_FORWARD) |
---|
| 376 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
| 377 | else if( event.type == KeyMapper::PEV_BACKWARD) |
---|
| 378 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
| 379 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 380 | { |
---|
| 381 | this->xMouse += event.xRel; |
---|
| 382 | this->yMouse += event.yRel; |
---|
| 383 | } |
---|
[8705] | 384 | else if( event.type == KeyMapper::PEV_JUMP) |
---|
[9104] | 385 | this->bJump = event.bPressed; |
---|
[9003] | 386 | this->bPosBut = event.bPressed; |
---|
[8685] | 387 | } |
---|
| 388 | |
---|
| 389 | |
---|
| 390 | |
---|
| 391 | |
---|