Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 3186

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

Last part of the cleanup: world entities.

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