Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 2206

Last change on this file since 2206 was 2161, checked in by landauf, 16 years ago
  • added more exceptions to handle problems while loading a level or single objects
  • dedicated server runs and clients may join, but there are still some heavy problems
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 "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    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->fireEvent(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  bool Trigger::isTriggered(TriggerMode mode)
161  {
162//    if (this->bUpdating_)
163//      return this->bTriggered_;
164
165//    this->bUpdating_ = true;
166    if (this->children_.size() != 0)
167    {
168      bool returnval = false;
169
170      switch (mode)
171      {
172        case TM_EventTriggerAND:
173          returnval = checkAnd();
174          break;
175        case TM_EventTriggerOR:
176          returnval = checkOr();
177          break;
178        case TM_EventTriggerXOR:
179          returnval = checkXor();
180          break;
181        default:
182          returnval = false;
183          break;
184      }
185//      this->bUpdating_ = false;
186
187      return returnval;
188    }
189    return true;
190  }
191
192  bool Trigger::checkAnd()
193  {
194    std::set<Trigger*>::iterator it;
195    for(it = this->children_.begin(); it != this->children_.end(); ++it)
196    {
197      if (!(*it)->isActive())
198        return false;
199    }
200    return true;
201  }
202
203  bool Trigger::checkOr()
204  {
205    std::set<Trigger*>::iterator it;
206    for(it = this->children_.begin(); it != this->children_.end(); ++it)
207    {
208      if ((*it)->isActive())
209        return true;
210    }
211    return false;
212  }
213
214  bool Trigger::checkXor()
215  {
216    std::set<Trigger*>::iterator it;
217    bool test = false;
218    for(it = this->children_.begin(); it != this->children_.end(); ++it)
219    {
220      if (test && (*it)->isActive())
221        return false;
222      if ((*it)->isActive())
223        test = true;
224    }
225    return test;
226  }
227
228  bool Trigger::switchState()
229  {
230    if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0))
231     || (!(this->latestState_ & 2))                      && (this->remainingActivations_ == 0))
232      return false;
233    else
234    {
235      this->latestState_ ^= 2; // toggle state bit
236
237      // increase activation count
238      if (this->latestState_ & 2 && this->remainingActivations_ > 0)
239        this->remainingActivations_--;
240
241      this->storeState();
242
243      return true;
244    }
245  }
246
247  void Trigger::storeState()
248  {
249    // put state change into queue
250    this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_));
251    // reset time since last event
252    this->timeSinceLastEvent_ = 0.0;
253
254    if (this->stateChanges_.size() == 1)
255      this->remainingTime_ = this->stateChanges_.front().first;
256  }
257
258  void Trigger::setDelay(float delay)
259  {
260    this->delay_ = delay;
261    this->timeSinceLastEvent_ = delay;
262  }
263
264  void Trigger::setMode(const std::string& modeName)
265  {
266    if (modeName == "and")
267      this->setMode(TM_EventTriggerAND);
268    else if (modeName == "or")
269      this->setMode(TM_EventTriggerOR);
270    else if (modeName == "xor")
271      this->setMode(TM_EventTriggerXOR);
272  }
273
274  std::string Trigger::getModeString() const
275  {
276    if (this->mode_ == TM_EventTriggerAND)
277      return std::string("and");
278    else if (this->mode_ == TM_EventTriggerOR)
279      return std::string("or");
280    else if (this->mode_ == TM_EventTriggerXOR)
281      return std::string("xor");
282    else
283      return std::string("and");
284  }
285
286  void Trigger::addTrigger(Trigger* trigger)
287  {
288    if (this != trigger)
289      this->children_.insert(trigger);
290  }
291
292  const Trigger* Trigger::getTrigger(unsigned int index) const
293  {
294    if (this->children_.size() <= index)
295      return NULL;
296
297    std::set<Trigger*>::const_iterator it;
298    it = this->children_.begin();
299
300    for (unsigned int i = 0; i != index; ++i)
301      ++it;
302
303    return (*it);
304  }
305
306  void Trigger::debugFlares(bool bVisible)
307  {
308    for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
309      it->setVisible(bVisible);
310  }
311
312  void Trigger::setBillboardColour(const ColourValue& colour)
313  {
314    this->debugBillboard_.setColour(colour);
315  }
316
317  void Trigger::changedVisibility()
318  {
319    SUPER(Trigger, changedVisibility);
320
321    this->debugBillboard_.setVisible(this->isVisible());
322  }
323}
Note: See TracBrowser for help on using the repository browser.