| 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 "OrxonoxStableHeaders.h" |
|---|
| 30 | #include "Trigger.h" |
|---|
| 31 | |
|---|
| 32 | #include <OgreBillboard.h> |
|---|
| 33 | #include "core/Debug.h" |
|---|
| 34 | #include "core/CoreIncludes.h" |
|---|
| 35 | #include "core/ConsoleCommand.h" |
|---|
| 36 | #include "core/XMLPort.h" |
|---|
| 37 | |
|---|
| 38 | namespace orxonox |
|---|
| 39 | { |
|---|
| 40 | |
|---|
| 41 | ConsoleCommand(Trigger, setVisibility, AccessLevel::Debug, false).setDefaultValues(0); |
|---|
| 42 | |
|---|
| 43 | CreateFactory(Trigger); |
|---|
| 44 | |
|---|
| 45 | Trigger::Trigger() |
|---|
| 46 | { |
|---|
| 47 | RegisterObject(Trigger); |
|---|
| 48 | |
|---|
| 49 | mode_ = TM_EventTriggerAND; |
|---|
| 50 | bActive_ = false; |
|---|
| 51 | bInvertMode_ = false; |
|---|
| 52 | delay_ = 0.0; |
|---|
| 53 | bTriggered_ = false; |
|---|
| 54 | bUpdating_ = false; |
|---|
| 55 | remainingActivations_ = -1; |
|---|
| 56 | bStayTriggered_ = false; |
|---|
| 57 | latestState_ = 0x0; |
|---|
| 58 | |
|---|
| 59 | debugBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); |
|---|
| 60 | this->getNode()->attachObject(debugBillboard_.getBillboardSet()); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | Trigger::~Trigger() |
|---|
| 64 | { |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void Trigger::init() |
|---|
| 68 | { |
|---|
| 69 | this->setVisibility(true); |
|---|
| 70 | timeSinceLastEvent_ = delay_; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void Trigger::setVisibility(bool bVisible) |
|---|
| 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 | { |
|---|
| 83 | |
|---|
| 84 | bool newTriggered; |
|---|
| 85 | if (latestState_ % 2 == 1 && this->bStayTriggered_) |
|---|
| 86 | newTriggered = true; |
|---|
| 87 | else |
|---|
| 88 | newTriggered = this->isTriggered(); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | // check if new triggering event is really new |
|---|
| 92 | if(this->latestState_ % 2 != newTriggered) |
|---|
| 93 | { |
|---|
| 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 | } |
|---|
| 105 | } |
|---|
| 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; |
|---|
| 120 | bActive_ = newState & 2; |
|---|
| 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; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | void Trigger::setBillboardColour(ColourValue colour) |
|---|
| 142 | { |
|---|
| 143 | this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 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 | |
|---|
| 157 | bool Trigger::isTriggered(TriggerMode mode) |
|---|
| 158 | { |
|---|
| 159 | if(bUpdating_) |
|---|
| 160 | return bTriggered_; |
|---|
| 161 | |
|---|
| 162 | if( children_.size() != 0 ) |
|---|
| 163 | { |
|---|
| 164 | bUpdating_ = true; |
|---|
| 165 | bool returnval = false; |
|---|
| 166 | switch(mode) |
|---|
| 167 | { |
|---|
| 168 | case TM_EventTriggerAND: |
|---|
| 169 | returnval = checkAnd(); |
|---|
| 170 | break; |
|---|
| 171 | case TM_EventTriggerOR: |
|---|
| 172 | returnval = checkOr(); |
|---|
| 173 | break; |
|---|
| 174 | case TM_EventTriggerXOR: |
|---|
| 175 | returnval = checkXor(); |
|---|
| 176 | break; |
|---|
| 177 | default: |
|---|
| 178 | returnval = false; |
|---|
| 179 | break; |
|---|
| 180 | } |
|---|
| 181 | if(bInvertMode_) |
|---|
| 182 | return !returnval; |
|---|
| 183 | else |
|---|
| 184 | return returnval; |
|---|
| 185 | } |
|---|
| 186 | return true; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | void Trigger::setDelay(float delay) |
|---|
| 190 | { |
|---|
| 191 | this->delay_ = delay; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 195 | { |
|---|
| 196 | WorldEntity::XMLPort(xmlelement, mode); |
|---|
| 197 | |
|---|
| 198 | XMLPortParamLoadOnly(Trigger, "delay", setDelay, xmlelement, mode); |
|---|
| 199 | XMLPortParamLoadOnly(Trigger, "stayTriggered", setStayTriggered, xmlelement, mode); |
|---|
| 200 | XMLPortParamLoadOnly(Trigger, "activations", setActivations, xmlelement, mode); |
|---|
| 201 | |
|---|
| 202 | this->init(); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | void Trigger::addTrigger(Trigger* trig) |
|---|
| 206 | { |
|---|
| 207 | if (this != trig) |
|---|
| 208 | this->children_.insert(trig); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | bool Trigger::switchState() |
|---|
| 212 | { |
|---|
| 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(); |
|---|
| 219 | |
|---|
| 220 | return true; |
|---|
| 221 | } |
|---|
| 222 | return false; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | bool Trigger::checkAnd() |
|---|
| 227 | { |
|---|
| 228 | std::set<Trigger*>::iterator it; |
|---|
| 229 | for(it = this->children_.begin(); it != this->children_.end(); it++) |
|---|
| 230 | { |
|---|
| 231 | if(!((*it)->isActive())) |
|---|
| 232 | return false; |
|---|
| 233 | } |
|---|
| 234 | return true; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | bool Trigger::checkOr() |
|---|
| 238 | { |
|---|
| 239 | std::set<Trigger*>::iterator it; |
|---|
| 240 | for(it = this->children_.begin(); it != this->children_.end(); it++) |
|---|
| 241 | { |
|---|
| 242 | if((*it)->isActive()) |
|---|
| 243 | return true; |
|---|
| 244 | } |
|---|
| 245 | return false; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 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 | { |
|---|
| 254 | if(test && (*it)->isActive()) |
|---|
| 255 | return false; |
|---|
| 256 | if((*it)->isActive()) |
|---|
| 257 | test = true; |
|---|
| 258 | } |
|---|
| 259 | return test; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | } |
|---|