Changeset 6187 for code/branches/presentation2/src/orxonox/items
- Timestamp:
- Dec 1, 2009, 6:48:51 PM (15 years ago)
- Location:
- code/branches/presentation2/src/orxonox/items
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2/src/orxonox/items/MultiStateEngine.cc
r5929 r6187 22 22 * Author: 23 23 * Fabian 'x3n' Landau 24 * Reto Grieder 24 25 * Co-authors: 25 26 * ... … … 29 30 #include "MultiStateEngine.h" 30 31 32 extern "C" { 33 #include <lua.h> 34 } 35 36 #include "util/Convert.h" 31 37 #include "core/CoreIncludes.h" 32 38 #include "core/GameMode.h" 39 #include "core/LuaState.h" 33 40 #include "core/XMLPort.h" 41 #include "worldentities/EffectContainer.h" 34 42 #include "worldentities/pawns/SpaceShip.h" 35 43 … … 37 45 { 38 46 static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20; 39 40 static const unsigned char STATE_ACTIVE = 1;41 static const unsigned char STATE_FORWARD = 2;42 static const unsigned char STATE_BOOST = 4;43 static const unsigned char STATE_BRAKE = 8;44 47 45 48 CreateFactory(MultiStateEngine); … … 49 52 RegisterObject(MultiStateEngine); 50 53 54 this->lua_ = new LuaState(); 51 55 this->state_ = 0; 52 56 … … 59 63 { 60 64 // We have no ship, so the effects are not attached and won't be destroyed automatically 61 for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it) 62 (*it)->destroy(); 63 for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it) 64 (*it)->destroy(); 65 for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it) 66 (*it)->destroy(); 67 for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it) 68 (*it)->destroy(); 65 for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) 66 for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2) 67 (*it2)->destroy(); 68 delete this->lua_; 69 69 } 70 70 } … … 73 73 { 74 74 SUPER(MultiStateEngine, XMLPort, xmlelement, mode); 75 76 XMLPortObject(MultiStateEngine, WorldEntity, "active", addActiveEffect, getActiveEffect, xmlelement, mode); 77 XMLPortObject(MultiStateEngine, WorldEntity, "forward", addForwardEffect, getForwardEffect, xmlelement, mode); 78 XMLPortObject(MultiStateEngine, WorldEntity, "boost", addBoostEffect, getBoostEffect, xmlelement, mode); 79 XMLPortObject(MultiStateEngine, WorldEntity, "brake", addBrakeEffect, getBrakeEffect, xmlelement, mode); 75 XMLPortObject(MultiStateEngine, EffectContainer, "", addEffectContainer, getEffectContainer, xmlelement, mode); 80 76 } 81 77 … … 97 93 98 94 bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD); 99 bool boost = (this->getShip()->getBoost() && forward);100 bool brake = (direction.z > 0 && velocity.z < 0);101 bool active = (direction != Vector3::ZERO && !brake);102 95 103 if (active) 104 this->state_ |= STATE_ACTIVE; 96 int newState = 0; 97 if (this->getShip()->getBoost() && forward) 98 newState = Boost; 99 else if (forward && !newState) // newState == Boost 100 newState = Normal; 101 else if (direction.z > 0 && velocity.z < 0) 102 newState = Brake; 105 103 else 106 this->state_ &= ~STATE_ACTIVE;104 newState = Idle; 107 105 108 if (forward) 109 this->state_ |= STATE_FORWARD; 110 else 111 this->state_ &= ~STATE_FORWARD; 106 if (newState != this->state_) 107 { 108 int changes = newState | this->state_; 109 if (changes & Idle) 110 { 111 lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle); 112 lua_setglobal(this->lua_->getInternalLuaState(), "idle"); 113 } 114 if (changes & Normal) 115 { 116 lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal); 117 lua_setglobal(this->lua_->getInternalLuaState(), "normal"); 118 } 119 if (changes & Brake) 120 { 121 lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake); 122 lua_setglobal(this->lua_->getInternalLuaState(), "brake"); 123 } 124 if (changes & Boost) 125 { 126 lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost); 127 lua_setglobal(this->lua_->getInternalLuaState(), "boost"); 128 } 112 129 113 if (boost) 114 this->state_ |= STATE_BOOST; 115 else 116 this->state_ &= ~STATE_BOOST; 130 // Update all effect conditions 131 for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) 132 (*it)->updateCondition(); 117 133 118 if (brake) 119 this->state_ |= STATE_BRAKE; 120 else 121 this->state_ &= ~STATE_BRAKE; 134 this->state_ = newState; 135 } 122 136 } 123 137 124 138 if (GameMode::isMaster()) 125 139 { 126 for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)127 (*it)->setMainState(this->state_ & STATE_ACTIVE);128 for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)129 (*it)->setMainState(this->state_ & STATE_FORWARD);130 for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)131 (*it)->setMainState(this->state_ & STATE_BOOST);132 for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)133 (*it)->setMainState(this->state_ & STATE_BRAKE);134 140 } 135 141 } … … 145 151 return; 146 152 147 for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it) 148 ship->attach(*it); 149 for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it) 150 ship->attach(*it); 151 for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it) 152 ship->attach(*it); 153 for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it) 154 ship->attach(*it); 153 for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) 154 for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2) 155 this->getShip()->attach(*it2); 155 156 } 156 157 157 void MultiStateEngine::add ActiveEffect(WorldEntity* effect)158 void MultiStateEngine::addEffectContainer(EffectContainer* effect) 158 159 { 159 this->activeEffects_.push_back(effect); 160 if (effect == NULL) 161 return; 162 effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size())); 163 this->effectContainers_.push_back(effect); 160 164 if (this->getShip()) 161 this->getShip()->attach(effect); 165 { 166 for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it) 167 this->getShip()->attach(*it); 168 } 162 169 } 163 170 164 void MultiStateEngine::addForwardEffect(WorldEntity* effect) 165 { 166 this->forwardEffects_.push_back(effect); 167 if (this->getShip()) 168 this->getShip()->attach(effect); 169 } 170 171 void MultiStateEngine::addBoostEffect(WorldEntity* effect) 172 { 173 this->boostEffects_.push_back(effect); 174 if (this->getShip()) 175 this->getShip()->attach(effect); 176 } 177 178 void MultiStateEngine::addBrakeEffect(WorldEntity* effect) 179 { 180 this->brakeEffects_.push_back(effect); 181 if (this->getShip()) 182 this->getShip()->attach(effect); 183 } 184 185 WorldEntity* MultiStateEngine::getActiveEffect(unsigned int index) const 171 EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const 186 172 { 187 173 unsigned int i = 0; 188 for (std:: list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)174 for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) 189 175 { 190 176 if (i == index) 191 177 return (*it); 192 ++i;193 178 } 194 return 0; 195 } 196 197 WorldEntity* MultiStateEngine::getForwardEffect(unsigned int index) const 198 { 199 unsigned int i = 0; 200 for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it) 201 { 202 if (i == index) 203 return (*it); 204 ++i; 205 } 206 return 0; 207 } 208 209 WorldEntity* MultiStateEngine::getBoostEffect(unsigned int index) const 210 { 211 unsigned int i = 0; 212 for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it) 213 { 214 if (i == index) 215 return (*it); 216 ++i; 217 } 218 return 0; 219 } 220 221 WorldEntity* MultiStateEngine::getBrakeEffect(unsigned int index) const 222 { 223 unsigned int i = 0; 224 for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it) 225 { 226 if (i == index) 227 return (*it); 228 ++i; 229 } 230 return 0; 179 return NULL; 231 180 } 232 181 } -
code/branches/presentation2/src/orxonox/items/MultiStateEngine.h
r5781 r6187 22 22 * Author: 23 23 * Fabian 'x3n' Landau 24 * Reto Grieder 24 25 * Co-authors: 25 26 * ... … … 32 33 #include "OrxonoxPrereqs.h" 33 34 34 #include < list>35 #include <vector> 35 36 #include "Engine.h" 36 37 37 38 namespace orxonox 38 39 { 40 // forward declaration 41 class EffectContainer; 42 39 43 class _OrxonoxExport MultiStateEngine : public Engine 40 44 { 41 45 public: 46 enum EngineState 47 { 48 Idle = 1, 49 Normal = 2, 50 Brake = 4, 51 Boost = 8 52 }; 53 42 54 MultiStateEngine(BaseObject* creator); 43 55 virtual ~MultiStateEngine(); … … 50 62 virtual void addToSpaceShip(SpaceShip* ship); 51 63 52 void addActiveEffect(WorldEntity* effect); 53 void addForwardEffect(WorldEntity* effect); 54 void addBoostEffect(WorldEntity* effect); 55 void addBrakeEffect(WorldEntity* effect); 56 57 WorldEntity* getActiveEffect(unsigned int index) const; 58 WorldEntity* getForwardEffect(unsigned int index) const; 59 WorldEntity* getBoostEffect(unsigned int index) const; 60 WorldEntity* getBrakeEffect(unsigned int index) const; 64 void addEffectContainer(EffectContainer* effect); 65 EffectContainer* getEffectContainer(unsigned int index) const; 61 66 62 67 private: 63 unsigned char state_; 64 std::list<WorldEntity*> activeEffects_; 65 std::list<WorldEntity*> forwardEffects_; 66 std::list<WorldEntity*> boostEffects_; 67 std::list<WorldEntity*> brakeEffects_; 68 int state_; 69 LuaState* lua_; 70 std::vector<EffectContainer*> effectContainers_; 68 71 }; 69 72 }
Note: See TracChangeset
for help on using the changeset viewer.