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