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