Changeset 1693 for code/branches/script_trigger/src
- Timestamp:
- Sep 1, 2008, 9:29:27 PM (16 years ago)
- Location:
- code/branches/script_trigger/src/orxonox
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/script_trigger/src/orxonox/CMakeLists.txt
r1383 r1693 26 26 objects/Script.cc 27 27 objects/Trigger.cc 28 objects/DistanceTrigger.cc 28 29 29 30 # objects/weapon/AmmunitionDump.cc -
code/branches/script_trigger/src/orxonox/objects/Trigger.cc
r1671 r1693 34 34 #include "core/CoreIncludes.h" 35 35 #include "core/ConsoleCommand.h" 36 #include "core/XMLPort.h" 36 37 37 38 namespace orxonox … … 46 47 RegisterObject(Trigger); 47 48 48 targetMask_.exclude(Class(BaseObject));49 50 //testing51 49 mode_ = TM_EventTriggerAND; 52 bActive_ = true; 53 triggingTime_ = 100; 50 bActive_ = false; 51 bInvertMode_ = false; 52 delay_ = 0.0; 53 bTriggered_ = false; 54 bUpdating_ = false; 54 55 55 56 debugBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); … … 61 62 } 62 63 63 bool Trigger::isTriggered() 64 { 65 return this->isTriggered(this->mode_); 66 } 67 68 void Trigger::setVisibility(int bVisible) 64 void Trigger::init() 65 { 66 this->setVisibility(true); 67 timeSinceLastEvent_ = delay_; 68 } 69 70 void Trigger::setVisibility(bool bVisible) 69 71 { 70 72 if(bVisible) … … 76 78 void Trigger::tick(float dt) 77 79 { 78 //COUT(0) << "Scale: " << this->getScale() << std::endl; 79 if(bActive_) 80 { 81 //this->actualTime_ += dt; 82 if(this->isTriggered()) 80 81 bool newTriggered = this->isTriggered(); 82 83 84 // check if new triggering event is really new 85 if(this->latestState_ % 2 != newTriggered) 86 { 87 // create new state 88 if(newTriggered) 83 89 { 84 this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(ColourValue(0.0, 1.0, 0.0)); 90 latestState_ |= 0x1; // set trigger bit 91 latestState_ ^= 0x10; // toggle state bit 85 92 } 86 } 93 else 94 { 95 latestState_ &= 0x11111110; // set trigger bit 96 } 97 98 // put state change into queue 99 this->stateChanges_.push(std::pair<float,char>(timeSinceLastEvent_, latestState_)); 100 // reset time since last event 101 timeSinceLastEvent_ = 0.0; 102 103 if(this->stateChanges_.size() == 1) 104 remainingTime_ = stateChanges_.front().first; 105 } 106 107 if(remainingTime_ > 0.0) 108 { 109 remainingTime_ -= dt; 110 // only increase when acctually waiting for a state in the queue 111 if(timeSinceLastEvent_ >= 0.0) 112 timeSinceLastEvent_ += dt; 113 } 114 115 while(remainingTime_ <= 0.0 && stateChanges_.size() > 0) 116 { 117 // time ran out, change state to new one 118 char newState = stateChanges_.front().second; 119 bTriggered_ = newState % 2; 120 bActive_ = newState>>1 % 2; 121 this->stateChanges_.pop(); 122 if(stateChanges_.size() != 0) 123 remainingTime_ = stateChanges_.front().first; 124 else 125 timeSinceLastEvent_ = delay_; 126 } 127 128 129 130 if (bTriggered_ && bActive_) 131 this->setBillboardColour(ColourValue(0.5, 1.0, 0.0)); 132 else if (!bTriggered_ && bActive_) 133 this->setBillboardColour(ColourValue(0.0, 1.0, 0.0)); 134 else if (bTriggered_ && !bActive_) 135 this->setBillboardColour(ColourValue(1.0, 0.5, 0.0)); 136 else 137 this->setBillboardColour(ColourValue(1.0, 0.0, 0.0)); 138 bUpdating_ = false; 139 } 140 141 void Trigger::setBillboardColour(ColourValue colour) 142 { 143 this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour); 87 144 } 88 145 89 146 bool Trigger::isTriggered(TriggerMode mode) 90 147 { 148 if(bUpdating_) 149 return bTriggered_; 150 91 151 if( children_.size() != 0 ) 92 152 { 153 bUpdating_ = true; 154 bool returnval = false; 93 155 switch(mode) 94 156 { 95 157 case TM_EventTriggerAND: 96 return checkAnd();158 returnval = checkAnd(); 97 159 break; 98 160 case TM_EventTriggerOR: 99 return checkOr();161 returnval = checkOr(); 100 162 break; 101 163 case TM_EventTriggerXOR: 102 return checkXor(); 103 break; 104 case TM_EventTriggerNOT: 105 return checkNot(); 164 returnval = checkXor(); 106 165 break; 107 166 default: 108 return false;167 returnval = false; 109 168 break; 110 169 } 170 if(bInvertMode_) 171 return !returnval; 172 else 173 return returnval; 111 174 } 112 175 return true; 113 176 } 114 177 178 void Trigger::setDelay(float delay) 179 { 180 this->delay_ = delay; 181 } 182 115 183 void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) 116 184 { 117 185 WorldEntity::XMLPort(xmlelement, mode); 186 187 XMLPortParamLoadOnly(Trigger, "delay", setDelay, xmlelement, mode); 188 189 this->init(); 118 190 } 119 191 … … 124 196 } 125 197 126 void Trigger::addTargets(std::string targets) 127 { 128 Identifier* targetId = ID(targets); 129 targetMask_.include(targetId); 130 // trigger shouldn't react on itself or other triggers 131 targetMask_.exclude(Class(Trigger), true); 132 } 133 134 void Trigger::removeTargets(std::string targets) 135 { 136 Identifier* targetId = ID(targets); 137 targetMask_.exclude(targetId); 138 } 198 bool Trigger::switchState() 199 { 200 latestState_ ^= 0x10; // toggle state bit 201 // put state change into queue 202 this->stateChanges_.push(std::pair<float,char>(timeSinceLastEvent_, latestState_)); 203 // reset time since last event 204 timeSinceLastEvent_ = 0.0; 205 206 if(this->stateChanges_.size() == 1) 207 remainingTime_ = stateChanges_.front().first; 208 } 209 139 210 140 211 bool Trigger::checkAnd() … … 158 229 } 159 230 return false; 160 }161 162 bool Trigger::checkNot()163 {164 std::set<Trigger*>::iterator it;165 for(it = this->children_.begin(); it != this->children_.end(); it++)166 {167 if((*it)->isTriggered())168 return false;169 }170 return true;171 231 } 172 232 … … 185 245 } 186 246 187 /*bool Trigger::checkDelay()188 {189 if (triggingTime_ < actualTime_)190 return true;191 else192 return false;193 }194 195 bool Trigger::checkDistance()196 {197 // Iterate through all WorldEntities198 for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; it++)199 {200 Vector3 distanceVec = it->getNode()->getWorldPosition() - this->getNode()->getWorldPosition();201 if (distanceVec.length() < radius_)202 return true;203 }204 return false;205 206 }*/207 208 247 } -
code/branches/script_trigger/src/orxonox/objects/Trigger.h
r1671 r1693 31 31 32 32 #include <set> 33 #include <queue> 33 34 34 35 #include "OrxonoxPrereqs.h" 35 36 36 37 #include "WorldEntity.h" 37 #include "core/BaseObject.h" 38 #include "core/ClassTreeMask.h" 38 39 39 #include "../tools/BillboardSet.h" 40 40 … … 46 46 TM_EventTriggerOR, 47 47 TM_EventTriggerXOR, 48 TM_EventTriggerNOT49 48 }; 50 49 … … 55 54 ~Trigger(); 56 55 57 bool isTriggered();58 bool isTriggered(TriggerMode mode);56 inline bool isTriggered() { return this->isTriggered(this->mode_); } 57 virtual bool isTriggered(TriggerMode mode); 59 58 void addTrigger(Trigger* trig); 60 void addTargets(std::string targets);61 void removeTargets(std::string targets);62 void setVisibility(int bVisible);59 void setVisibility(bool bVisible); 60 void setDelay(float delay); 61 bool switchState(); 63 62 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 64 63 inline TriggerMode getMode() { return mode_; } 65 64 inline void setMode(TriggerMode mode) { this->mode_ = mode; } 66 65 void tick(float dt); 67 inline void reset(float time) { this->actualTime_ = 0; this->triggingTime_ = time; }68 inline void reset() { reset(triggingTime_); }69 66 70 67 private: 68 void init(); 71 69 bool checkAnd(); 72 70 bool checkOr(); 73 71 bool checkXor(); 74 bool checkNot();72 void setBillboardColour(ColourValue colour); 75 73 76 74 private: 77 75 std::set<Trigger*> children_; 76 std::queue<std::pair<float,char> > stateChanges_; 77 float remainingTime_; 78 float timeSinceLastEvent_; 78 79 TriggerMode mode_; 79 float triggingTime_;80 float actualTime_;81 float radius_;82 80 bool bActive_; 83 ClassTreeMask targetMask_; 81 bool bInvertMode_; 82 bool bTriggered_; 83 bool bUpdating_; 84 84 BillboardSet debugBillboard_; 85 float delay_; 86 char latestState_; 85 87 }; 86 88
Note: See TracChangeset
for help on using the changeset viewer.