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