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 | * Reto Grieder |
---|
24 | * Co-authors: |
---|
25 | * Felix Schulthess |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief |
---|
32 | */ |
---|
33 | |
---|
34 | #include "Radar.h" |
---|
35 | #include <cfloat> |
---|
36 | #include <cassert> |
---|
37 | #include "core/CoreIncludes.h" |
---|
38 | #include "core/ConsoleCommand.h" |
---|
39 | #include "core/ObjectList.h" |
---|
40 | #include "RadarListener.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | SetConsoleCommand(Radar, cycleNavigationFocus, true).accessLevel(AccessLevel::User); |
---|
45 | SetConsoleCommand(Radar, releaseNavigationFocus, true).accessLevel(AccessLevel::User); |
---|
46 | |
---|
47 | Radar* Radar::instance_s = 0; |
---|
48 | |
---|
49 | Radar::Radar() |
---|
50 | : focus_(0) |
---|
51 | , objectTypeCounter_(0) |
---|
52 | { |
---|
53 | assert(instance_s == 0); |
---|
54 | instance_s = this; |
---|
55 | |
---|
56 | // TODO: make this mapping configurable. Maybe there's a possibility with self configured |
---|
57 | // configValues.. |
---|
58 | this->objectTypes_["Asteroid"] = RadarViewable::Dot; |
---|
59 | this->objectTypes_["SpaceShip"] = RadarViewable::Square; |
---|
60 | this->objectTypes_["AsdfQwerty"] = RadarViewable::Triangle; |
---|
61 | |
---|
62 | /*WorldEntity* object; |
---|
63 | object = new WorldEntity(); |
---|
64 | object->setPosition(2000.0, 0.0, 0.0); |
---|
65 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
66 | object = new WorldEntity(); |
---|
67 | object->setPosition(0.0, 2000.0, 0.0); |
---|
68 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
69 | object = new WorldEntity(); |
---|
70 | object->setPosition(0.0, 0.0, 2000.0); |
---|
71 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
72 | object = new WorldEntity(); |
---|
73 | object->setPosition(10000.0,16000.0,0.0); |
---|
74 | addRadarObject(object);*/ |
---|
75 | |
---|
76 | } |
---|
77 | |
---|
78 | Radar::~Radar() |
---|
79 | { |
---|
80 | instance_s = 0; |
---|
81 | } |
---|
82 | |
---|
83 | const RadarViewable* Radar::getFocus() |
---|
84 | { |
---|
85 | return *(this->itFocus_); |
---|
86 | } |
---|
87 | |
---|
88 | RadarViewable::Shape Radar::addObjectDescription(const std::string name) |
---|
89 | { |
---|
90 | std::map<std::string, RadarViewable::Shape>::iterator it = this->objectTypes_.find(name); |
---|
91 | if (it == this->objectTypes_.end()) |
---|
92 | return this->objectTypes_[name] = RadarViewable::Square; // default, configure!! |
---|
93 | else |
---|
94 | return this->objectTypes_[name]; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | void Radar::tick(float dt) |
---|
99 | { |
---|
100 | SUPER(Radar, tick, dt); |
---|
101 | |
---|
102 | if (this->focus_ != *(this->itFocus_)) |
---|
103 | { |
---|
104 | // focus object was deleted, release focus |
---|
105 | this->focus_ = 0; |
---|
106 | this->itFocus_ = 0; |
---|
107 | } |
---|
108 | |
---|
109 | for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener) |
---|
110 | { |
---|
111 | (*itListener)->radarTick(dt); |
---|
112 | |
---|
113 | for (ObjectList<RadarViewable>::iterator itElement = ObjectList<RadarViewable>::begin(); itElement; ++itElement) |
---|
114 | { |
---|
115 | if ((*itElement)->getRadarVisibility()) |
---|
116 | if ((*itListener)->getRadarSensitivity() > (*itElement)->getRadarObjectCamouflage()) |
---|
117 | (*itListener)->displayObject(*itElement, *itElement == this->focus_); |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void Radar::cycleFocus() |
---|
123 | { |
---|
124 | if (ObjectList<RadarViewable>::begin() == 0) |
---|
125 | { |
---|
126 | // list is empty |
---|
127 | this->itFocus_ = 0; |
---|
128 | this->focus_ = 0; |
---|
129 | } |
---|
130 | /* |
---|
131 | else if (this->owner_) |
---|
132 | { |
---|
133 | Vector3 localPosition = this->owner_->getPosition(); |
---|
134 | Vector3 targetPosition = localPosition; |
---|
135 | if (*(this->itFocus_)) |
---|
136 | targetPosition = this->itFocus_->getRVWorldPosition(); |
---|
137 | |
---|
138 | // find the closed object further away than targetPosition |
---|
139 | float currentDistance = localPosition.squaredDistance(targetPosition); |
---|
140 | float nextDistance = FLT_MAX; |
---|
141 | float minimumDistance = FLT_MAX; |
---|
142 | ObjectList<RadarViewable>::iterator itFallback = 0; |
---|
143 | |
---|
144 | for (ObjectList<RadarViewable>::iterator it = ObjectList<RadarViewable>::begin(); it; ++it) |
---|
145 | { |
---|
146 | if (*it == static_cast<RadarViewable*>(this)->owner_) |
---|
147 | continue; |
---|
148 | |
---|
149 | float targetDistance = localPosition.squaredDistance((*it)->getRVWorldPosition()); |
---|
150 | if (targetDistance > currentDistance && targetDistance < nextDistance) |
---|
151 | { |
---|
152 | this->itFocus_ = it; |
---|
153 | nextDistance = targetDistance; |
---|
154 | } |
---|
155 | if (targetDistance < minimumDistance) |
---|
156 | { |
---|
157 | itFallback = it; |
---|
158 | minimumDistance = targetDistance; |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | if (nextDistance == FLT_MAX) |
---|
163 | { |
---|
164 | // we already had the furthest object |
---|
165 | this->itFocus_ = itFallback; |
---|
166 | this->focus_ = *itFallback; |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | this->focus_ = *(this->itFocus_); |
---|
171 | } |
---|
172 | } |
---|
173 | */ |
---|
174 | } |
---|
175 | |
---|
176 | void Radar::releaseFocus() |
---|
177 | { |
---|
178 | this->itFocus_ = 0; |
---|
179 | this->focus_ = 0; |
---|
180 | } |
---|
181 | |
---|
182 | void Radar::listObjects() const |
---|
183 | { |
---|
184 | COUT(3) << "List of RadarObjects:\n"; |
---|
185 | // iterate through all Radar Objects |
---|
186 | unsigned int i = 0; |
---|
187 | for (ObjectList<RadarViewable>::iterator it = ObjectList<RadarViewable>::begin(); it; ++it, ++i) |
---|
188 | { |
---|
189 | COUT(3) << i++ << ": " << (*it)->getRVWorldPosition() << std::endl; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | /*static*/ Radar& Radar::getInstance() |
---|
195 | { |
---|
196 | assert(instance_s); |
---|
197 | return *instance_s; |
---|
198 | } |
---|
199 | |
---|
200 | /*static*/ void Radar::cycleNavigationFocus() |
---|
201 | { |
---|
202 | // avoid using getInstance because of the assert(). |
---|
203 | // User might call this fuction even if HUDNavigation doesn't exist. |
---|
204 | if (instance_s) |
---|
205 | instance_s->cycleFocus(); |
---|
206 | } |
---|
207 | |
---|
208 | /*static*/ void Radar::releaseNavigationFocus() |
---|
209 | { |
---|
210 | // avoid using getInstance because of the assert(). |
---|
211 | // User might call this fuction even if HUDNavigation doesn't exist. |
---|
212 | if (instance_s) |
---|
213 | instance_s->releaseFocus(); |
---|
214 | } |
---|
215 | } |
---|