Changeset 2029 for code/branches/objecthierarchy/src/orxonox/objects
- Timestamp:
- Oct 28, 2008, 8:37:09 PM (16 years ago)
- Location:
- code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/DistanceTrigger.cc
r2027 r2029 41 41 RegisterObject(DistanceTrigger); 42 42 43 targetMask_.exclude(Class(BaseObject)); 43 this->distance_ = 100; 44 this->targetMask_.exclude(Class(BaseObject)); 44 45 } 45 46 46 47 DistanceTrigger::~DistanceTrigger() 47 48 { 49 } 50 51 void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) 52 { 53 SUPER(DistanceTrigger, XMLPort, xmlelement, mode); 54 55 XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f); 56 XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode); 48 57 } 49 58 … … 61 70 COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl; 62 71 std::set<Ogre::Node*>::iterator it; 63 for (it = this->targetSet_.begin(); it != this->targetSet_.end(); it++)72 for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it) 64 73 { 65 74 COUT(4) << *it << std::endl; … … 87 96 { 88 97 // Iterate through all WorldEntities 89 for (Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; it++)98 for (Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; ++it) 90 99 { 91 100 // check if WorldEntity is a target 92 if (targetMask_.isIncluded(it->getIdentifier()))101 if (targetMask_.isIncluded(it->getIdentifier())) 93 102 { 94 103 Vector3 distanceVec = it->getNode()->getWorldPosition() - this->getNode()->getWorldPosition(); 95 104 if (distanceVec.length() < this->distance_) 96 {97 105 return true; 98 }99 106 } 100 107 } … … 103 110 } 104 111 105 void DistanceTrigger::setDistance(float dist)106 {107 this->distance_ = dist;108 }109 110 void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)111 {112 Trigger::XMLPort(xmlelement, mode);113 114 XMLPortParamLoadOnly(DistanceTrigger, "distance", setDistance, xmlelement, mode);115 XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode);116 }117 118 112 bool DistanceTrigger::isTriggered(TriggerMode mode) 119 113 { 120 if (Trigger::isTriggered(mode))114 if (Trigger::isTriggered(mode)) 121 115 return checkDistance(); 122 116 else -
code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/DistanceTrigger.h
r2027 r2029 46 46 DistanceTrigger(BaseObject* creator); 47 47 ~DistanceTrigger(); 48 49 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 50 48 51 void addTarget(Ogre::Node* targetNode); 52 void addTargets(const std::string& targets); 49 53 void removeTarget(Ogre::Node* targetNode); 50 void addTargets(const std::string& targets);51 54 void removeTargets(const std::string& targets); 52 void setDistance(float dist); 53 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 55 56 inline void setDistance(float distance) 57 { this->distance_ = distance; } 58 inline float getDistance() const 59 { return this->distance_; } 60 54 61 bool checkDistance(); 55 62 -
code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/Trigger.cc
r2027 r2029 48 48 RegisterObject(Trigger); 49 49 50 mode_ = TM_EventTriggerAND; 51 bActive_ = false; 52 bInvertMode_ = false; 53 delay_ = 0.0; 54 bTriggered_ = false; 55 bUpdating_ = false; 56 remainingActivations_ = -1; 57 bStayOn_ = false; 58 latestState_ = 0x0; 50 this->mode_ = TM_EventTriggerAND; 51 52 this->bActive_ = false; 53 this->bTriggered_ = false; 54 this->latestState_ = 0x0; 55 56 this->bInvertMode_ = false; 57 this->bSwitch_ = false; 58 this->bStayActive_ = false; 59 this->delay_ = 0.0; 60 this->remainingActivations_ = -1; 61 62 // this->bUpdating_ = false; 59 63 60 64 if (this->getScene() && this->getScene()->getSceneManager()) 61 65 { 62 debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);63 debugBillboard_.setVisible(false);64 } 65 66 this->getNode()->attachObject( debugBillboard_.getBillboardSet());66 this->debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); 67 this->debugBillboard_.setVisible(false); 68 } 69 70 this->getNode()->attachObject(this->debugBillboard_.getBillboardSet()); 67 71 } 68 72 … … 71 75 } 72 76 73 void Trigger::init() 74 { 75 timeSinceLastEvent_ = delay_; 76 } 77 78 void Trigger::debugFlares(bool bVisible) 79 { 80 for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it) 81 it->setVisible(bVisible); 82 } 83 84 void Trigger::changedVisibility() 85 { 86 debugBillboard_.setVisible(this->isVisible()); 87 } 88 89 const Trigger* Trigger::getTrigger(unsigned int index) const 90 { 91 if (children_.size() <= index) 92 return NULL; 93 94 std::set<Trigger*>::const_iterator it; 95 it = children_.begin(); 96 97 for ( unsigned int i = 0; i != index; i++ ) 98 { 99 it++; 100 } 101 return *it; 77 void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) 78 { 79 SUPER(Trigger, XMLPort, xmlelement, mode); 80 81 XMLPortParam(Trigger, "delay", setDelay, getDelay, xmlelement, mode).defaultValues(0.0f); 82 XMLPortParam(Trigger, "switch", setSwitch, getSwitch, xmlelement, mode).defaultValues(false); 83 XMLPortParam(Trigger, "stayactive", setStayActive, getStayActive, xmlelement, mode).defaultValues(false); 84 XMLPortParam(Trigger, "activations", setActivations, getActivations, xmlelement, mode).defaultValues(-1); 85 XMLPortParam(Trigger, "invert", setInvert, getInvert, xmlelement, mode).defaultValues(false); 86 XMLPortParamTemplate(Trigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&).defaultValues("or"); 87 88 XMLPortObject(Trigger, Trigger, "", addTrigger, getTrigger, xmlelement, mode); 102 89 } 103 90 … … 105 92 { 106 93 107 bool newTriggered; 108 newTriggered = this->isTriggered(); 94 bool newTriggered = this->isTriggered(); 109 95 110 96 // check if new triggering event is really new 111 if ((this->latestState_ & 0x1) != newTriggered)97 if ((this->latestState_ & 0x1) != newTriggered) 112 98 { 113 99 // create new state 114 if (newTriggered)100 if (newTriggered) 115 101 { 116 latestState_ |= 1; // set trigger bit102 this->latestState_ |= 1; // set trigger bit to 1 117 103 this->switchState(); 118 104 } 119 105 else 120 106 { 121 latestState_ &= 0xFE; // set trigger bit122 if (this->bS tayOn_)107 this->latestState_ &= 0xFE; // set trigger bit to 0 108 if (this->bSwitch_) 123 109 this->storeState(); 124 110 else … … 127 113 } 128 114 129 if (remainingTime_ > 0.0)130 { 131 remainingTime_ -= dt;115 if (this->remainingTime_ > 0.0) 116 { 117 this->remainingTime_ -= dt; 132 118 // only increase when acctually waiting for a state in the queue 133 if (timeSinceLastEvent_ >= 0.0)134 t imeSinceLastEvent_ += dt;135 } 136 137 while (remainingTime_ <= 0.0 &&stateChanges_.size() > 0)119 if (this->timeSinceLastEvent_ >= 0.0) 120 this->timeSinceLastEvent_ += dt; 121 } 122 123 while (this->remainingTime_ <= 0.0 && this->stateChanges_.size() > 0) 138 124 { 139 125 // time ran out, change state to new one 140 char newState = stateChanges_.front().second;141 bTriggered_ = (newState & 0x1);142 bActive_ = newState & 2;126 char newState = this->stateChanges_.front().second; 127 this->bTriggered_ = (newState & 0x1); 128 this->bActive_ = newState & 2; 143 129 this->stateChanges_.pop(); 144 if (stateChanges_.size() != 0)145 remainingTime_ =stateChanges_.front().first;130 if (this->stateChanges_.size() != 0) 131 this->remainingTime_ = this->stateChanges_.front().first; 146 132 else 147 timeSinceLastEvent_ = delay_; 148 } 149 150 151 152 if (bTriggered_ && bActive_) 133 this->timeSinceLastEvent_ = this->delay_; 134 } 135 136 if (this->bTriggered_ && this->bActive_) 153 137 this->setBillboardColour(ColourValue(0.5, 1.0, 0.0)); 154 else if (! bTriggered_ &&bActive_)138 else if (!this->bTriggered_ && this->bActive_) 155 139 this->setBillboardColour(ColourValue(0.0, 1.0, 0.0)); 156 else if ( bTriggered_ && !bActive_)140 else if (this->bTriggered_ && !this->bActive_) 157 141 this->setBillboardColour(ColourValue(1.0, 0.5, 0.0)); 158 142 else 159 143 this->setBillboardColour(ColourValue(1.0, 0.0, 0.0)); 160 bUpdating_ = false;161 }162 163 void Trigger::setBillboardColour(ColourValue colour)164 {165 this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour);166 }167 168 void Trigger::storeState()169 {170 // put state change into queue171 this->stateChanges_.push(std::pair<float,char>(timeSinceLastEvent_, latestState_));172 // reset time since last event173 timeSinceLastEvent_ = 0.0;174 175 if(this->stateChanges_.size() == 1)176 remainingTime_ = stateChanges_.front().first;177 144 } 178 145 179 146 bool Trigger::isTriggered(TriggerMode mode) 180 147 { 181 if(bUpdating_)182 returnbTriggered_;183 184 if( children_.size() != 0 ) 185 {186 bUpdating_ = true;148 // if (this->bUpdating_) 149 // return this->bTriggered_; 150 151 // this->bUpdating_ = true; 152 if (this->children_.size() != 0) 153 { 187 154 bool returnval = false; 188 switch(mode) 155 156 switch (mode) 189 157 { 190 158 case TM_EventTriggerAND: … … 201 169 break; 202 170 } 203 if(bInvertMode_) 171 // this->bUpdating_ = false; 172 173 if (this->bInvertMode_) 204 174 return !returnval; 205 175 else … … 209 179 } 210 180 211 void Trigger::setDelay(float delay)212 {213 this->delay_ = delay;214 }215 216 void Trigger::setMode(const std::string& modeName)217 {218 if (modeName == "and")219 setMode(TM_EventTriggerAND);220 else if (modeName == "or")221 setMode(TM_EventTriggerOR);222 else if (modeName == "xor")223 setMode(TM_EventTriggerXOR);224 }225 226 void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)227 {228 WorldEntity::XMLPort(xmlelement, mode);229 230 XMLPortParamLoadOnly(Trigger, "delay", setDelay, xmlelement, mode);231 XMLPortParamLoadOnly(Trigger, "stayOn", setStayOn, xmlelement, mode);232 XMLPortParamLoadOnly(Trigger, "activations", setActivations, xmlelement, mode);233 XMLPortParamLoadOnly(Trigger, "invert", setInvert, xmlelement, mode);234 XMLPortParamLoadOnlyTemplate(Trigger, "mode", setMode, xmlelement, mode, const std::string&);235 236 XMLPortObject(Trigger, Trigger, "", addTrigger, getTrigger, xmlelement, mode);237 238 this->init();239 }240 241 void Trigger::addTrigger(Trigger* trig)242 {243 if (this != trig)244 this->children_.insert(trig);245 }246 247 bool Trigger::switchState()248 {249 if ( remainingActivations_ <= -1 || this->latestState_ & 2 || remainingActivations_ > 0)250 {251 this->latestState_ ^= 2; // toggle state bit252 // increase activation count253 if (this->latestState_ & 2) remainingActivations_--;254 this->storeState();255 256 return true;257 }258 return false;259 }260 261 262 181 bool Trigger::checkAnd() 263 182 { 264 183 std::set<Trigger*>::iterator it; 265 for(it = this->children_.begin(); it != this->children_.end(); it++)266 { 267 if (!((*it)->isActive()))184 for(it = this->children_.begin(); it != this->children_.end(); ++it) 185 { 186 if (!(*it)->isActive()) 268 187 return false; 269 188 } … … 274 193 { 275 194 std::set<Trigger*>::iterator it; 276 for(it = this->children_.begin(); it != this->children_.end(); it++)277 { 278 if ((*it)->isActive())195 for(it = this->children_.begin(); it != this->children_.end(); ++it) 196 { 197 if ((*it)->isActive()) 279 198 return true; 280 199 } … … 286 205 std::set<Trigger*>::iterator it; 287 206 bool test = false; 288 for(it = this->children_.begin(); it != this->children_.end(); it++)289 { 290 if (test && (*it)->isActive())207 for(it = this->children_.begin(); it != this->children_.end(); ++it) 208 { 209 if (test && (*it)->isActive()) 291 210 return false; 292 if ((*it)->isActive())211 if ((*it)->isActive()) 293 212 test = true; 294 213 } … … 296 215 } 297 216 217 bool Trigger::switchState() 218 { 219 if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0)) 220 || (!(this->latestState_ & 2)) && (this->remainingActivations_ == 0)) 221 return false; 222 else 223 { 224 this->latestState_ ^= 2; // toggle state bit 225 226 // increase activation count 227 if (this->latestState_ & 2 && this->remainingActivations_ > 0) 228 this->remainingActivations_--; 229 230 this->storeState(); 231 232 return true; 233 } 234 } 235 236 void Trigger::storeState() 237 { 238 // put state change into queue 239 this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_)); 240 // reset time since last event 241 this->timeSinceLastEvent_ = 0.0; 242 243 if (this->stateChanges_.size() == 1) 244 this->remainingTime_ = this->stateChanges_.front().first; 245 } 246 247 void Trigger::setDelay(float delay) 248 { 249 this->delay_ = delay; 250 this->timeSinceLastEvent_ = delay; 251 } 252 253 void Trigger::setMode(const std::string& modeName) 254 { 255 if (modeName == "and") 256 this->setMode(TM_EventTriggerAND); 257 else if (modeName == "or") 258 this->setMode(TM_EventTriggerOR); 259 else if (modeName == "xor") 260 this->setMode(TM_EventTriggerXOR); 261 } 262 263 std::string Trigger::getModeString() const 264 { 265 if (this->mode_ == TM_EventTriggerAND) 266 return std::string("and"); 267 else if (this->mode_ == TM_EventTriggerOR) 268 return std::string("or"); 269 else if (this->mode_ == TM_EventTriggerXOR) 270 return std::string("xor"); 271 else 272 return std::string("and"); 273 } 274 275 void Trigger::addTrigger(Trigger* trigger) 276 { 277 if (this != trigger) 278 this->children_.insert(trigger); 279 } 280 281 const Trigger* Trigger::getTrigger(unsigned int index) const 282 { 283 if (this->children_.size() <= index) 284 return NULL; 285 286 std::set<Trigger*>::const_iterator it; 287 it = this->children_.begin(); 288 289 for (unsigned int i = 0; i != index; ++i) 290 ++it; 291 292 return (*it); 293 } 294 295 void Trigger::debugFlares(bool bVisible) 296 { 297 for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it) 298 it->setVisible(bVisible); 299 } 300 301 void Trigger::setBillboardColour(const ColourValue& colour) 302 { 303 this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour); 304 } 305 306 void Trigger::changedVisibility() 307 { 308 SUPER(Trigger, changedVisibility); 309 310 this->debugBillboard_.setVisible(this->isVisible()); 311 } 298 312 } -
code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/Trigger.h
r2027 r2029 38 38 #include "tools/BillboardSet.h" 39 39 40 namespace orxonox {41 40 namespace orxonox 41 { 42 42 enum TriggerMode 43 43 { … … 59 59 { return bActive_; } 60 60 61 void addTrigger(Trigger* trig );61 void addTrigger(Trigger* trigger); 62 62 const Trigger* getTrigger(unsigned int index) const; 63 63 64 void setMode(const std::string& modeName); 65 inline void setMode(TriggerMode mode) 66 { this->mode_ = mode; } 64 67 inline TriggerMode getMode() const 65 68 { return mode_; } 66 inline void setMode(TriggerMode mode)67 { this->mode_ = mode; }68 void setMode(const std::string& modeName);69 69 70 inline void setInvert(int invert) 71 { bInvertMode_ = invert; } 72 inline void setStayOn(int stayOn) 73 { this->bStayOn_ = (stayOn == 1) ? true : false; } 70 inline void setInvert(bool bInvert) 71 { this->bInvertMode_ = bInvert; } 72 inline bool getInvert() const 73 { return this->bInvertMode_; } 74 75 inline void setSwitch(bool bSwitch) 76 { this->bSwitch_ = bSwitch; } 77 inline bool getSwitch() const 78 { return this->bSwitch_; } 79 80 inline void setStayActive(bool bStayActive) 81 { this->bStayActive_ = bStayActive; } 82 inline bool getStayActive() const 83 { return this->bStayActive_; } 84 74 85 inline void setActivations(int activations) 75 86 { this->remainingActivations_ = activations; } 87 inline int getActivations() const 88 { return this->remainingActivations_; } 89 76 90 void setDelay(float delay); 91 inline float getDelay() const 92 { return this->delay_; } 93 77 94 bool switchState(); 78 95 79 96 static void debugFlares(bool bVisible); 80 97 virtual void changedVisibility(); 81 82 private:83 void init();84 bool checkAnd();85 bool checkOr();86 bool checkXor();87 void setBillboardColour(ColourValue colour);88 void storeState();89 98 90 99 protected: … … 93 102 94 103 private: 95 std::set<Trigger*> children_; 96 std::queue<std::pair<float,char> > stateChanges_; 104 bool checkAnd(); 105 bool checkOr(); 106 bool checkXor(); 107 void setBillboardColour(const ColourValue& colour); 108 void storeState(); 109 std::string getModeString() const; 110 111 bool bActive_; 112 bool bTriggered_; 113 114 TriggerMode mode_; 115 bool bInvertMode_; 116 bool bSwitch_; 117 bool bStayActive_; 118 float delay_; 119 int remainingActivations_; 120 121 char latestState_; 97 122 float remainingTime_; 98 123 float timeSinceLastEvent_; 99 TriggerMode mode_; 100 bool bActive_; 101 bool bInvertMode_; 102 bool bTriggered_; 103 bool bUpdating_; 124 125 // bool bUpdating_; 104 126 BillboardSet debugBillboard_; 105 float delay_; 106 int remainingActivations_; 107 bool bStayOn_; 108 char latestState_; 127 128 std::set<Trigger*> children_; 129 std::queue<std::pair<float, char> > stateChanges_; 109 130 }; 110 131
Note: See TracChangeset
for help on using the changeset viewer.