| 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 |  | 
|---|
| 22 | #include "shared_network_data.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include "src/world_entities/creatures/fps_player.h" | 
|---|
| 25 | #include "src/world_entities/npcs/generic_npc.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "src/lib/util/loading/load_param.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include "player.h" | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | ObjectListDefinition(Spectator); | 
|---|
| 33 | CREATE_FACTORY(Spectator); | 
|---|
| 34 |  | 
|---|
| 35 | Spectator* Spectator::ghost = NULL; | 
|---|
| 36 | Playable* Spectator::regularPlayable = NULL; | 
|---|
| 37 |  | 
|---|
| 38 | #include "state.h" | 
|---|
| 39 | #include "shell_command.h" | 
|---|
| 40 |               | 
|---|
| 41 | SHELL_COMMAND( enableGhost, Spectator, enableGhost ) | 
|---|
| 42 |              ->describe("fly around") | 
|---|
| 43 |              ->setAlias("ghost"); | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | /** | 
|---|
| 47 |  *  destructs the Spectator, deletes alocated memory | 
|---|
| 48 |  */ | 
|---|
| 49 | Spectator::~Spectator () | 
|---|
| 50 | { | 
|---|
| 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))); | 
|---|
| 76 |   this->registerObject(this, Spectator::_objectList); | 
|---|
| 77 |  | 
|---|
| 78 |   this->getWeaponManager().changeWeaponConfig(1); | 
|---|
| 79 |  | 
|---|
| 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; | 
|---|
| 86 |  | 
|---|
| 87 |   this->setHealthMax(100); | 
|---|
| 88 |   this->setHealth(80); | 
|---|
| 89 |  | 
|---|
| 90 |   this->mouseDir = this->getAbsDir(); | 
|---|
| 91 |  | 
|---|
| 92 |   //add events to the eventlist | 
|---|
| 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); | 
|---|
| 98 |   registerEvent(KeyMapper::PEV_JUMP); | 
|---|
| 99 |   registerEvent(EV_MOUSE_MOTION); | 
|---|
| 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 |  | 
|---|
| 108 |  | 
|---|
| 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 ) ); | 
|---|
| 114 |  | 
|---|
| 115 |   this->setPlayDirection(Quaternion(0,Vector(0,1,0)), 0.); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | /** | 
|---|
| 120 |  * loads the Settings of a Spectator from an XML-element. | 
|---|
| 121 |  * @param root the XML-element to load the Spectator's properties from | 
|---|
| 122 |  */ | 
|---|
| 123 | void Spectator::loadParams(const TiXmlElement* root) | 
|---|
| 124 | { | 
|---|
| 125 |   Playable::loadParams(root); | 
|---|
| 126 |    | 
|---|
| 127 |   LoadParam(root, "allowGhost", this, Spectator, allowGhost) | 
|---|
| 128 |     .describe("Allows the Player to fly around"); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 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 ( !regularPlayable ) | 
|---|
| 152 |   { | 
|---|
| 153 |     if ( !State::getPlayer() || !State::getPlayer()->getPlayable() ) | 
|---|
| 154 |       return; | 
|---|
| 155 |      | 
|---|
| 156 |     regularPlayable = State::getPlayer()->getPlayable(); | 
|---|
| 157 |      | 
|---|
| 158 |     ghost->setAbsCoor( regularPlayable->getAbsCoor() ); | 
|---|
| 159 |     ghost->setAbsDir( regularPlayable->getAbsDir() ); | 
|---|
| 160 |      | 
|---|
| 161 |     State::getPlayer()->setPlayable( ghost ); | 
|---|
| 162 |   } | 
|---|
| 163 |   else | 
|---|
| 164 |   { | 
|---|
| 165 |     if ( !State::getPlayer() || !State::getPlayer()->getPlayable() ) | 
|---|
| 166 |       return; | 
|---|
| 167 |      | 
|---|
| 168 |     State::getPlayer()->setPlayable( regularPlayable ); | 
|---|
| 169 |     regularPlayable = NULL; | 
|---|
| 170 |   } | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | void Spectator::setPlayDirection(const Quaternion& quat, float speed) | 
|---|
| 174 | { | 
|---|
| 175 |   this->mouseDir = quat; | 
|---|
| 176 |   this->angleY = quat.getHeading(); | 
|---|
| 177 |   this->angleX = quat.getAttitude(); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 |  | 
|---|
| 181 | void Spectator::reset() | 
|---|
| 182 | { | 
|---|
| 183 |   this->bLeft = false; | 
|---|
| 184 |   this->bRight = false; | 
|---|
| 185 |   this->bForward = false; | 
|---|
| 186 |   this->bBackward = false; | 
|---|
| 187 |   this->xMouse = 0.0f; | 
|---|
| 188 |   this->yMouse = 0.0f; | 
|---|
| 189 |  | 
|---|
| 190 |   this->setHealth(80); | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 |  | 
|---|
| 194 | void Spectator::enter() | 
|---|
| 195 | { | 
|---|
| 196 |   dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false ); | 
|---|
| 197 |   this->attachCamera(); | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | void Spectator::leave() | 
|---|
| 201 | { | 
|---|
| 202 |   dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); | 
|---|
| 203 |   this->detachCamera(); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 |  | 
|---|
| 207 | /** | 
|---|
| 208 |  *  this function is called, when two entities collide | 
|---|
| 209 |  * @param entity: the world entity with whom it collides | 
|---|
| 210 |  * | 
|---|
| 211 |  * Implement behaviour like damage application or other miscellaneous collision stuff in this function | 
|---|
| 212 |  * @todo dont let Spectator fly through walls | 
|---|
| 213 |  */ | 
|---|
| 214 | void Spectator::collidesWith(WorldEntity* entity, const Vector& location) | 
|---|
| 215 | { | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 |  | 
|---|
| 220 | /** | 
|---|
| 221 |  *  the function called for each passing timeSnap | 
|---|
| 222 |  * @param time The timespan passed since last update | 
|---|
| 223 |  */ | 
|---|
| 224 | void Spectator::tick (float time) | 
|---|
| 225 | { | 
|---|
| 226 | //   Playable::tick( time ); | 
|---|
| 227 |  | 
|---|
| 228 |   if( ( xMouse != 0 || yMouse != 0 ) && ( !State::isOnline() || this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) ) | 
|---|
| 229 |   { | 
|---|
| 230 |     xMouse *= time / 10; | 
|---|
| 231 |     yMouse *= time / 10; | 
|---|
| 232 |  | 
|---|
| 233 |     angleX -= xMouse; | 
|---|
| 234 |     angleY -= yMouse; | 
|---|
| 235 |  | 
|---|
| 236 | //     if ( angleY > 1.90 ) | 
|---|
| 237 | //       angleY = 1.95; | 
|---|
| 238 | //  | 
|---|
| 239 | //     if ( angleY < -1.07 ) | 
|---|
| 240 | //       angleY = -1.07; | 
|---|
| 241 |      | 
|---|
| 242 |     xMouse = yMouse = 0; | 
|---|
| 243 |  | 
|---|
| 244 |     this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) ); | 
|---|
| 245 |  | 
|---|
| 246 |      | 
|---|
| 247 |      | 
|---|
| 248 |   } | 
|---|
| 249 |  | 
|---|
| 250 |   this->setAbsDir( this->mouseDir ); | 
|---|
| 251 |  | 
|---|
| 252 |   Vector velocity; | 
|---|
| 253 |  | 
|---|
| 254 |   if ( this->bForward ) | 
|---|
| 255 |   { | 
|---|
| 256 |     velocity += this->getAbsDirX(); | 
|---|
| 257 |   } | 
|---|
| 258 |  | 
|---|
| 259 |   if ( this->bBackward ) | 
|---|
| 260 |   { | 
|---|
| 261 |     velocity -= this->getAbsDirX(); | 
|---|
| 262 |   } | 
|---|
| 263 |  | 
|---|
| 264 |   if ( this->bRight ) | 
|---|
| 265 |   { | 
|---|
| 266 |     velocity += this->getAbsDirZ(); | 
|---|
| 267 |   } | 
|---|
| 268 |  | 
|---|
| 269 |   if ( this->bLeft ) | 
|---|
| 270 |   { | 
|---|
| 271 |     velocity -= this->getAbsDirZ(); | 
|---|
| 272 |   } | 
|---|
| 273 |  | 
|---|
| 274 |   velocity *= 100; | 
|---|
| 275 |  | 
|---|
| 276 |   this->shiftCoor( velocity*time ); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | /** | 
|---|
| 280 |  * @todo switch statement ?? | 
|---|
| 281 |  */ | 
|---|
| 282 | void Spectator::process(const Event &event) | 
|---|
| 283 | { | 
|---|
| 284 |   Playable::process(event); | 
|---|
| 285 |  | 
|---|
| 286 |   if( event.type == KeyMapper::PEV_LEFT) | 
|---|
| 287 |     this->bLeft = event.bPressed; | 
|---|
| 288 |   else if( event.type == KeyMapper::PEV_RIGHT) | 
|---|
| 289 |     this->bRight = event.bPressed; | 
|---|
| 290 |   else if( event.type == KeyMapper::PEV_FORWARD) | 
|---|
| 291 |     this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); | 
|---|
| 292 |   else if( event.type == KeyMapper::PEV_BACKWARD) | 
|---|
| 293 |     this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); | 
|---|
| 294 |   else if( event.type == EV_MOUSE_MOTION) | 
|---|
| 295 |   { | 
|---|
| 296 |     this->xMouse += event.xRel; | 
|---|
| 297 |     this->yMouse += event.yRel; | 
|---|
| 298 |   } | 
|---|
| 299 |   else if( event.type == KeyMapper::PEV_FIRE1 ) | 
|---|
| 300 |   { | 
|---|
| 301 |     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 ); | 
|---|
| 302 | //     FPSPlayer * fps = new FPSPlayer(); | 
|---|
| 303 | //     //GenericNPC* fps = new GenericNPC(); | 
|---|
| 304 | //     WorldEntity* fps = new WorldEntity(); | 
|---|
| 305 | //     //WorldEntity * fps = new WorldEntity(); | 
|---|
| 306 | //  | 
|---|
| 307 | //     fps->setAbsCoor( this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() ); | 
|---|
| 308 | //     fps->setAbsDir( this->getAbsDir() ); | 
|---|
| 309 | //     fps->loadMD2Texture( "doom_guy.png" ); | 
|---|
| 310 | //     fps->loadModel( "models/creatures/doom_guy.md2", 10.0f ); | 
|---|
| 311 | //     fps->toList( OM_GROUP_00); | 
|---|
| 312 | //     //fps->loadModel( "models/ships/terran_cruizer.obj" ); | 
|---|
| 313 |  | 
|---|
| 314 | //     ((Playable*)fps)->setPlayDirection(  0, 0, 1, 0 ); | 
|---|
| 315 |   } | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 |  | 
|---|
| 319 |  | 
|---|
| 320 |  | 
|---|
| 321 |  | 
|---|