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