[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 | |
---|
[1906] | 29 | #include "OrxonoxStableHeaders.h" |
---|
[1693] | 30 | #include "DistanceTrigger.h" |
---|
| 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
| 34 | |
---|
| 35 | namespace orxonox { |
---|
| 36 | |
---|
| 37 | CreateFactory(DistanceTrigger); |
---|
| 38 | |
---|
[2019] | 39 | DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator) |
---|
[1693] | 40 | { |
---|
| 41 | RegisterObject(DistanceTrigger); |
---|
| 42 | |
---|
[2029] | 43 | this->distance_ = 100; |
---|
| 44 | this->targetMask_.exclude(Class(BaseObject)); |
---|
[1693] | 45 | } |
---|
| 46 | |
---|
| 47 | DistanceTrigger::~DistanceTrigger() |
---|
| 48 | { |
---|
| 49 | } |
---|
| 50 | |
---|
[2029] | 51 | void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 52 | { |
---|
| 53 | SUPER(DistanceTrigger, XMLPort, xmlelement, mode); |
---|
| 54 | |
---|
| 55 | XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f); |
---|
| 56 | XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode); |
---|
| 57 | } |
---|
| 58 | |
---|
[1693] | 59 | void DistanceTrigger::addTarget(Ogre::Node* targetNode) |
---|
| 60 | { |
---|
| 61 | this->targetSet_.insert(targetNode); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void DistanceTrigger::removeTarget(Ogre::Node* targetNode) |
---|
| 65 | { |
---|
| 66 | int returnval = this->targetSet_.erase(targetNode); |
---|
| 67 | if (returnval == 0) |
---|
| 68 | { |
---|
| 69 | COUT(2) << "Warning: Node " << targetNode << " did not exist in targetSet of trigger " << this << " !" << std::endl; |
---|
| 70 | COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl; |
---|
| 71 | std::set<Ogre::Node*>::iterator it; |
---|
[2029] | 72 | for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it) |
---|
[1693] | 73 | { |
---|
| 74 | COUT(4) << *it << std::endl; |
---|
| 75 | } |
---|
| 76 | COUT(4) << "End of targetSet of trigger " << this << std::endl; |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
[1969] | 80 | void DistanceTrigger::addTargets(const std::string& targets) |
---|
[1693] | 81 | { |
---|
[1961] | 82 | Identifier* targetId = ClassByString(targets); |
---|
[1693] | 83 | targetMask_.include(targetId); |
---|
| 84 | // trigger shouldn't react on itself or other triggers |
---|
| 85 | targetMask_.exclude(Class(Trigger), true); |
---|
| 86 | |
---|
| 87 | } |
---|
| 88 | |
---|
[1969] | 89 | void DistanceTrigger::removeTargets(const std::string& targets) |
---|
[1693] | 90 | { |
---|
[1961] | 91 | Identifier* targetId = ClassByString(targets); |
---|
[1693] | 92 | targetMask_.exclude(targetId); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | bool DistanceTrigger::checkDistance() |
---|
| 96 | { |
---|
| 97 | // Iterate through all WorldEntities |
---|
[2029] | 98 | for (Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; ++it) |
---|
[1693] | 99 | { |
---|
| 100 | // check if WorldEntity is a target |
---|
[2029] | 101 | if (targetMask_.isIncluded(it->getIdentifier())) |
---|
[1693] | 102 | { |
---|
| 103 | Vector3 distanceVec = it->getNode()->getWorldPosition() - this->getNode()->getWorldPosition(); |
---|
| 104 | if (distanceVec.length() < this->distance_) |
---|
| 105 | return true; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | return false; |
---|
| 109 | |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | bool DistanceTrigger::isTriggered(TriggerMode mode) |
---|
| 113 | { |
---|
[2029] | 114 | if (Trigger::isTriggered(mode)) |
---|
[1693] | 115 | return checkDistance(); |
---|
| 116 | else |
---|
| 117 | return false; |
---|
| 118 | } |
---|
| 119 | } |
---|