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 <OgreNode.h> |
---|
33 | |
---|
34 | #include "core/CoreIncludes.h" |
---|
35 | #include "core/XMLPort.h" |
---|
36 | |
---|
37 | #include "orxonox/objects/worldentities/pawns/Pawn.h" |
---|
38 | |
---|
39 | namespace orxonox |
---|
40 | { |
---|
41 | CreateFactory(DistanceTrigger); |
---|
42 | |
---|
43 | DistanceTrigger::DistanceTrigger(BaseObject* creator) : PlayerTrigger(creator) |
---|
44 | { |
---|
45 | RegisterObject(DistanceTrigger); |
---|
46 | |
---|
47 | this->distance_ = 100; |
---|
48 | this->targetMask_.exclude(Class(BaseObject)); |
---|
49 | this->setForPlayer(false); //!< Normally hasn't just ControllableEntities as targets. |
---|
50 | } |
---|
51 | |
---|
52 | DistanceTrigger::~DistanceTrigger() |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
57 | { |
---|
58 | SUPER(DistanceTrigger, XMLPort, xmlelement, mode); |
---|
59 | |
---|
60 | XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f); |
---|
61 | XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("ControllableEntity"); |
---|
62 | } |
---|
63 | |
---|
64 | void DistanceTrigger::addTarget(Ogre::Node* targetNode) |
---|
65 | { |
---|
66 | this->targetSet_.insert(targetNode); |
---|
67 | } |
---|
68 | |
---|
69 | void DistanceTrigger::removeTarget(Ogre::Node* targetNode) |
---|
70 | { |
---|
71 | int returnval = this->targetSet_.erase(targetNode); |
---|
72 | if (returnval == 0) |
---|
73 | { |
---|
74 | COUT(2) << "Warning: Node " << targetNode << " did not exist in targetSet of trigger " << this << " !" << std::endl; |
---|
75 | COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl; |
---|
76 | std::set<Ogre::Node*>::iterator it; |
---|
77 | for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it) |
---|
78 | { |
---|
79 | COUT(4) << *it << std::endl; |
---|
80 | } |
---|
81 | COUT(4) << "End of targetSet of trigger " << this << std::endl; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | void DistanceTrigger::addTargets(const std::string& targets) |
---|
86 | { |
---|
87 | Identifier* targetId = ClassByString(targets); |
---|
88 | |
---|
89 | //! Checks whether the target is (or is derived from) a ControllableEntity. |
---|
90 | Identifier* controllableEntityId = Class(ControllableEntity); |
---|
91 | if(targetId->isA(controllableEntityId)) |
---|
92 | { |
---|
93 | this->setForPlayer(true); |
---|
94 | } |
---|
95 | |
---|
96 | if (!targetId) |
---|
97 | { |
---|
98 | COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ")" << std::endl; |
---|
99 | return; |
---|
100 | } |
---|
101 | |
---|
102 | this->targetMask_.include(targetId); |
---|
103 | |
---|
104 | // trigger shouldn't react on itself or other triggers |
---|
105 | this->targetMask_.exclude(Class(Trigger), true); |
---|
106 | |
---|
107 | // we only want WorldEntities |
---|
108 | ClassTreeMask WEMask; |
---|
109 | WEMask.include(Class(WorldEntity)); |
---|
110 | this->targetMask_ *= WEMask; |
---|
111 | |
---|
112 | this->notifyMaskUpdate(); |
---|
113 | } |
---|
114 | |
---|
115 | void DistanceTrigger::removeTargets(const std::string& targets) |
---|
116 | { |
---|
117 | Identifier* targetId = ClassByString(targets); |
---|
118 | this->targetMask_.exclude(targetId); |
---|
119 | } |
---|
120 | |
---|
121 | bool DistanceTrigger::checkDistance() |
---|
122 | { |
---|
123 | // Iterate through all objects |
---|
124 | for (ClassTreeMaskObjectIterator it = this->targetMask_.begin(); it != this->targetMask_.end(); ++it) |
---|
125 | { |
---|
126 | WorldEntity* entity = dynamic_cast<WorldEntity*>(*it); |
---|
127 | if (!entity) |
---|
128 | continue; |
---|
129 | |
---|
130 | Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); |
---|
131 | if (distanceVec.length() < this->distance_) |
---|
132 | { |
---|
133 | |
---|
134 | // If the target is a player (resp. is a, or is derived from a, ControllableEntity) the triggeringPlayer is set to the target entity. |
---|
135 | if(this->isForPlayer()) |
---|
136 | { |
---|
137 | Pawn* player = dynamic_cast<Pawn*>(entity); |
---|
138 | this->setTriggeringPlayer(player); |
---|
139 | } |
---|
140 | |
---|
141 | return true; |
---|
142 | } |
---|
143 | } |
---|
144 | return false; |
---|
145 | } |
---|
146 | |
---|
147 | bool DistanceTrigger::isTriggered(TriggerMode mode) |
---|
148 | { |
---|
149 | if (Trigger::isTriggered(mode)) |
---|
150 | { |
---|
151 | return checkDistance(); |
---|
152 | } |
---|
153 | else |
---|
154 | return false; |
---|
155 | } |
---|
156 | } |
---|