Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2029 for code


Ignore:
Timestamp:
Oct 28, 2008, 8:37:09 PM (16 years ago)
Author:
landauf
Message:

some changes in Trigger and DistanceTrigger

Location:
code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/DistanceTrigger.cc

    r2027 r2029  
    4141    RegisterObject(DistanceTrigger);
    4242
    43     targetMask_.exclude(Class(BaseObject));
     43    this->distance_ = 100;
     44    this->targetMask_.exclude(Class(BaseObject));
    4445  }
    4546
    4647  DistanceTrigger::~DistanceTrigger()
    4748  {
     49  }
     50
     51  void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     52  {
     53    SUPER(DistanceTrigger, XMLPort, xmlelement, mode);
     54
     55    XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f);
     56    XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode);
    4857  }
    4958
     
    6170      COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl;
    6271      std::set<Ogre::Node*>::iterator it;
    63       for(it = this->targetSet_.begin(); it != this->targetSet_.end(); it++)
     72      for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it)
    6473      {
    6574        COUT(4) << *it << std::endl;
     
    8796  {
    8897    // Iterate through all WorldEntities
    89     for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; it++)
     98    for (Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; ++it)
    9099    {
    91100      // check if WorldEntity is a target
    92       if(targetMask_.isIncluded(it->getIdentifier()))
     101      if (targetMask_.isIncluded(it->getIdentifier()))
    93102      {
    94103        Vector3 distanceVec = it->getNode()->getWorldPosition() - this->getNode()->getWorldPosition();
    95104        if (distanceVec.length() < this->distance_)
    96         {
    97105          return true;
    98         }
    99106      }
    100107    }
     
    103110  }
    104111
    105   void DistanceTrigger::setDistance(float dist)
    106   {
    107     this->distance_ = dist;
    108   }
    109 
    110   void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    111   {
    112     Trigger::XMLPort(xmlelement, mode);
    113 
    114     XMLPortParamLoadOnly(DistanceTrigger, "distance", setDistance, xmlelement, mode);
    115     XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode);
    116   }
    117 
    118112  bool DistanceTrigger::isTriggered(TriggerMode mode)
    119113  {
    120     if(Trigger::isTriggered(mode))
     114    if (Trigger::isTriggered(mode))
    121115      return checkDistance();
    122116    else
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/DistanceTrigger.h

    r2027 r2029  
    4646      DistanceTrigger(BaseObject* creator);
    4747      ~DistanceTrigger();
     48
     49      virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     50
    4851      void addTarget(Ogre::Node* targetNode);
     52      void addTargets(const std::string& targets);
    4953      void removeTarget(Ogre::Node* targetNode);
    50       void addTargets(const std::string& targets);
    5154      void removeTargets(const std::string& targets);
    52       void setDistance(float dist);
    53       virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     55
     56      inline void setDistance(float distance)
     57        { this->distance_ = distance; }
     58      inline float getDistance() const
     59        { return this->distance_; }
     60
    5461      bool checkDistance();
    5562
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/Trigger.cc

    r2027 r2029  
    4848    RegisterObject(Trigger);
    4949
    50     mode_ = TM_EventTriggerAND;
    51     bActive_ = false;
    52     bInvertMode_ = false;
    53     delay_ = 0.0;
    54     bTriggered_ = false;
    55     bUpdating_ = false;
    56     remainingActivations_ = -1;
    57     bStayOn_ = false;
    58     latestState_ = 0x0;
     50    this->mode_ = TM_EventTriggerAND;
     51
     52    this->bActive_ = false;
     53    this->bTriggered_ = false;
     54    this->latestState_ = 0x0;
     55
     56    this->bInvertMode_ = false;
     57    this->bSwitch_ = false;
     58    this->bStayActive_ = false;
     59    this->delay_ = 0.0;
     60    this->remainingActivations_ = -1;
     61
     62//    this->bUpdating_ = false;
    5963
    6064    if (this->getScene() && this->getScene()->getSceneManager())
    6165    {
    62       debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
    63       debugBillboard_.setVisible(false);
    64     }
    65 
    66     this->getNode()->attachObject(debugBillboard_.getBillboardSet());
     66      this->debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
     67      this->debugBillboard_.setVisible(false);
     68    }
     69
     70    this->getNode()->attachObject(this->debugBillboard_.getBillboardSet());
    6771  }
    6872
     
    7175  }
    7276
    73   void Trigger::init()
    74   {
    75     timeSinceLastEvent_ = delay_;
    76   }
    77 
    78   void Trigger::debugFlares(bool bVisible)
    79   {
    80     for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
    81       it->setVisible(bVisible);
    82   }
    83 
    84   void Trigger::changedVisibility()
    85   {
    86     debugBillboard_.setVisible(this->isVisible());
    87   }
    88 
    89   const Trigger* Trigger::getTrigger(unsigned int index) const
    90   {
    91     if (children_.size() <= index)
    92       return NULL;
    93 
    94     std::set<Trigger*>::const_iterator it;
    95     it = children_.begin();
    96 
    97     for ( unsigned int i = 0; i != index; i++ )
    98     {
    99       it++;
    100     }
    101     return *it;
     77  void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     78  {
     79    SUPER(Trigger, XMLPort, xmlelement, mode);
     80
     81    XMLPortParam(Trigger, "delay",       setDelay,       getDelay,       xmlelement, mode).defaultValues(0.0f);
     82    XMLPortParam(Trigger, "switch",      setSwitch,      getSwitch,      xmlelement, mode).defaultValues(false);
     83    XMLPortParam(Trigger, "stayactive",  setStayActive,  getStayActive,  xmlelement, mode).defaultValues(false);
     84    XMLPortParam(Trigger, "activations", setActivations, getActivations, xmlelement, mode).defaultValues(-1);
     85    XMLPortParam(Trigger, "invert",      setInvert,      getInvert,      xmlelement, mode).defaultValues(false);
     86    XMLPortParamTemplate(Trigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&).defaultValues("or");
     87
     88    XMLPortObject(Trigger, Trigger, "", addTrigger, getTrigger, xmlelement, mode);
    10289  }
    10390
     
    10592  {
    10693
    107     bool newTriggered;
    108     newTriggered = this->isTriggered();
     94    bool newTriggered = this->isTriggered();
    10995
    11096    // check if new triggering event is really new
    111     if((this->latestState_ & 0x1) != newTriggered)
     97    if ((this->latestState_ & 0x1) != newTriggered)
    11298    {
    11399      // create new state
    114       if(newTriggered)
     100      if (newTriggered)
    115101      {
    116         latestState_ |= 1; // set trigger bit
     102        this->latestState_ |= 1; // set trigger bit to 1
    117103        this->switchState();
    118104      }
    119105      else
    120106      {
    121         latestState_ &= 0xFE; // set trigger bit
    122         if (this->bStayOn_)
     107        this->latestState_ &= 0xFE; // set trigger bit to 0
     108        if (this->bSwitch_)
    123109          this->storeState();
    124110        else
     
    127113    }
    128114
    129     if(remainingTime_ > 0.0)
    130     {
    131       remainingTime_ -= dt;
     115    if (this->remainingTime_ > 0.0)
     116    {
     117      this->remainingTime_ -= dt;
    132118      // only increase when acctually waiting for a state in the queue
    133       if(timeSinceLastEvent_ >= 0.0)
    134         timeSinceLastEvent_ += dt;
    135     }
    136 
    137     while(remainingTime_ <= 0.0 && stateChanges_.size() > 0)
     119      if (this->timeSinceLastEvent_ >= 0.0)
     120        this->timeSinceLastEvent_ += dt;
     121    }
     122
     123    while (this->remainingTime_ <= 0.0 && this->stateChanges_.size() > 0)
    138124    {
    139125      // time ran out, change state to new one
    140       char newState = stateChanges_.front().second;
    141       bTriggered_ = (newState & 0x1);
    142       bActive_ = newState & 2;
     126      char newState = this->stateChanges_.front().second;
     127      this->bTriggered_ = (newState & 0x1);
     128      this->bActive_ = newState & 2;
    143129      this->stateChanges_.pop();
    144       if(stateChanges_.size() != 0)
    145         remainingTime_ = stateChanges_.front().first;
     130      if (this->stateChanges_.size() != 0)
     131        this->remainingTime_ = this->stateChanges_.front().first;
    146132      else
    147         timeSinceLastEvent_ = delay_;
    148     }
    149 
    150 
    151 
    152     if (bTriggered_ && bActive_)
     133        this->timeSinceLastEvent_ = this->delay_;
     134    }
     135
     136    if (this->bTriggered_ && this->bActive_)
    153137      this->setBillboardColour(ColourValue(0.5, 1.0, 0.0));
    154     else if (!bTriggered_ && bActive_)
     138    else if (!this->bTriggered_ && this->bActive_)
    155139      this->setBillboardColour(ColourValue(0.0, 1.0, 0.0));
    156     else if (bTriggered_ && !bActive_)
     140    else if (this->bTriggered_ && !this->bActive_)
    157141      this->setBillboardColour(ColourValue(1.0, 0.5, 0.0));
    158142    else
    159143      this->setBillboardColour(ColourValue(1.0, 0.0, 0.0));
    160     bUpdating_ = false;
    161   }
    162 
    163   void Trigger::setBillboardColour(ColourValue colour)
    164   {
    165     this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour);
    166   }
    167 
    168   void Trigger::storeState()
    169   {
    170     // put state change into queue
    171     this->stateChanges_.push(std::pair<float,char>(timeSinceLastEvent_, latestState_));
    172     // reset time since last event
    173     timeSinceLastEvent_ = 0.0;
    174 
    175     if(this->stateChanges_.size() == 1)
    176       remainingTime_ = stateChanges_.front().first;
    177144  }
    178145
    179146  bool Trigger::isTriggered(TriggerMode mode)
    180147  {
    181     if(bUpdating_)
    182       return bTriggered_;
    183 
    184     if( children_.size() != 0 )
    185     {
    186       bUpdating_ = true;
     148//    if (this->bUpdating_)
     149//      return this->bTriggered_;
     150
     151//    this->bUpdating_ = true;
     152    if (this->children_.size() != 0)
     153    {
    187154      bool returnval = false;
    188       switch(mode)
     155
     156      switch (mode)
    189157      {
    190158        case TM_EventTriggerAND:
     
    201169          break;
    202170      }
    203       if(bInvertMode_)
     171//      this->bUpdating_ = false;
     172
     173      if (this->bInvertMode_)
    204174        return !returnval;
    205175      else
     
    209179  }
    210180
    211   void Trigger::setDelay(float delay)
    212   {
    213     this->delay_ = delay;
    214   }
    215 
    216   void Trigger::setMode(const std::string& modeName)
    217   {
    218     if (modeName == "and")
    219       setMode(TM_EventTriggerAND);
    220     else if (modeName == "or")
    221       setMode(TM_EventTriggerOR);
    222     else if (modeName == "xor")
    223       setMode(TM_EventTriggerXOR);
    224   }
    225 
    226   void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    227   {
    228     WorldEntity::XMLPort(xmlelement, mode);
    229 
    230     XMLPortParamLoadOnly(Trigger, "delay", setDelay, xmlelement, mode);
    231     XMLPortParamLoadOnly(Trigger, "stayOn", setStayOn, xmlelement, mode);
    232     XMLPortParamLoadOnly(Trigger, "activations", setActivations, xmlelement, mode);
    233     XMLPortParamLoadOnly(Trigger, "invert", setInvert, xmlelement, mode);
    234     XMLPortParamLoadOnlyTemplate(Trigger, "mode", setMode, xmlelement, mode, const std::string&);
    235 
    236     XMLPortObject(Trigger, Trigger, "", addTrigger, getTrigger, xmlelement, mode);
    237 
    238     this->init();
    239   }
    240 
    241   void Trigger::addTrigger(Trigger* trig)
    242   {
    243     if (this != trig)
    244       this->children_.insert(trig);
    245   }
    246 
    247   bool Trigger::switchState()
    248   {
    249     if ( remainingActivations_ <= -1 || this->latestState_ & 2 || remainingActivations_ > 0)
    250     {
    251       this->latestState_ ^= 2; // toggle state bit
    252       // increase activation count
    253       if (this->latestState_ & 2) remainingActivations_--;
    254       this->storeState();
    255 
    256       return true;
    257     }
    258     return false;
    259   }
    260 
    261 
    262181  bool Trigger::checkAnd()
    263182  {
    264183    std::set<Trigger*>::iterator it;
    265     for(it = this->children_.begin(); it != this->children_.end(); it++)
    266     {
    267       if(!((*it)->isActive()))
     184    for(it = this->children_.begin(); it != this->children_.end(); ++it)
     185    {
     186      if (!(*it)->isActive())
    268187        return false;
    269188    }
     
    274193  {
    275194    std::set<Trigger*>::iterator it;
    276     for(it = this->children_.begin(); it != this->children_.end(); it++)
    277     {
    278       if((*it)->isActive())
     195    for(it = this->children_.begin(); it != this->children_.end(); ++it)
     196    {
     197      if ((*it)->isActive())
    279198        return true;
    280199    }
     
    286205    std::set<Trigger*>::iterator it;
    287206    bool test = false;
    288     for(it = this->children_.begin(); it != this->children_.end(); it++)
    289     {
    290       if(test && (*it)->isActive())
     207    for(it = this->children_.begin(); it != this->children_.end(); ++it)
     208    {
     209      if (test && (*it)->isActive())
    291210        return false;
    292       if((*it)->isActive())
     211      if ((*it)->isActive())
    293212        test = true;
    294213    }
     
    296215  }
    297216
     217  bool Trigger::switchState()
     218  {
     219    if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0))
     220     || (!(this->latestState_ & 2))                      && (this->remainingActivations_ == 0))
     221      return false;
     222    else
     223    {
     224      this->latestState_ ^= 2; // toggle state bit
     225
     226      // increase activation count
     227      if (this->latestState_ & 2 && this->remainingActivations_ > 0)
     228        this->remainingActivations_--;
     229
     230      this->storeState();
     231
     232      return true;
     233    }
     234  }
     235
     236  void Trigger::storeState()
     237  {
     238    // put state change into queue
     239    this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_));
     240    // reset time since last event
     241    this->timeSinceLastEvent_ = 0.0;
     242
     243    if (this->stateChanges_.size() == 1)
     244      this->remainingTime_ = this->stateChanges_.front().first;
     245  }
     246
     247  void Trigger::setDelay(float delay)
     248  {
     249    this->delay_ = delay;
     250    this->timeSinceLastEvent_ = delay;
     251  }
     252
     253  void Trigger::setMode(const std::string& modeName)
     254  {
     255    if (modeName == "and")
     256      this->setMode(TM_EventTriggerAND);
     257    else if (modeName == "or")
     258      this->setMode(TM_EventTriggerOR);
     259    else if (modeName == "xor")
     260      this->setMode(TM_EventTriggerXOR);
     261  }
     262
     263  std::string Trigger::getModeString() const
     264  {
     265    if (this->mode_ == TM_EventTriggerAND)
     266      return std::string("and");
     267    else if (this->mode_ == TM_EventTriggerOR)
     268      return std::string("or");
     269    else if (this->mode_ == TM_EventTriggerXOR)
     270      return std::string("xor");
     271    else
     272      return std::string("and");
     273  }
     274
     275  void Trigger::addTrigger(Trigger* trigger)
     276  {
     277    if (this != trigger)
     278      this->children_.insert(trigger);
     279  }
     280
     281  const Trigger* Trigger::getTrigger(unsigned int index) const
     282  {
     283    if (this->children_.size() <= index)
     284      return NULL;
     285
     286    std::set<Trigger*>::const_iterator it;
     287    it = this->children_.begin();
     288
     289    for (unsigned int i = 0; i != index; ++i)
     290      ++it;
     291
     292    return (*it);
     293  }
     294
     295  void Trigger::debugFlares(bool bVisible)
     296  {
     297    for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
     298      it->setVisible(bVisible);
     299  }
     300
     301  void Trigger::setBillboardColour(const ColourValue& colour)
     302  {
     303    this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour);
     304  }
     305
     306  void Trigger::changedVisibility()
     307  {
     308    SUPER(Trigger, changedVisibility);
     309
     310    this->debugBillboard_.setVisible(this->isVisible());
     311  }
    298312}
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/Trigger.h

    r2027 r2029  
    3838#include "tools/BillboardSet.h"
    3939
    40 namespace orxonox {
    41 
     40namespace orxonox
     41{
    4242  enum TriggerMode
    4343  {
     
    5959        { return bActive_; }
    6060
    61       void addTrigger(Trigger* trig);
     61      void addTrigger(Trigger* trigger);
    6262      const Trigger* getTrigger(unsigned int index) const;
    6363
     64      void setMode(const std::string& modeName);
     65      inline void setMode(TriggerMode mode)
     66        { this->mode_ = mode; }
    6467      inline TriggerMode getMode() const
    6568        { return mode_; }
    66       inline void setMode(TriggerMode mode)
    67         { this->mode_ = mode; }
    68       void setMode(const std::string& modeName);
    6969
    70       inline void setInvert(int invert)
    71         { bInvertMode_ = invert; }
    72       inline void setStayOn(int stayOn)
    73         { this->bStayOn_ = (stayOn == 1) ? true : false; }
     70      inline void setInvert(bool bInvert)
     71        { this->bInvertMode_ = bInvert; }
     72      inline bool getInvert() const
     73        { return this->bInvertMode_; }
     74
     75      inline void setSwitch(bool bSwitch)
     76        { this->bSwitch_ = bSwitch; }
     77      inline bool getSwitch() const
     78        { return this->bSwitch_; }
     79
     80      inline void setStayActive(bool bStayActive)
     81        { this->bStayActive_ = bStayActive; }
     82      inline bool getStayActive() const
     83        { return this->bStayActive_; }
     84
    7485      inline void setActivations(int activations)
    7586        { this->remainingActivations_ = activations; }
     87      inline int getActivations() const
     88        { return this->remainingActivations_; }
     89
    7690      void setDelay(float delay);
     91      inline float getDelay() const
     92        { return this->delay_; }
     93
    7794      bool switchState();
    7895
    7996      static void debugFlares(bool bVisible);
    8097      virtual void changedVisibility();
    81 
    82     private:
    83       void init();
    84       bool checkAnd();
    85       bool checkOr();
    86       bool checkXor();
    87       void setBillboardColour(ColourValue colour);
    88       void storeState();
    8998
    9099    protected:
     
    93102
    94103    private:
    95       std::set<Trigger*> children_;
    96       std::queue<std::pair<float,char> > stateChanges_;
     104      bool checkAnd();
     105      bool checkOr();
     106      bool checkXor();
     107      void setBillboardColour(const ColourValue& colour);
     108      void storeState();
     109      std::string getModeString() const;
     110
     111      bool bActive_;
     112      bool bTriggered_;
     113
     114      TriggerMode mode_;
     115      bool bInvertMode_;
     116      bool bSwitch_;
     117      bool bStayActive_;
     118      float delay_;
     119      int remainingActivations_;
     120
     121      char latestState_;
    97122      float remainingTime_;
    98123      float timeSinceLastEvent_;
    99       TriggerMode mode_;
    100       bool bActive_;
    101       bool bInvertMode_;
    102       bool bTriggered_;
    103       bool bUpdating_;
     124
     125//      bool bUpdating_;
    104126      BillboardSet debugBillboard_;
    105       float delay_;
    106       int remainingActivations_;
    107       bool bStayOn_;
    108       char latestState_;
     127
     128      std::set<Trigger*> children_;
     129      std::queue<std::pair<float, char> > stateChanges_;
    109130  };
    110131
Note: See TracChangeset for help on using the changeset viewer.