Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed trigger switch problem

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