Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2051 was 2031, checked in by landauf, 16 years ago

some small adjustments in the network

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