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