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