Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 4, 2010, 10:44:13 PM (15 years ago)
Author:
dafrick
Message:

Documented and simplified DistanceMultiTrigger.

Location:
code/trunk/src/modules/objects/triggers
Files:
4 edited

Legend:

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

    r6807 r6857  
    2727*/
    2828
     29/**
     30    @file DistanceMultiTrigger.cc
     31    @brief Implementation of the DistanceMultiTrigger class.
     32*/
     33
    2934#include "DistanceMultiTrigger.h"
    3035
     
    3641   
    3742    CreateFactory(DistanceMultiTrigger);
    38    
     43
     44    /**
     45    @brief
     46        Default Constructor. Registers the object and initializes default values.
     47    */
    3948    DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator)
    4049    {
     
    4352        this->distance_ = 100.0f;
    4453    }
    45    
     54
     55    /**
     56    @brief
     57        Destructor.
     58    */
    4659    DistanceMultiTrigger::~DistanceMultiTrigger()
    4760    {
    4861       
    4962    }
    50    
     63
     64    /**
     65    @brief
     66        Method for creating a DistanceMultiTrigger object through XML.
     67    */
    5168    void DistanceMultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    5269    {
    5370        SUPER(DistanceMultiTrigger, XMLPort, xmlelement, mode);
    5471
    55         XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f);
     72        XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode);
    5673    }
    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    */
    5881    std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void)
    5982    {
    60         ClassTreeMask targetMask = this->getTargetMask();
    61        
     83        ClassTreeMask& targetMask = this->getTargetMask();
     84
    6285        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
    63112        // Check for new objects that are in range
    64113        for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it)
     
    69118
    70119            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_)
    72122            {
     123                // Add the object to the objects that are in range.
    73124                if(!this->addToRange(entity))
    74125                    continue;
    75                
     126
     127                // If no queue has been created, yet.
    76128                if(queue == NULL)
    77                 {
    78129                    queue = new std::queue<MultiTriggerState*>();
    79                 }
     130
     131                // Create a state and append it to the queue.
    80132                MultiTriggerState* state = new MultiTriggerState;
    81133                state->bTriggered = true;
     
    85137        }
    86138       
    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             else
    106                 ++it;
    107         }
    108        
    109139        return queue;
    110140    }
  • code/trunk/src/modules/objects/triggers/DistanceMultiTrigger.h

    r6800 r6857  
    2727*/
    2828
     29/**
     30    @file DistanceMultiTrigger.h
     31    @brief Definition of the DistanceMultiTrigger class.
     32*/
     33
    2934#ifndef _DistanceMultiTrigger_H__
    3035#define _DistanceMultiTrigger_H__
     
    4045{
    4146
     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    */
    4255    class _ObjectsExport DistanceMultiTrigger : public MultiTrigger
    4356    {
    4457       
    4558        public:
    46             DistanceMultiTrigger(BaseObject* creator);
    47             ~DistanceMultiTrigger();
     59            DistanceMultiTrigger(BaseObject* creator); //!< Default Constructor. Registers the object and initializes default values.
     60            ~DistanceMultiTrigger(); //!< Destructor.
    4861           
    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            */
    5168            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            */
    5374            inline float getDistance() const
    5475                { return this->distance_; }
    5576               
    5677        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            */
    5985            inline bool inRange(WorldEntity* entity)
    6086                { 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)
    6293                { 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)
    64100                { return this->range_.erase(entity) > 0; }
    65101               
    66102        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.
    69105       
    70106    };
  • code/trunk/src/modules/objects/triggers/MultiTrigger.cc

    r6855 r6857  
    108108        XMLPortParam(MultiTrigger, "invert", setInvert, getInvert, xmlelement, mode);
    109109        XMLPortParamTemplate(MultiTrigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&);
    110         XMLPortParamLoadOnly(MultiTrigger, "target", addTargets, xmlelement, mode).defaultValues("ControllableEntity"); //TODO: Remove load only
     110        XMLPortParamLoadOnly(MultiTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn"); //TODO: Remove load only
    111111
    112112        //TODO: Maybe nicer with explicit subgroup, e.g. triggers
  • code/trunk/src/modules/objects/triggers/MultiTrigger.h

    r6855 r6857  
    8282            '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.
    8383            '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'.
    8585            Also there is the possibility of appending MultiTriggers to the MultiTrigger just by adding them as subobjects in the XML description of your MultiTrigger.
    8686
Note: See TracChangeset for help on using the changeset viewer.