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