- Timestamp:
- Apr 7, 2011, 10:19:16 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/dockingsystem2/src/modules/objects/triggers/DistanceTrigger.cc
r8079 r8206 42 42 namespace orxonox 43 43 { 44 45 /*static*/ const std::string DistanceTrigger::beaconModeOff_s = "off"; 46 /*static*/ const std::string DistanceTrigger::beaconModeIdentify_s = "identify"; 47 /*static*/ const std::string DistanceTrigger::beaconModeExlcude_s = "exclude"; 48 44 49 CreateFactory(DistanceTrigger); 45 50 46 DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator) 51 DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator), beaconMask_(NULL) 47 52 { 48 53 RegisterObject(DistanceTrigger); … … 51 56 this->targetMask_.exclude(Class(BaseObject)); 52 57 this->targetName_ = ""; 53 this->singleTargetMode_ = false;54 58 } 55 59 56 60 DistanceTrigger::~DistanceTrigger() 57 61 { 62 if(this->beaconMask_ != NULL) 63 delete this->beaconMask_; 58 64 } 59 65 … … 64 70 XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f); 65 71 XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn"); 72 XMLPortParam(DistanceTrigger, "beaconMode", setBeaconMode, getBeaconMode, xmlelement, mode); 66 73 XMLPortParam(DistanceTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode); 67 74 } … … 127 134 bool DistanceTrigger::checkDistance() 128 135 { 136 // Check for new objects that are in range 137 ClassTreeMask targetMask = this->targetMask_; 138 if(this->beaconMode_ == distanceTriggerBeaconMode::identify) 139 targetMask = *this->beaconMask_; 140 129 141 // Iterate through all objects 130 for (ClassTreeMaskObjectIterator it = t his->targetMask_.begin(); it != this->targetMask_.end(); ++it)142 for (ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it) 131 143 { 132 144 WorldEntity* entity = orxonox_cast<WorldEntity*>(*it); 133 145 if (!entity) 134 146 continue; 135 136 // If the DistanceTrigger is in single-target mode. 137 if(this->singleTargetMode_) 138 { 139 // If the object that is a target is no DistanceTriggerBeacon, then the DistanceTrigger can't be in single-target-mode. 140 if(!(*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier())) 147 148 // If the DistanceTrigger is in identify mode and the DistanceTriggerBeacon attached to the object has the wrong name we ignore it. 149 if(this->beaconMode_ == distanceTriggerBeaconMode::identify) 150 { 151 if(entity->getName() != this->targetName_) 152 continue; 153 // If the object, the DistanceTriggerBeacon is attached to, is not a target of this DistanceMultiTrigger. 154 else if(this->targetMask_.isExcluded(entity->getParent()->getIdentifier())) 155 continue; 156 } 157 158 // If the DistanceTrigger is in exclude mode and the DistanceTriggerBeacon attached to the object has the right name, we ignore it. 159 if(this->beaconMode_ == distanceTriggerBeaconMode::exclude) 160 { 161 162 const std::set<WorldEntity*> attached = entity->getAttachedObjects(); 163 bool found = false; 164 for(std::set<WorldEntity*>::const_iterator it = attached.begin(); it != attached.end(); it++) 141 165 { 142 this->singleTargetMode_ = false; 143 COUT(2) << "DistanceTrigger " << this->getName() << " (&" << this << ")" << "is in single-target mode but the target is '" << entity->getIdentifier()->getName() << "' instead of DistanceTriggerBeacon. Setting single-target mode to false." << std::endl; 166 if((*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()) && static_cast<DistanceTriggerBeacon*>(*it)->getName() == this->targetName_) 167 { 168 found = true; 169 break; 170 } 144 171 } 145 // If the target name and the name of the DistancTriggerBeacon don't match. 146 else if(entity->getName().compare(this->targetName_) != 0) 172 if(found) 147 173 continue; 148 174 } … … 152 178 { 153 179 154 // If the target is a player (resp. is a, or is derived from a, ControllableEntity) the triggeringPlayer is set to the target entity.180 // If the target is a player (resp. is a, or is derived from a, Pawn) the triggeringPlayer is set to the target entity. 155 181 if(this->isForPlayer()) 156 182 { 157 183 158 // Change the entity to the parent of the DistanceTriggerBeacon (if in single-target-mode), which is the entity to which the beacon is attached.159 if(this-> singleTargetMode_)184 // Change the entity to the parent of the DistanceTriggerBeacon (if in identify-mode), which is the entity to which the beacon is attached. 185 if(this->beaconMode_ == distanceTriggerBeaconMode::identify) 160 186 entity = entity->getParent(); 161 187 … … 170 196 return false; 171 197 } 198 199 /** 200 @brief 201 Set the beacon mode. 202 @param mode 203 The mode as an enum. 204 */ 205 void DistanceTrigger::setBeaconModeDirect(distanceTriggerBeaconMode::Value mode) 206 { 207 this->beaconMode_ = mode; 208 if(this->beaconMode_ == distanceTriggerBeaconMode::identify && this->beaconMask_ == NULL) 209 { 210 this->beaconMask_ = new ClassTreeMask(); 211 this->beaconMask_->exclude(Class(BaseObject)); 212 this->beaconMask_->include(Class(DistanceTriggerBeacon)); 213 } 214 } 215 216 /** 217 @brief 218 Get the beacon mode. 219 @return 220 Returns the mode as a string. 221 */ 222 const std::string& DistanceTrigger::getBeaconMode(void) const 223 { 224 switch(this->getBeaconModeDirect()) 225 { 226 case distanceTriggerBeaconMode::off : 227 return DistanceTrigger::beaconModeOff_s; 228 case distanceTriggerBeaconMode::identify: 229 return DistanceTrigger::beaconModeIdentify_s; 230 case distanceTriggerBeaconMode::exclude: 231 return DistanceTrigger::beaconModeExlcude_s; 232 default : 233 assert(0); // This is impossible. 234 return BLANKSTRING; 235 } 236 } 237 238 /** 239 @brief 240 Set the beacon mode. 241 @param mode 242 The mode as a string. 243 */ 244 void DistanceTrigger::setBeaconMode(const std::string& mode) 245 { 246 if(mode == DistanceTrigger::beaconModeOff_s) 247 this->setBeaconModeDirect(distanceTriggerBeaconMode::off); 248 else if(mode == DistanceTrigger::beaconModeIdentify_s) 249 this->setBeaconModeDirect(distanceTriggerBeaconMode::identify); 250 else if(mode == DistanceTrigger::beaconModeExlcude_s) 251 this->setBeaconModeDirect(distanceTriggerBeaconMode::exclude); 252 else 253 COUT(1) << "Invalid beacon mode in DistanceTrigger." << endl; 254 } 172 255 173 256 bool DistanceTrigger::isTriggered(TriggerMode::Value mode)
Note: See TracChangeset
for help on using the changeset viewer.