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