Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/notifications/src/modules/objects/triggers/Trigger.cc @ 7319

Last change on this file since 7319 was 7319, checked in by dafrick, 14 years ago

Small changes for testing purposes.

  • 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/GameMode.h"
33#include "core/XMLPort.h"
34#include "core/command/ConsoleCommand.h"
35#include "Scene.h"
36
37namespace orxonox
38{
39
40  SetConsoleCommand("Trigger", "debugFlares", &Trigger::debugFlares).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->setSyncMode(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      COUT(1) << "Trigger triggered." << std::endl; //TODO: Remove debug.
163    this->fireEvent(bIsTriggered);
164  }
165
166  bool Trigger::isTriggered(TriggerMode::Value mode)
167  {
168//    if (this->bUpdating_)
169//      return this->bTriggered_;
170
171//    this->bUpdating_ = true;
172    if (this->children_.size() != 0)
173    {
174      bool returnval = false;
175
176      switch (mode)
177      {
178        case TriggerMode::EventTriggerAND:
179          returnval = checkAnd();
180          break;
181        case TriggerMode::EventTriggerOR:
182          returnval = checkOr();
183          break;
184        case TriggerMode::EventTriggerXOR:
185          returnval = checkXor();
186          break;
187        default:
188          returnval = false;
189          break;
190      }
191//      this->bUpdating_ = false;
192
193      return returnval;
194    }
195    return true;
196  }
197
198  bool Trigger::checkAnd()
199  {
200    std::set<Trigger*>::iterator it;
201    for(it = this->children_.begin(); it != this->children_.end(); ++it)
202    {
203      if (!(*it)->isActive())
204        return false;
205    }
206    return true;
207  }
208
209  bool Trigger::checkOr()
210  {
211    std::set<Trigger*>::iterator it;
212    for(it = this->children_.begin(); it != this->children_.end(); ++it)
213    {
214      if ((*it)->isActive())
215        return true;
216    }
217    return false;
218  }
219
220  bool Trigger::checkXor()
221  {
222    std::set<Trigger*>::iterator it;
223    bool test = false;
224    for(it = this->children_.begin(); it != this->children_.end(); ++it)
225    {
226      if (test && (*it)->isActive())
227        return false;
228      if ((*it)->isActive())
229        test = true;
230    }
231    return test;
232  }
233
234  bool Trigger::switchState()
235  {
236    if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0))
237     || (!(this->latestState_ & 2)                       && (this->remainingActivations_ == 0)))
238      return false;
239    else
240    {
241      this->latestState_ ^= 2; // toggle state bit
242
243      // increase activation count
244      if (this->latestState_ & 2 && this->remainingActivations_ > 0)
245        this->remainingActivations_--;
246
247      this->storeState();
248
249      return true;
250    }
251  }
252
253  void Trigger::storeState()
254  {
255    // put state change into queue
256    this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_));
257    // reset time since last event
258    this->timeSinceLastEvent_ = 0.0;
259
260    if (this->stateChanges_.size() == 1)
261      this->remainingTime_ = this->stateChanges_.front().first;
262  }
263
264  void Trigger::setDelay(float delay)
265  {
266    this->delay_ = delay;
267    this->timeSinceLastEvent_ = delay;
268  }
269
270  void Trigger::setMode(const std::string& modeName)
271  {
272    if (modeName == "and")
273      this->setMode(TriggerMode::EventTriggerAND);
274    else if (modeName == "or")
275      this->setMode(TriggerMode::EventTriggerOR);
276    else if (modeName == "xor")
277      this->setMode(TriggerMode::EventTriggerXOR);
278  }
279
280  std::string Trigger::getModeString() const
281  {
282    if (this->mode_ == TriggerMode::EventTriggerAND)
283      return "and";
284    else if (this->mode_ == TriggerMode::EventTriggerOR)
285      return "or";
286    else if (this->mode_ == TriggerMode::EventTriggerXOR)
287      return "xor";
288    else
289      return "and";
290  }
291
292  void Trigger::addTrigger(Trigger* trigger)
293  {
294    if (this != trigger)
295      this->children_.insert(trigger);
296  }
297
298  const Trigger* Trigger::getTrigger(unsigned int index) const
299  {
300    if (this->children_.size() <= index)
301      return NULL;
302
303    std::set<Trigger*>::const_iterator it;
304    it = this->children_.begin();
305
306    for (unsigned int i = 0; i != index; ++i)
307      ++it;
308
309    return (*it);
310  }
311
312  void Trigger::debugFlares(bool bVisible)
313  {
314    for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
315      it->setVisible(bVisible);
316  }
317
318  void Trigger::setBillboardColour(const ColourValue& colour)
319  {
320    this->debugBillboard_.setColour(colour);
321  }
322
323  void Trigger::changedVisibility()
324  {
325    SUPER(Trigger, changedVisibility);
326
327    this->debugBillboard_.setVisible(this->isVisible());
328  }
329}
Note: See TracBrowser for help on using the repository browser.