Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 2029

Last change on this file since 2029 was 2029, checked in by landauf, 16 years ago

some changes in Trigger and DistanceTrigger

File size: 8.4 KB
RevLine 
[1383]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Benjamin Knecht
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[1906]29#include "OrxonoxStableHeaders.h"
[1383]30#include "Trigger.h"
31
[1906]32#include <OgreBillboard.h>
[1961]33#include "util/Debug.h"
[1383]34#include "core/CoreIncludes.h"
[1671]35#include "core/ConsoleCommand.h"
[1693]36#include "core/XMLPort.h"
[2019]37#include "objects/Scene.h"
[1383]38
39namespace orxonox
40{
[1671]41
[1969]42  SetConsoleCommand(Trigger, debugFlares, false).defaultValues(false);
[1671]43
[1383]44  CreateFactory(Trigger);
45
[2019]46  Trigger::Trigger(BaseObject* creator) : PositionableEntity(creator)
[1383]47  {
48    RegisterObject(Trigger);
49
[2029]50    this->mode_ = TM_EventTriggerAND;
[1671]51
[2029]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;
63
[2019]64    if (this->getScene() && this->getScene()->getSceneManager())
65    {
[2029]66      this->debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
67      this->debugBillboard_.setVisible(false);
[2019]68    }
[1969]69
[2029]70    this->getNode()->attachObject(this->debugBillboard_.getBillboardSet());
[1383]71  }
72
73  Trigger::~Trigger()
74  {
75  }
76
[2029]77  void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[1383]78  {
[2029]79    SUPER(Trigger, XMLPort, xmlelement, mode);
[1383]80
[2029]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");
[1671]87
[2029]88    XMLPortObject(Trigger, Trigger, "", addTrigger, getTrigger, xmlelement, mode);
[1969]89  }
90
[1671]91  void Trigger::tick(float dt)
92  {
[1693]93
[2029]94    bool newTriggered = this->isTriggered();
[1693]95
[1954]96    // check if new triggering event is really new
[2029]97    if ((this->latestState_ & 0x1) != newTriggered)
[1954]98    {
99      // create new state
[2029]100      if (newTriggered)
[1671]101      {
[2029]102        this->latestState_ |= 1; // set trigger bit to 1
[1954]103        this->switchState();
104      }
105      else
106      {
[2029]107        this->latestState_ &= 0xFE; // set trigger bit to 0
108        if (this->bSwitch_)
[1954]109          this->storeState();
110        else
[1851]111          this->switchState();
[1671]112      }
[1954]113    }
[1693]114
[2029]115    if (this->remainingTime_ > 0.0)
[1693]116    {
[2029]117      this->remainingTime_ -= dt;
[1693]118      // only increase when acctually waiting for a state in the queue
[2029]119      if (this->timeSinceLastEvent_ >= 0.0)
120        this->timeSinceLastEvent_ += dt;
[1693]121    }
122
[2029]123    while (this->remainingTime_ <= 0.0 && this->stateChanges_.size() > 0)
[1693]124    {
125      // time ran out, change state to new one
[2029]126      char newState = this->stateChanges_.front().second;
127      this->bTriggered_ = (newState & 0x1);
128      this->bActive_ = newState & 2;
[1693]129      this->stateChanges_.pop();
[2029]130      if (this->stateChanges_.size() != 0)
131        this->remainingTime_ = this->stateChanges_.front().first;
[1693]132      else
[2029]133        this->timeSinceLastEvent_ = this->delay_;
[1693]134    }
135
[2029]136    if (this->bTriggered_ && this->bActive_)
[1693]137      this->setBillboardColour(ColourValue(0.5, 1.0, 0.0));
[2029]138    else if (!this->bTriggered_ && this->bActive_)
[1693]139      this->setBillboardColour(ColourValue(0.0, 1.0, 0.0));
[2029]140    else if (this->bTriggered_ && !this->bActive_)
[1693]141      this->setBillboardColour(ColourValue(1.0, 0.5, 0.0));
142    else
143      this->setBillboardColour(ColourValue(1.0, 0.0, 0.0));
[1671]144  }
145
[1541]146  bool Trigger::isTriggered(TriggerMode mode)
147  {
[2029]148//    if (this->bUpdating_)
149//      return this->bTriggered_;
[1693]150
[2029]151//    this->bUpdating_ = true;
152    if (this->children_.size() != 0)
[1541]153    {
[1693]154      bool returnval = false;
[2029]155
156      switch (mode)
[1671]157      {
158        case TM_EventTriggerAND:
[1693]159          returnval = checkAnd();
[1671]160          break;
161        case TM_EventTriggerOR:
[1693]162          returnval = checkOr();
[1671]163          break;
164        case TM_EventTriggerXOR:
[1693]165          returnval = checkXor();
[1671]166          break;
167        default:
[1693]168          returnval = false;
[1671]169          break;
170      }
[2029]171//      this->bUpdating_ = false;
172
173      if (this->bInvertMode_)
[1693]174        return !returnval;
175      else
176        return returnval;
[1541]177    }
[1671]178    return true;
[1541]179  }
180
[2029]181  bool Trigger::checkAnd()
182  {
183    std::set<Trigger*>::iterator it;
184    for(it = this->children_.begin(); it != this->children_.end(); ++it)
185    {
186      if (!(*it)->isActive())
187        return false;
188    }
189    return true;
190  }
191
192  bool Trigger::checkOr()
193  {
194    std::set<Trigger*>::iterator it;
195    for(it = this->children_.begin(); it != this->children_.end(); ++it)
196    {
197      if ((*it)->isActive())
198        return true;
199    }
200    return false;
201  }
202
203  bool Trigger::checkXor()
204  {
205    std::set<Trigger*>::iterator it;
206    bool test = false;
207    for(it = this->children_.begin(); it != this->children_.end(); ++it)
208    {
209      if (test && (*it)->isActive())
210        return false;
211      if ((*it)->isActive())
212        test = true;
213    }
214    return test;
215  }
216
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
[1693]247  void Trigger::setDelay(float delay)
248  {
249    this->delay_ = delay;
[2029]250    this->timeSinceLastEvent_ = delay;
[1693]251  }
252
[1969]253  void Trigger::setMode(const std::string& modeName)
[1954]254  {
255    if (modeName == "and")
[2029]256      this->setMode(TM_EventTriggerAND);
[1954]257    else if (modeName == "or")
[2029]258      this->setMode(TM_EventTriggerOR);
[1954]259    else if (modeName == "xor")
[2029]260      this->setMode(TM_EventTriggerXOR);
[1954]261  }
262
[2029]263  std::string Trigger::getModeString() const
[1550]264  {
[2029]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");
[1550]273  }
274
[2029]275  void Trigger::addTrigger(Trigger* trigger)
[1383]276  {
[2029]277    if (this != trigger)
278      this->children_.insert(trigger);
[1383]279  }
280
[2029]281  const Trigger* Trigger::getTrigger(unsigned int index) const
[1541]282  {
[2029]283    if (this->children_.size() <= index)
284      return NULL;
[1541]285
[2029]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);
[1550]293  }
294
[2029]295  void Trigger::debugFlares(bool bVisible)
[1541]296  {
[2029]297    for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
298      it->setVisible(bVisible);
[1541]299  }
300
[2029]301  void Trigger::setBillboardColour(const ColourValue& colour)
[1541]302  {
[2029]303    this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour);
[1541]304  }
305
[2029]306  void Trigger::changedVisibility()
[1671]307  {
[2029]308    SUPER(Trigger, changedVisibility);
309
310    this->debugBillboard_.setVisible(this->isVisible());
[1671]311  }
[1383]312}
Note: See TracBrowser for help on using the repository browser.