Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/worldentities/triggers/Trigger.cc @ 3177

Last change on this file since 3177 was 3159, checked in by rgrieder, 15 years ago

Several small fixes and some removed warnings

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