Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 9, 2011, 3:33:06 PM (13 years ago)
Author:
dafrick
Message:

Adding changes made to DistanceTrigger also in trunk.
Also documenting trigger.

File:
1 edited

Legend:

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

    r8079 r8213  
    4343{
    4444
     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   
    4549    CreateFactory(DistanceMultiTrigger);
    4650
     
    4953        Default Constructor. Registers the object and initializes default values.
    5054    */
    51     DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator)
     55    DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator), beaconMask_(NULL)
    5256    {
    5357        RegisterObject(DistanceMultiTrigger);
    5458
    5559        this->distance_ = 100.0f;
     60        this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::off);
    5661        this->targetName_ = "";
    57         this->singleTargetMode_ = false;
    5862    }
    5963
     
    6468    DistanceMultiTrigger::~DistanceMultiTrigger()
    6569    {
    66 
     70        if(this->beaconMask_ != NULL)
     71            delete this->beaconMask_;
    6772    }
    6873
     
    7681
    7782        XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode);
     83        XMLPortParam(DistanceMultiTrigger, "beaconMode", setBeaconMode, getBeaconMode, xmlelement, mode);
    7884        XMLPortParam(DistanceMultiTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode);
    7985    }
     
    126132
    127133        // 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 we are in identify-mode another target mask has to be applies to find the DistanceTriggerBeacons.
     136        if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify)
     137            targetMask = *this->beaconMask_;
     138
     139        // Iterate through all objects that are targets of the DistanceMultiTrigger.
     140        for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it)
    129141        {
    130142            WorldEntity* entity = static_cast<WorldEntity*>(*it);
    131143
    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()))
     144            // If the DistanceMultiTrigger is in identify-mode and the DistanceTriggerBeacon attached to the object has the wrong name we ignore it.
     145            if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify)
     146            {
     147                if(entity->getName() != this->targetName_)
     148                    continue;
     149                // If the object, the DistanceTriggerBeacon is attached to, is not a target of this DistanceMultiTrigger.
     150                else if(this->getTargetMask().isExcluded(entity->getParent()->getIdentifier()))
     151                    continue;
     152            }
     153           
     154            // If the DistanceMultiTrigger is in exclude mode and the DistanceTriggerBeacon attached to the object has the right name, we ignore it.
     155            if(this->beaconMode_ == distanceMultiTriggerBeaconMode::exclude)
     156            {
     157               
     158                const std::set<WorldEntity*> attached = entity->getAttachedObjects();
     159                bool found = false;
     160                for(std::set<WorldEntity*>::const_iterator it = attached.begin(); it != attached.end(); it++)
    137161                {
    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;
     162                    if((*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()) && static_cast<DistanceTriggerBeacon*>(*it)->getName() == this->targetName_)
     163                    {
     164                        found = true;
     165                        break;
     166                    }
    140167                }
    141                 // If the target name and the name of the DistancTriggerBeacon don't match.
    142                 else if(entity->getName().compare(this->targetName_) != 0)
     168                if(found)
    143169                    continue;
    144170            }
     
    153179                    continue;
    154180
    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_)
     181                // Change the entity to the parent of the DistanceTriggerBeacon (if in identify-mode), which is the entity to which the beacon is attached.
     182                if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify)
    157183                    entity = entity->getParent();
    158184
     
    171197        return queue;
    172198    }
    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;
     199   
     200    /**
     201    @brief
     202        Set the beacon mode.
     203    @param mode
     204        The mode as an enum.
     205    */
     206    void DistanceMultiTrigger::setBeaconModeDirect(distanceMultiTriggerBeaconMode::Value mode)
     207    {
     208        this->beaconMode_ = mode;
     209        if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify && this->beaconMask_ == NULL)
     210        {
     211            this->beaconMask_ = new ClassTreeMask();
     212            this->beaconMask_->exclude(Class(BaseObject));
     213            this->beaconMask_->include(Class(DistanceTriggerBeacon));
     214        }
     215    }
     216   
     217    /**
     218    @brief
     219        Get the beacon mode.
     220    @return
     221        Returns the mode as a string.
     222    */
     223    const std::string& DistanceMultiTrigger::getBeaconMode(void) const
     224    {
     225        switch(this->getBeaconModeDirect())
     226        {
     227            case distanceMultiTriggerBeaconMode::off :
     228                return DistanceMultiTrigger::beaconModeOff_s;
     229            case distanceMultiTriggerBeaconMode::identify:
     230                return DistanceMultiTrigger::beaconModeIdentify_s;
     231            case distanceMultiTriggerBeaconMode::exclude:
     232                return DistanceMultiTrigger::beaconModeExlcude_s;
     233            default :
     234                assert(0); // This is impossible.
     235                return BLANKSTRING;
     236        }
     237    }
     238   
     239    /**
     240    @brief
     241        Set the beacon mode.
     242    @param mode
     243        The mode as a string.
     244    */
     245    void DistanceMultiTrigger::setBeaconMode(const std::string& mode)
     246    {
     247        if(mode == DistanceMultiTrigger::beaconModeOff_s)
     248            this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::off);
     249        else if(mode == DistanceMultiTrigger::beaconModeIdentify_s)
     250            this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::identify);
     251        else if(mode == DistanceMultiTrigger::beaconModeExlcude_s)
     252            this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::exclude);
    185253        else
    186             this->singleTargetMode_ = false;
    187 
    188         this->targetName_ = targetname;
     254            COUT(1) << "Invalid beacon mode in DistanceMultiTrigger." << endl;
    189255    }
    190256
Note: See TracChangeset for help on using the changeset viewer.