Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 2485

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