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