Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spacerace/src/modules/objects/triggers/Trigger.cc @ 8572

Last change on this file since 8572 was 8572, checked in by dafrick, 14 years ago

Adding some useful debug output.

  • Property svn:eol-style set to native
File size: 7.2 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/**
30    @file Trigger.cc
31    @brief Implementation of the Trigger class.
32    @ingroup NormalTrigger
33*/
34
35#include "Trigger.h"
36
37#include "core/CoreIncludes.h"
38#include "core/GameMode.h"
39#include "core/XMLPort.h"
40#include "core/command/ConsoleCommand.h"
41#include "Scene.h"
42
43namespace orxonox
44{
45
46  SetConsoleCommand("Trigger", "debugFlares", &Trigger::debugFlares).defaultValues(false);
47
48  CreateFactory(Trigger);
49
50  Trigger::Trigger(BaseObject* creator) : TriggerBase(creator)
51  {
52    RegisterObject(Trigger);
53
54    this->bActive_ = false;
55    this->bTriggered_ = false;
56    this->latestState_ = 0x0;
57
58    this->remainingTime_ = 0.0f;
59    this->timeSinceLastEvent_ = 0.0f;
60
61//    this->bUpdating_ = false;
62
63    if (this->getScene() && GameMode::showsGraphics())
64    {
65      this->debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
66      this->debugBillboard_.setVisible(false);
67
68      if (this->debugBillboard_.getBillboardSet())
69          this->attachOgreObject(this->debugBillboard_.getBillboardSet());
70    }
71
72    this->setSyncMode(0x0);
73  }
74
75  Trigger::~Trigger()
76  {
77  }
78
79  void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
80  {
81    SUPER(Trigger, XMLPort, xmlelement, mode);
82  }
83
84  void Trigger::tick(float dt)
85  {
86    if(this->bFirstTick_)
87    {
88      this->bFirstTick_ = false;
89      this->triggered(false);
90    }
91
92    // Check if the object is active (this is NOT Trigger::isActive()!)
93    if (!this->BaseObject::isActive())
94        return;
95
96    SUPER(Trigger, tick, dt);
97
98    bool newTriggered = this->isTriggered() ^ this->getInvert();
99
100    // check if new triggering event is really new
101    if ((this->latestState_ & 0x1) != newTriggered)
102    {
103      // create new state
104      if (newTriggered)
105      {
106        this->latestState_ |= 1; // set trigger bit to 1
107        this->switchState();
108      }
109      else
110      {
111        this->latestState_ &= 0xFE; // set trigger bit to 0
112        if (!this->getSwitch())
113          this->switchState();
114      }
115    }
116
117    if (this->remainingTime_ > 0.0)
118    {
119      this->remainingTime_ -= dt;
120      // only increase when actually waiting for a state in the queue
121      if (this->timeSinceLastEvent_ >= 0.0)
122        this->timeSinceLastEvent_ += dt;
123    }
124
125    while (this->remainingTime_ <= 0.0 && this->stateChanges_.size() > 0)
126    {
127      // time ran out, change state to new one
128      char newState = this->stateChanges_.front().second;
129      this->bTriggered_ = (newState & 0x1);
130      this->bActive_ = newState & 2;
131      COUT(4) << this->getIdentifier()->getName() << " '" << this->getName() << "' (&" << this << ") changed state. active: " << this->bActive_ << ", triggered: " << this->bTriggered_ << "." << std::endl;
132      this->triggered(this->bActive_);
133      this->stateChanges_.pop();
134      if (this->stateChanges_.size() != 0)
135        this->remainingTime_ = this->stateChanges_.front().first;
136      else
137        this->timeSinceLastEvent_ = this->getDelay();
138    }
139
140    if (this->bTriggered_ && this->bActive_)
141      this->setBillboardColour(ColourValue(0.5, 1.0, 0.0));
142    else if (!this->bTriggered_ && this->bActive_)
143      this->setBillboardColour(ColourValue(0.0, 1.0, 0.0));
144    else if (this->bTriggered_ && !this->bActive_)
145      this->setBillboardColour(ColourValue(1.0, 0.5, 0.0));
146    else
147      this->setBillboardColour(ColourValue(1.0, 0.0, 0.0));
148  }
149
150  void Trigger::triggered(bool bIsTriggered)
151  {
152    this->fireEvent(bIsTriggered);
153  }
154
155  bool Trigger::isTriggered(TriggerMode::Value mode)
156  {
157//    if (this->bUpdating_)
158//      return this->bTriggered_;
159
160//    this->bUpdating_ = true;
161    if (this->children_.size() != 0)
162    {
163      bool returnval = false;
164
165      switch (mode)
166      {
167        case TriggerMode::EventTriggerAND:
168          returnval = checkAnd();
169          break;
170        case TriggerMode::EventTriggerOR:
171          returnval = checkOr();
172          break;
173        case TriggerMode::EventTriggerXOR:
174          returnval = checkXor();
175          break;
176        default:
177          returnval = false;
178          break;
179      }
180//      this->bUpdating_ = false;
181
182      return returnval;
183    }
184    return true;
185  }
186
187  bool Trigger::checkAnd()
188  {
189    std::set<TriggerBase*>::iterator it;
190    for(it = this->children_.begin(); it != this->children_.end(); ++it)
191    {
192      if (!(*it)->isActive())
193        return false;
194    }
195    return true;
196  }
197
198  bool Trigger::checkOr()
199  {
200    std::set<TriggerBase*>::iterator it;
201    for(it = this->children_.begin(); it != this->children_.end(); ++it)
202    {
203      if ((*it)->isActive())
204        return true;
205    }
206    return false;
207  }
208
209  bool Trigger::checkXor()
210  {
211    std::set<TriggerBase*>::iterator it;
212    bool test = false;
213    for(it = this->children_.begin(); it != this->children_.end(); ++it)
214    {
215      if (test && (*it)->isActive())
216        return false;
217      if ((*it)->isActive())
218        test = true;
219    }
220    return test;
221  }
222
223  bool Trigger::switchState()
224  {
225    if (( (this->latestState_ & 2) && this->getStayActive() && (this->remainingActivations_ <= 0))
226     || (!(this->latestState_ & 2)                          && (this->remainingActivations_ == 0)))
227      return false;
228    else
229    {
230      this->latestState_ ^= 2; // toggle state bit
231
232      // increase activation count
233      if (this->latestState_ & 2 && this->remainingActivations_ > 0)
234        this->remainingActivations_--;
235
236      this->storeState();
237
238      return true;
239    }
240  }
241
242  void Trigger::storeState()
243  {
244    // put state change into queue
245    this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_));
246    // reset time since last event
247    this->timeSinceLastEvent_ = 0.0;
248
249    if (this->stateChanges_.size() == 1)
250      this->remainingTime_ = this->stateChanges_.front().first;
251  }
252
253  void Trigger::delayChanged(void)
254  {
255    this->timeSinceLastEvent_ = this->getDelay();
256  }
257
258  void Trigger::debugFlares(bool bVisible)
259  {
260    for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it)
261      it->setVisible(bVisible);
262  }
263
264  void Trigger::setBillboardColour(const ColourValue& colour)
265  {
266    this->debugBillboard_.setColour(colour);
267  }
268
269  void Trigger::changedVisibility()
270  {
271    SUPER(Trigger, changedVisibility);
272
273    this->debugBillboard_.setVisible(this->isVisible());
274  }
275}
Note: See TracBrowser for help on using the repository browser.