Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_merge/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 2493

Last change on this file since 2493 was 2442, checked in by rgrieder, 15 years ago

Finally merged physics stuff. Target is physics_merge because I'll have to do some testing first.

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