Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 28, 2008, 5:15:45 PM (16 years ago)
Author:
bknecht
Message:

LuaBind ↔Script confusion solved (thx Reto for noticing)\n Solved 4 state problem, however a Trigger may return an one tick old state instead of the newest one. This can probably be solved in another way though.

Location:
code/branches/script_trigger
Files:
3 added
3 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/script_trigger/bin/levels/sample.oxw

    r1693 r1851  
    3838<Model position="-200,1000,500" scale="10" mesh="hoover_turbine.mesh" yawpitchroll="-90,-90,0" /-->
    3939
    40 <DistanceTrigger position="100,0,0" scale="2" distance="80" delay="10.0" target="SpaceShip"/>
     40<DistanceTrigger position="100,0,0" scale="2" distance="80" activations="2" target="SpaceShip"/>
    4141
    4242
  • code/branches/script_trigger/src/orxonox/objects/DistanceTrigger.h

    r1693 r1851  
    5252      void setDistance(float dist);
    5353      virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     54      bool checkDistance();
     55
     56    protected:
    5457      bool isTriggered(TriggerMode mode);
    55       bool checkDistance();
    5658
    5759    private:
  • code/branches/script_trigger/src/orxonox/objects/Trigger.cc

    r1693 r1851  
    5353    bTriggered_ = false;
    5454    bUpdating_ = false;
     55    remainingActivations_ = -1;
     56    bStayTriggered_ = false;
     57    latestState_ = 0x0;
    5558
    5659    debugBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
     
    7982  {
    8083
    81     bool newTriggered = this->isTriggered();
    82 
    83 
    84     // check if new triggering event is really new
    85     if(this->latestState_ % 2 != newTriggered)
    86     {
    87       // create new state
    88       if(newTriggered)
     84    bool newTriggered;
     85    if (latestState_ % 2 == 1 && this->bStayTriggered_)
     86      newTriggered = true;
     87    else
     88      newTriggered = this->isTriggered();
     89
     90
     91      // check if new triggering event is really new
     92      if(this->latestState_ % 2 != newTriggered)
    8993      {
    90         latestState_ |= 0x1; // set trigger bit
    91         latestState_ ^= 0x10; // toggle state bit
     94        // create new state
     95        if(newTriggered)
     96        {
     97          latestState_ |= 1; // set trigger bit
     98          this->switchState();
     99        }
     100        else
     101        {
     102          latestState_ &= 0xFE; // set trigger bit
     103          this->storeState();
     104        }
    92105      }
    93       else
    94       {
    95         latestState_ &= 0x11111110; // set trigger bit
    96       }
    97 
    98       // put state change into queue
    99       this->stateChanges_.push(std::pair<float,char>(timeSinceLastEvent_, latestState_));
    100       // reset time since last event
    101       timeSinceLastEvent_ = 0.0;
    102 
    103       if(this->stateChanges_.size() == 1)
    104         remainingTime_ = stateChanges_.front().first;
    105     }
    106106
    107107    if(remainingTime_ > 0.0)
     
    118118      char newState = stateChanges_.front().second;
    119119      bTriggered_ = newState % 2;
    120       bActive_ = newState>>1 % 2;
     120      bActive_ = newState & 2;
    121121      this->stateChanges_.pop();
    122122      if(stateChanges_.size() != 0)
     
    144144  }
    145145
     146  void Trigger::storeState()
     147  {
     148    // put state change into queue
     149    this->stateChanges_.push(std::pair<float,char>(timeSinceLastEvent_, latestState_));
     150    // reset time since last event
     151    timeSinceLastEvent_ = 0.0;
     152
     153    if(this->stateChanges_.size() == 1)
     154      remainingTime_ = stateChanges_.front().first;
     155  }
     156
    146157  bool Trigger::isTriggered(TriggerMode mode)
    147158  {
     
    186197
    187198    XMLPortParamLoadOnly(Trigger, "delay", setDelay, xmlelement, mode);
     199    XMLPortParamLoadOnly(Trigger, "stayTriggered", setStayTriggered, xmlelement, mode);
     200    XMLPortParamLoadOnly(Trigger, "activations", setActivations, xmlelement, mode);
    188201
    189202    this->init();
     
    198211  bool Trigger::switchState()
    199212  {
    200     latestState_ ^= 0x10; // toggle state bit
    201     // put state change into queue
    202     this->stateChanges_.push(std::pair<float,char>(timeSinceLastEvent_, latestState_));
    203     // reset time since last event
    204     timeSinceLastEvent_ = 0.0;
    205 
    206     if(this->stateChanges_.size() == 1)
    207       remainingTime_ = stateChanges_.front().first;
     213    if ( remainingActivations_ == -1 || this->latestState_ & 2 || remainingActivations_ > 0)
     214    {
     215      this->latestState_ ^= 2; // toggle state bit
     216      // increase activation count
     217      if (this->latestState_ & 2) remainingActivations_--;
     218      this->storeState();
     219
     220      return true;
     221    }
     222    return false;
    208223  }
    209224
     
    214229    for(it = this->children_.begin(); it != this->children_.end(); it++)
    215230    {
    216       if(!((*it)->isTriggered()))
     231      if(!((*it)->isActive()))
    217232        return false;
    218233    }
     
    225240    for(it = this->children_.begin(); it != this->children_.end(); it++)
    226241    {
    227       if((*it)->isTriggered())
     242      if((*it)->isActive())
    228243        return true;
    229244    }
     
    237252    for(it = this->children_.begin(); it != this->children_.end(); it++)
    238253    {
    239       if(test && (*it)->isTriggered())
     254      if(test && (*it)->isActive())
    240255        return false;
    241       if((*it)->isTriggered())
     256      if((*it)->isActive())
    242257        test = true;
    243258    }
  • code/branches/script_trigger/src/orxonox/objects/Trigger.h

    r1693 r1851  
    5454      ~Trigger();
    5555
    56       inline bool isTriggered() { return this->isTriggered(this->mode_); }
    57       virtual bool isTriggered(TriggerMode mode);
     56      inline bool isActive() { return bActive_; }
    5857      void addTrigger(Trigger* trig);
    5958      void setVisibility(bool bVisible);
     
    6362      inline TriggerMode getMode() { return mode_; }
    6463      inline void setMode(TriggerMode mode) { this->mode_ = mode; }
     64      inline void setStayTriggered(float stayTriggered) { if (stayTriggered == 1.0) this->bStayTriggered_ = true; else this->bStayTriggered_ = false; }
     65      inline void setActivations(int activations) { this->remainingActivations_ = activations; }
    6566      void tick(float dt);
    6667
     
    7172      bool checkXor();
    7273      void setBillboardColour(ColourValue colour);
     74      void storeState();
     75
     76    protected:
     77      inline bool isTriggered() { return this->isTriggered(this->mode_); }
     78      virtual bool isTriggered(TriggerMode mode);
    7379
    7480    private:
     
    8490      BillboardSet debugBillboard_;
    8591      float delay_;
     92      int remainingActivations_;
     93      bool bStayTriggered_;
    8694      char latestState_;
    8795  };
Note: See TracChangeset for help on using the changeset viewer.