[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 | |
---|
[1906] | 29 | #include "OrxonoxStableHeaders.h" |
---|
[1383] | 30 | #include "Trigger.h" |
---|
| 31 | |
---|
[1906] | 32 | #include <OgreBillboard.h> |
---|
[1961] | 33 | #include "util/Debug.h" |
---|
[1383] | 34 | #include "core/CoreIncludes.h" |
---|
[1671] | 35 | #include "core/ConsoleCommand.h" |
---|
[1693] | 36 | #include "core/XMLPort.h" |
---|
[2019] | 37 | #include "objects/Scene.h" |
---|
[1383] | 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
[1671] | 41 | |
---|
[1969] | 42 | SetConsoleCommand(Trigger, debugFlares, false).defaultValues(false); |
---|
[1671] | 43 | |
---|
[1383] | 44 | CreateFactory(Trigger); |
---|
| 45 | |
---|
[2019] | 46 | Trigger::Trigger(BaseObject* creator) : PositionableEntity(creator) |
---|
[1383] | 47 | { |
---|
| 48 | RegisterObject(Trigger); |
---|
| 49 | |
---|
[2029] | 50 | this->mode_ = TM_EventTriggerAND; |
---|
[1671] | 51 | |
---|
[2069] | 52 | this->bFirstTick_ = true; |
---|
[2029] | 53 | this->bActive_ = false; |
---|
| 54 | this->bTriggered_ = false; |
---|
| 55 | this->latestState_ = 0x0; |
---|
| 56 | |
---|
| 57 | this->bInvertMode_ = false; |
---|
| 58 | this->bSwitch_ = false; |
---|
| 59 | this->bStayActive_ = false; |
---|
[2071] | 60 | this->delay_ = 0.0f; |
---|
| 61 | this->remainingTime_ = 0.0f; |
---|
| 62 | this->timeSinceLastEvent_ = 0.0f; |
---|
[2029] | 63 | this->remainingActivations_ = -1; |
---|
| 64 | |
---|
| 65 | // this->bUpdating_ = false; |
---|
| 66 | |
---|
[2019] | 67 | if (this->getScene() && this->getScene()->getSceneManager()) |
---|
| 68 | { |
---|
[2029] | 69 | this->debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); |
---|
| 70 | this->debugBillboard_.setVisible(false); |
---|
[2171] | 71 | |
---|
| 72 | if (this->debugBillboard_.getBillboardSet()) |
---|
| 73 | this->getNode()->attachObject(this->debugBillboard_.getBillboardSet()); |
---|
[2019] | 74 | } |
---|
[1969] | 75 | |
---|
[2031] | 76 | this->setObjectMode(0x0); |
---|
[1383] | 77 | } |
---|
| 78 | |
---|
| 79 | Trigger::~Trigger() |
---|
| 80 | { |
---|
| 81 | } |
---|
| 82 | |
---|
[2029] | 83 | void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[1383] | 84 | { |
---|
[2029] | 85 | SUPER(Trigger, XMLPort, xmlelement, mode); |
---|
[1383] | 86 | |
---|
[2029] | 87 | XMLPortParam(Trigger, "delay", setDelay, getDelay, xmlelement, mode).defaultValues(0.0f); |
---|
| 88 | XMLPortParam(Trigger, "switch", setSwitch, getSwitch, xmlelement, mode).defaultValues(false); |
---|
| 89 | XMLPortParam(Trigger, "stayactive", setStayActive, getStayActive, xmlelement, mode).defaultValues(false); |
---|
| 90 | XMLPortParam(Trigger, "activations", setActivations, getActivations, xmlelement, mode).defaultValues(-1); |
---|
| 91 | XMLPortParam(Trigger, "invert", setInvert, getInvert, xmlelement, mode).defaultValues(false); |
---|
| 92 | XMLPortParamTemplate(Trigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&).defaultValues("or"); |
---|
[1671] | 93 | |
---|
[2029] | 94 | XMLPortObject(Trigger, Trigger, "", addTrigger, getTrigger, xmlelement, mode); |
---|
[1969] | 95 | } |
---|
| 96 | |
---|
[1671] | 97 | void Trigger::tick(float dt) |
---|
| 98 | { |
---|
[2069] | 99 | if (this->bFirstTick_) |
---|
| 100 | { |
---|
| 101 | this->bFirstTick_ = false; |
---|
| 102 | this->fireEvent(false); |
---|
| 103 | } |
---|
[1693] | 104 | |
---|
[2071] | 105 | // Check if the object is active (this is NOT Trigger::isActive()!) |
---|
| 106 | if (!this->BaseObject::isActive()) |
---|
| 107 | return; |
---|
| 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; |
---|
[2065] | 142 | this->fireEvent(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 | |
---|
[1541] | 160 | bool Trigger::isTriggered(TriggerMode mode) |
---|
| 161 | { |
---|
[2029] | 162 | // if (this->bUpdating_) |
---|
| 163 | // return this->bTriggered_; |
---|
[1693] | 164 | |
---|
[2029] | 165 | // this->bUpdating_ = true; |
---|
| 166 | if (this->children_.size() != 0) |
---|
[1541] | 167 | { |
---|
[1693] | 168 | bool returnval = false; |
---|
[2029] | 169 | |
---|
| 170 | switch (mode) |
---|
[1671] | 171 | { |
---|
| 172 | case TM_EventTriggerAND: |
---|
[1693] | 173 | returnval = checkAnd(); |
---|
[1671] | 174 | break; |
---|
| 175 | case TM_EventTriggerOR: |
---|
[1693] | 176 | returnval = checkOr(); |
---|
[1671] | 177 | break; |
---|
| 178 | case TM_EventTriggerXOR: |
---|
[1693] | 179 | returnval = checkXor(); |
---|
[1671] | 180 | break; |
---|
| 181 | default: |
---|
[1693] | 182 | returnval = false; |
---|
[1671] | 183 | break; |
---|
| 184 | } |
---|
[2029] | 185 | // this->bUpdating_ = false; |
---|
| 186 | |
---|
[2069] | 187 | return returnval; |
---|
[1541] | 188 | } |
---|
[1671] | 189 | return true; |
---|
[1541] | 190 | } |
---|
| 191 | |
---|
[2029] | 192 | bool Trigger::checkAnd() |
---|
| 193 | { |
---|
| 194 | std::set<Trigger*>::iterator it; |
---|
| 195 | for(it = this->children_.begin(); it != this->children_.end(); ++it) |
---|
| 196 | { |
---|
| 197 | if (!(*it)->isActive()) |
---|
| 198 | return false; |
---|
| 199 | } |
---|
| 200 | return true; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | bool Trigger::checkOr() |
---|
| 204 | { |
---|
| 205 | std::set<Trigger*>::iterator it; |
---|
| 206 | for(it = this->children_.begin(); it != this->children_.end(); ++it) |
---|
| 207 | { |
---|
| 208 | if ((*it)->isActive()) |
---|
| 209 | return true; |
---|
| 210 | } |
---|
| 211 | return false; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | bool Trigger::checkXor() |
---|
| 215 | { |
---|
| 216 | std::set<Trigger*>::iterator it; |
---|
| 217 | bool test = false; |
---|
| 218 | for(it = this->children_.begin(); it != this->children_.end(); ++it) |
---|
| 219 | { |
---|
| 220 | if (test && (*it)->isActive()) |
---|
| 221 | return false; |
---|
| 222 | if ((*it)->isActive()) |
---|
| 223 | test = true; |
---|
| 224 | } |
---|
| 225 | return test; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | bool Trigger::switchState() |
---|
| 229 | { |
---|
| 230 | if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0)) |
---|
| 231 | || (!(this->latestState_ & 2)) && (this->remainingActivations_ == 0)) |
---|
| 232 | return false; |
---|
| 233 | else |
---|
| 234 | { |
---|
| 235 | this->latestState_ ^= 2; // toggle state bit |
---|
| 236 | |
---|
| 237 | // increase activation count |
---|
| 238 | if (this->latestState_ & 2 && this->remainingActivations_ > 0) |
---|
| 239 | this->remainingActivations_--; |
---|
| 240 | |
---|
| 241 | this->storeState(); |
---|
| 242 | |
---|
| 243 | return true; |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | void Trigger::storeState() |
---|
| 248 | { |
---|
| 249 | // put state change into queue |
---|
| 250 | this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_)); |
---|
| 251 | // reset time since last event |
---|
| 252 | this->timeSinceLastEvent_ = 0.0; |
---|
| 253 | |
---|
| 254 | if (this->stateChanges_.size() == 1) |
---|
| 255 | this->remainingTime_ = this->stateChanges_.front().first; |
---|
| 256 | } |
---|
| 257 | |
---|
[1693] | 258 | void Trigger::setDelay(float delay) |
---|
| 259 | { |
---|
| 260 | this->delay_ = delay; |
---|
[2029] | 261 | this->timeSinceLastEvent_ = delay; |
---|
[1693] | 262 | } |
---|
| 263 | |
---|
[1969] | 264 | void Trigger::setMode(const std::string& modeName) |
---|
[1954] | 265 | { |
---|
| 266 | if (modeName == "and") |
---|
[2029] | 267 | this->setMode(TM_EventTriggerAND); |
---|
[1954] | 268 | else if (modeName == "or") |
---|
[2029] | 269 | this->setMode(TM_EventTriggerOR); |
---|
[1954] | 270 | else if (modeName == "xor") |
---|
[2029] | 271 | this->setMode(TM_EventTriggerXOR); |
---|
[1954] | 272 | } |
---|
| 273 | |
---|
[2029] | 274 | std::string Trigger::getModeString() const |
---|
[1550] | 275 | { |
---|
[2029] | 276 | if (this->mode_ == TM_EventTriggerAND) |
---|
| 277 | return std::string("and"); |
---|
| 278 | else if (this->mode_ == TM_EventTriggerOR) |
---|
| 279 | return std::string("or"); |
---|
| 280 | else if (this->mode_ == TM_EventTriggerXOR) |
---|
| 281 | return std::string("xor"); |
---|
| 282 | else |
---|
| 283 | return std::string("and"); |
---|
[1550] | 284 | } |
---|
| 285 | |
---|
[2029] | 286 | void Trigger::addTrigger(Trigger* trigger) |
---|
[1383] | 287 | { |
---|
[2029] | 288 | if (this != trigger) |
---|
| 289 | this->children_.insert(trigger); |
---|
[1383] | 290 | } |
---|
| 291 | |
---|
[2029] | 292 | const Trigger* Trigger::getTrigger(unsigned int index) const |
---|
[1541] | 293 | { |
---|
[2029] | 294 | if (this->children_.size() <= index) |
---|
| 295 | return NULL; |
---|
[1541] | 296 | |
---|
[2029] | 297 | std::set<Trigger*>::const_iterator it; |
---|
| 298 | it = this->children_.begin(); |
---|
| 299 | |
---|
| 300 | for (unsigned int i = 0; i != index; ++i) |
---|
| 301 | ++it; |
---|
| 302 | |
---|
| 303 | return (*it); |
---|
[1550] | 304 | } |
---|
| 305 | |
---|
[2029] | 306 | void Trigger::debugFlares(bool bVisible) |
---|
[1541] | 307 | { |
---|
[2029] | 308 | for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it) |
---|
| 309 | it->setVisible(bVisible); |
---|
[1541] | 310 | } |
---|
| 311 | |
---|
[2029] | 312 | void Trigger::setBillboardColour(const ColourValue& colour) |
---|
[1541] | 313 | { |
---|
[2171] | 314 | this->debugBillboard_.setColour(colour); |
---|
[1541] | 315 | } |
---|
| 316 | |
---|
[2029] | 317 | void Trigger::changedVisibility() |
---|
[1671] | 318 | { |
---|
[2029] | 319 | SUPER(Trigger, changedVisibility); |
---|
| 320 | |
---|
| 321 | this->debugBillboard_.setVisible(this->isVisible()); |
---|
[1671] | 322 | } |
---|
[1383] | 323 | } |
---|