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