Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 3064

Last change on this file since 3064 was 3060, checked in by scheusso, 15 years ago

merged sound2 back to trunk

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