Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 2500

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

merged pickups2 to presentation

File size: 8.8 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>
[2459]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
[2459]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())
[2459]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
[2485]110    SUPER(Trigger, tick, dt);
111
[2069]112    bool newTriggered = this->isTriggered() ^ this->bInvertMode_;
[1693]113
[1954]114    // check if new triggering event is really new
[2029]115    if ((this->latestState_ & 0x1) != newTriggered)
[1954]116    {
117      // create new state
[2029]118      if (newTriggered)
[1671]119      {
[2029]120        this->latestState_ |= 1; // set trigger bit to 1
[1954]121        this->switchState();
122      }
123      else
124      {
[2029]125        this->latestState_ &= 0xFE; // set trigger bit to 0
[2078]126        if (!this->bSwitch_)
[1851]127          this->switchState();
[1671]128      }
[1954]129    }
[1693]130
[2029]131    if (this->remainingTime_ > 0.0)
[1693]132    {
[2029]133      this->remainingTime_ -= dt;
[1693]134      // only increase when acctually waiting for a state in the queue
[2029]135      if (this->timeSinceLastEvent_ >= 0.0)
136        this->timeSinceLastEvent_ += dt;
[1693]137    }
138
[2029]139    while (this->remainingTime_ <= 0.0 && this->stateChanges_.size() > 0)
[1693]140    {
141      // time ran out, change state to new one
[2029]142      char newState = this->stateChanges_.front().second;
143      this->bTriggered_ = (newState & 0x1);
144      this->bActive_ = newState & 2;
[2065]145      this->fireEvent(this->bActive_);
[1693]146      this->stateChanges_.pop();
[2029]147      if (this->stateChanges_.size() != 0)
148        this->remainingTime_ = this->stateChanges_.front().first;
[1693]149      else
[2029]150        this->timeSinceLastEvent_ = this->delay_;
[1693]151    }
152
[2029]153    if (this->bTriggered_ && this->bActive_)
[1693]154      this->setBillboardColour(ColourValue(0.5, 1.0, 0.0));
[2029]155    else if (!this->bTriggered_ && this->bActive_)
[1693]156      this->setBillboardColour(ColourValue(0.0, 1.0, 0.0));
[2029]157    else if (this->bTriggered_ && !this->bActive_)
[1693]158      this->setBillboardColour(ColourValue(1.0, 0.5, 0.0));
159    else
160      this->setBillboardColour(ColourValue(1.0, 0.0, 0.0));
[1671]161  }
162
[1541]163  bool Trigger::isTriggered(TriggerMode mode)
164  {
[2029]165//    if (this->bUpdating_)
166//      return this->bTriggered_;
[1693]167
[2029]168//    this->bUpdating_ = true;
169    if (this->children_.size() != 0)
[1541]170    {
[1693]171      bool returnval = false;
[2029]172
173      switch (mode)
[1671]174      {
175        case TM_EventTriggerAND:
[1693]176          returnval = checkAnd();
[1671]177          break;
178        case TM_EventTriggerOR:
[1693]179          returnval = checkOr();
[1671]180          break;
181        case TM_EventTriggerXOR:
[1693]182          returnval = checkXor();
[1671]183          break;
184        default:
[1693]185          returnval = false;
[1671]186          break;
187      }
[2029]188//      this->bUpdating_ = false;
189
[2069]190      return returnval;
[1541]191    }
[1671]192    return true;
[1541]193  }
194
[2029]195  bool Trigger::checkAnd()
196  {
197    std::set<Trigger*>::iterator it;
198    for(it = this->children_.begin(); it != this->children_.end(); ++it)
199    {
200      if (!(*it)->isActive())
201        return false;
202    }
203    return true;
204  }
205
206  bool Trigger::checkOr()
207  {
208    std::set<Trigger*>::iterator it;
209    for(it = this->children_.begin(); it != this->children_.end(); ++it)
210    {
211      if ((*it)->isActive())
212        return true;
213    }
214    return false;
215  }
216
217  bool Trigger::checkXor()
218  {
219    std::set<Trigger*>::iterator it;
220    bool test = false;
221    for(it = this->children_.begin(); it != this->children_.end(); ++it)
222    {
223      if (test && (*it)->isActive())
224        return false;
225      if ((*it)->isActive())
226        test = true;
227    }
228    return test;
229  }
230
231  bool Trigger::switchState()
232  {
233    if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0))
234     || (!(this->latestState_ & 2))                      && (this->remainingActivations_ == 0))
235      return false;
236    else
237    {
238      this->latestState_ ^= 2; // toggle state bit
239
240      // increase activation count
241      if (this->latestState_ & 2 && this->remainingActivations_ > 0)
242        this->remainingActivations_--;
243
244      this->storeState();
245
246      return true;
247    }
248  }
249
250  void Trigger::storeState()
251  {
252    // put state change into queue
253    this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_));
254    // reset time since last event
255    this->timeSinceLastEvent_ = 0.0;
256
257    if (this->stateChanges_.size() == 1)
258      this->remainingTime_ = this->stateChanges_.front().first;
259  }
260
[1693]261  void Trigger::setDelay(float delay)
262  {
263    this->delay_ = delay;
[2029]264    this->timeSinceLastEvent_ = delay;
[1693]265  }
266
[1969]267  void Trigger::setMode(const std::string& modeName)
[1954]268  {
269    if (modeName == "and")
[2029]270      this->setMode(TM_EventTriggerAND);
[1954]271    else if (modeName == "or")
[2029]272      this->setMode(TM_EventTriggerOR);
[1954]273    else if (modeName == "xor")
[2029]274      this->setMode(TM_EventTriggerXOR);
[1954]275  }
276
[2029]277  std::string Trigger::getModeString() const
[1550]278  {
[2029]279    if (this->mode_ == TM_EventTriggerAND)
280      return std::string("and");
281    else if (this->mode_ == TM_EventTriggerOR)
282      return std::string("or");
283    else if (this->mode_ == TM_EventTriggerXOR)
284      return std::string("xor");
285    else
286      return std::string("and");
[1550]287  }
288
[2029]289  void Trigger::addTrigger(Trigger* trigger)
[1383]290  {
[2029]291    if (this != trigger)
292      this->children_.insert(trigger);
[1383]293  }
294
[2029]295  const Trigger* Trigger::getTrigger(unsigned int index) const
[1541]296  {
[2029]297    if (this->children_.size() <= index)
298      return NULL;
[1541]299
[2029]300    std::set<Trigger*>::const_iterator it;
301    it = this->children_.begin();
302
303    for (unsigned int i = 0; i != index; ++i)
304      ++it;
305
306    return (*it);
[1550]307  }
308
[2029]309  void Trigger::debugFlares(bool bVisible)
[1541]310  {
[2029]311    for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
312      it->setVisible(bVisible);
[1541]313  }
314
[2029]315  void Trigger::setBillboardColour(const ColourValue& colour)
[1541]316  {
[2171]317    this->debugBillboard_.setColour(colour);
[1541]318  }
319
[2029]320  void Trigger::changedVisibility()
[1671]321  {
[2029]322    SUPER(Trigger, changedVisibility);
323
324    this->debugBillboard_.setVisible(this->isVisible());
[1671]325  }
[1383]326}
Note: See TracBrowser for help on using the repository browser.