[1693] | 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 | |
---|
[7601] | 29 | /** |
---|
| 30 | @file DistanceTrigger.cc |
---|
| 31 | @brief Implementation of the DistanceTrigger class. |
---|
| 32 | @ingroup NormalTrigger |
---|
| 33 | */ |
---|
| 34 | |
---|
[1693] | 35 | #include "DistanceTrigger.h" |
---|
| 36 | |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
[8213] | 39 | |
---|
[5735] | 40 | #include "worldentities/pawns/Pawn.h" |
---|
[8213] | 41 | |
---|
[6906] | 42 | #include "DistanceTriggerBeacon.h" |
---|
[2261] | 43 | |
---|
[2071] | 44 | namespace orxonox |
---|
| 45 | { |
---|
[1693] | 46 | |
---|
[8213] | 47 | /*static*/ const std::string DistanceTrigger::beaconModeOff_s = "off"; |
---|
| 48 | /*static*/ const std::string DistanceTrigger::beaconModeIdentify_s = "identify"; |
---|
| 49 | /*static*/ const std::string DistanceTrigger::beaconModeExlcude_s = "exclude"; |
---|
[1693] | 50 | |
---|
[9667] | 51 | RegisterClass(DistanceTrigger); |
---|
[1693] | 52 | |
---|
[8213] | 53 | /** |
---|
| 54 | @brief |
---|
| 55 | Constructor. Registers and initializes the object. |
---|
| 56 | */ |
---|
[9667] | 57 | DistanceTrigger::DistanceTrigger(Context* context) : Trigger(context) |
---|
[8213] | 58 | { |
---|
| 59 | RegisterObject(DistanceTrigger); |
---|
[1693] | 60 | |
---|
[8213] | 61 | this->distance_ = 100; |
---|
| 62 | this->targetMask_.exclude(Class(BaseObject)); |
---|
| 63 | this->targetName_ = ""; |
---|
[8706] | 64 | this->beaconMask_.exclude(Class(BaseObject)); |
---|
| 65 | this->beaconMask_.include(Class(DistanceTriggerBeacon)); |
---|
[8213] | 66 | } |
---|
[2029] | 67 | |
---|
[8213] | 68 | /** |
---|
| 69 | @brief |
---|
| 70 | Destructor. |
---|
| 71 | */ |
---|
| 72 | DistanceTrigger::~DistanceTrigger() |
---|
[1693] | 73 | { |
---|
[8706] | 74 | |
---|
[1693] | 75 | } |
---|
| 76 | |
---|
[8213] | 77 | /** |
---|
| 78 | @brief |
---|
| 79 | Method for creating a DistanceTrigger object through XML. |
---|
| 80 | */ |
---|
| 81 | void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 82 | { |
---|
| 83 | SUPER(DistanceTrigger, XMLPort, xmlelement, mode); |
---|
[3034] | 84 | |
---|
[8213] | 85 | XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f); |
---|
| 86 | XMLPortParamLoadOnly(DistanceTrigger, "target", addTarget, xmlelement, mode).defaultValues("Pawn"); |
---|
| 87 | XMLPortParam(DistanceTrigger, "beaconMode", setBeaconMode, getBeaconMode, xmlelement, mode); |
---|
| 88 | XMLPortParam(DistanceTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode); |
---|
[2261] | 89 | } |
---|
[3034] | 90 | |
---|
[8213] | 91 | /** |
---|
| 92 | @brief |
---|
| 93 | Add some target to the DistanceTrigger. |
---|
| 94 | @param targetStr |
---|
| 95 | The target class name as a string. |
---|
| 96 | */ |
---|
| 97 | void DistanceTrigger::addTarget(const std::string& targetStr) |
---|
[2071] | 98 | { |
---|
[8213] | 99 | Identifier* targetId = ClassByString(targetStr); |
---|
[2069] | 100 | |
---|
[8213] | 101 | // Checks whether the target is (or is derived from) a Pawn and if so set the PlayerTrigger aspect of this trigger to be for the player, meaning, that from this Trigger one can derive the Pawn that caused it to trigger. |
---|
| 102 | Identifier* pawnId = Class(Pawn); |
---|
| 103 | if(targetId->isA(pawnId)) |
---|
| 104 | this->setForPlayer(true); |
---|
[2069] | 105 | |
---|
[11071] | 106 | if (targetId == nullptr) |
---|
[8213] | 107 | { |
---|
[8858] | 108 | orxout(internal_error, context::triggers) << "\"" << targetStr << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << endl; |
---|
[8213] | 109 | return; |
---|
| 110 | } |
---|
[1693] | 111 | |
---|
[8213] | 112 | // Add the target to the target mask. |
---|
| 113 | this->targetMask_.include(targetId); |
---|
[3033] | 114 | |
---|
[8213] | 115 | // The DistanceTrigger shouldn't react to itself or other triggers. |
---|
| 116 | this->targetMask_.exclude(Class(TriggerBase), true); |
---|
[1693] | 117 | |
---|
[8213] | 118 | // We only want WorldEntities (since only they have a position) |
---|
| 119 | ClassTreeMask WEMask; |
---|
| 120 | WEMask.include(Class(WorldEntity)); |
---|
| 121 | this->targetMask_ *= WEMask; |
---|
[1693] | 122 | |
---|
[8213] | 123 | this->notifyMaskUpdate(); // Inform interested parties that the target mask has been updated. |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | @brief |
---|
| 128 | Remove some target from the DistanceTrigger. |
---|
| 129 | @param targetStr |
---|
| 130 | The target class name as a string. |
---|
| 131 | */ |
---|
| 132 | void DistanceTrigger::removeTarget(const std::string& targetStr) |
---|
[1693] | 133 | { |
---|
[8213] | 134 | Identifier* targetId = ClassByString(targetStr); |
---|
| 135 | this->targetMask_.exclude(targetId); |
---|
| 136 | } |
---|
[2069] | 137 | |
---|
[8213] | 138 | /** |
---|
| 139 | @brief |
---|
| 140 | Check, whether there are entities that are targets of this DistanceTrigger in its range. |
---|
| 141 | @return |
---|
| 142 | Returns true if there are valid entities in its range. |
---|
| 143 | */ |
---|
| 144 | bool DistanceTrigger::checkDistance() |
---|
| 145 | { |
---|
| 146 | // Check whether there is a cached object, it still exists and whether it is still in range, if so nothing further needs to be done. |
---|
[11071] | 147 | if(this->cache_ != nullptr) |
---|
[7301] | 148 | { |
---|
[10624] | 149 | if((this->cache_->getWorldPosition() - this->getWorldPosition()).length() < this->distance_) |
---|
[8213] | 150 | return true; |
---|
| 151 | else |
---|
| 152 | this->cache_.reset(); |
---|
[7301] | 153 | } |
---|
[8213] | 154 | |
---|
| 155 | // Check for new objects that are in range |
---|
| 156 | ClassTreeMask targetMask = this->targetMask_; |
---|
| 157 | // If we are in identify-mode another target mask has to be applies to find the DistanceTriggerBeacons. |
---|
[11071] | 158 | if(this->beaconMode_ == DistanceTriggerBeaconMode::identify) |
---|
[8706] | 159 | targetMask = this->beaconMask_; |
---|
[7163] | 160 | |
---|
[8213] | 161 | // Iterate through all objects that are targets of the DistanceTrigger. |
---|
| 162 | for (ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it) |
---|
[3034] | 163 | { |
---|
[8213] | 164 | WorldEntity* entity = static_cast<WorldEntity*>(*it); |
---|
[6906] | 165 | |
---|
[8213] | 166 | // If the DistanceTrigger is in identify-mode and the DistanceTriggerBeacon attached to the object has the wrong name we ignore it. |
---|
[11071] | 167 | if(this->beaconMode_ == DistanceTriggerBeaconMode::identify) |
---|
[8213] | 168 | { |
---|
| 169 | if(entity->getName() != this->targetName_) |
---|
| 170 | continue; |
---|
| 171 | // If the object, the DistanceTriggerBeacon is attached to, is not a target of this DistanceMultiTrigger. |
---|
| 172 | else if(this->targetMask_.isExcluded(entity->getParent()->getIdentifier())) |
---|
| 173 | continue; |
---|
| 174 | } |
---|
[6906] | 175 | |
---|
[8213] | 176 | // If the DistanceTrigger is in exclude mode and the DistanceTriggerBeacon attached to the object has the right name, we ignore it. |
---|
[11071] | 177 | if(this->beaconMode_ == DistanceTriggerBeaconMode::exclude) |
---|
[8213] | 178 | { |
---|
| 179 | |
---|
[11071] | 180 | const std::set<WorldEntity*> attachedObjects = entity->getAttachedObjects(); |
---|
[8213] | 181 | bool found = false; |
---|
[11071] | 182 | for(WorldEntity* attachedObject : attachedObjects) |
---|
[8213] | 183 | { |
---|
[11071] | 184 | if(attachedObject->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()) && static_cast<DistanceTriggerBeacon*>(attachedObject)->getName() == this->targetName_) |
---|
[8213] | 185 | { |
---|
| 186 | found = true; |
---|
| 187 | break; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | if(found) |
---|
| 191 | continue; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | // Check if the entity is in range. |
---|
| 195 | Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); |
---|
| 196 | if (distanceVec.length() < this->distance_) |
---|
| 197 | { |
---|
| 198 | // If the target is a player (resp. is a, or is derived from a, Pawn) the triggeringPlayer is set to the target entity. |
---|
| 199 | if(this->isForPlayer()) |
---|
| 200 | { |
---|
| 201 | // Change the entity to the parent of the DistanceTriggerBeacon (if in identify-mode), which is the entity to which the beacon is attached. |
---|
[11071] | 202 | if(this->beaconMode_ == DistanceTriggerBeaconMode::identify) |
---|
[8213] | 203 | entity = entity->getParent(); |
---|
| 204 | |
---|
[8706] | 205 | Pawn* pawn = orxonox_cast<Pawn*>(entity); |
---|
[11071] | 206 | if(pawn != nullptr) |
---|
[8706] | 207 | this->setTriggeringPawn(pawn); |
---|
| 208 | else |
---|
[11071] | 209 | orxout(internal_warning, context::triggers) << "Pawn was nullptr." << endl; |
---|
[8213] | 210 | } |
---|
| 211 | |
---|
| 212 | // Add the entity to the cache. |
---|
[10624] | 213 | this->cache_ = entity; |
---|
[8213] | 214 | |
---|
| 215 | return true; |
---|
| 216 | } |
---|
[3034] | 217 | } |
---|
| 218 | |
---|
[8213] | 219 | return false; |
---|
[1693] | 220 | } |
---|
[7163] | 221 | |
---|
[8213] | 222 | /** |
---|
| 223 | @brief |
---|
| 224 | Set the beacon mode. |
---|
| 225 | @param mode |
---|
| 226 | The mode as an enum. |
---|
| 227 | */ |
---|
[11071] | 228 | void DistanceTrigger::setBeaconModeDirect(DistanceTriggerBeaconMode mode) |
---|
[8213] | 229 | { |
---|
| 230 | this->beaconMode_ = mode; |
---|
| 231 | } |
---|
[1693] | 232 | |
---|
[8213] | 233 | /** |
---|
| 234 | @brief |
---|
| 235 | Get the beacon mode. |
---|
| 236 | @return |
---|
| 237 | Returns the mode as a string. |
---|
| 238 | */ |
---|
| 239 | const std::string& DistanceTrigger::getBeaconMode(void) const |
---|
[2261] | 240 | { |
---|
[8213] | 241 | switch(this->getBeaconModeDirect()) |
---|
| 242 | { |
---|
[11071] | 243 | case DistanceTriggerBeaconMode::off : |
---|
[8213] | 244 | return DistanceTrigger::beaconModeOff_s; |
---|
[11071] | 245 | case DistanceTriggerBeaconMode::identify: |
---|
[8213] | 246 | return DistanceTrigger::beaconModeIdentify_s; |
---|
[11071] | 247 | case DistanceTriggerBeaconMode::exclude: |
---|
[8213] | 248 | return DistanceTrigger::beaconModeExlcude_s; |
---|
| 249 | default : |
---|
| 250 | assert(0); // This is impossible. |
---|
| 251 | return BLANKSTRING; |
---|
| 252 | } |
---|
[2261] | 253 | } |
---|
[8213] | 254 | |
---|
| 255 | /** |
---|
| 256 | @brief |
---|
| 257 | Set the beacon mode. |
---|
| 258 | @param mode |
---|
| 259 | The mode as a string. |
---|
| 260 | */ |
---|
| 261 | void DistanceTrigger::setBeaconMode(const std::string& mode) |
---|
| 262 | { |
---|
| 263 | if(mode == DistanceTrigger::beaconModeOff_s) |
---|
[11071] | 264 | this->setBeaconModeDirect(DistanceTriggerBeaconMode::off); |
---|
[8213] | 265 | else if(mode == DistanceTrigger::beaconModeIdentify_s) |
---|
[11071] | 266 | this->setBeaconModeDirect(DistanceTriggerBeaconMode::identify); |
---|
[8213] | 267 | else if(mode == DistanceTrigger::beaconModeExlcude_s) |
---|
[11071] | 268 | this->setBeaconModeDirect(DistanceTriggerBeaconMode::exclude); |
---|
[8213] | 269 | else |
---|
[8858] | 270 | orxout(internal_error, context::triggers) << "Invalid beacon mode in DistanceTrigger." << endl; |
---|
[8213] | 271 | } |
---|
| 272 | |
---|
| 273 | /** |
---|
| 274 | @brief |
---|
| 275 | Check whether the DistanceTrigger is triggered. |
---|
| 276 | It is triggered if it is triggered according only to its mode (i.e. its sub-triggers) and if a target is in range. |
---|
[8706] | 277 | @param mode |
---|
| 278 | The mode for which it is tested, whether the DistanceTrigger is triggered. |
---|
| 279 | @return |
---|
[8213] | 280 | Returns true if it is triggered ,false if not. |
---|
| 281 | */ |
---|
[11071] | 282 | bool DistanceTrigger::isTriggered(TriggerMode mode) |
---|
[8213] | 283 | { |
---|
| 284 | if (Trigger::isTriggered(mode)) |
---|
| 285 | return checkDistance(); |
---|
| 286 | else |
---|
| 287 | return false; |
---|
| 288 | } |
---|
[1693] | 289 | } |
---|