Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_merge/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 2442

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

Finally merged physics stuff. Target is physics_merge because I'll have to do some testing first.

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