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