| 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 | CREATE_FACTORY(Spectator, CL_SPECTATOR); | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | using namespace std; | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | /** | 
|---|
| 31 | *  destructs the Spectator, deletes alocated memory | 
|---|
| 32 | */ | 
|---|
| 33 | Spectator::~Spectator () | 
|---|
| 34 | { | 
|---|
| 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))); | 
|---|
| 60 | this->setClassID(CL_SPECTATOR, "Spectator"); | 
|---|
| 61 |  | 
|---|
| 62 | this->getWeaponManager().changeWeaponConfig(1); | 
|---|
| 63 |  | 
|---|
| 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; | 
|---|
| 70 |  | 
|---|
| 71 | this->setHealthMax(100); | 
|---|
| 72 | this->setHealth(80); | 
|---|
| 73 |  | 
|---|
| 74 | this->mouseDir = this->getAbsDir(); | 
|---|
| 75 |  | 
|---|
| 76 | //add events to the eventlist | 
|---|
| 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); | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | * loads the Settings of a Spectator from an XML-element. | 
|---|
| 102 | * @param root the XML-element to load the Spaceship's properties from | 
|---|
| 103 | */ | 
|---|
| 104 | void Spectator::loadParams(const TiXmlElement* root) | 
|---|
| 105 | { | 
|---|
| 106 | Playable::loadParams(root); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | void Spectator::setPlayDirection(const Quaternion& quat, float speed) | 
|---|
| 110 | { | 
|---|
| 111 | this->mouseDir = quat; | 
|---|
| 112 | this->angleY = quat.getHeading(); | 
|---|
| 113 | this->angleX = quat.getAttitude(); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 117 | void Spectator::reset() | 
|---|
| 118 | { | 
|---|
| 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 ); | 
|---|
| 133 | this->attachCamera(); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | void Spectator::leave() | 
|---|
| 137 | { | 
|---|
| 138 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); | 
|---|
| 139 | this->detachCamera(); | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 |  | 
|---|
| 143 | /** | 
|---|
| 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 | 
|---|
| 149 | */ | 
|---|
| 150 | void Spectator::collidesWith(WorldEntity* entity, const Vector& location) | 
|---|
| 151 | { | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 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) | 
|---|
| 161 | { | 
|---|
| 162 | Playable::tick( time ); | 
|---|
| 163 |  | 
|---|
| 164 | if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) | 
|---|
| 165 | { | 
|---|
| 166 | xMouse *= time / 10; | 
|---|
| 167 | yMouse *= time / 10; | 
|---|
| 168 |  | 
|---|
| 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 |  | 
|---|
| 180 | xMouse = yMouse = 0; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | this->setAbsDir( this->mouseDir ); | 
|---|
| 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 ); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /** | 
|---|
| 213 | * @todo switch statement ?? | 
|---|
| 214 | */ | 
|---|
| 215 | void Spectator::process(const Event &event) | 
|---|
| 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 | } | 
|---|
| 233 |  | 
|---|
| 234 |  | 
|---|
| 235 |  | 
|---|
| 236 |  | 
|---|