| [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 |  | 
|---|
 | 27 |  | 
|---|
 | 28 | ObjectListDefinition(Spectator); | 
|---|
| [9869] | 29 | CREATE_FACTORY(Spectator); | 
|---|
| [8067] | 30 |  | 
|---|
 | 31 |  | 
|---|
| [10108] | 32 |  | 
|---|
 | 33 | #include "state.h" | 
|---|
 | 34 |  | 
|---|
 | 35 |  | 
|---|
| [8067] | 36 | /** | 
|---|
| [8228] | 37 |  *  destructs the Spectator, deletes alocated memory | 
|---|
| [8067] | 38 |  */ | 
|---|
| [8228] | 39 | Spectator::~Spectator () | 
|---|
| [8067] | 40 | { | 
|---|
| [8228] | 41 |   this->setPlayer(NULL); | 
|---|
 | 42 | } | 
|---|
 | 43 |  | 
|---|
 | 44 |  | 
|---|
 | 45 | /** | 
|---|
 | 46 |  *  creates a new Spectator from Xml Data | 
|---|
 | 47 |  * @param root the xml element containing Spectator data | 
|---|
 | 48 |  | 
|---|
 | 49 |    @todo add more parameters to load | 
|---|
 | 50 |  */ | 
|---|
 | 51 | Spectator::Spectator(const TiXmlElement* root) | 
|---|
 | 52 | { | 
|---|
 | 53 |   this->init(); | 
|---|
 | 54 |   if (root != NULL) | 
|---|
 | 55 |     this->loadParams(root); | 
|---|
 | 56 |  | 
|---|
 | 57 | } | 
|---|
 | 58 |  | 
|---|
 | 59 |  | 
|---|
 | 60 | /** | 
|---|
 | 61 |  * initializes a Spectator | 
|---|
 | 62 |  */ | 
|---|
 | 63 | void Spectator::init() | 
|---|
 | 64 | { | 
|---|
 | 65 | //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); | 
|---|
| [9869] | 66 |   this->registerObject(this, Spectator::_objectList); | 
|---|
| [8067] | 67 |  | 
|---|
| [8228] | 68 |   this->getWeaponManager().changeWeaponConfig(1); | 
|---|
 | 69 |  | 
|---|
| [8067] | 70 |   this->bLeft = false; | 
|---|
 | 71 |   this->bRight = false; | 
|---|
 | 72 |   this->bForward = false; | 
|---|
 | 73 |   this->bBackward = false; | 
|---|
 | 74 |   this->xMouse = 0.0f; | 
|---|
 | 75 |   this->yMouse = 0.0f; | 
|---|
| [8228] | 76 |  | 
|---|
 | 77 |   this->setHealthMax(100); | 
|---|
 | 78 |   this->setHealth(80); | 
|---|
 | 79 |  | 
|---|
 | 80 |   this->mouseDir = this->getAbsDir(); | 
|---|
 | 81 |  | 
|---|
 | 82 |   //add events to the eventlist | 
|---|
| [8067] | 83 |   registerEvent(KeyMapper::PEV_FORWARD); | 
|---|
 | 84 |   registerEvent(KeyMapper::PEV_BACKWARD); | 
|---|
 | 85 |   registerEvent(KeyMapper::PEV_LEFT); | 
|---|
 | 86 |   registerEvent(KeyMapper::PEV_RIGHT); | 
|---|
 | 87 |   registerEvent(KeyMapper::PEV_FIRE1); | 
|---|
| [10114] | 88 |   registerEvent(KeyMapper::PEV_JUMP); | 
|---|
| [8067] | 89 |   registerEvent(EV_MOUSE_MOTION); | 
|---|
| [8228] | 90 |  | 
|---|
 | 91 |   this->getWeaponManager().setSlotCount(0); | 
|---|
 | 92 |  | 
|---|
 | 93 |   this->getWeaponManager().getFixedTarget()->setParent(this); | 
|---|
 | 94 |   this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0); | 
|---|
 | 95 |  | 
|---|
 | 96 |   dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); | 
|---|
 | 97 |  | 
|---|
| [9869] | 98 |  | 
|---|
| [8067] | 99 |   registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); | 
|---|
 | 100 |   registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); | 
|---|
 | 101 |   registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) ); | 
|---|
 | 102 |   registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) ); | 
|---|
 | 103 |   registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) ); | 
|---|
 | 104 | } | 
|---|
 | 105 |  | 
|---|
| [8228] | 106 |  | 
|---|
| [8067] | 107 | /** | 
|---|
| [8228] | 108 |  * loads the Settings of a Spectator from an XML-element. | 
|---|
 | 109 |  * @param root the XML-element to load the Spaceship's properties from | 
|---|
| [8067] | 110 |  */ | 
|---|
| [8228] | 111 | void Spectator::loadParams(const TiXmlElement* root) | 
|---|
| [8067] | 112 | { | 
|---|
| [8228] | 113 |   Playable::loadParams(root); | 
|---|
| [8067] | 114 | } | 
|---|
 | 115 |  | 
|---|
| [8228] | 116 | void Spectator::setPlayDirection(const Quaternion& quat, float speed) | 
|---|
| [8067] | 117 | { | 
|---|
| [8228] | 118 |   this->mouseDir = quat; | 
|---|
 | 119 |   this->angleY = quat.getHeading(); | 
|---|
 | 120 |   this->angleX = quat.getAttitude(); | 
|---|
| [8067] | 121 | } | 
|---|
 | 122 |  | 
|---|
| [8228] | 123 |  | 
|---|
 | 124 | void Spectator::reset() | 
|---|
| [8067] | 125 | { | 
|---|
| [8228] | 126 |   this->bLeft = false; | 
|---|
 | 127 |   this->bRight = false; | 
|---|
 | 128 |   this->bForward = false; | 
|---|
 | 129 |   this->bBackward = false; | 
|---|
 | 130 |   this->xMouse = 0.0f; | 
|---|
 | 131 |   this->yMouse = 0.0f; | 
|---|
 | 132 |  | 
|---|
 | 133 |   this->setHealth(80); | 
|---|
 | 134 | } | 
|---|
 | 135 |  | 
|---|
 | 136 |  | 
|---|
 | 137 | void Spectator::enter() | 
|---|
 | 138 | { | 
|---|
 | 139 |   dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false ); | 
|---|
| [8067] | 140 |   this->attachCamera(); | 
|---|
 | 141 | } | 
|---|
 | 142 |  | 
|---|
| [8228] | 143 | void Spectator::leave() | 
|---|
| [8067] | 144 | { | 
|---|
| [8228] | 145 |   dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); | 
|---|
| [8067] | 146 |   this->detachCamera(); | 
|---|
 | 147 | } | 
|---|
 | 148 |  | 
|---|
| [8228] | 149 |  | 
|---|
| [8067] | 150 | /** | 
|---|
| [8228] | 151 |  *  this function is called, when two entities collide | 
|---|
 | 152 |  * @param entity: the world entity with whom it collides | 
|---|
 | 153 |  * | 
|---|
 | 154 |  * Implement behaviour like damage application or other miscellaneous collision stuff in this function | 
|---|
 | 155 |  * @todo dont let Spectator fly through walls | 
|---|
| [8067] | 156 |  */ | 
|---|
| [8228] | 157 | void Spectator::collidesWith(WorldEntity* entity, const Vector& location) | 
|---|
| [8067] | 158 | { | 
|---|
 | 159 | } | 
|---|
 | 160 |  | 
|---|
| [8228] | 161 |  | 
|---|
 | 162 |  | 
|---|
 | 163 | /** | 
|---|
 | 164 |  *  the function called for each passing timeSnap | 
|---|
 | 165 |  * @param time The timespan passed since last update | 
|---|
 | 166 |  */ | 
|---|
 | 167 | void Spectator::tick (float time) | 
|---|
| [8067] | 168 | { | 
|---|
 | 169 |   Playable::tick( time ); | 
|---|
| [9869] | 170 |  | 
|---|
| [10108] | 171 |   if( ( xMouse != 0 || yMouse != 0 ) && ( !State::isOnline() || this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) ) | 
|---|
| [8067] | 172 |   { | 
|---|
 | 173 |     xMouse *= time / 10; | 
|---|
 | 174 |     yMouse *= time / 10; | 
|---|
| [9869] | 175 |  | 
|---|
| [8147] | 176 |     angleX -= xMouse; | 
|---|
 | 177 |     angleY -= yMouse; | 
|---|
| [9869] | 178 |  | 
|---|
| [8147] | 179 |     if ( angleY > 2.05 ) | 
|---|
 | 180 |       angleY = 2.05; | 
|---|
| [9869] | 181 |  | 
|---|
| [8147] | 182 |     if ( angleY < -1.15 ) | 
|---|
 | 183 |       angleY = -1.15; | 
|---|
| [9869] | 184 |  | 
|---|
| [8147] | 185 |     this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) ); | 
|---|
| [9869] | 186 |  | 
|---|
| [8067] | 187 |     xMouse = yMouse = 0; | 
|---|
 | 188 |   } | 
|---|
| [9869] | 189 |  | 
|---|
| [8067] | 190 |   this->setAbsDir( this->mouseDir ); | 
|---|
| [9869] | 191 |  | 
|---|
| [8147] | 192 |   Vector velocity; | 
|---|
| [9869] | 193 |  | 
|---|
| [8147] | 194 |   if ( this->bForward ) | 
|---|
 | 195 |   { | 
|---|
 | 196 |     velocity += this->getAbsDirX(); | 
|---|
 | 197 |   } | 
|---|
| [9869] | 198 |  | 
|---|
| [8147] | 199 |   if ( this->bBackward ) | 
|---|
 | 200 |   { | 
|---|
 | 201 |     velocity -= this->getAbsDirX(); | 
|---|
 | 202 |   } | 
|---|
| [9869] | 203 |  | 
|---|
| [8147] | 204 |   if ( this->bRight ) | 
|---|
 | 205 |   { | 
|---|
 | 206 |     velocity += this->getAbsDirZ(); | 
|---|
 | 207 |   } | 
|---|
| [9869] | 208 |  | 
|---|
| [8147] | 209 |   if ( this->bLeft ) | 
|---|
 | 210 |   { | 
|---|
 | 211 |     velocity -= this->getAbsDirZ(); | 
|---|
 | 212 |   } | 
|---|
| [9869] | 213 |  | 
|---|
| [8147] | 214 |   velocity *= 100; | 
|---|
| [9869] | 215 |  | 
|---|
| [8147] | 216 |   this->shiftCoor( velocity*time ); | 
|---|
| [8067] | 217 | } | 
|---|
 | 218 |  | 
|---|
| [8228] | 219 | /** | 
|---|
 | 220 |  * @todo switch statement ?? | 
|---|
 | 221 |  */ | 
|---|
 | 222 | void Spectator::process(const Event &event) | 
|---|
| [8067] | 223 | { | 
|---|
 | 224 |   Playable::process(event); | 
|---|
 | 225 |  | 
|---|
 | 226 |   if( event.type == KeyMapper::PEV_LEFT) | 
|---|
 | 227 |     this->bLeft = event.bPressed; | 
|---|
 | 228 |   else if( event.type == KeyMapper::PEV_RIGHT) | 
|---|
 | 229 |     this->bRight = event.bPressed; | 
|---|
 | 230 |   else if( event.type == KeyMapper::PEV_FORWARD) | 
|---|
 | 231 |     this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); | 
|---|
 | 232 |   else if( event.type == KeyMapper::PEV_BACKWARD) | 
|---|
 | 233 |     this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); | 
|---|
 | 234 |   else if( event.type == EV_MOUSE_MOTION) | 
|---|
 | 235 |   { | 
|---|
 | 236 |     this->xMouse += event.xRel; | 
|---|
 | 237 |     this->yMouse += event.yRel; | 
|---|
 | 238 |   } | 
|---|
| [10114] | 239 |   else if( event.type == KeyMapper::PEV_JUMP) | 
|---|
 | 240 |   { | 
|---|
 | 241 | //     FPSPlayer * fps = new FPSPlayer(); | 
|---|
 | 242 |     //GenericNPC* fps = new GenericNPC(); | 
|---|
 | 243 |     WorldEntity* fps = new WorldEntity(); | 
|---|
 | 244 |     //WorldEntity * fps = new WorldEntity(); | 
|---|
 | 245 |  | 
|---|
 | 246 |     fps->setAbsCoor( this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() ); | 
|---|
 | 247 |     fps->setAbsDir( this->getAbsDir() ); | 
|---|
 | 248 |     fps->loadMD2Texture( "doom_guy.png" ); | 
|---|
 | 249 |     fps->loadModel( "models/creatures/doom_guy.md2", 10.0f ); | 
|---|
 | 250 |     fps->toList( OM_GROUP_00); | 
|---|
 | 251 |     //fps->loadModel( "models/ships/terran_cruizer.obj" ); | 
|---|
 | 252 |  | 
|---|
 | 253 |     //((Playable*)fps)->setPlayDirection(  0, 0, 1, 0 ); | 
|---|
 | 254 |   } | 
|---|
| [8067] | 255 | } | 
|---|
| [8228] | 256 |  | 
|---|
 | 257 |  | 
|---|
 | 258 |  | 
|---|
 | 259 |  | 
|---|