[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" |
---|
[9922] | 25 | #include "src/world_entities/npcs/generic_npc.h" |
---|
[9915] | 26 | |
---|
[9925] | 27 | |
---|
| 28 | ObjectListDefinition(Spectator); |
---|
[9869] | 29 | CREATE_FACTORY(Spectator); |
---|
[8067] | 30 | |
---|
| 31 | |
---|
| 32 | /** |
---|
[8228] | 33 | * destructs the Spectator, deletes alocated memory |
---|
[8067] | 34 | */ |
---|
[8228] | 35 | Spectator::~Spectator () |
---|
[8067] | 36 | { |
---|
[8228] | 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))); |
---|
[9869] | 62 | this->registerObject(this, Spectator::_objectList); |
---|
[8067] | 63 | |
---|
[8228] | 64 | this->getWeaponManager().changeWeaponConfig(1); |
---|
| 65 | |
---|
[8067] | 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; |
---|
[8228] | 72 | |
---|
| 73 | this->setHealthMax(100); |
---|
| 74 | this->setHealth(80); |
---|
| 75 | |
---|
| 76 | this->mouseDir = this->getAbsDir(); |
---|
| 77 | |
---|
| 78 | //add events to the eventlist |
---|
[8067] | 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); |
---|
[9915] | 84 | registerEvent(KeyMapper::PEV_JUMP); |
---|
[8067] | 85 | registerEvent(EV_MOUSE_MOTION); |
---|
[8228] | 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 | |
---|
[9869] | 94 | |
---|
[8067] | 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 | |
---|
[8228] | 102 | |
---|
[8067] | 103 | /** |
---|
[8228] | 104 | * loads the Settings of a Spectator from an XML-element. |
---|
| 105 | * @param root the XML-element to load the Spaceship's properties from |
---|
[8067] | 106 | */ |
---|
[8228] | 107 | void Spectator::loadParams(const TiXmlElement* root) |
---|
[8067] | 108 | { |
---|
[8228] | 109 | Playable::loadParams(root); |
---|
[8067] | 110 | } |
---|
| 111 | |
---|
[8228] | 112 | void Spectator::setPlayDirection(const Quaternion& quat, float speed) |
---|
[8067] | 113 | { |
---|
[8228] | 114 | this->mouseDir = quat; |
---|
| 115 | this->angleY = quat.getHeading(); |
---|
| 116 | this->angleX = quat.getAttitude(); |
---|
[8067] | 117 | } |
---|
| 118 | |
---|
[8228] | 119 | |
---|
| 120 | void Spectator::reset() |
---|
[8067] | 121 | { |
---|
[8228] | 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 ); |
---|
[8067] | 136 | this->attachCamera(); |
---|
| 137 | } |
---|
| 138 | |
---|
[8228] | 139 | void Spectator::leave() |
---|
[8067] | 140 | { |
---|
[8228] | 141 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
[8067] | 142 | this->detachCamera(); |
---|
| 143 | } |
---|
| 144 | |
---|
[8228] | 145 | |
---|
[8067] | 146 | /** |
---|
[8228] | 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 |
---|
[8067] | 152 | */ |
---|
[8228] | 153 | void Spectator::collidesWith(WorldEntity* entity, const Vector& location) |
---|
[8067] | 154 | { |
---|
| 155 | } |
---|
| 156 | |
---|
[8228] | 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) |
---|
[8067] | 164 | { |
---|
| 165 | Playable::tick( time ); |
---|
[9869] | 166 | |
---|
[8708] | 167 | if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) |
---|
[8067] | 168 | { |
---|
| 169 | xMouse *= time / 10; |
---|
| 170 | yMouse *= time / 10; |
---|
[9869] | 171 | |
---|
[8147] | 172 | angleX -= xMouse; |
---|
| 173 | angleY -= yMouse; |
---|
[9869] | 174 | |
---|
[8147] | 175 | if ( angleY > 2.05 ) |
---|
| 176 | angleY = 2.05; |
---|
[9869] | 177 | |
---|
[8147] | 178 | if ( angleY < -1.15 ) |
---|
| 179 | angleY = -1.15; |
---|
[9869] | 180 | |
---|
[8147] | 181 | this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) ); |
---|
[9869] | 182 | |
---|
[8067] | 183 | xMouse = yMouse = 0; |
---|
| 184 | } |
---|
[9869] | 185 | |
---|
[8067] | 186 | this->setAbsDir( this->mouseDir ); |
---|
[9869] | 187 | |
---|
[8147] | 188 | Vector velocity; |
---|
[9869] | 189 | |
---|
[8147] | 190 | if ( this->bForward ) |
---|
| 191 | { |
---|
| 192 | velocity += this->getAbsDirX(); |
---|
| 193 | } |
---|
[9869] | 194 | |
---|
[8147] | 195 | if ( this->bBackward ) |
---|
| 196 | { |
---|
| 197 | velocity -= this->getAbsDirX(); |
---|
| 198 | } |
---|
[9869] | 199 | |
---|
[8147] | 200 | if ( this->bRight ) |
---|
| 201 | { |
---|
| 202 | velocity += this->getAbsDirZ(); |
---|
| 203 | } |
---|
[9869] | 204 | |
---|
[8147] | 205 | if ( this->bLeft ) |
---|
| 206 | { |
---|
| 207 | velocity -= this->getAbsDirZ(); |
---|
| 208 | } |
---|
[9869] | 209 | |
---|
[8147] | 210 | velocity *= 100; |
---|
[9869] | 211 | |
---|
[8147] | 212 | this->shiftCoor( velocity*time ); |
---|
[8067] | 213 | } |
---|
| 214 | |
---|
[8228] | 215 | /** |
---|
| 216 | * @todo switch statement ?? |
---|
| 217 | */ |
---|
| 218 | void Spectator::process(const Event &event) |
---|
[8067] | 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 | } |
---|
[9915] | 235 | else if( event.type == KeyMapper::PEV_JUMP) |
---|
| 236 | { |
---|
[9922] | 237 | // FPSPlayer * fps = new FPSPlayer(); |
---|
| 238 | //GenericNPC* fps = new GenericNPC(); |
---|
| 239 | WorldEntity* fps = new WorldEntity(); |
---|
[9921] | 240 | //WorldEntity * fps = new WorldEntity(); |
---|
[9922] | 241 | |
---|
[9915] | 242 | fps->setAbsCoor( this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() ); |
---|
| 243 | fps->setAbsDir( this->getAbsDir() ); |
---|
[9921] | 244 | fps->loadMD2Texture( "doom_guy.png" ); |
---|
| 245 | fps->loadModel( "models/creatures/doom_guy.md2", 10.0f ); |
---|
[9922] | 246 | fps->toList( OM_GROUP_00); |
---|
[9921] | 247 | //fps->loadModel( "models/ships/terran_cruizer.obj" ); |
---|
[9915] | 248 | |
---|
| 249 | //((Playable*)fps)->setPlayDirection( 0, 0, 1, 0 ); |
---|
| 250 | } |
---|
[8067] | 251 | } |
---|
[8228] | 252 | |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | |
---|