Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 7, 2011, 10:19:16 PM (14 years ago)
Author:
dafrick
Message:

Extending DistanceTrigger (both the normal and the MultiTrigger version). DistanceTriggerBeacons, can now also be used to exclude specific objects from triggering a DistanceTrigger.
Beware: The syntax for the DistanceTrigger, used with a DistanceTriggerBeacon has changed.
It was: <DistanceTrigger target="DistanceTriggerBeacon" targetname="someBeacon" />
And is now: <DistanceTrigger target="WhateverTargetYouWantYourTriggerToReactTo" beaconMode="identify" targetname="someBeacon" />
Consult the documentation in DistanceMultiTrigger for it's specific usage, the DistanceTrigger works analogously.

File:
1 edited

Legend:

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

    r8079 r8206  
    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(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify)
     136            targetMask = *this->beaconMask_;
     137
     138        for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it)
    129139        {
    130140            WorldEntity* entity = static_cast<WorldEntity*>(*it);
    131141
    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()))
     142            // If the DistanceMultiTrigger is in identify mode and the DistanceTriggerBeacon attached to the object has the wrong name we ignore it.
     143            if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify)
     144            {
     145                if(entity->getName() != this->targetName_)
     146                    continue;
     147                // If the object, the DistanceTriggerBeacon is attached to, is not a target of this DistanceMultiTrigger.
     148                else if(this->getTargetMask().isExcluded(entity->getParent()->getIdentifier()))
     149                    continue;
     150            }
     151           
     152            // If the DistanceMultiTrigger is in exclude mode and the DistanceTriggerBeacon attached to the object has the right name, we ignore it.
     153            if(this->beaconMode_ == distanceMultiTriggerBeaconMode::exclude)
     154            {
     155               
     156                const std::set<WorldEntity*> attached = entity->getAttachedObjects();
     157                bool found = false;
     158                for(std::set<WorldEntity*>::const_iterator it = attached.begin(); it != attached.end(); it++)
    137159                {
    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;
     160                    if((*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()) && static_cast<DistanceTriggerBeacon*>(*it)->getName() == this->targetName_)
     161                    {
     162                        found = true;
     163                        break;
     164                    }
    140165                }
    141                 // If the target name and the name of the DistancTriggerBeacon don't match.
    142                 else if(entity->getName().compare(this->targetName_) != 0)
     166                if(found)
    143167                    continue;
    144168            }
     
    153177                    continue;
    154178
    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_)
     179                // Change the entity to the parent of the DistanceTriggerBeacon (if in identify-mode), which is the entity to which the beacon is attached.
     180                if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify)
    157181                    entity = entity->getParent();
    158182
     
    171195        return queue;
    172196    }
    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;
     197   
     198    /**
     199    @brief
     200        Set the beacon mode.
     201    @param mode
     202        The mode as an enum.
     203    */
     204    void DistanceMultiTrigger::setBeaconModeDirect(distanceMultiTriggerBeaconMode::Value mode)
     205    {
     206        this->beaconMode_ = mode;
     207        if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify && this->beaconMask_ == NULL)
     208        {
     209            this->beaconMask_ = new ClassTreeMask();
     210            this->beaconMask_->exclude(Class(BaseObject));
     211            this->beaconMask_->include(Class(DistanceTriggerBeacon));
     212        }
     213    }
     214   
     215    /**
     216    @brief
     217        Get the beacon mode.
     218    @return
     219        Returns the mode as a string.
     220    */
     221    const std::string& DistanceMultiTrigger::getBeaconMode(void) const
     222    {
     223        switch(this->getBeaconModeDirect())
     224        {
     225            case distanceMultiTriggerBeaconMode::off :
     226                return DistanceMultiTrigger::beaconModeOff_s;
     227            case distanceMultiTriggerBeaconMode::identify:
     228                return DistanceMultiTrigger::beaconModeIdentify_s;
     229            case distanceMultiTriggerBeaconMode::exclude:
     230                return DistanceMultiTrigger::beaconModeExlcude_s;
     231            default :
     232                assert(0); // This is impossible.
     233                return BLANKSTRING;
     234        }
     235    }
     236   
     237    /**
     238    @brief
     239        Set the beacon mode.
     240    @param mode
     241        The mode as a string.
     242    */
     243    void DistanceMultiTrigger::setBeaconMode(const std::string& mode)
     244    {
     245        if(mode == DistanceMultiTrigger::beaconModeOff_s)
     246            this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::off);
     247        else if(mode == DistanceMultiTrigger::beaconModeIdentify_s)
     248            this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::identify);
     249        else if(mode == DistanceMultiTrigger::beaconModeExlcude_s)
     250            this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::exclude);
    185251        else
    186             this->singleTargetMode_ = false;
    187 
    188         this->targetName_ = targetname;
     252            COUT(1) << "Invalid beacon mode in DistanceMultiTrigger." << endl;
    189253    }
    190254
Note: See TracChangeset for help on using the changeset viewer.