[8067] | 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: Christoph Renner |
---|
| 13 | co-programmer: ... |
---|
| 14 | |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #include "spectator.h" |
---|
| 18 | |
---|
| 19 | #include "src/lib/util/loading/factory.h" |
---|
| 20 | #include "key_mapper.h" |
---|
| 21 | |
---|
[8708] | 22 | #include "shared_network_data.h" |
---|
[8067] | 23 | |
---|
[10114] | 24 | #include "src/world_entities/creatures/fps_player.h" |
---|
| 25 | #include "src/world_entities/npcs/generic_npc.h" |
---|
| 26 | |
---|
[10618] | 27 | #include "src/lib/util/loading/load_param.h" |
---|
[10114] | 28 | |
---|
[10618] | 29 | #include "player.h" |
---|
| 30 | |
---|
| 31 | |
---|
[10114] | 32 | ObjectListDefinition(Spectator); |
---|
[9869] | 33 | CREATE_FACTORY(Spectator); |
---|
[8067] | 34 | |
---|
[10618] | 35 | Spectator* Spectator::ghost = NULL; |
---|
| 36 | Playable* Spectator::regularPlayable = NULL; |
---|
[8067] | 37 | |
---|
[10108] | 38 | #include "state.h" |
---|
[10618] | 39 | #include "shell_command.h" |
---|
| 40 | |
---|
| 41 | SHELL_COMMAND( enableGhost, Spectator, enableGhost ) |
---|
| 42 | ->describe("fly around") |
---|
| 43 | ->setAlias("ghost"); |
---|
[10108] | 44 | |
---|
| 45 | |
---|
[8067] | 46 | /** |
---|
[8228] | 47 | * destructs the Spectator, deletes alocated memory |
---|
[8067] | 48 | */ |
---|
[8228] | 49 | Spectator::~Spectator () |
---|
[8067] | 50 | { |
---|
[8228] | 51 | this->setPlayer(NULL); |
---|
[10682] | 52 | if ( this == ghost ) |
---|
| 53 | ghost = NULL; |
---|
[8228] | 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * creates a new Spectator from Xml Data |
---|
| 59 | * @param root the xml element containing Spectator data |
---|
| 60 | |
---|
| 61 | @todo add more parameters to load |
---|
| 62 | */ |
---|
| 63 | Spectator::Spectator(const TiXmlElement* root) |
---|
| 64 | { |
---|
| 65 | this->init(); |
---|
| 66 | if (root != NULL) |
---|
| 67 | this->loadParams(root); |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * initializes a Spectator |
---|
| 74 | */ |
---|
| 75 | void Spectator::init() |
---|
| 76 | { |
---|
| 77 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
[9869] | 78 | this->registerObject(this, Spectator::_objectList); |
---|
[8067] | 79 | |
---|
[8228] | 80 | this->getWeaponManager().changeWeaponConfig(1); |
---|
| 81 | |
---|
[8067] | 82 | this->bLeft = false; |
---|
| 83 | this->bRight = false; |
---|
| 84 | this->bForward = false; |
---|
| 85 | this->bBackward = false; |
---|
| 86 | this->xMouse = 0.0f; |
---|
| 87 | this->yMouse = 0.0f; |
---|
[8228] | 88 | |
---|
| 89 | this->setHealthMax(100); |
---|
| 90 | this->setHealth(80); |
---|
| 91 | |
---|
| 92 | this->mouseDir = this->getAbsDir(); |
---|
| 93 | |
---|
| 94 | //add events to the eventlist |
---|
[8067] | 95 | registerEvent(KeyMapper::PEV_FORWARD); |
---|
| 96 | registerEvent(KeyMapper::PEV_BACKWARD); |
---|
| 97 | registerEvent(KeyMapper::PEV_LEFT); |
---|
| 98 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
| 99 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
[10114] | 100 | registerEvent(KeyMapper::PEV_JUMP); |
---|
[8067] | 101 | registerEvent(EV_MOUSE_MOTION); |
---|
[8228] | 102 | |
---|
| 103 | this->getWeaponManager().setSlotCount(0); |
---|
| 104 | |
---|
| 105 | this->getWeaponManager().getFixedTarget()->setParent(this); |
---|
| 106 | this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0); |
---|
| 107 | |
---|
| 108 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
| 109 | |
---|
[9869] | 110 | |
---|
[8067] | 111 | registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); |
---|
| 112 | registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); |
---|
| 113 | registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) ); |
---|
| 114 | registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) ); |
---|
| 115 | registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) ); |
---|
[10401] | 116 | |
---|
| 117 | this->setPlayDirection(Quaternion(0,Vector(0,1,0)), 0.); |
---|
[8067] | 118 | } |
---|
| 119 | |
---|
[8228] | 120 | |
---|
[8067] | 121 | /** |
---|
[8228] | 122 | * loads the Settings of a Spectator from an XML-element. |
---|
[10618] | 123 | * @param root the XML-element to load the Spectator's properties from |
---|
[8067] | 124 | */ |
---|
[8228] | 125 | void Spectator::loadParams(const TiXmlElement* root) |
---|
[8067] | 126 | { |
---|
[8228] | 127 | Playable::loadParams(root); |
---|
[10618] | 128 | |
---|
| 129 | LoadParam(root, "allowGhost", this, Spectator, allowGhost) |
---|
| 130 | .describe("Allows the Player to fly around"); |
---|
[8067] | 131 | } |
---|
| 132 | |
---|
[10618] | 133 | |
---|
| 134 | |
---|
| 135 | void Spectator::allowGhost( bool flag ) |
---|
| 136 | { |
---|
| 137 | PRINTF(0)( "SPECTATOR ALLOWGHOST: %d\n", flag ); |
---|
| 138 | if ( flag ) |
---|
| 139 | { |
---|
| 140 | assert( ghost == NULL && "only one flySpectator allowed" ); |
---|
| 141 | |
---|
| 142 | ghost = this; |
---|
| 143 | } |
---|
| 144 | else |
---|
| 145 | { |
---|
| 146 | ghost = NULL; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | void Spectator::enableGhost( ) |
---|
| 152 | { |
---|
| 153 | if ( !ghost ) |
---|
| 154 | { |
---|
| 155 | Spectator* spec = new Spectator(); |
---|
| 156 | spec->allowGhost( true ); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | if ( !regularPlayable ) |
---|
| 160 | { |
---|
| 161 | if ( !State::getPlayer() || !State::getPlayer()->getPlayable() ) |
---|
| 162 | return; |
---|
| 163 | |
---|
| 164 | regularPlayable = State::getPlayer()->getPlayable(); |
---|
| 165 | |
---|
| 166 | ghost->setAbsCoor( regularPlayable->getAbsCoor() ); |
---|
| 167 | ghost->setAbsDir( regularPlayable->getAbsDir() ); |
---|
| 168 | |
---|
| 169 | State::getPlayer()->setPlayable( ghost ); |
---|
| 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
| 173 | if ( !State::getPlayer() || !State::getPlayer()->getPlayable() ) |
---|
| 174 | return; |
---|
| 175 | |
---|
| 176 | State::getPlayer()->setPlayable( regularPlayable ); |
---|
| 177 | regularPlayable = NULL; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
[8228] | 181 | void Spectator::setPlayDirection(const Quaternion& quat, float speed) |
---|
[8067] | 182 | { |
---|
[8228] | 183 | this->mouseDir = quat; |
---|
| 184 | this->angleY = quat.getHeading(); |
---|
| 185 | this->angleX = quat.getAttitude(); |
---|
[8067] | 186 | } |
---|
| 187 | |
---|
[8228] | 188 | |
---|
| 189 | void Spectator::reset() |
---|
[8067] | 190 | { |
---|
[8228] | 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 Spectator::enter() |
---|
| 203 | { |
---|
| 204 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false ); |
---|
[8067] | 205 | this->attachCamera(); |
---|
| 206 | } |
---|
| 207 | |
---|
[8228] | 208 | void Spectator::leave() |
---|
[8067] | 209 | { |
---|
[8228] | 210 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
[8067] | 211 | this->detachCamera(); |
---|
| 212 | } |
---|
| 213 | |
---|
[8228] | 214 | |
---|
[8067] | 215 | /** |
---|
[8228] | 216 | * this function is called, when two entities collide |
---|
| 217 | * @param entity: the world entity with whom it collides |
---|
| 218 | * |
---|
| 219 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 220 | * @todo dont let Spectator fly through walls |
---|
[8067] | 221 | */ |
---|
[8228] | 222 | void Spectator::collidesWith(WorldEntity* entity, const Vector& location) |
---|
[8067] | 223 | { |
---|
| 224 | } |
---|
| 225 | |
---|
[8228] | 226 | |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * the function called for each passing timeSnap |
---|
| 230 | * @param time The timespan passed since last update |
---|
| 231 | */ |
---|
| 232 | void Spectator::tick (float time) |
---|
[8067] | 233 | { |
---|
[10401] | 234 | // Playable::tick( time ); |
---|
[9869] | 235 | |
---|
[10108] | 236 | if( ( xMouse != 0 || yMouse != 0 ) && ( !State::isOnline() || this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) ) |
---|
[8067] | 237 | { |
---|
| 238 | xMouse *= time / 10; |
---|
| 239 | yMouse *= time / 10; |
---|
[9869] | 240 | |
---|
[8147] | 241 | angleX -= xMouse; |
---|
| 242 | angleY -= yMouse; |
---|
[9869] | 243 | |
---|
[10401] | 244 | // if ( angleY > 1.90 ) |
---|
| 245 | // angleY = 1.95; |
---|
| 246 | // |
---|
| 247 | // if ( angleY < -1.07 ) |
---|
| 248 | // angleY = -1.07; |
---|
| 249 | |
---|
| 250 | xMouse = yMouse = 0; |
---|
[9869] | 251 | |
---|
[8147] | 252 | this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) ); |
---|
[9869] | 253 | |
---|
[10401] | 254 | |
---|
| 255 | |
---|
[8067] | 256 | } |
---|
[9869] | 257 | |
---|
[8067] | 258 | this->setAbsDir( this->mouseDir ); |
---|
[9869] | 259 | |
---|
[8147] | 260 | Vector velocity; |
---|
[9869] | 261 | |
---|
[8147] | 262 | if ( this->bForward ) |
---|
| 263 | { |
---|
| 264 | velocity += this->getAbsDirX(); |
---|
| 265 | } |
---|
[9869] | 266 | |
---|
[8147] | 267 | if ( this->bBackward ) |
---|
| 268 | { |
---|
| 269 | velocity -= this->getAbsDirX(); |
---|
| 270 | } |
---|
[9869] | 271 | |
---|
[8147] | 272 | if ( this->bRight ) |
---|
| 273 | { |
---|
| 274 | velocity += this->getAbsDirZ(); |
---|
| 275 | } |
---|
[9869] | 276 | |
---|
[8147] | 277 | if ( this->bLeft ) |
---|
| 278 | { |
---|
| 279 | velocity -= this->getAbsDirZ(); |
---|
| 280 | } |
---|
[9869] | 281 | |
---|
[8147] | 282 | velocity *= 100; |
---|
[9869] | 283 | |
---|
[8147] | 284 | this->shiftCoor( velocity*time ); |
---|
[8067] | 285 | } |
---|
| 286 | |
---|
[8228] | 287 | /** |
---|
| 288 | * @todo switch statement ?? |
---|
| 289 | */ |
---|
| 290 | void Spectator::process(const Event &event) |
---|
[8067] | 291 | { |
---|
| 292 | Playable::process(event); |
---|
| 293 | |
---|
| 294 | if( event.type == KeyMapper::PEV_LEFT) |
---|
| 295 | this->bLeft = event.bPressed; |
---|
| 296 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
| 297 | this->bRight = event.bPressed; |
---|
| 298 | else if( event.type == KeyMapper::PEV_FORWARD) |
---|
| 299 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
| 300 | else if( event.type == KeyMapper::PEV_BACKWARD) |
---|
| 301 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
| 302 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 303 | { |
---|
| 304 | this->xMouse += event.xRel; |
---|
| 305 | this->yMouse += event.yRel; |
---|
| 306 | } |
---|
[10618] | 307 | else if( event.type == KeyMapper::PEV_FIRE1 ) |
---|
[10114] | 308 | { |
---|
[10618] | 309 | PRINTF(0)( "CURRENT POS: (%f, %f, %f) ROT (%f, (%f, %f, %f))\n", this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ(), this->getAbsDir().w, this->getAbsDir().v.x, this->getAbsDir().v.y, this->getAbsDir().v.z ); |
---|
[10114] | 310 | // FPSPlayer * fps = new FPSPlayer(); |
---|
[10401] | 311 | // //GenericNPC* fps = new GenericNPC(); |
---|
| 312 | // WorldEntity* fps = new WorldEntity(); |
---|
| 313 | // //WorldEntity * fps = new WorldEntity(); |
---|
| 314 | // |
---|
| 315 | // fps->setAbsCoor( this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() ); |
---|
| 316 | // fps->setAbsDir( this->getAbsDir() ); |
---|
| 317 | // fps->loadMD2Texture( "doom_guy.png" ); |
---|
| 318 | // fps->loadModel( "models/creatures/doom_guy.md2", 10.0f ); |
---|
| 319 | // fps->toList( OM_GROUP_00); |
---|
| 320 | // //fps->loadModel( "models/ships/terran_cruizer.obj" ); |
---|
[10114] | 321 | |
---|
[10401] | 322 | // ((Playable*)fps)->setPlayDirection( 0, 0, 1, 0 ); |
---|
[10114] | 323 | } |
---|
[8067] | 324 | } |
---|
[8228] | 325 | |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | |
---|
[10618] | 329 | |
---|