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