[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; |
---|
| 56 | bStayTriggered_ = false; |
---|
| 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 | |
---|
| 81 | void Trigger::tick(float dt) |
---|
| 82 | { |
---|
[1693] | 83 | |
---|
[1851] | 84 | bool newTriggered; |
---|
| 85 | if (latestState_ % 2 == 1 && this->bStayTriggered_) |
---|
| 86 | newTriggered = true; |
---|
| 87 | else |
---|
| 88 | newTriggered = this->isTriggered(); |
---|
[1693] | 89 | |
---|
| 90 | |
---|
[1851] | 91 | // check if new triggering event is really new |
---|
| 92 | if(this->latestState_ % 2 != newTriggered) |
---|
[1671] | 93 | { |
---|
[1851] | 94 | // create new state |
---|
| 95 | if(newTriggered) |
---|
| 96 | { |
---|
| 97 | latestState_ |= 1; // set trigger bit |
---|
| 98 | this->switchState(); |
---|
| 99 | } |
---|
| 100 | else |
---|
| 101 | { |
---|
| 102 | latestState_ &= 0xFE; // set trigger bit |
---|
| 103 | this->storeState(); |
---|
| 104 | } |
---|
[1671] | 105 | } |
---|
[1693] | 106 | |
---|
| 107 | if(remainingTime_ > 0.0) |
---|
| 108 | { |
---|
| 109 | remainingTime_ -= dt; |
---|
| 110 | // only increase when acctually waiting for a state in the queue |
---|
| 111 | if(timeSinceLastEvent_ >= 0.0) |
---|
| 112 | timeSinceLastEvent_ += dt; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | while(remainingTime_ <= 0.0 && stateChanges_.size() > 0) |
---|
| 116 | { |
---|
| 117 | // time ran out, change state to new one |
---|
| 118 | char newState = stateChanges_.front().second; |
---|
| 119 | bTriggered_ = newState % 2; |
---|
[1851] | 120 | bActive_ = newState & 2; |
---|
[1693] | 121 | this->stateChanges_.pop(); |
---|
| 122 | if(stateChanges_.size() != 0) |
---|
| 123 | remainingTime_ = stateChanges_.front().first; |
---|
| 124 | else |
---|
| 125 | timeSinceLastEvent_ = delay_; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | if (bTriggered_ && bActive_) |
---|
| 131 | this->setBillboardColour(ColourValue(0.5, 1.0, 0.0)); |
---|
| 132 | else if (!bTriggered_ && bActive_) |
---|
| 133 | this->setBillboardColour(ColourValue(0.0, 1.0, 0.0)); |
---|
| 134 | else if (bTriggered_ && !bActive_) |
---|
| 135 | this->setBillboardColour(ColourValue(1.0, 0.5, 0.0)); |
---|
| 136 | else |
---|
| 137 | this->setBillboardColour(ColourValue(1.0, 0.0, 0.0)); |
---|
| 138 | bUpdating_ = false; |
---|
[1671] | 139 | } |
---|
| 140 | |
---|
[1693] | 141 | void Trigger::setBillboardColour(ColourValue colour) |
---|
| 142 | { |
---|
| 143 | this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour); |
---|
| 144 | } |
---|
| 145 | |
---|
[1851] | 146 | void Trigger::storeState() |
---|
| 147 | { |
---|
| 148 | // put state change into queue |
---|
| 149 | this->stateChanges_.push(std::pair<float,char>(timeSinceLastEvent_, latestState_)); |
---|
| 150 | // reset time since last event |
---|
| 151 | timeSinceLastEvent_ = 0.0; |
---|
| 152 | |
---|
| 153 | if(this->stateChanges_.size() == 1) |
---|
| 154 | remainingTime_ = stateChanges_.front().first; |
---|
| 155 | } |
---|
| 156 | |
---|
[1541] | 157 | bool Trigger::isTriggered(TriggerMode mode) |
---|
| 158 | { |
---|
[1693] | 159 | if(bUpdating_) |
---|
| 160 | return bTriggered_; |
---|
| 161 | |
---|
[1671] | 162 | if( children_.size() != 0 ) |
---|
[1541] | 163 | { |
---|
[1693] | 164 | bUpdating_ = true; |
---|
| 165 | bool returnval = false; |
---|
[1671] | 166 | switch(mode) |
---|
| 167 | { |
---|
| 168 | case TM_EventTriggerAND: |
---|
[1693] | 169 | returnval = checkAnd(); |
---|
[1671] | 170 | break; |
---|
| 171 | case TM_EventTriggerOR: |
---|
[1693] | 172 | returnval = checkOr(); |
---|
[1671] | 173 | break; |
---|
| 174 | case TM_EventTriggerXOR: |
---|
[1693] | 175 | returnval = checkXor(); |
---|
[1671] | 176 | break; |
---|
| 177 | default: |
---|
[1693] | 178 | returnval = false; |
---|
[1671] | 179 | break; |
---|
| 180 | } |
---|
[1693] | 181 | if(bInvertMode_) |
---|
| 182 | return !returnval; |
---|
| 183 | else |
---|
| 184 | return returnval; |
---|
[1541] | 185 | } |
---|
[1671] | 186 | return true; |
---|
[1541] | 187 | } |
---|
| 188 | |
---|
[1693] | 189 | void Trigger::setDelay(float delay) |
---|
| 190 | { |
---|
| 191 | this->delay_ = delay; |
---|
| 192 | } |
---|
| 193 | |
---|
[1550] | 194 | void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 195 | { |
---|
| 196 | WorldEntity::XMLPort(xmlelement, mode); |
---|
[1693] | 197 | |
---|
| 198 | XMLPortParamLoadOnly(Trigger, "delay", setDelay, xmlelement, mode); |
---|
[1851] | 199 | XMLPortParamLoadOnly(Trigger, "stayTriggered", setStayTriggered, xmlelement, mode); |
---|
| 200 | XMLPortParamLoadOnly(Trigger, "activations", setActivations, xmlelement, mode); |
---|
[1693] | 201 | |
---|
| 202 | this->init(); |
---|
[1550] | 203 | } |
---|
| 204 | |
---|
[1383] | 205 | void Trigger::addTrigger(Trigger* trig) |
---|
| 206 | { |
---|
| 207 | if (this != trig) |
---|
[1671] | 208 | this->children_.insert(trig); |
---|
[1383] | 209 | } |
---|
| 210 | |
---|
[1693] | 211 | bool Trigger::switchState() |
---|
[1541] | 212 | { |
---|
[1851] | 213 | if ( remainingActivations_ == -1 || this->latestState_ & 2 || remainingActivations_ > 0) |
---|
| 214 | { |
---|
| 215 | this->latestState_ ^= 2; // toggle state bit |
---|
| 216 | // increase activation count |
---|
| 217 | if (this->latestState_ & 2) remainingActivations_--; |
---|
| 218 | this->storeState(); |
---|
[1541] | 219 | |
---|
[1851] | 220 | return true; |
---|
| 221 | } |
---|
| 222 | return false; |
---|
[1550] | 223 | } |
---|
| 224 | |
---|
[1693] | 225 | |
---|
[1541] | 226 | bool Trigger::checkAnd() |
---|
| 227 | { |
---|
| 228 | std::set<Trigger*>::iterator it; |
---|
[1671] | 229 | for(it = this->children_.begin(); it != this->children_.end(); it++) |
---|
[1541] | 230 | { |
---|
[1851] | 231 | if(!((*it)->isActive())) |
---|
[1541] | 232 | return false; |
---|
| 233 | } |
---|
| 234 | return true; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | bool Trigger::checkOr() |
---|
| 238 | { |
---|
| 239 | std::set<Trigger*>::iterator it; |
---|
[1671] | 240 | for(it = this->children_.begin(); it != this->children_.end(); it++) |
---|
[1541] | 241 | { |
---|
[1851] | 242 | if((*it)->isActive()) |
---|
[1541] | 243 | return true; |
---|
| 244 | } |
---|
| 245 | return false; |
---|
| 246 | } |
---|
| 247 | |
---|
[1671] | 248 | bool Trigger::checkXor() |
---|
| 249 | { |
---|
| 250 | std::set<Trigger*>::iterator it; |
---|
| 251 | bool test = false; |
---|
| 252 | for(it = this->children_.begin(); it != this->children_.end(); it++) |
---|
| 253 | { |
---|
[1851] | 254 | if(test && (*it)->isActive()) |
---|
[1671] | 255 | return false; |
---|
[1851] | 256 | if((*it)->isActive()) |
---|
[1671] | 257 | test = true; |
---|
| 258 | } |
---|
| 259 | return test; |
---|
| 260 | } |
---|
| 261 | |
---|
[1383] | 262 | } |
---|