Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 7, 2011, 10:19:16 PM (13 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/DistanceTrigger.cc

    r8079 r8206  
    4242namespace orxonox
    4343{
     44   
     45  /*static*/ const std::string DistanceTrigger::beaconModeOff_s = "off";
     46  /*static*/ const std::string DistanceTrigger::beaconModeIdentify_s = "identify";
     47  /*static*/ const std::string DistanceTrigger::beaconModeExlcude_s = "exclude";
     48   
    4449  CreateFactory(DistanceTrigger);
    4550
    46   DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator)
     51  DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator), beaconMask_(NULL)
    4752  {
    4853    RegisterObject(DistanceTrigger);
     
    5156    this->targetMask_.exclude(Class(BaseObject));
    5257    this->targetName_ = "";
    53     this->singleTargetMode_ = false;
    5458  }
    5559
    5660  DistanceTrigger::~DistanceTrigger()
    5761  {
     62    if(this->beaconMask_ != NULL)
     63      delete this->beaconMask_;
    5864  }
    5965
     
    6470    XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f);
    6571    XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn");
     72    XMLPortParam(DistanceTrigger, "beaconMode", setBeaconMode, getBeaconMode, xmlelement, mode);
    6673    XMLPortParam(DistanceTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode);
    6774  }
     
    127134  bool DistanceTrigger::checkDistance()
    128135  {
     136    // Check for new objects that are in range
     137    ClassTreeMask targetMask = this->targetMask_;
     138    if(this->beaconMode_ == distanceTriggerBeaconMode::identify)
     139     targetMask = *this->beaconMask_;
     140   
    129141    // Iterate through all objects
    130     for (ClassTreeMaskObjectIterator it = this->targetMask_.begin(); it != this->targetMask_.end(); ++it)
     142    for (ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it)
    131143    {
    132144      WorldEntity* entity = orxonox_cast<WorldEntity*>(*it);
    133145      if (!entity)
    134146        continue;
    135 
    136       // If the DistanceTrigger is in single-target mode.
    137       if(this->singleTargetMode_)
    138       {
    139         // If the object that is a target is no DistanceTriggerBeacon, then the DistanceTrigger can't be in single-target-mode.
    140         if(!(*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()))
     147     
     148       // If the DistanceTrigger is in identify mode and the DistanceTriggerBeacon attached to the object has the wrong name we ignore it.
     149      if(this->beaconMode_ == distanceTriggerBeaconMode::identify)
     150      {
     151        if(entity->getName() != this->targetName_)
     152            continue;
     153        // If the object, the DistanceTriggerBeacon is attached to, is not a target of this DistanceMultiTrigger.
     154        else if(this->targetMask_.isExcluded(entity->getParent()->getIdentifier()))
     155            continue;
     156      }
     157   
     158      // If the DistanceTrigger is in exclude mode and the DistanceTriggerBeacon attached to the object has the right name, we ignore it.
     159      if(this->beaconMode_ == distanceTriggerBeaconMode::exclude)
     160      {
     161       
     162        const std::set<WorldEntity*> attached = entity->getAttachedObjects();
     163        bool found = false;
     164        for(std::set<WorldEntity*>::const_iterator it = attached.begin(); it != attached.end(); it++)
    141165        {
    142           this->singleTargetMode_ = false;
    143           COUT(2) << "DistanceTrigger " << 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;
     166          if((*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()) && static_cast<DistanceTriggerBeacon*>(*it)->getName() == this->targetName_)
     167          {
     168            found = true;
     169            break;
     170          }
    144171        }
    145         // If the target name and the name of the DistancTriggerBeacon don't match.
    146         else if(entity->getName().compare(this->targetName_) != 0)
     172        if(found)
    147173          continue;
    148174      }
     
    152178      {
    153179
    154         // If the target is a player (resp. is a, or is derived from a, ControllableEntity) the triggeringPlayer is set to the target entity.
     180        // If the target is a player (resp. is a, or is derived from a, Pawn) the triggeringPlayer is set to the target entity.
    155181        if(this->isForPlayer())
    156182        {
    157183
    158           // Change the entity to the parent of the DistanceTriggerBeacon (if in single-target-mode), which is the entity to which the beacon is attached.
    159           if(this->singleTargetMode_)
     184          // Change the entity to the parent of the DistanceTriggerBeacon (if in identify-mode), which is the entity to which the beacon is attached.
     185          if(this->beaconMode_ == distanceTriggerBeaconMode::identify)
    160186            entity = entity->getParent();
    161187
     
    170196    return false;
    171197  }
     198 
     199  /**
     200  @brief
     201    Set the beacon mode.
     202  @param mode
     203    The mode as an enum.
     204  */
     205  void DistanceTrigger::setBeaconModeDirect(distanceTriggerBeaconMode::Value mode)
     206  {
     207    this->beaconMode_ = mode;
     208    if(this->beaconMode_ == distanceTriggerBeaconMode::identify && this->beaconMask_ == NULL)
     209    {
     210      this->beaconMask_ = new ClassTreeMask();
     211      this->beaconMask_->exclude(Class(BaseObject));
     212      this->beaconMask_->include(Class(DistanceTriggerBeacon));
     213    }
     214  }
     215   
     216  /**
     217  @brief
     218    Get the beacon mode.
     219  @return
     220    Returns the mode as a string.
     221  */
     222  const std::string& DistanceTrigger::getBeaconMode(void) const
     223  {
     224    switch(this->getBeaconModeDirect())
     225    {
     226      case distanceTriggerBeaconMode::off :
     227        return DistanceTrigger::beaconModeOff_s;
     228      case distanceTriggerBeaconMode::identify:
     229        return DistanceTrigger::beaconModeIdentify_s;
     230      case distanceTriggerBeaconMode::exclude:
     231        return DistanceTrigger::beaconModeExlcude_s;
     232      default :
     233        assert(0); // This is impossible.
     234        return BLANKSTRING;
     235    }
     236  }
     237   
     238  /**
     239  @brief
     240    Set the beacon mode.
     241  @param mode
     242    The mode as a string.
     243  */
     244  void DistanceTrigger::setBeaconMode(const std::string& mode)
     245  {
     246    if(mode == DistanceTrigger::beaconModeOff_s)
     247      this->setBeaconModeDirect(distanceTriggerBeaconMode::off);
     248    else if(mode == DistanceTrigger::beaconModeIdentify_s)
     249      this->setBeaconModeDirect(distanceTriggerBeaconMode::identify);
     250    else if(mode == DistanceTrigger::beaconModeExlcude_s)
     251      this->setBeaconModeDirect(distanceTriggerBeaconMode::exclude);
     252    else
     253      COUT(1) << "Invalid beacon mode in DistanceTrigger." << endl;
     254  }
    172255
    173256  bool DistanceTrigger::isTriggered(TriggerMode::Value mode)
Note: See TracChangeset for help on using the changeset viewer.