- Timestamp:
- Apr 7, 2011, 10:19:16 PM (14 years ago)
- Location:
- code/branches/dockingsystem2/src/modules/objects/triggers
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/dockingsystem2/src/modules/objects/triggers/DistanceMultiTrigger.cc
r8079 r8206 43 43 { 44 44 45 /*static*/ const std::string DistanceMultiTrigger::beaconModeOff_s = "off"; 46 /*static*/ const std::string DistanceMultiTrigger::beaconModeIdentify_s = "identify"; 47 /*static*/ const std::string DistanceMultiTrigger::beaconModeExlcude_s = "exclude"; 48 45 49 CreateFactory(DistanceMultiTrigger); 46 50 … … 49 53 Default Constructor. Registers the object and initializes default values. 50 54 */ 51 DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator) 55 DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator), beaconMask_(NULL) 52 56 { 53 57 RegisterObject(DistanceMultiTrigger); 54 58 55 59 this->distance_ = 100.0f; 60 this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::off); 56 61 this->targetName_ = ""; 57 this->singleTargetMode_ = false;58 62 } 59 63 … … 64 68 DistanceMultiTrigger::~DistanceMultiTrigger() 65 69 { 66 70 if(this->beaconMask_ != NULL) 71 delete this->beaconMask_; 67 72 } 68 73 … … 76 81 77 82 XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode); 83 XMLPortParam(DistanceMultiTrigger, "beaconMode", setBeaconMode, getBeaconMode, xmlelement, mode); 78 84 XMLPortParam(DistanceMultiTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode); 79 85 } … … 126 132 127 133 // Check for new objects that are in range 128 for(ClassTreeMaskObjectIterator it = this->getTargetMask().begin(); it != this->getTargetMask().end(); ++it) 134 ClassTreeMask targetMask = this->getTargetMask(); 135 if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify) 136 targetMask = *this->beaconMask_; 137 138 for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it) 129 139 { 130 140 WorldEntity* entity = static_cast<WorldEntity*>(*it); 131 141 132 // If the DistanceMultiTrigger is in single-target mode. 133 if(this->singleTargetMode_) 134 { 135 // If the object that is a target is no DistanceTriggerBeacon, then the DistanceMultiTrigger can't be in single-target mode. 136 if(!entity->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier())) 142 // If the DistanceMultiTrigger is in identify mode and the DistanceTriggerBeacon attached to the object has the wrong name we ignore it. 143 if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify) 144 { 145 if(entity->getName() != this->targetName_) 146 continue; 147 // If the object, the DistanceTriggerBeacon is attached to, is not a target of this DistanceMultiTrigger. 148 else if(this->getTargetMask().isExcluded(entity->getParent()->getIdentifier())) 149 continue; 150 } 151 152 // If the DistanceMultiTrigger is in exclude mode and the DistanceTriggerBeacon attached to the object has the right name, we ignore it. 153 if(this->beaconMode_ == distanceMultiTriggerBeaconMode::exclude) 154 { 155 156 const std::set<WorldEntity*> attached = entity->getAttachedObjects(); 157 bool found = false; 158 for(std::set<WorldEntity*>::const_iterator it = attached.begin(); it != attached.end(); it++) 137 159 { 138 this->singleTargetMode_ = false; 139 COUT(2) << "DistanceMultiTrigger " << 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; 160 if((*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()) && static_cast<DistanceTriggerBeacon*>(*it)->getName() == this->targetName_) 161 { 162 found = true; 163 break; 164 } 140 165 } 141 // If the target name and the name of the DistancTriggerBeacon don't match. 142 else if(entity->getName().compare(this->targetName_) != 0) 166 if(found) 143 167 continue; 144 168 } … … 153 177 continue; 154 178 155 // Change the entity to the parent of the DistanceTriggerBeacon (if in single-target-mode), which is the entity to which the beacon is attached.156 if(this-> singleTargetMode_)179 // Change the entity to the parent of the DistanceTriggerBeacon (if in identify-mode), which is the entity to which the beacon is attached. 180 if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify) 157 181 entity = entity->getParent(); 158 182 … … 171 195 return queue; 172 196 } 173 174 /** 175 @brief 176 Set the target name of DistanceTriggerBeacons that triggers this DistanceMultiTrigger. 177 @param targetname 178 The name of the DistanceTriggerBeacon as a string. 179 */ 180 void DistanceMultiTrigger::setTargetName(const std::string& targetname) 181 { 182 // If the targetname is no blank string single-target mode is enabled. 183 if(targetname != "") 184 this->singleTargetMode_ = true; 197 198 /** 199 @brief 200 Set the beacon mode. 201 @param mode 202 The mode as an enum. 203 */ 204 void DistanceMultiTrigger::setBeaconModeDirect(distanceMultiTriggerBeaconMode::Value mode) 205 { 206 this->beaconMode_ = mode; 207 if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify && this->beaconMask_ == NULL) 208 { 209 this->beaconMask_ = new ClassTreeMask(); 210 this->beaconMask_->exclude(Class(BaseObject)); 211 this->beaconMask_->include(Class(DistanceTriggerBeacon)); 212 } 213 } 214 215 /** 216 @brief 217 Get the beacon mode. 218 @return 219 Returns the mode as a string. 220 */ 221 const std::string& DistanceMultiTrigger::getBeaconMode(void) const 222 { 223 switch(this->getBeaconModeDirect()) 224 { 225 case distanceMultiTriggerBeaconMode::off : 226 return DistanceMultiTrigger::beaconModeOff_s; 227 case distanceMultiTriggerBeaconMode::identify: 228 return DistanceMultiTrigger::beaconModeIdentify_s; 229 case distanceMultiTriggerBeaconMode::exclude: 230 return DistanceMultiTrigger::beaconModeExlcude_s; 231 default : 232 assert(0); // This is impossible. 233 return BLANKSTRING; 234 } 235 } 236 237 /** 238 @brief 239 Set the beacon mode. 240 @param mode 241 The mode as a string. 242 */ 243 void DistanceMultiTrigger::setBeaconMode(const std::string& mode) 244 { 245 if(mode == DistanceMultiTrigger::beaconModeOff_s) 246 this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::off); 247 else if(mode == DistanceMultiTrigger::beaconModeIdentify_s) 248 this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::identify); 249 else if(mode == DistanceMultiTrigger::beaconModeExlcude_s) 250 this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::exclude); 185 251 else 186 this->singleTargetMode_ = false; 187 188 this->targetName_ = targetname; 252 COUT(1) << "Invalid beacon mode in DistanceMultiTrigger." << endl; 189 253 } 190 254 -
code/branches/dockingsystem2/src/modules/objects/triggers/DistanceMultiTrigger.h
r7601 r8206 51 51 /** 52 52 @brief 53 The DistanceMultiTrigger is a MultiTrigger that triggers whenever an object (that is of the specified target type) is in a specified range of the DistanceMultiTrigger. The object can be specified further by adding a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" (by just attaching it) to the objects that can trigger this DistanceMultiTrigger and specify the name of the @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" with the parameter <em>targetname</em> and only objects that have a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" with that name will trigger the DistanceMultiTrigger. 53 Enum for the beacon mode of the DistanceMultiTrigger. 54 55 @ingroup MultiTrigger 56 */ 57 namespace distanceMultiTriggerBeaconMode 58 { 59 enum Value { 60 off, 61 identify, 62 exclude 63 }; 64 } 65 66 /** 67 @brief 68 The DistanceMultiTrigger is a MultiTrigger that triggers whenever an object (that is of the specified target type) is in a specified range of the DistanceMultiTrigger. The object can be specified further by setting the <em>beaconMode</em> and attaching a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" to the object. 54 69 Parameters are (additional to the ones of MultiTrigger): 55 70 - @b distance Which specifies the maximum distance at which the DistanceMultiTrigger still triggers. Default is 100. 56 - @b targetname Which, if not left blank, causes the DistancMultiTrigger to be in <em>single-target</em> mode, meaning, that it only reacts to objects that have a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" (therefore the target has to be set to @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" for it to work), with the name specified by <em>targetname</em>, attached. 71 - @b beaconMode Which specifies, whether the DistanceMultiTrigger operates on @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacons" or not. If <em>off</em> the DistanceMultiTrigger works as usual. If set to <em>identify</em> the DistanceMultiTrigger is only triggered by objects that have a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon", with the same name as specified in <em>targetname</em>, attached to them. If set to <em>exclude</em> the DistanceMultiTrigger is only triggered by objects that don't have a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon", with the same name as specified in <em>targetname</em>, attached to them. Default is <em>off</em>. 72 - @b targetname Which, if not left blank, causes the DistanceMultiTrigger to be in <em>identify</em> beaconMode (unless otherwise specified), meaning, that it only reacts to objects that have a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" with the name specified by <em>targetname</em>, attached. 57 73 58 74 A simple DistanceMultiTrigger would look like this: … … 63 79 An implementation that only reacts to objects with a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" attached would look like this: 64 80 @code 65 <DistanceMultiTrigger position="0,0,0" target=" DistanceMultiTrigger" targetname="beacon1" distance="30" />81 <DistanceMultiTrigger position="0,0,0" target="Pawn" beaconMode="identify" targetname="beacon1" distance="30" /> 66 82 @endcode 67 83 This particular DistanceMultiTrigger would only react if an object was in range, that had a @ref orxonox::DistanceTriggerBeacon "DistanceTriggerBeacon" with the name <em>beacon1</em> attached. … … 83 99 84 100 void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a DistanceMultiTrigger object through XML. 85 86 void setTargetName(const std::string& targetname); //!< Set the target name of DistanceTriggerBeacons that triggers this DistanceMultiTrigger. 87 /** 88 @brief Get the target name of the DistanceTriggerbeacon, that triggers this DistanceMultiTrigger. 89 @return Returns the target name as a string. 90 */ 91 inline const std::string& getTargetName(void) 92 { return this->targetName_; } 93 101 94 102 /** 95 103 @brief Set the distance at which the DistanceMultiTrigger triggers. … … 104 112 inline float getDistance() const 105 113 { return this->distance_; } 114 115 void setBeaconModeDirect(distanceMultiTriggerBeaconMode::Value mode); //!< Set the beacon mode. 116 /** 117 @brief Get the beacon mode. 118 @return Returns the mode as an enum. 119 */ 120 inline distanceMultiTriggerBeaconMode::Value getBeaconModeDirect(void) const 121 { return this->beaconMode_; } 122 void setBeaconMode(const std::string& mode); //!< Set the beacon mode. 123 const std::string& getBeaconMode(void) const; //!< Get the beacon mode. 124 125 /** 126 @brief Set the target name of DistanceTriggerBeacons that triggers this DistanceMultiTrigger. 127 @param targetname The name of the DistanceTriggerBeacon as a string. 128 */ 129 inline void setTargetName(const std::string& targetname) 130 { this->targetName_ = targetname; } 131 /** 132 @brief Get the target name of the DistanceTriggerbeacon, that triggers this DistanceMultiTrigger. 133 @return Returns the target name as a string. 134 */ 135 inline const std::string& getTargetName(void) const 136 { return this->targetName_; } 106 137 107 138 protected: … … 112 143 113 144 private: 145 //! Strings for the beacon modes. 146 static const std::string beaconModeOff_s; 147 static const std::string beaconModeIdentify_s; 148 static const std::string beaconModeExlcude_s; 149 114 150 float distance_; //!< The distance at which the DistanceMultiTrigger triggers. 151 152 distanceMultiTriggerBeaconMode::Value beaconMode_; //!< The beacon mode, the DistanceMultiTrigger is in. 115 153 std::string targetName_; //!< The target name, used in <em>single-target</em> mode. 116 bool singleTargetMode_; //!< To indicate whe the MultiDistanceTrigger is in <em>single-target</em> mode.154 ClassTreeMask* beaconMask_; //!< A mask, that only accepts DistanceTriggerBeacons. 117 155 118 156 std::map<WorldEntity*, WeakPtr<WorldEntity>* > range_; //!< The set of entities that currently are in range of the DistanceMultiTrigger. -
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) -
code/branches/dockingsystem2/src/modules/objects/triggers/DistanceTrigger.h
r8079 r8206 45 45 namespace orxonox 46 46 { 47 48 /** 49 @brief 50 Enum for the beacon mode of the DistanceTrigger. 51 52 @ingroup NormalTrigger 53 */ 54 namespace distanceTriggerBeaconMode 55 { 56 enum Value { 57 off, 58 identify, 59 exclude 60 }; 61 } 47 62 48 63 /** … … 69 84 void removeTargets(const std::string& targets); 70 85 71 inline void setTargetName(const std::string& targetname)72 { if(targetname != "") this->singleTargetMode_ = true; else this->singleTargetMode_ = false; this->targetName_ = targetname; }73 inline const std::string& getTargetName(void)74 { return this->targetName_; }75 76 86 inline void setDistance(float distance) 77 87 { this->distance_ = distance; } 78 88 inline float getDistance() const 79 89 { return this->distance_; } 90 91 void setBeaconModeDirect(distanceTriggerBeaconMode::Value mode); //!< Set the beacon mode. 92 /** 93 @brief Get the beacon mode. 94 @return Returns the mode as an enum. 95 */ 96 inline distanceTriggerBeaconMode::Value getBeaconModeDirect(void) const 97 { return this->beaconMode_; } 98 void setBeaconMode(const std::string& mode); //!< Set the beacon mode. 99 const std::string& getBeaconMode(void) const; //!< Get the beacon mode. 100 101 inline void setTargetName(const std::string& targetname) 102 { this->targetName_ = targetname; } 103 inline const std::string& getTargetName(void) 104 { return this->targetName_; } 80 105 81 106 bool checkDistance(); … … 88 113 89 114 private: 115 //! Strings for the beacon modes. 116 static const std::string beaconModeOff_s; 117 static const std::string beaconModeIdentify_s; 118 static const std::string beaconModeExlcude_s; 119 90 120 std::set<Ogre::Node*> targetSet_; 121 122 distanceTriggerBeaconMode::Value beaconMode_; 91 123 std::string targetName_; 124 ClassTreeMask* beaconMask_; //!< A mask, that only accepts DistanceTriggerBeacons. 125 92 126 float distance_; 93 bool singleTargetMode_;94 127 95 128 }; -
code/branches/dockingsystem2/src/modules/objects/triggers/MultiTrigger.cc
r8193 r8206 312 312 313 313 // We only want WorldEntities 314 //TODO: Really?315 314 ClassTreeMask WEMask; 316 315 WEMask.include(Class(WorldEntity));
Note: See TracChangeset
for help on using the changeset viewer.