[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 | |
---|
| 29 | #include "Trigger.h" |
---|
| 30 | |
---|
[1550] | 31 | #include "core/Debug.h" |
---|
| 32 | |
---|
[1383] | 33 | #include "core/CoreIncludes.h" |
---|
| 34 | |
---|
| 35 | namespace orxonox |
---|
| 36 | { |
---|
| 37 | CreateFactory(Trigger); |
---|
| 38 | |
---|
| 39 | Trigger::Trigger() |
---|
| 40 | { |
---|
| 41 | RegisterObject(Trigger); |
---|
| 42 | |
---|
[1541] | 43 | targetMask_.exclude(Class(BaseObject)); |
---|
[1550] | 44 | |
---|
| 45 | if (getSoftDebugLevel() >= ORX_DEBUG) |
---|
| 46 | { |
---|
| 47 | debugBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); |
---|
| 48 | this->getNode()->attachObject(debugBillboard_.getBillboardSet()); |
---|
| 49 | } |
---|
[1383] | 50 | } |
---|
| 51 | |
---|
| 52 | Trigger::~Trigger() |
---|
| 53 | { |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | bool Trigger::isTriggered() |
---|
| 57 | { |
---|
[1541] | 58 | return this->isTriggered(this->mode_); |
---|
[1383] | 59 | } |
---|
| 60 | |
---|
[1541] | 61 | bool Trigger::isTriggered(TriggerMode mode) |
---|
| 62 | { |
---|
| 63 | switch(mode) |
---|
| 64 | { |
---|
| 65 | case TM_EventTriggerAnd: |
---|
| 66 | return checkAnd(); |
---|
| 67 | break; |
---|
| 68 | case TM_EventTriggerOr: |
---|
| 69 | return checkOr(); |
---|
| 70 | break; |
---|
| 71 | case TM_DelayTrigger: |
---|
| 72 | return checkDelay(); |
---|
| 73 | break; |
---|
| 74 | case TM_DistanceTrigger: |
---|
| 75 | return checkDistance(); |
---|
| 76 | break; |
---|
| 77 | case TM_DistanceEventTriggerAnd: |
---|
| 78 | if (checkDistance() && checkAnd()) |
---|
| 79 | return true; |
---|
| 80 | else |
---|
| 81 | return false; |
---|
| 82 | break; |
---|
| 83 | case TM_DistanceEventTriggerOr: |
---|
| 84 | if (checkDistance() && checkOr()) |
---|
| 85 | return true; |
---|
| 86 | else |
---|
| 87 | return false; |
---|
| 88 | break; |
---|
| 89 | default: |
---|
| 90 | return false; |
---|
| 91 | break; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
[1550] | 95 | void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 96 | { |
---|
| 97 | WorldEntity::XMLPort(xmlelement, mode); |
---|
| 98 | } |
---|
| 99 | |
---|
[1383] | 100 | void Trigger::addTrigger(Trigger* trig) |
---|
| 101 | { |
---|
| 102 | if (this != trig) |
---|
[1550] | 103 | this->subTriggers_.insert(trig); |
---|
[1383] | 104 | } |
---|
| 105 | |
---|
[1541] | 106 | void Trigger::addTargets(std::string targets) |
---|
| 107 | { |
---|
| 108 | Identifier* targetId = ID(targets); |
---|
| 109 | targetMask_.include(targetId); |
---|
| 110 | // trigger shouldn't react on itself or other triggers |
---|
| 111 | targetMask_.exclude(Class(Trigger), true); |
---|
| 112 | } |
---|
| 113 | |
---|
[1550] | 114 | void Trigger::removeTargets(std::string targets) |
---|
| 115 | { |
---|
| 116 | Identifier* targetId = ID(targets); |
---|
| 117 | targetMask_.exclude(targetId); |
---|
| 118 | } |
---|
| 119 | |
---|
[1541] | 120 | bool Trigger::checkAnd() |
---|
| 121 | { |
---|
| 122 | std::set<Trigger*>::iterator it; |
---|
[1550] | 123 | for(it = this->subTriggers_.begin(); it != this->subTriggers_.end(); it++) |
---|
[1541] | 124 | { |
---|
| 125 | if(!((*it)->isTriggered())) |
---|
| 126 | return false; |
---|
| 127 | } |
---|
| 128 | return true; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | bool Trigger::checkOr() |
---|
| 132 | { |
---|
| 133 | std::set<Trigger*>::iterator it; |
---|
[1550] | 134 | for(it = this->subTriggers_.begin(); it != this->subTriggers_.end(); it++) |
---|
[1541] | 135 | { |
---|
| 136 | if((*it)->isTriggered()) |
---|
| 137 | return true; |
---|
| 138 | } |
---|
| 139 | return false; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | bool Trigger::checkDelay() |
---|
| 143 | { |
---|
| 144 | if (triggingTime_ < actualTime_) |
---|
| 145 | return true; |
---|
| 146 | else |
---|
| 147 | return false; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | bool Trigger::checkDistance() |
---|
| 151 | { |
---|
| 152 | // Iterate through all WorldEntities |
---|
| 153 | for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; it++) |
---|
| 154 | { |
---|
| 155 | Vector3 distanceVec = it->getNode()->getWorldPosition() - this->getNode()->getWorldPosition(); |
---|
| 156 | if (distanceVec.length() < radius_) |
---|
| 157 | return true; |
---|
| 158 | } |
---|
| 159 | return false; |
---|
| 160 | |
---|
| 161 | } |
---|
| 162 | |
---|
[1383] | 163 | } |
---|