Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 2518

Last change on this file since 2518 was 2361, checked in by landauf, 16 years ago
  • Fixed a problem with SUPER in combination with a pure-virtual base-function when called from a direct child of the base-class.
  • Added random number generator initialization to Core (configurable)
  • Fixed a bug in Convert.h
  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/ceguilua/src/orxonox/objects/Trigger.cc1802-1808
    /code/branches/core3/src/orxonox/objects/Trigger.cc1572-1739
    /code/branches/gcc43/src/orxonox/objects/Trigger.cc1580
    /code/branches/gui/src/orxonox/objects/Trigger.cc1635-1723
    /code/branches/input/src/orxonox/objects/Trigger.cc1629-1636
    /code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/Trigger.cc2100,​2110-2169
    /code/branches/script_trigger/src/orxonox/objects/Trigger.cc1295-1953,​1955
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 "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.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() && this->getScene()->getSceneManager())
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->getNode()->attachObject(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->fireEvent(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->fireEvent(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  bool Trigger::isTriggered(TriggerMode mode)
163  {
164//    if (this->bUpdating_)
165//      return this->bTriggered_;
166
167//    this->bUpdating_ = true;
168    if (this->children_.size() != 0)
169    {
170      bool returnval = false;
171
172      switch (mode)
173      {
174        case TM_EventTriggerAND:
175          returnval = checkAnd();
176          break;
177        case TM_EventTriggerOR:
178          returnval = checkOr();
179          break;
180        case TM_EventTriggerXOR:
181          returnval = checkXor();
182          break;
183        default:
184          returnval = false;
185          break;
186      }
187//      this->bUpdating_ = false;
188
189      return returnval;
190    }
191    return true;
192  }
193
194  bool Trigger::checkAnd()
195  {
196    std::set<Trigger*>::iterator it;
197    for(it = this->children_.begin(); it != this->children_.end(); ++it)
198    {
199      if (!(*it)->isActive())
200        return false;
201    }
202    return true;
203  }
204
205  bool Trigger::checkOr()
206  {
207    std::set<Trigger*>::iterator it;
208    for(it = this->children_.begin(); it != this->children_.end(); ++it)
209    {
210      if ((*it)->isActive())
211        return true;
212    }
213    return false;
214  }
215
216  bool Trigger::checkXor()
217  {
218    std::set<Trigger*>::iterator it;
219    bool test = false;
220    for(it = this->children_.begin(); it != this->children_.end(); ++it)
221    {
222      if (test && (*it)->isActive())
223        return false;
224      if ((*it)->isActive())
225        test = true;
226    }
227    return test;
228  }
229
230  bool Trigger::switchState()
231  {
232    if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0))
233     || (!(this->latestState_ & 2))                      && (this->remainingActivations_ == 0))
234      return false;
235    else
236    {
237      this->latestState_ ^= 2; // toggle state bit
238
239      // increase activation count
240      if (this->latestState_ & 2 && this->remainingActivations_ > 0)
241        this->remainingActivations_--;
242
243      this->storeState();
244
245      return true;
246    }
247  }
248
249  void Trigger::storeState()
250  {
251    // put state change into queue
252    this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_));
253    // reset time since last event
254    this->timeSinceLastEvent_ = 0.0;
255
256    if (this->stateChanges_.size() == 1)
257      this->remainingTime_ = this->stateChanges_.front().first;
258  }
259
260  void Trigger::setDelay(float delay)
261  {
262    this->delay_ = delay;
263    this->timeSinceLastEvent_ = delay;
264  }
265
266  void Trigger::setMode(const std::string& modeName)
267  {
268    if (modeName == "and")
269      this->setMode(TM_EventTriggerAND);
270    else if (modeName == "or")
271      this->setMode(TM_EventTriggerOR);
272    else if (modeName == "xor")
273      this->setMode(TM_EventTriggerXOR);
274  }
275
276  std::string Trigger::getModeString() const
277  {
278    if (this->mode_ == TM_EventTriggerAND)
279      return std::string("and");
280    else if (this->mode_ == TM_EventTriggerOR)
281      return std::string("or");
282    else if (this->mode_ == TM_EventTriggerXOR)
283      return std::string("xor");
284    else
285      return std::string("and");
286  }
287
288  void Trigger::addTrigger(Trigger* trigger)
289  {
290    if (this != trigger)
291      this->children_.insert(trigger);
292  }
293
294  const Trigger* Trigger::getTrigger(unsigned int index) const
295  {
296    if (this->children_.size() <= index)
297      return NULL;
298
299    std::set<Trigger*>::const_iterator it;
300    it = this->children_.begin();
301
302    for (unsigned int i = 0; i != index; ++i)
303      ++it;
304
305    return (*it);
306  }
307
308  void Trigger::debugFlares(bool bVisible)
309  {
310    for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
311      it->setVisible(bVisible);
312  }
313
314  void Trigger::setBillboardColour(const ColourValue& colour)
315  {
316    this->debugBillboard_.setColour(colour);
317  }
318
319  void Trigger::changedVisibility()
320  {
321    SUPER(Trigger, changedVisibility);
322
323    this->debugBillboard_.setVisible(this->isVisible());
324  }
325}
Note: See TracBrowser for help on using the repository browser.