Changeset 6857 for code/trunk/src/modules/objects/triggers
- Timestamp:
- May 4, 2010, 10:44:13 PM (15 years ago)
- Location:
- code/trunk/src/modules/objects/triggers
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/objects/triggers/DistanceMultiTrigger.cc
r6807 r6857 27 27 */ 28 28 29 /** 30 @file DistanceMultiTrigger.cc 31 @brief Implementation of the DistanceMultiTrigger class. 32 */ 33 29 34 #include "DistanceMultiTrigger.h" 30 35 … … 36 41 37 42 CreateFactory(DistanceMultiTrigger); 38 43 44 /** 45 @brief 46 Default Constructor. Registers the object and initializes default values. 47 */ 39 48 DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator) 40 49 { … … 43 52 this->distance_ = 100.0f; 44 53 } 45 54 55 /** 56 @brief 57 Destructor. 58 */ 46 59 DistanceMultiTrigger::~DistanceMultiTrigger() 47 60 { 48 61 49 62 } 50 63 64 /** 65 @brief 66 Method for creating a DistanceMultiTrigger object through XML. 67 */ 51 68 void DistanceMultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) 52 69 { 53 70 SUPER(DistanceMultiTrigger, XMLPort, xmlelement, mode); 54 71 55 XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode) .defaultValues(100.0f);72 XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode); 56 73 } 57 74 75 /** 76 @brief 77 This method is called by the MultiTrigger to get information about new trigger events that need to be looked at. 78 79 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. 80 */ 58 81 std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void) 59 82 { 60 ClassTreeMask targetMask = this->getTargetMask();61 83 ClassTreeMask& targetMask = this->getTargetMask(); 84 62 85 std::queue<MultiTriggerState*>* queue = NULL; 86 87 // Check for objects that were in range but no longer are. Iterate through all objects, that are in range. 88 for(std::set<WorldEntity*>::iterator it = this->range_.begin(); it != this->range_.end(); ) 89 { 90 Vector3 distanceVec = (*it)->getWorldPosition() - this->getWorldPosition(); 91 // If the object is no longer in range. 92 if (distanceVec.length() > this->distance_) 93 { 94 WorldEntity* temp = *(it++); 95 if(!this->removeFromRange(temp)) 96 continue; 97 98 // If no queue has been created, yet. 99 if(queue == NULL) 100 queue = new std::queue<MultiTriggerState*>(); 101 102 // Create a state and append it to the queue. 103 MultiTriggerState* state = new MultiTriggerState; 104 state->bTriggered = false; 105 state->originator = temp; 106 queue->push(state); 107 } 108 else 109 ++it; 110 } 111 63 112 // Check for new objects that are in range 64 113 for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it) … … 69 118 70 119 Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); 71 if (distanceVec.length() < this->distance_) 120 // If the object is in range. 121 if (distanceVec.length() <= this->distance_) 72 122 { 123 // Add the object to the objects that are in range. 73 124 if(!this->addToRange(entity)) 74 125 continue; 75 126 127 // If no queue has been created, yet. 76 128 if(queue == NULL) 77 {78 129 queue = new std::queue<MultiTriggerState*>(); 79 } 130 131 // Create a state and append it to the queue. 80 132 MultiTriggerState* state = new MultiTriggerState; 81 133 state->bTriggered = true; … … 85 137 } 86 138 87 for(std::set<WorldEntity*>::iterator it = this->range_.begin(); it != this->range_.end(); )88 {89 Vector3 distanceVec = (*it)->getWorldPosition() - this->getWorldPosition();90 if (distanceVec.length() >= this->distance_)91 {92 WorldEntity* temp = *(it++);93 if(!this->removeFromRange(temp))94 continue;95 96 if(queue == NULL)97 {98 queue = new std::queue<MultiTriggerState*>();99 }100 MultiTriggerState* state = new MultiTriggerState;101 state->bTriggered = false;102 state->originator = temp;103 queue->push(state);104 }105 else106 ++it;107 }108 109 139 return queue; 110 140 } -
code/trunk/src/modules/objects/triggers/DistanceMultiTrigger.h
r6800 r6857 27 27 */ 28 28 29 /** 30 @file DistanceMultiTrigger.h 31 @brief Definition of the DistanceMultiTrigger class. 32 */ 33 29 34 #ifndef _DistanceMultiTrigger_H__ 30 35 #define _DistanceMultiTrigger_H__ … … 40 45 { 41 46 47 /** 48 @brief 49 The DistanceMultiTrigger is a trigger that triggers whenever an object (that is of the specified target type) is in a specified range of the DistanceMultiTrigger. 50 @see MultiTrigger.h 51 For more information on MultiTriggers. 52 @author 53 Damian 'Mozork' Frick 54 */ 42 55 class _ObjectsExport DistanceMultiTrigger : public MultiTrigger 43 56 { 44 57 45 58 public: 46 DistanceMultiTrigger(BaseObject* creator); 47 ~DistanceMultiTrigger(); 59 DistanceMultiTrigger(BaseObject* creator); //!< Default Constructor. Registers the object and initializes default values. 60 ~DistanceMultiTrigger(); //!< Destructor. 48 61 49 void XMLPort(Element& xmlelement, XMLPort::Mode mode); 50 62 void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a DistanceMultiTrigger object through XML. 63 64 /** 65 @brief Set the distance at which the DistanceMultiTrigger triggers. 66 @param distance The distance. 67 */ 51 68 inline void setDistance(float distance) 52 { this->distance_ = distance; } 69 { if(distance >= 0) this->distance_ = distance; } 70 /** 71 @brief Get the distance at which the DistanceMultiTrigger triggers. 72 @return Returns the distance. 73 */ 53 74 inline float getDistance() const 54 75 { return this->distance_; } 55 76 56 77 protected: 57 virtual std::queue<MultiTriggerState*>* letTrigger(void); 58 78 virtual std::queue<MultiTriggerState*>* letTrigger(void); //!< This method is called by the MultiTrigger to get information about new trigger events that need to be looked at. 79 80 /** 81 @brief Check whether a given entity is currently (since the last update) in range of the DistanceMultiTrigger. 82 @param entity A pointer to the entity. 83 @return Returns true if the entity is in the range. 84 */ 59 85 inline bool inRange(WorldEntity* entity) 60 86 { return this->range_.find(entity) != this->range_.end(); } 61 bool addToRange(WorldEntity* entity) 87 /** 88 @brief Add a given entity to the entities, that currently are in range of the DistanceMultiTrigger. 89 @param entity A pointer to the entity. 90 @return Returns true if successful, false if not. 91 */ 92 inline bool addToRange(WorldEntity* entity) 62 93 { std::pair<std::set<WorldEntity*>::iterator, bool> pair = this->range_.insert(entity); return pair.second; } 63 bool removeFromRange(WorldEntity* entity) 94 /** 95 @brief Remove a given entity from the set of entities, that currently are in range of the DistanceMultiTrigger. 96 @param entity A pointer ot the entity. 97 @return Returns true if successful. 98 */ 99 inline bool removeFromRange(WorldEntity* entity) 64 100 { return this->range_.erase(entity) > 0; } 65 101 66 102 private: 67 float distance_; 68 std::set<WorldEntity*> range_; 103 float distance_; //!< The distance at which the DistanceMultiTrigger triggers. 104 std::set<WorldEntity*> range_; //!< The set of entities that currently are in range of the DistanceMultiTrigger. 69 105 70 106 }; -
code/trunk/src/modules/objects/triggers/MultiTrigger.cc
r6855 r6857 108 108 XMLPortParam(MultiTrigger, "invert", setInvert, getInvert, xmlelement, mode); 109 109 XMLPortParamTemplate(MultiTrigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&); 110 XMLPortParamLoadOnly(MultiTrigger, "target", addTargets, xmlelement, mode).defaultValues(" ControllableEntity"); //TODO: Remove load only110 XMLPortParamLoadOnly(MultiTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn"); //TODO: Remove load only 111 111 112 112 //TODO: Maybe nicer with explicit subgroup, e.g. triggers -
code/trunk/src/modules/objects/triggers/MultiTrigger.h
r6855 r6857 82 82 'simultaniousTriggerers': The number of simultanious triggerers limits the number of object that are allowed to trigger the MultiTrigger at the same time. Or a little more precisely, the number of distinct objects the MultiTrigger has 'triggered' states for, at each point in time. 83 83 'mode': The mode describes how the MultiTrigger acts in relation to all the MultiTriggers, that are appended to it. There are 3 modes: 'and', meaning that the MultiTrigger can only be triggered if all the appended MultiTriggers are active. 'or', meaning that the MultiTrigger can only triggered if at least one of the appendend MultiTriggers is active. And 'xor', meaning that the MultiTrigger can only be triggered if one and only one appended MultiTrigger is active. Notice, that I wrote 'can only be active', that implies, that there is an addtitional condition to the activity of the MultiTrigger and that is the fulfillment of the triggering condition (the MultiTrigger itself doesn't have one, but all derived classes should). Also bear in mind, that the activity of a MultiTrigger is still coupled to the object that triggered it. The default is 'and'. 84 'target': The target describes the kind of objects that are allowed to trigger this MultiTrigger. The default is ' ControllableEntity'.84 'target': The target describes the kind of objects that are allowed to trigger this MultiTrigger. The default is 'Pawn'. 85 85 Also there is the possibility of appending MultiTriggers to the MultiTrigger just by adding them as subobjects in the XML description of your MultiTrigger. 86 86
Note: See TracChangeset
for help on using the changeset viewer.