| 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 | |
|---|
| 29 | #include "key_mapper.h" |
|---|
| 30 | |
|---|
| 31 | #include "debug.h" |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 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; |
|---|
| 76 | |
|---|
| 77 | this->xMouse = 0.0f; |
|---|
| 78 | this->yMouse = 0.0f; |
|---|
| 79 | |
|---|
| 80 | this->setHealthMax(100); |
|---|
| 81 | this->setHealth(80); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | this->cameraNode.setParent(this); |
|---|
| 85 | |
|---|
| 86 | this->attitude = this->getAbsDir().getAttitude(); |
|---|
| 87 | this->heading = this->getAbsDir().getHeading(); |
|---|
| 88 | |
|---|
| 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); |
|---|
| 95 | registerEvent(KeyMapper::PEV_JUMP); |
|---|
| 96 | registerEvent(EV_MOUSE_MOTION); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | // weapon manager for the fps |
|---|
| 101 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
|---|
| 102 | |
|---|
| 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 | |
|---|
| 119 | this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode); |
|---|
| 120 | this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | // network registration |
|---|
| 124 | registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); |
|---|
| 125 | registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); |
|---|
| 126 | registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) ); |
|---|
| 127 | registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) ); |
|---|
| 128 | // registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) ); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | // collision reaction registration |
|---|
| 132 | this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * loads the Settings of a FPSPlayer from an XML-element. |
|---|
| 138 | * @param root the XML-element to load the Spaceship's properties from |
|---|
| 139 | */ |
|---|
| 140 | void FPSPlayer::loadParams(const TiXmlElement* root) |
|---|
| 141 | { |
|---|
| 142 | Playable::loadParams(root); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed) |
|---|
| 146 | { |
|---|
| 147 | this->attitude = this->getAbsDir().getAttitude(); |
|---|
| 148 | this->heading = this->getAbsDir().getHeading(); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | void FPSPlayer::reset() |
|---|
| 153 | { |
|---|
| 154 | this->bLeft = false; |
|---|
| 155 | this->bRight = false; |
|---|
| 156 | this->bForward = false; |
|---|
| 157 | this->bBackward = false; |
|---|
| 158 | this->xMouse = 0.0f; |
|---|
| 159 | this->yMouse = 0.0f; |
|---|
| 160 | |
|---|
| 161 | this->setHealth(80); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | void FPSPlayer::enter() |
|---|
| 166 | { |
|---|
| 167 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true ); |
|---|
| 168 | |
|---|
| 169 | State::getCameraNode()->setParentSoft(&this->cameraNode); |
|---|
| 170 | State::getCameraTargetNode()->setParentSoft(&this->cameraNode); |
|---|
| 171 | |
|---|
| 172 | this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode()); |
|---|
| 173 | this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0); |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | State::getCameraNode()->setRelCoor(0,0,0); |
|---|
| 177 | State::getCameraTargetNode()->setRelCoor(10,0,0); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | void FPSPlayer::leave() |
|---|
| 181 | { |
|---|
| 182 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
|---|
| 183 | this->detachCamera(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * the function called for each passing timeSnap |
|---|
| 190 | * @param time The timespan passed since last update |
|---|
| 191 | */ |
|---|
| 192 | void FPSPlayer::tick (float time) |
|---|
| 193 | { |
|---|
| 194 | Playable::tick( time ); |
|---|
| 195 | |
|---|
| 196 | if( ( xMouse != 0 || yMouse != 0 ) /*&& this->getOwner() == this->getHostID() */) |
|---|
| 197 | { |
|---|
| 198 | xMouse *= time ; |
|---|
| 199 | yMouse *= time ; |
|---|
| 200 | |
|---|
| 201 | heading -= xMouse; |
|---|
| 202 | attitude-= yMouse; |
|---|
| 203 | |
|---|
| 204 | if ( attitude > 2.05 ) |
|---|
| 205 | attitude = 2.05; |
|---|
| 206 | |
|---|
| 207 | else if ( attitude < -1.15 ) |
|---|
| 208 | attitude = -1.15; |
|---|
| 209 | |
|---|
| 210 | this->setAbsDir(Quaternion(heading, Vector(0,1,0))); |
|---|
| 211 | this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) )); |
|---|
| 212 | |
|---|
| 213 | xMouse = yMouse = 0; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | // this->setAbsDir( this->mouseDir ); |
|---|
| 217 | |
|---|
| 218 | Vector velocity; |
|---|
| 219 | |
|---|
| 220 | if ( this->bForward ) |
|---|
| 221 | { |
|---|
| 222 | velocity += this->getAbsDirX(); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | if ( this->bBackward ) |
|---|
| 226 | { |
|---|
| 227 | velocity -= this->getAbsDirX(); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | if ( this->bRight ) |
|---|
| 231 | { |
|---|
| 232 | velocity += this->getAbsDirZ(); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | if ( this->bLeft ) |
|---|
| 236 | { |
|---|
| 237 | velocity -= this->getAbsDirZ(); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | velocity *= 100; |
|---|
| 241 | |
|---|
| 242 | this->shiftCoor( velocity*time ); |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL)) |
|---|
| 248 | { |
|---|
| 249 | ((InteractiveModel*)this->getModel(0))->tick(time); |
|---|
| 250 | // |
|---|
| 251 | // // handle animations differently |
|---|
| 252 | // if( this->bJump && likely(this->getModel(0) != NULL)) |
|---|
| 253 | // { |
|---|
| 254 | // ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP); |
|---|
| 255 | // } |
|---|
| 256 | // else if( this->bFire && likely(this->getModel(0) != NULL)) |
|---|
| 257 | // { |
|---|
| 258 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK) |
|---|
| 259 | // ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK); |
|---|
| 260 | // } |
|---|
| 261 | // else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL)) |
|---|
| 262 | // { |
|---|
| 263 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN) |
|---|
| 264 | // ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); |
|---|
| 265 | // } |
|---|
| 266 | // else if (likely(this->getModel(0) != NULL)) |
|---|
| 267 | // { |
|---|
| 268 | // if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND) |
|---|
| 269 | // ((InteractiveModel*)this->getModel(0))->setAnimation(STAND); |
|---|
| 270 | // } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | /** |
|---|
| 278 | * draws the MD2Creature after transforming it. |
|---|
| 279 | */ |
|---|
| 280 | void FPSPlayer::draw () const |
|---|
| 281 | { |
|---|
| 282 | // only draw if this entity is not the player since the player nevers sees himself |
|---|
| 283 | if( this->getCurrentPlayer() == NULL) |
|---|
| 284 | WorldEntity::draw(); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | /** |
|---|
| 290 | * process |
|---|
| 291 | */ |
|---|
| 292 | void FPSPlayer::process(const Event &event) |
|---|
| 293 | { |
|---|
| 294 | Playable::process(event); |
|---|
| 295 | |
|---|
| 296 | if( event.type == KeyMapper::PEV_LEFT) |
|---|
| 297 | this->bLeft = event.bPressed; |
|---|
| 298 | else if( event.type == KeyMapper::PEV_RIGHT) |
|---|
| 299 | this->bRight = event.bPressed; |
|---|
| 300 | else if( event.type == KeyMapper::PEV_FORWARD) |
|---|
| 301 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
|---|
| 302 | else if( event.type == KeyMapper::PEV_BACKWARD) |
|---|
| 303 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
|---|
| 304 | else if( event.type == EV_MOUSE_MOTION) |
|---|
| 305 | { |
|---|
| 306 | this->xMouse += event.xRel; |
|---|
| 307 | this->yMouse += event.yRel; |
|---|
| 308 | } |
|---|
| 309 | else if( event.type == KeyMapper::PEV_JUMP) |
|---|
| 310 | this->getAbsCoor().debug(); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|