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