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