Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed a bug in the event system
fixed a bug in Trigger and tweaked some features

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