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