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