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