| 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 "weapons/weapon_manager.h" |
|---|
| 25 | #include "weapons/test_gun.h" |
|---|
| 26 | #include "weapons/turret.h" |
|---|
| 27 | #include "weapons/cannon.h" |
|---|
| 28 | #include "weapons/fps_sniper_rifle.h" |
|---|
| 29 | |
|---|
| 30 | #include "key_mapper.h" |
|---|
| 31 | |
|---|
| 32 | #include "debug.h" |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER); |
|---|
| 38 | |
|---|
| 39 | #include "script_class.h" |
|---|
| 40 | CREATE_SCRIPTABLE_CLASS(FPSPlayer, CL_FPS_PLAYER, |
|---|
| 41 | addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor)) |
|---|
| 42 | ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX)) |
|---|
| 43 | ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY)) |
|---|
| 44 | ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ)) |
|---|
| 45 | ); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * destructs the FPSPlayer, deletes alocated memory |
|---|
| 50 | */ |
|---|
| 51 | FPSPlayer::~FPSPlayer () |
|---|
| 52 | { |
|---|
| 53 | this->setPlayer(NULL); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * creates a new FPSPlayer from Xml Data |
|---|
| 59 | * @param root the xml element containing FPSPlayer data |
|---|
| 60 | * |
|---|
| 61 | */ |
|---|
| 62 | FPSPlayer::FPSPlayer(const TiXmlElement* root) |
|---|
| 63 | { |
|---|
| 64 | this->init(); |
|---|
| 65 | |
|---|
| 66 | if (root != NULL) |
|---|
| 67 | this->loadParams(root); |
|---|
| 68 | |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * initializes a FPSPlayer |
|---|
| 74 | */ |
|---|
| 75 | void FPSPlayer::init() |
|---|
| 76 | { |
|---|
| 77 | this->setClassID(CL_FPS_PLAYER, "FPSPlayer"); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | this->bLeft = false; |
|---|
| 81 | this->bRight = false; |
|---|
| 82 | this->bForward = false; |
|---|
| 83 | this->bBackward = false; |
|---|
| 84 | this->bJump = false; |
|---|
| 85 | |
|---|
| 86 | this->xMouse = 0.0f; |
|---|
| 87 | this->yMouse = 0.0f; |
|---|
| 88 | |
|---|
| 89 | this->setHealthMax(100); |
|---|
| 90 | this->setHealth(80); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | this->cameraNode.setParent(this); |
|---|
| 94 | |
|---|
| 95 | this->attitude = this->getAbsDir().getAttitude(); |
|---|
| 96 | this->heading = this->getAbsDir().getHeading(); |
|---|
| 97 | |
|---|
| 98 | //add events to the eventlist |
|---|
| 99 | registerEvent(KeyMapper::PEV_FORWARD); |
|---|
| 100 | registerEvent(KeyMapper::PEV_BACKWARD); |
|---|
| 101 | registerEvent(KeyMapper::PEV_LEFT); |
|---|
| 102 | registerEvent(KeyMapper::PEV_RIGHT); |
|---|
| 103 | registerEvent(KeyMapper::PEV_FIRE1); |
|---|
| 104 | registerEvent(KeyMapper::PEV_JUMP); |
|---|
| 105 | registerEvent(EV_MOUSE_MOTION); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | // weapon manager for the fps |
|---|
| 110 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
|---|
| 111 | |
|---|
| 112 | Weapon* wpRight = new FPSSniperRifle(0); |
|---|
| 113 | wpRight->setName("testGun Right"); |
|---|
| 114 | /* Weapon* wpLeft = new TestGun(1);*/ |
|---|
| 115 | // Weapon* wpLeft = new Turret(); |
|---|
| 116 | // wpLeft->setName("testGun Left"); |
|---|
| 117 | |
|---|
| 118 | // this->addWeapon(wpLeft, 1, 0); |
|---|
| 119 | this->addWeapon(wpRight,1, 0); |
|---|
| 120 | this->getWeaponManager().changeWeaponConfig(1); |
|---|
| 121 | |
|---|
| 122 | this->getWeaponManager().setSlotCount(2); |
|---|
| 123 | this->getWeaponManager().setSlotPosition(0, Vector(0.0, 5.0, 0.0)); |
|---|
| 124 | this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(0,1,0))); |
|---|
| 125 | this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
|---|
| 126 | this->getWeaponManager().setSlotPosition(1, Vector(-0.5, .2, 1.9)); |
|---|
| 127 | this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
|---|
| 128 | |
|---|
| 129 | this->getWeaponManager().setParentNode(&this->cameraNode); |
|---|
| 130 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
|---|
| 131 | |
|---|
| 132 | this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode); |
|---|
| 133 | this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | // network registration |
|---|
| 137 | registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); |
|---|
| 138 | registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); |
|---|
| 139 | registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) ); |
|---|
| 140 | registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) ); |
|---|
| 141 | // registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) ); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | // collision reaction registration |
|---|
| 145 | this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * loads the Settings of a FPSPlayer from an XML-element. |
|---|
| 151 | * @param root the XML-element to load the Spaceship's properties from |
|---|
| 152 | */ |
|---|
| 153 | void FPSPlayer::loadParams(const TiXmlElement* root) |
|---|
| 154 | { |
|---|
| 155 | Playable::loadParams(root); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed) |
|---|
| 159 | { |
|---|
| 160 | this->attitude = this->getAbsDir().getAttitude(); |
|---|
| 161 | this->heading = this->getAbsDir().getHeading(); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | void FPSPlayer::reset() |
|---|
| 166 | { |
|---|
| 167 | this->bLeft = false; |
|---|
| 168 | this->bRight = false; |
|---|
| 169 | this->bForward = false; |
|---|
| 170 | this->bBackward = false; |
|---|
| 171 | this->xMouse = 0.0f; |
|---|
| 172 | this->yMouse = 0.0f; |
|---|
| 173 | |
|---|
| 174 | this->setHealth(80); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | void FPSPlayer::enter() |
|---|
| 179 | { |
|---|
| 180 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true ); |
|---|
| 181 | |
|---|
| 182 | State::getCameraNode()->setParentSoft(&this->cameraNode); |
|---|
| 183 | State::getCameraTargetNode()->setParentSoft(&this->cameraNode); |
|---|
| 184 | |
|---|
| 185 | this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode()); |
|---|
| 186 | this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | State::getCameraNode()->setRelCoor(0,0,0); |
|---|
| 190 | State::getCameraTargetNode()->setRelCoor(10,0,0); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | void FPSPlayer::leave() |
|---|
| 194 | { |
|---|
| 195 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
|---|
| 196 | this->detachCamera(); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | /** |
|---|
| 202 | * the function called for each passing timeSnap |
|---|
| 203 | * @param time The timespan passed since last update |
|---|
| 204 | */ |
|---|
| 205 | void FPSPlayer::tick (float time) |
|---|
| 206 | { |
|---|
| 207 | Playable::tick( time ); |
|---|
| 208 | |
|---|
| 209 | if( ( xMouse != 0 || yMouse != 0 ) /*&& this->getOwner() == this->getHostID() */) |
|---|
| 210 | { |
|---|
| 211 | xMouse *= time ; |
|---|
| 212 | yMouse *= time ; |
|---|
| 213 | |
|---|
| 214 | heading -= xMouse; |
|---|
| 215 | attitude-= yMouse; |
|---|
| 216 | |
|---|
| 217 | if ( attitude > 2.05 ) |
|---|
| 218 | attitude = 2.05; |
|---|
| 219 | |
|---|
| 220 | else if ( attitude < -1.15 ) |
|---|
| 221 | attitude = -1.15; |
|---|
| 222 | |
|---|
| 223 | this->setAbsDir(Quaternion(heading, Vector(0,1,0))); |
|---|
| 224 | this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) )); |
|---|
| 225 | |
|---|
| 226 | xMouse = yMouse = 0; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | // this->setAbsDir( this->mouseDir ); |
|---|
| 230 | |
|---|
| 231 | Vector velocity; |
|---|
| 232 | |
|---|
| 233 | if ( this->bForward ) |
|---|
| 234 | { |
|---|
| 235 | velocity += this->getAbsDirX(); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | if ( this->bBackward ) |
|---|
| 239 | { |
|---|
| 240 | velocity -= this->getAbsDirX(); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | if ( this->bRight ) |
|---|
| 244 | { |
|---|
| 245 | velocity += this->getAbsDirZ(); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | if ( this->bLeft ) |
|---|
| 249 | { |
|---|
| 250 | velocity -= this->getAbsDirZ(); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | velocity *= 100; |
|---|
| 254 | |
|---|
| 255 | this->shiftCoor( velocity*time ); |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL)) |
|---|
| 261 | { |
|---|
| 262 | ((InteractiveModel*)this->getModel(0))->tick(time); |
|---|
| 263 | // |
|---|
| 264 | // // handle animations differently |
|---|
| 265 | // if( this->bJump && likely(this->getModel(0) != NULL)) |
|---|
| 266 | // { |
|---|
| 267 | // ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP); |
|---|
| 268 | // } |
|---|
| 269 | // else if( this->bFire && likely(this->getModel(0) != NULL)) |
|---|
| 270 | // { |
|---|
| 271 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK) |
|---|
| 272 | // ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK); |
|---|
| 273 | // } |
|---|
| 274 | // else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL)) |
|---|
| 275 | // { |
|---|
| 276 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN) |
|---|
| 277 | // ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); |
|---|
| 278 | // } |
|---|
| 279 | // else if (likely(this->getModel(0) != NULL)) |
|---|
| 280 | // { |
|---|
| 281 | // if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND) |
|---|
| 282 | // ((InteractiveModel*)this->getModel(0))->setAnimation(STAND); |
|---|
| 283 | // } |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | /** |
|---|
| 291 | * draws the MD2Creature after transforming it. |
|---|
| 292 | */ |
|---|
| 293 | void FPSPlayer::draw () const |
|---|
| 294 | { |
|---|
| 295 | // only draw if this entity is not the player since the player nevers sees himself |
|---|
| 296 | if( this->getCurrentPlayer() == NULL) |
|---|
| 297 | WorldEntity::draw(); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | /** |
|---|
| 303 | * process |
|---|
| 304 | */ |
|---|
| 305 | void FPSPlayer::process(const Event &event) |
|---|
| 306 | { |
|---|
| 307 | Playable::process(event); |
|---|
| 308 | |
|---|
| 309 | if( event.type == KeyMapper::PEV_LEFT) |
|---|
| 310 | this->bLeft = event.bPressed; |
|---|
| 311 | else if( event.type == KeyMapper::PEV_RIGHT) |
|---|
| 312 | this->bRight = event.bPressed; |
|---|
| 313 | else if( event.type == KeyMapper::PEV_FORWARD) |
|---|
| 314 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
|---|
| 315 | else if( event.type == KeyMapper::PEV_BACKWARD) |
|---|
| 316 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
|---|
| 317 | else if( event.type == EV_MOUSE_MOTION) |
|---|
| 318 | { |
|---|
| 319 | this->xMouse += event.xRel; |
|---|
| 320 | this->yMouse += event.yRel; |
|---|
| 321 | } |
|---|
| 322 | else if( event.type == KeyMapper::PEV_JUMP) |
|---|
| 323 | this->getAbsCoor().debug(); |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|