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