Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed a bug in the event system
fixed a bug in Trigger and tweaked some features

File size: 8.5 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
[2069]52    this->bFirstTick_ = true;
[2029]53    this->bActive_ = false;
54    this->bTriggered_ = false;
55    this->latestState_ = 0x0;
56
57    this->bInvertMode_ = false;
58    this->bSwitch_ = false;
59    this->bStayActive_ = false;
60    this->delay_ = 0.0;
61    this->remainingActivations_ = -1;
62
63//    this->bUpdating_ = false;
64
[2019]65    if (this->getScene() && this->getScene()->getSceneManager())
66    {
[2029]67      this->debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
68      this->debugBillboard_.setVisible(false);
[2019]69    }
[1969]70
[2029]71    this->getNode()->attachObject(this->debugBillboard_.getBillboardSet());
[2031]72    this->setObjectMode(0x0);
[1383]73  }
74
75  Trigger::~Trigger()
76  {
77  }
78
[2029]79  void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[1383]80  {
[2029]81    SUPER(Trigger, XMLPort, xmlelement, mode);
[1383]82
[2029]83    XMLPortParam(Trigger, "delay",       setDelay,       getDelay,       xmlelement, mode).defaultValues(0.0f);
84    XMLPortParam(Trigger, "switch",      setSwitch,      getSwitch,      xmlelement, mode).defaultValues(false);
85    XMLPortParam(Trigger, "stayactive",  setStayActive,  getStayActive,  xmlelement, mode).defaultValues(false);
86    XMLPortParam(Trigger, "activations", setActivations, getActivations, xmlelement, mode).defaultValues(-1);
87    XMLPortParam(Trigger, "invert",      setInvert,      getInvert,      xmlelement, mode).defaultValues(false);
88    XMLPortParamTemplate(Trigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&).defaultValues("or");
[1671]89
[2029]90    XMLPortObject(Trigger, Trigger, "", addTrigger, getTrigger, xmlelement, mode);
[1969]91  }
92
[1671]93  void Trigger::tick(float dt)
94  {
[2069]95    if (this->bFirstTick_)
96    {
97      this->bFirstTick_ = false;
98      this->fireEvent(false);
99    }
[1693]100
[2069]101    bool newTriggered = this->isTriggered() ^ this->bInvertMode_;
[1693]102
[1954]103    // check if new triggering event is really new
[2029]104    if ((this->latestState_ & 0x1) != newTriggered)
[1954]105    {
106      // create new state
[2029]107      if (newTriggered)
[1671]108      {
[2029]109        this->latestState_ |= 1; // set trigger bit to 1
[1954]110        this->switchState();
111      }
112      else
113      {
[2029]114        this->latestState_ &= 0xFE; // set trigger bit to 0
115        if (this->bSwitch_)
[1954]116          this->storeState();
117        else
[1851]118          this->switchState();
[1671]119      }
[1954]120    }
[1693]121
[2029]122    if (this->remainingTime_ > 0.0)
[1693]123    {
[2029]124      this->remainingTime_ -= dt;
[1693]125      // only increase when acctually waiting for a state in the queue
[2029]126      if (this->timeSinceLastEvent_ >= 0.0)
127        this->timeSinceLastEvent_ += dt;
[1693]128    }
129
[2029]130    while (this->remainingTime_ <= 0.0 && this->stateChanges_.size() > 0)
[1693]131    {
132      // time ran out, change state to new one
[2029]133      char newState = this->stateChanges_.front().second;
134      this->bTriggered_ = (newState & 0x1);
135      this->bActive_ = newState & 2;
[2065]136      this->fireEvent(this->bActive_);
[1693]137      this->stateChanges_.pop();
[2029]138      if (this->stateChanges_.size() != 0)
139        this->remainingTime_ = this->stateChanges_.front().first;
[1693]140      else
[2029]141        this->timeSinceLastEvent_ = this->delay_;
[1693]142    }
143
[2029]144    if (this->bTriggered_ && this->bActive_)
[1693]145      this->setBillboardColour(ColourValue(0.5, 1.0, 0.0));
[2029]146    else if (!this->bTriggered_ && this->bActive_)
[1693]147      this->setBillboardColour(ColourValue(0.0, 1.0, 0.0));
[2029]148    else if (this->bTriggered_ && !this->bActive_)
[1693]149      this->setBillboardColour(ColourValue(1.0, 0.5, 0.0));
150    else
151      this->setBillboardColour(ColourValue(1.0, 0.0, 0.0));
[1671]152  }
153
[1541]154  bool Trigger::isTriggered(TriggerMode mode)
155  {
[2029]156//    if (this->bUpdating_)
157//      return this->bTriggered_;
[1693]158
[2029]159//    this->bUpdating_ = true;
160    if (this->children_.size() != 0)
[1541]161    {
[1693]162      bool returnval = false;
[2029]163
164      switch (mode)
[1671]165      {
166        case TM_EventTriggerAND:
[1693]167          returnval = checkAnd();
[1671]168          break;
169        case TM_EventTriggerOR:
[1693]170          returnval = checkOr();
[1671]171          break;
172        case TM_EventTriggerXOR:
[1693]173          returnval = checkXor();
[1671]174          break;
175        default:
[1693]176          returnval = false;
[1671]177          break;
178      }
[2029]179//      this->bUpdating_ = false;
180
[2069]181      return returnval;
[1541]182    }
[1671]183    return true;
[1541]184  }
185
[2029]186  bool Trigger::checkAnd()
187  {
188    std::set<Trigger*>::iterator it;
189    for(it = this->children_.begin(); it != this->children_.end(); ++it)
190    {
191      if (!(*it)->isActive())
192        return false;
193    }
194    return true;
195  }
196
197  bool Trigger::checkOr()
198  {
199    std::set<Trigger*>::iterator it;
200    for(it = this->children_.begin(); it != this->children_.end(); ++it)
201    {
202      if ((*it)->isActive())
203        return true;
204    }
205    return false;
206  }
207
208  bool Trigger::checkXor()
209  {
210    std::set<Trigger*>::iterator it;
211    bool test = false;
212    for(it = this->children_.begin(); it != this->children_.end(); ++it)
213    {
214      if (test && (*it)->isActive())
215        return false;
216      if ((*it)->isActive())
217        test = true;
218    }
219    return test;
220  }
221
222  bool Trigger::switchState()
223  {
224    if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0))
225     || (!(this->latestState_ & 2))                      && (this->remainingActivations_ == 0))
226      return false;
227    else
228    {
229      this->latestState_ ^= 2; // toggle state bit
230
231      // increase activation count
232      if (this->latestState_ & 2 && this->remainingActivations_ > 0)
233        this->remainingActivations_--;
234
235      this->storeState();
236
237      return true;
238    }
239  }
240
241  void Trigger::storeState()
242  {
243    // put state change into queue
244    this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_));
245    // reset time since last event
246    this->timeSinceLastEvent_ = 0.0;
247
248    if (this->stateChanges_.size() == 1)
249      this->remainingTime_ = this->stateChanges_.front().first;
250  }
251
[1693]252  void Trigger::setDelay(float delay)
253  {
254    this->delay_ = delay;
[2029]255    this->timeSinceLastEvent_ = delay;
[1693]256  }
257
[1969]258  void Trigger::setMode(const std::string& modeName)
[1954]259  {
260    if (modeName == "and")
[2029]261      this->setMode(TM_EventTriggerAND);
[1954]262    else if (modeName == "or")
[2029]263      this->setMode(TM_EventTriggerOR);
[1954]264    else if (modeName == "xor")
[2029]265      this->setMode(TM_EventTriggerXOR);
[1954]266  }
267
[2029]268  std::string Trigger::getModeString() const
[1550]269  {
[2029]270    if (this->mode_ == TM_EventTriggerAND)
271      return std::string("and");
272    else if (this->mode_ == TM_EventTriggerOR)
273      return std::string("or");
274    else if (this->mode_ == TM_EventTriggerXOR)
275      return std::string("xor");
276    else
277      return std::string("and");
[1550]278  }
279
[2029]280  void Trigger::addTrigger(Trigger* trigger)
[1383]281  {
[2029]282    if (this != trigger)
283      this->children_.insert(trigger);
[1383]284  }
285
[2029]286  const Trigger* Trigger::getTrigger(unsigned int index) const
[1541]287  {
[2029]288    if (this->children_.size() <= index)
289      return NULL;
[1541]290
[2029]291    std::set<Trigger*>::const_iterator it;
292    it = this->children_.begin();
293
294    for (unsigned int i = 0; i != index; ++i)
295      ++it;
296
297    return (*it);
[1550]298  }
299
[2029]300  void Trigger::debugFlares(bool bVisible)
[1541]301  {
[2029]302    for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
303      it->setVisible(bVisible);
[1541]304  }
305
[2029]306  void Trigger::setBillboardColour(const ColourValue& colour)
[1541]307  {
[2029]308    this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour);
[1541]309  }
310
[2029]311  void Trigger::changedVisibility()
[1671]312  {
[2029]313    SUPER(Trigger, changedVisibility);
314
315    this->debugBillboard_.setVisible(this->isVisible());
[1671]316  }
[1383]317}
Note: See TracBrowser for help on using the repository browser.