- Timestamp:
- Aug 31, 2010, 11:31:37 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/objects/triggers/DistanceMultiTrigger.cc
r7163 r7301 62 62 DistanceMultiTrigger::~DistanceMultiTrigger() 63 63 { 64 64 65 } 65 66 … … 81 82 82 83 In this implementation we iterate through all possible objects and check whether the fact that they are in range or not has changed and fire and hand a state ofer to the MultiTrigger if so. 84 @return 85 Returns a pointer to a queue of MultiTriggerState pointers, containing all the necessary information to decide whether these states should indeed become new states of the MultiTrigger. 83 86 */ 84 87 std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void) … … 92 95 WorldEntity* entity = it->second->get(); 93 96 WorldEntity* key = it->first; 97 it++; // Incrementing the iterator in advance, since we don't need the current anymore and we potentially are going to delete the current element thus invalidating the iterator. 98 // If the entity no longer exists. 94 99 if(entity == NULL) 95 100 { 96 ++it;97 101 this->removeFromRange(key); 98 102 continue; … … 103 107 if (distanceVec.length() > this->distance_) 104 108 { 109 // If for some reason the entity could not be removed. 105 110 if(!this->removeFromRange(key)) 106 {107 ++it;108 111 continue; 109 }110 112 111 113 // If no queue has been created, yet. … … 115 117 // Create a state and append it to the queue. 116 118 MultiTriggerState* state = new MultiTriggerState; 117 state->bTriggered = false; 119 state->bTriggered = false; // Because the entity went out of range. 118 120 state->originator = entity; 119 121 queue->push(state); 120 122 } 121 else122 ++it;123 123 } 124 124 125 ClassTreeMask& targetMask = this->getTargetMask();126 127 125 // Check for new objects that are in range 128 for(ClassTreeMaskObjectIterator it = t argetMask.begin(); it != targetMask.end(); ++it)126 for(ClassTreeMaskObjectIterator it = this->getTargetMask().begin(); it != this->getTargetMask().end(); ++it) 129 127 { 130 128 WorldEntity* entity = static_cast<WorldEntity*>(*it); 131 if (entity == NULL) //If the object is no WorldEntity or is already in range. 132 continue; 133 134 // If the DistanceMultiTrigger is in single-target-mode. 129 130 // If the DistanceMultiTrigger is in single-target mode. 135 131 if(this->singleTargetMode_) 136 132 { 137 133 // If the object that is a target is no DistanceTriggerBeacon, then the DistanceMultiTrigger can't be in single-target-mode. 138 if(!(*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier())) 134 if(!entity->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier())) 135 { 139 136 this->singleTargetMode_ = false; 140 // If the target name and the name of the DistancTriggreBeacon don't match. 137 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; 138 } 139 // If the target name and the name of the DistancTriggerBeacon don't match. 141 140 else if(entity->getName().compare(this->targetName_) != 0) 142 141 continue; … … 148 147 { 149 148 // Add the object to the objects that are in range. 149 // Objects that already are in range are not added twice, because in a map (this->range_) each key can only exist once and thus addToRange() will reject all attempts of duplicate entries. 150 150 if(!this->addToRange(entity)) 151 151 continue; … … 161 161 // Create a state and append it to the queue. 162 162 MultiTriggerState* state = new MultiTriggerState; 163 state->bTriggered = true; 163 state->bTriggered = true; // Because the entity came into range. 164 164 state->originator = entity; 165 165 queue->push(state); … … 170 170 } 171 171 172 /** 173 @brief 174 Set the target name of DistanceTriggerBeacons that triggers this DistanceMultiTrigger. 175 @param targetname 176 The name of the DistanceTriggerBeacon as a string. 177 */ 178 void DistanceMultiTrigger::setTargetName(const std::string& targetname) 179 { 180 // If the targetname is no blank string single-target mode is enabled. 181 if(targetname.compare(BLANKSTRING) != 0) 182 this->singleTargetMode_ = true; 183 else 184 this->singleTargetMode_ = false; 185 186 this->targetName_ = targetname; 187 } 188 189 /** 190 @brief 191 Add a given entity to the entities, that currently are in range of the DistanceMultiTrigger. 192 @param entity 193 A pointer to the entity. 194 @return 195 Returns true if successful, false if not. 196 */ 197 bool DistanceMultiTrigger::addToRange(WorldEntity* entity) 198 { 199 WeakPtr<WorldEntity>* weakptr = new WeakPtr<WorldEntity>(entity); 200 std::pair<std::map<WorldEntity*, WeakPtr<WorldEntity>* >::iterator, bool> pair = this->range_.insert(std::pair<WorldEntity*, WeakPtr<WorldEntity>* >(entity, weakptr)); 201 202 if(!pair.second) 203 { 204 delete weakptr; 205 return false; 206 } 207 208 return true; 209 } 210 211 /** 212 @brief 213 Remove a given entity from the set of entities, that currently are in range of the DistanceMultiTrigger. 214 @param entity 215 A pointer ot the entity. 216 @return 217 Returns true if successful. 218 */ 219 bool DistanceMultiTrigger::removeFromRange(WorldEntity* entity) 220 { 221 WeakPtr<WorldEntity>* weakptr = this->range_.find(entity)->second; 222 bool erased = this->range_.erase(entity) > 0; 223 if(erased) 224 delete weakptr; 225 return erased; 226 } 227 172 228 }
Note: See TracChangeset
for help on using the changeset viewer.