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