Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/triggers/Trigger.cc @ 5741

Last change on this file since 5741 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

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