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