Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1693


Ignore:
Timestamp:
Sep 1, 2008, 9:29:27 PM (16 years ago)
Author:
bknecht
Message:

Trigger delay queue is working, DistanceTrigger can be added to level and reacts… cool stuff

Location:
code/branches/script_trigger
Files:
2 added
4 edited

Legend:

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

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

    r1383 r1693  
    2626  objects/Script.cc
    2727  objects/Trigger.cc
     28  objects/DistanceTrigger.cc
    2829
    2930#  objects/weapon/AmmunitionDump.cc
  • code/branches/script_trigger/src/orxonox/objects/Trigger.cc

    r1671 r1693  
    3434#include "core/CoreIncludes.h"
    3535#include "core/ConsoleCommand.h"
     36#include "core/XMLPort.h"
    3637
    3738namespace orxonox
     
    4647    RegisterObject(Trigger);
    4748
    48     targetMask_.exclude(Class(BaseObject));
    49 
    50     //testing
    5149    mode_ = TM_EventTriggerAND;
    52     bActive_ = true;
    53     triggingTime_ = 100;
     50    bActive_ = false;
     51    bInvertMode_ = false;
     52    delay_ = 0.0;
     53    bTriggered_ = false;
     54    bUpdating_ = false;
    5455
    5556    debugBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
     
    6162  }
    6263
    63   bool Trigger::isTriggered()
    64   {
    65     return this->isTriggered(this->mode_);
    66   }
    67 
    68   void Trigger::setVisibility(int bVisible)
     64  void Trigger::init()
     65  {
     66    this->setVisibility(true);
     67    timeSinceLastEvent_ = delay_;
     68  }
     69
     70  void Trigger::setVisibility(bool bVisible)
    6971  {
    7072    if(bVisible)
     
    7678  void Trigger::tick(float dt)
    7779  {
    78     //COUT(0) << "Scale: " << this->getScale() << std::endl;
    79     if(bActive_)
    80     {
    81       //this->actualTime_ += dt;
    82       if(this->isTriggered())
     80
     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)
    8389      {
    84         this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(ColourValue(0.0, 1.0, 0.0));
     90        latestState_ |= 0x1; // set trigger bit
     91        latestState_ ^= 0x10; // toggle state bit
    8592      }
    86     }
     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    }
     106
     107    if(remainingTime_ > 0.0)
     108    {
     109      remainingTime_ -= dt;
     110      // only increase when acctually waiting for a state in the queue
     111      if(timeSinceLastEvent_ >= 0.0)
     112        timeSinceLastEvent_ += dt;
     113    }
     114
     115    while(remainingTime_ <= 0.0 && stateChanges_.size() > 0)
     116    {
     117      // time ran out, change state to new one
     118      char newState = stateChanges_.front().second;
     119      bTriggered_ = newState % 2;
     120      bActive_ = newState>>1 % 2;
     121      this->stateChanges_.pop();
     122      if(stateChanges_.size() != 0)
     123        remainingTime_ = stateChanges_.front().first;
     124      else
     125        timeSinceLastEvent_ = delay_;
     126    }
     127
     128
     129
     130    if (bTriggered_ && bActive_)
     131      this->setBillboardColour(ColourValue(0.5, 1.0, 0.0));
     132    else if (!bTriggered_ && bActive_)
     133      this->setBillboardColour(ColourValue(0.0, 1.0, 0.0));
     134    else if (bTriggered_ && !bActive_)
     135      this->setBillboardColour(ColourValue(1.0, 0.5, 0.0));
     136    else
     137      this->setBillboardColour(ColourValue(1.0, 0.0, 0.0));
     138    bUpdating_ = false;
     139  }
     140
     141  void Trigger::setBillboardColour(ColourValue colour)
     142  {
     143    this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour);
    87144  }
    88145
    89146  bool Trigger::isTriggered(TriggerMode mode)
    90147  {
     148    if(bUpdating_)
     149      return bTriggered_;
     150
    91151    if( children_.size() != 0 )
    92152    {
     153      bUpdating_ = true;
     154      bool returnval = false;
    93155      switch(mode)
    94156      {
    95157        case TM_EventTriggerAND:
    96           return checkAnd();
     158          returnval = checkAnd();
    97159          break;
    98160        case TM_EventTriggerOR:
    99           return checkOr();
     161          returnval = checkOr();
    100162          break;
    101163        case TM_EventTriggerXOR:
    102           return checkXor();
    103           break;
    104         case TM_EventTriggerNOT:
    105           return checkNot();
     164          returnval = checkXor();
    106165          break;
    107166        default:
    108           return false;
     167          returnval = false;
    109168          break;
    110169      }
     170      if(bInvertMode_)
     171        return !returnval;
     172      else
     173        return returnval;
    111174    }
    112175    return true;
    113176  }
    114177
     178  void Trigger::setDelay(float delay)
     179  {
     180    this->delay_ = delay;
     181  }
     182
    115183  void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    116184  {
    117185    WorldEntity::XMLPort(xmlelement, mode);
     186
     187    XMLPortParamLoadOnly(Trigger, "delay", setDelay, xmlelement, mode);
     188
     189    this->init();
    118190  }
    119191
     
    124196  }
    125197
    126   void Trigger::addTargets(std::string targets)
    127   {
    128     Identifier* targetId = ID(targets);
    129     targetMask_.include(targetId);
    130     // trigger shouldn't react on itself or other triggers
    131     targetMask_.exclude(Class(Trigger), true);
    132   }
    133 
    134   void Trigger::removeTargets(std::string targets)
    135   {
    136     Identifier* targetId = ID(targets);
    137     targetMask_.exclude(targetId);
    138   }
     198  bool Trigger::switchState()
     199  {
     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;
     208  }
     209
    139210
    140211  bool Trigger::checkAnd()
     
    158229    }
    159230    return false;
    160   }
    161 
    162   bool Trigger::checkNot()
    163   {
    164     std::set<Trigger*>::iterator it;
    165     for(it = this->children_.begin(); it != this->children_.end(); it++)
    166     {
    167       if((*it)->isTriggered())
    168         return false;
    169     }
    170     return true;
    171231  }
    172232
     
    185245  }
    186246
    187   /*bool Trigger::checkDelay()
    188   {
    189     if (triggingTime_ < actualTime_)
    190       return true;
    191     else
    192       return false;
    193   }
    194 
    195   bool Trigger::checkDistance()
    196   {
    197     // Iterate through all WorldEntities
    198     for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; it++)
    199     {
    200       Vector3 distanceVec = it->getNode()->getWorldPosition() - this->getNode()->getWorldPosition();
    201       if (distanceVec.length() < radius_)
    202         return true;
    203     }
    204     return false;
    205 
    206   }*/
    207 
    208247}
  • code/branches/script_trigger/src/orxonox/objects/Trigger.h

    r1671 r1693  
    3131
    3232#include <set>
     33#include <queue>
    3334
    3435#include "OrxonoxPrereqs.h"
    3536
    3637#include "WorldEntity.h"
    37 #include "core/BaseObject.h"
    38 #include "core/ClassTreeMask.h"
     38
    3939#include "../tools/BillboardSet.h"
    4040
     
    4646    TM_EventTriggerOR,
    4747    TM_EventTriggerXOR,
    48     TM_EventTriggerNOT
    4948  };
    5049
     
    5554      ~Trigger();
    5655
    57       bool isTriggered();
    58       bool isTriggered(TriggerMode mode);
     56      inline bool isTriggered() { return this->isTriggered(this->mode_); }
     57      virtual bool isTriggered(TriggerMode mode);
    5958      void addTrigger(Trigger* trig);
    60       void addTargets(std::string targets);
    61       void removeTargets(std::string targets);
    62       void setVisibility(int bVisible);
     59      void setVisibility(bool bVisible);
     60      void setDelay(float delay);
     61      bool switchState();
    6362      virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    6463      inline TriggerMode getMode() { return mode_; }
    6564      inline void setMode(TriggerMode mode) { this->mode_ = mode; }
    6665      void tick(float dt);
    67       inline void reset(float time) { this->actualTime_ = 0; this->triggingTime_ = time; }
    68       inline void reset() { reset(triggingTime_); }
    6966
    7067    private:
     68      void init();
    7169      bool checkAnd();
    7270      bool checkOr();
    7371      bool checkXor();
    74       bool checkNot();
     72      void setBillboardColour(ColourValue colour);
    7573
    7674    private:
    7775      std::set<Trigger*> children_;
     76      std::queue<std::pair<float,char> > stateChanges_;
     77      float remainingTime_;
     78      float timeSinceLastEvent_;
    7879      TriggerMode mode_;
    79       float triggingTime_;
    80       float actualTime_;
    81       float radius_;
    8280      bool bActive_;
    83       ClassTreeMask targetMask_;
     81      bool bInvertMode_;
     82      bool bTriggered_;
     83      bool bUpdating_;
    8484      BillboardSet debugBillboard_;
     85      float delay_;
     86      char latestState_;
    8587  };
    8688
Note: See TracChangeset for help on using the changeset viewer.