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