Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2184 was 2184, checked in by rgrieder, 16 years ago

Forgot to consider some files wile renaming StaticEntity to PositionableEntity again.

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