| 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 ( !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 |  | 
|---|
| 179 | void Spectator::setPlayDirection(const Quaternion& quat, float speed) | 
|---|
| 180 | { | 
|---|
| 181 | this->mouseDir = quat; | 
|---|
| 182 | this->angleY = quat.getHeading(); | 
|---|
| 183 | this->angleX = quat.getAttitude(); | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|
| 187 | void Spectator::reset() | 
|---|
| 188 | { | 
|---|
| 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 ); | 
|---|
| 203 | this->attachCamera(); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | void Spectator::leave() | 
|---|
| 207 | { | 
|---|
| 208 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); | 
|---|
| 209 | this->detachCamera(); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 |  | 
|---|
| 213 | /** | 
|---|
| 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 | 
|---|
| 219 | */ | 
|---|
| 220 | void Spectator::collidesWith(WorldEntity* entity, const Vector& location) | 
|---|
| 221 | { | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 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) | 
|---|
| 231 | { | 
|---|
| 232 | //   Playable::tick( time ); | 
|---|
| 233 |  | 
|---|
| 234 | if( ( xMouse != 0 || yMouse != 0 ) && ( !State::isOnline() || this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) ) | 
|---|
| 235 | { | 
|---|
| 236 | xMouse *= time / 10; | 
|---|
| 237 | yMouse *= time / 10; | 
|---|
| 238 |  | 
|---|
| 239 | angleX -= xMouse; | 
|---|
| 240 | angleY -= yMouse; | 
|---|
| 241 |  | 
|---|
| 242 | //     if ( angleY > 1.90 ) | 
|---|
| 243 | //       angleY = 1.95; | 
|---|
| 244 | // | 
|---|
| 245 | //     if ( angleY < -1.07 ) | 
|---|
| 246 | //       angleY = -1.07; | 
|---|
| 247 |  | 
|---|
| 248 | xMouse = yMouse = 0; | 
|---|
| 249 |  | 
|---|
| 250 | this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) ); | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 |  | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | this->setAbsDir( this->mouseDir ); | 
|---|
| 257 |  | 
|---|
| 258 | Vector velocity; | 
|---|
| 259 |  | 
|---|
| 260 | if ( this->bForward ) | 
|---|
| 261 | { | 
|---|
| 262 | velocity += this->getAbsDirX(); | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | if ( this->bBackward ) | 
|---|
| 266 | { | 
|---|
| 267 | velocity -= this->getAbsDirX(); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | if ( this->bRight ) | 
|---|
| 271 | { | 
|---|
| 272 | velocity += this->getAbsDirZ(); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | if ( this->bLeft ) | 
|---|
| 276 | { | 
|---|
| 277 | velocity -= this->getAbsDirZ(); | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 | velocity *= 100; | 
|---|
| 281 |  | 
|---|
| 282 | this->shiftCoor( velocity*time ); | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | /** | 
|---|
| 286 | * @todo switch statement ?? | 
|---|
| 287 | */ | 
|---|
| 288 | void Spectator::process(const Event &event) | 
|---|
| 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 | } | 
|---|
| 305 | else if( event.type == KeyMapper::PEV_FIRE1 ) | 
|---|
| 306 | { | 
|---|
| 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 ); | 
|---|
| 308 | //     FPSPlayer * fps = new FPSPlayer(); | 
|---|
| 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" ); | 
|---|
| 319 |  | 
|---|
| 320 | //     ((Playable*)fps)->setPlayDirection(  0, 0, 1, 0 ); | 
|---|
| 321 | } | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 |  | 
|---|
| 325 |  | 
|---|
| 326 |  | 
|---|
| 327 |  | 
|---|