Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 3050

Last change on this file since 3050 was 3033, checked in by landauf, 16 years ago

merged gametypes branch back to trunk

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