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