| 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 | #include "util/track/action_box.h" |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | ObjectListDefinition(Spectator); |
|---|
| 35 | CREATE_FACTORY(Spectator); |
|---|
| 36 | |
|---|
| 37 | Spectator* Spectator::ghost = NULL; |
|---|
| 38 | Playable* Spectator::regularPlayable = NULL; |
|---|
| 39 | |
|---|
| 40 | #include "state.h" |
|---|
| 41 | #include "shell_command.h" |
|---|
| 42 | |
|---|
| 43 | SHELL_COMMAND( enableGhost, Spectator, enableGhost ) |
|---|
| 44 | ->describe("fly around") |
|---|
| 45 | ->setAlias("ghost"); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * destructs the Spectator, deletes alocated memory |
|---|
| 50 | */ |
|---|
| 51 | Spectator::~Spectator () |
|---|
| 52 | { |
|---|
| 53 | this->setPlayer(NULL); |
|---|
| 54 | if ( this == ghost ) |
|---|
| 55 | ghost = NULL; |
|---|
| 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))); |
|---|
| 80 | this->registerObject(this, Spectator::_objectList); |
|---|
| 81 | |
|---|
| 82 | this->getWeaponManager().changeWeaponConfig(1); |
|---|
| 83 | |
|---|
| 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; |
|---|
| 90 | |
|---|
| 91 | this->setHealthMax(100); |
|---|
| 92 | this->setHealth(80); |
|---|
| 93 | |
|---|
| 94 | this->mouseDir = this->getAbsDir(); |
|---|
| 95 | |
|---|
| 96 | //add events to the eventlist |
|---|
| 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); |
|---|
| 102 | registerEvent(KeyMapper::PEV_JUMP); |
|---|
| 103 | registerEvent(EV_MOUSE_MOTION); |
|---|
| 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 | |
|---|
| 112 | |
|---|
| 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 ) ); |
|---|
| 118 | |
|---|
| 119 | this->setPlayDirection(Quaternion(0,Vector(0,1,0)), 0.); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * loads the Settings of a Spectator from an XML-element. |
|---|
| 125 | * @param root the XML-element to load the Spectator's properties from |
|---|
| 126 | */ |
|---|
| 127 | void Spectator::loadParams(const TiXmlElement* root) |
|---|
| 128 | { |
|---|
| 129 | Playable::loadParams(root); |
|---|
| 130 | |
|---|
| 131 | LoadParam(root, "allowGhost", this, Spectator, allowGhost) |
|---|
| 132 | .describe("Allows the Player to fly around"); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 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 | |
|---|
| 183 | void Spectator::setPlayDirection(const Quaternion& quat, float speed) |
|---|
| 184 | { |
|---|
| 185 | this->mouseDir = quat; |
|---|
| 186 | this->angleY = quat.getHeading(); |
|---|
| 187 | this->angleX = quat.getAttitude(); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | void Spectator::reset() |
|---|
| 192 | { |
|---|
| 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 ); |
|---|
| 207 | this->attachCamera(); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | void Spectator::leave() |
|---|
| 211 | { |
|---|
| 212 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
|---|
| 213 | this->detachCamera(); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | /** |
|---|
| 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 |
|---|
| 223 | */ |
|---|
| 224 | void Spectator::collidesWith(WorldEntity* entity, const Vector& location) |
|---|
| 225 | { |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 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) |
|---|
| 235 | { |
|---|
| 236 | // Playable::tick( time ); |
|---|
| 237 | |
|---|
| 238 | if( ( xMouse != 0 || yMouse != 0 ) && ( !State::isOnline() || this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) ) |
|---|
| 239 | { |
|---|
| 240 | xMouse *= time / 10; |
|---|
| 241 | yMouse *= time / 10; |
|---|
| 242 | |
|---|
| 243 | angleX -= xMouse; |
|---|
| 244 | angleY -= yMouse; |
|---|
| 245 | |
|---|
| 246 | // if ( angleY > 1.90 ) |
|---|
| 247 | // angleY = 1.95; |
|---|
| 248 | // |
|---|
| 249 | // if ( angleY < -1.07 ) |
|---|
| 250 | // angleY = -1.07; |
|---|
| 251 | |
|---|
| 252 | xMouse = yMouse = 0; |
|---|
| 253 | |
|---|
| 254 | this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) ); |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | this->setAbsDir( this->mouseDir ); |
|---|
| 261 | |
|---|
| 262 | Vector velocity; |
|---|
| 263 | |
|---|
| 264 | if ( this->bForward ) |
|---|
| 265 | { |
|---|
| 266 | velocity += this->getAbsDirX(); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | if ( this->bBackward ) |
|---|
| 270 | { |
|---|
| 271 | velocity -= this->getAbsDirX(); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | if ( this->bRight ) |
|---|
| 275 | { |
|---|
| 276 | velocity += this->getAbsDirZ(); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | if ( this->bLeft ) |
|---|
| 280 | { |
|---|
| 281 | velocity -= this->getAbsDirZ(); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | velocity *= 100; |
|---|
| 285 | |
|---|
| 286 | this->shiftCoor( velocity*time ); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | /** |
|---|
| 290 | * @todo switch statement ?? |
|---|
| 291 | */ |
|---|
| 292 | void Spectator::process(const Event &event) |
|---|
| 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); |
|---|
| 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 | } |
|---|
| 315 | else if( event.type == EV_MOUSE_MOTION) |
|---|
| 316 | { |
|---|
| 317 | this->xMouse += event.xRel; |
|---|
| 318 | this->yMouse += event.yRel; |
|---|
| 319 | } |
|---|
| 320 | else if( event.type == KeyMapper::PEV_FIRE1 ) |
|---|
| 321 | { |
|---|
| 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 ); |
|---|
| 323 | // FPSPlayer * fps = new FPSPlayer(); |
|---|
| 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" ); |
|---|
| 334 | |
|---|
| 335 | // ((Playable*)fps)->setPlayDirection( 0, 0, 1, 0 ); |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|