Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 2425

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