Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 31, 2010, 11:31:37 PM (14 years ago)
Author:
dafrick
Message:

Improving documentation for MultiTriggers, also some small bugfixes, simplifications and added features.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/objects/triggers/DistanceMultiTrigger.cc

    r7163 r7301  
    6262    DistanceMultiTrigger::~DistanceMultiTrigger()
    6363    {
     64
    6465    }
    6566
     
    8182
    8283        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.
    8386    */
    8487    std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void)
     
    9295            WorldEntity* entity = it->second->get();
    9396            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.
    9499            if(entity == NULL)
    95100            {
    96                 ++it;
    97101                this->removeFromRange(key);
    98102                continue;
     
    103107            if (distanceVec.length() > this->distance_)
    104108            {
     109                // If for some reason the entity could not be removed.
    105110                if(!this->removeFromRange(key))
    106                 {
    107                     ++it;
    108111                    continue;
    109                 }
    110112
    111113                // If no queue has been created, yet.
     
    115117                // Create a state and append it to the queue.
    116118                MultiTriggerState* state = new MultiTriggerState;
    117                 state->bTriggered = false;
     119                state->bTriggered = false; // Because the entity went out of range.
    118120                state->originator = entity;
    119121                queue->push(state);
    120122            }
    121             else
    122                 ++it;
    123123        }
    124124
    125         ClassTreeMask& targetMask = this->getTargetMask();
    126 
    127125        // Check for new objects that are in range
    128         for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it)
     126        for(ClassTreeMaskObjectIterator it = this->getTargetMask().begin(); it != this->getTargetMask().end(); ++it)
    129127        {
    130128            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.
    135131            if(this->singleTargetMode_)
    136132            {
    137133                // 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                {
    139136                    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.
    141140                else if(entity->getName().compare(this->targetName_) != 0)
    142141                    continue;
     
    148147            {
    149148                // 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.
    150150                if(!this->addToRange(entity))
    151151                    continue;
     
    161161                // Create a state and append it to the queue.
    162162                MultiTriggerState* state = new MultiTriggerState;
    163                 state->bTriggered = true;
     163                state->bTriggered = true; // Because the entity came into range.
    164164                state->originator = entity;
    165165                queue->push(state);
     
    170170    }
    171171
     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
    172228}
Note: See TracChangeset for help on using the changeset viewer.