[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
[1454] | 4 | * |
---|
[1505] | 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 | * |
---|
[1454] | 22 | * Author: |
---|
| 23 | * Felix Schulthess |
---|
| 24 | * Co-authors: |
---|
[1590] | 25 | * Reto Grieder |
---|
[7801] | 26 | * Oliver Scheuss |
---|
[9016] | 27 | * Matthias Spalinger |
---|
[1454] | 28 | * |
---|
| 29 | */ |
---|
[1393] | 30 | |
---|
[1601] | 31 | #include "HUDNavigation.h" |
---|
[1410] | 32 | |
---|
[6417] | 33 | #include <OgreCamera.h> |
---|
[7163] | 34 | #include <OgreFontManager.h> |
---|
[1393] | 35 | #include <OgreOverlayManager.h> |
---|
[1614] | 36 | #include <OgreTextAreaOverlayElement.h> |
---|
| 37 | #include <OgrePanelOverlayElement.h> |
---|
[1410] | 38 | |
---|
[9526] | 39 | #include <typeinfo> |
---|
| 40 | |
---|
[1614] | 41 | #include "util/Math.h" |
---|
[1616] | 42 | #include "util/Convert.h" |
---|
[10624] | 43 | #include "core/command/ConsoleCommandIncludes.h" |
---|
[1616] | 44 | #include "core/CoreIncludes.h" |
---|
| 45 | #include "core/XMLPort.h" |
---|
[6417] | 46 | #include "CameraManager.h" |
---|
[5929] | 47 | #include "Scene.h" |
---|
[5735] | 48 | #include "Radar.h" |
---|
[6417] | 49 | #include "graphics/Camera.h" |
---|
| 50 | #include "controllers/HumanController.h" |
---|
| 51 | #include "worldentities/pawns/Pawn.h" |
---|
[7163] | 52 | #include "worldentities/WorldEntity.h" |
---|
[9667] | 53 | #include "core/config/ConfigValueIncludes.h" |
---|
[7163] | 54 | #include "tools/TextureGenerator.h" |
---|
| 55 | // #include <boost/bind/bind_template.hpp> |
---|
[1393] | 56 | |
---|
[7163] | 57 | |
---|
[1393] | 58 | namespace orxonox |
---|
| 59 | { |
---|
[9526] | 60 | |
---|
| 61 | SetConsoleCommand("HUDNavigation","selectClosest", &HUDNavigation::selectClosestTarget).addShortcut().keybindMode(KeybindMode::OnPress); |
---|
| 62 | SetConsoleCommand("HUDNavigation","selectNext", &HUDNavigation::selectNextTarget).addShortcut().keybindMode(KeybindMode::OnPress); |
---|
| 63 | |
---|
| 64 | static bool compareDistance(std::pair<RadarViewable*, unsigned int> a, |
---|
| 65 | std::pair<RadarViewable*, unsigned int> b) |
---|
[9348] | 66 | { |
---|
| 67 | return a.second < b.second; |
---|
| 68 | } |
---|
[9667] | 69 | RegisterClass ( HUDNavigation ); |
---|
[2087] | 70 | |
---|
[9526] | 71 | HUDNavigation* HUDNavigation::localHUD_s = 0; |
---|
| 72 | |
---|
[9667] | 73 | HUDNavigation::HUDNavigation(Context* context) : |
---|
| 74 | OrxonoxOverlay(context) |
---|
[9348] | 75 | { |
---|
[9939] | 76 | RegisterObject(HUDNavigation); |
---|
| 77 | this->setConfigValues(); |
---|
[2087] | 78 | |
---|
[9348] | 79 | // Set default values |
---|
| 80 | this->setFont("Monofur"); |
---|
| 81 | this->setTextSize(0.05f); |
---|
[9526] | 82 | this->setNavMarkerSize(0.03f); |
---|
| 83 | this->setAimMarkerSize(0.02f); |
---|
[10258] | 84 | this->setHealthMarkerSize(0.06f); |
---|
| 85 | this->setHealthLevelMarkerSize(0.06f); |
---|
[9526] | 86 | |
---|
[9348] | 87 | this->setDetectionLimit(10000.0f); |
---|
[10294] | 88 | this->currentMunitionSpeed_ = 750.0f; |
---|
[9526] | 89 | |
---|
| 90 | this->closestTarget_ = true; |
---|
| 91 | this->nextTarget_ = false; |
---|
| 92 | HUDNavigation::localHUD_s = this; |
---|
[9348] | 93 | } |
---|
[2087] | 94 | |
---|
[9348] | 95 | HUDNavigation::~HUDNavigation() |
---|
| 96 | { |
---|
| 97 | if (this->isInitialized()) |
---|
| 98 | { |
---|
| 99 | for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end();) |
---|
[9526] | 100 | removeObject((it++)->first); |
---|
[9348] | 101 | } |
---|
| 102 | this->sortedObjectList_.clear(); |
---|
| 103 | } |
---|
[2087] | 104 | |
---|
[9348] | 105 | void HUDNavigation::setConfigValues() |
---|
| 106 | { |
---|
| 107 | SetConfigValue(markerLimit_, 3); |
---|
| 108 | SetConfigValue(showDistance_, false); |
---|
| 109 | } |
---|
[2087] | 110 | |
---|
[9348] | 111 | void HUDNavigation::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[7163] | 112 | { |
---|
[9348] | 113 | SUPER(HUDNavigation, XMLPort, xmlelement, mode); |
---|
[2087] | 114 | |
---|
[9526] | 115 | XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlelement, mode); |
---|
| 116 | XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlelement, mode); |
---|
| 117 | XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlelement, mode); |
---|
[9348] | 118 | XMLPortParam(HUDNavigation, "detectionLimit", setDetectionLimit, getDetectionLimit, xmlelement, mode); |
---|
[9526] | 119 | XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlelement, mode); |
---|
[10258] | 120 | XMLPortParam(HUDNavigation, "healthMarkerSize", setHealthMarkerSize, getHealthMarkerSize, xmlelement, mode); |
---|
| 121 | XMLPortParam(HUDNavigation, "healthLevelMarkerSize", setHealthLevelMarkerSize, getHealthLevelMarkerSize, xmlelement, mode); |
---|
| 122 | |
---|
[1393] | 123 | } |
---|
| 124 | |
---|
[9348] | 125 | void HUDNavigation::setFont(const std::string& font) |
---|
| 126 | { |
---|
| 127 | const Ogre::ResourcePtr& fontPtr = Ogre::FontManager::getSingleton().getByName(font); |
---|
| 128 | if (fontPtr.isNull()) |
---|
| 129 | { |
---|
| 130 | orxout(internal_warning) << "HUDNavigation: Font '" << font << "' not found" << endl; |
---|
| 131 | return; |
---|
| 132 | } |
---|
| 133 | this->fontName_ = font; |
---|
| 134 | for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it) |
---|
| 135 | { |
---|
| 136 | if (it->second.text_ != NULL) |
---|
[10292] | 137 | it->second.text_->setFontName(this->fontName_); |
---|
[9348] | 138 | } |
---|
| 139 | } |
---|
[1564] | 140 | |
---|
[9348] | 141 | const std::string& HUDNavigation::getFont() const |
---|
[1590] | 142 | { |
---|
[9348] | 143 | return this->fontName_; |
---|
[1590] | 144 | } |
---|
[9348] | 145 | |
---|
| 146 | void HUDNavigation::setTextSize(float size) |
---|
[1590] | 147 | { |
---|
[9348] | 148 | if (size <= 0.0f) |
---|
| 149 | { |
---|
| 150 | orxout(internal_warning) << "HUDNavigation: Negative font size not allowed" << endl; |
---|
| 151 | return; |
---|
| 152 | } |
---|
| 153 | this->textSize_ = size; |
---|
| 154 | for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it!=this->activeObjectList_.end(); ++it) |
---|
| 155 | { |
---|
| 156 | if (it->second.text_) |
---|
[10292] | 157 | it->second.text_->setCharHeight(size); |
---|
[9348] | 158 | } |
---|
[1590] | 159 | } |
---|
| 160 | |
---|
[9348] | 161 | float HUDNavigation::getTextSize() const |
---|
[1590] | 162 | { |
---|
[9348] | 163 | return this->textSize_; |
---|
[1590] | 164 | } |
---|
[9348] | 165 | |
---|
| 166 | float HUDNavigation::getArrowSizeX(int dist) const |
---|
[7163] | 167 | { |
---|
[9348] | 168 | if (dist < 600) |
---|
[9526] | 169 | dist = 600; |
---|
[9348] | 170 | return this->getActualSize().x * 900 * this->navMarkerSize_ / dist; |
---|
[7163] | 171 | } |
---|
[1590] | 172 | |
---|
[9348] | 173 | float HUDNavigation::getArrowSizeY(int dist) const |
---|
[1590] | 174 | { |
---|
[9348] | 175 | if (dist < 600) |
---|
[9526] | 176 | dist = 600; |
---|
[9348] | 177 | return this->getActualSize().y * 900 * this->navMarkerSize_ / dist; |
---|
[1590] | 178 | } |
---|
| 179 | |
---|
[9348] | 180 | void HUDNavigation::tick(float dt) |
---|
[1590] | 181 | { |
---|
[9348] | 182 | SUPER(HUDNavigation, tick, dt); |
---|
[1400] | 183 | |
---|
[9348] | 184 | Camera* cam = CameraManager::getInstance().getActiveCamera(); |
---|
| 185 | if (cam == NULL) |
---|
[9526] | 186 | return; |
---|
[9348] | 187 | const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix(); |
---|
[1399] | 188 | |
---|
[9348] | 189 | for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt) |
---|
[9526] | 190 | listIt->second = (int)((listIt->first->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition()).length() + 0.5f); |
---|
[1564] | 191 | |
---|
[9348] | 192 | this->sortedObjectList_.sort(compareDistance); |
---|
[9016] | 193 | |
---|
[9348] | 194 | unsigned int markerCount = 0; |
---|
| 195 | bool closeEnough = false; // only display objects that are close enough to be relevant for the player |
---|
[1399] | 196 | |
---|
[9526] | 197 | // if the selected object doesn't exist any more or is now out of range select the closest object |
---|
| 198 | std::map<RadarViewable*, ObjectInfo>::iterator selectedActiveObject = this->activeObjectList_.find(this->selectedTarget_); |
---|
| 199 | if(selectedActiveObject == this->activeObjectList_.end()) |
---|
| 200 | { |
---|
| 201 | this->closestTarget_ = true; |
---|
| 202 | } |
---|
| 203 | else if(this->detectionLimit_ < (this->selectedTarget_->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition()).length() + 0.5f) |
---|
| 204 | { |
---|
| 205 | this->closestTarget_ = true; |
---|
| 206 | selectedActiveObject->second.selected_ = false; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | bool nextHasToBeSelected = false; |
---|
| 210 | |
---|
[9348] | 211 | for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++markerCount, ++listIt) |
---|
| 212 | { |
---|
[10258] | 213 | |
---|
[9348] | 214 | std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(listIt->first); |
---|
| 215 | closeEnough = listIt->second < this->detectionLimit_; |
---|
| 216 | // display radarviewables on HUD if the marker limit and max-distance is not exceeded |
---|
[9526] | 217 | if (markerCount < this->markerLimit_ && (closeEnough || this->detectionLimit_ < 0)) |
---|
[1580] | 218 | { |
---|
[9348] | 219 | // Get Distance to HumanController and save it in the TextAreaOverlayElement. |
---|
| 220 | int dist = listIt->second; |
---|
| 221 | float textLength = 0.0f; |
---|
[1590] | 222 | |
---|
[9348] | 223 | if (this->showDistance_) |
---|
[1580] | 224 | { |
---|
[9348] | 225 | //display distance next to cursor |
---|
| 226 | it->second.text_->setCaption(multi_cast<std::string>(dist)); |
---|
| 227 | textLength = multi_cast<std::string>(dist).size() * it->second.text_->getCharHeight() * 0.3f; |
---|
[1399] | 228 | } |
---|
[9348] | 229 | else |
---|
| 230 | { |
---|
| 231 | //display name next to cursor |
---|
| 232 | it->second.text_->setCaption(it->first->getRadarName()); |
---|
| 233 | textLength = it->first->getRadarName().size() * it->second.text_->getCharHeight() * 0.3f; |
---|
| 234 | } |
---|
[7163] | 235 | |
---|
[9526] | 236 | // select the object that aim-assistant indicates |
---|
| 237 | if(this->closestTarget_) |
---|
| 238 | // select the closest object |
---|
| 239 | { |
---|
| 240 | if(listIt == this->sortedObjectList_.begin()) |
---|
| 241 | { |
---|
| 242 | it->second.selected_ = true; |
---|
| 243 | this->selectedTarget_ = it->first; |
---|
| 244 | } |
---|
| 245 | else if(it->second.selected_) |
---|
| 246 | { |
---|
| 247 | it->second.selected_ = false; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | } |
---|
| 251 | else if(this->nextTarget_) |
---|
| 252 | // select the next object in sortedObjectList |
---|
| 253 | { |
---|
| 254 | if(nextHasToBeSelected){ |
---|
| 255 | it->second.selected_ = true; |
---|
| 256 | this->selectedTarget_ = it->first; |
---|
| 257 | nextHasToBeSelected = false; |
---|
| 258 | } |
---|
| 259 | else if(it->second.selected_) |
---|
| 260 | { |
---|
| 261 | nextHasToBeSelected = true; |
---|
| 262 | it->second.selected_ = false; |
---|
| 263 | |
---|
| 264 | // check if there's a next object |
---|
| 265 | listIt++; |
---|
| 266 | if(listIt != this->sortedObjectList_.end()) |
---|
| 267 | { |
---|
| 268 | // and if the marker limit and max-distance are not exceeded for it |
---|
| 269 | if (markerCount + 1 >= this->markerLimit_ || |
---|
| 270 | (listIt->second > this->detectionLimit_ && detectionLimit_ >= 0)) |
---|
| 271 | { |
---|
| 272 | // otherwise select the closest object |
---|
| 273 | this->activeObjectList_.find(this->sortedObjectList_.begin()->first)->second.selected_ = true; |
---|
| 274 | this->selectedTarget_ = it->first; |
---|
| 275 | nextHasToBeSelected = false; |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | listIt--; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | |
---|
[10258] | 283 | |
---|
| 284 | |
---|
[9348] | 285 | // Transform to screen coordinates |
---|
| 286 | Vector3 pos = camTransform * it->first->getRVWorldPosition(); |
---|
[9016] | 287 | |
---|
[9348] | 288 | bool outOfView = true; |
---|
| 289 | if (pos.z > 1.0) |
---|
| 290 | { |
---|
| 291 | // z > 1.0 means that the object is behind the camera |
---|
| 292 | outOfView = true; |
---|
| 293 | // we have to switch all coordinates (if you don't know why, |
---|
| 294 | // try linear algebra lectures, because I can't explain..) |
---|
| 295 | pos.x = -pos.x; |
---|
| 296 | pos.y = -pos.y; |
---|
| 297 | } |
---|
| 298 | else |
---|
| 299 | outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0; |
---|
[9016] | 300 | |
---|
[9348] | 301 | if (outOfView) |
---|
[7163] | 302 | { |
---|
[9348] | 303 | // Object is not in view |
---|
| 304 | |
---|
| 305 | // Change material only if outOfView changed |
---|
| 306 | if (!it->second.wasOutOfView_) |
---|
[7163] | 307 | { |
---|
[10258] | 308 | it->second.health_->hide(); |
---|
| 309 | it->second.healthLevel_->hide(); |
---|
[9348] | 310 | it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("arrows.png", it->first->getRadarObjectColour())); |
---|
| 311 | it->second.wasOutOfView_ = true; |
---|
[9526] | 312 | it->second.target_->hide(); |
---|
[7163] | 313 | } |
---|
[9348] | 314 | |
---|
| 315 | //float xDistScale = this->getActualSize().x * 1000.0f * this->navMarkerSize_ / dist; |
---|
| 316 | //float yDistScale = this->getActualSize().y * 1000.0f * this->navMarkerSize_ / dist; |
---|
| 317 | |
---|
| 318 | // Adjust Arrowsize according to distance |
---|
| 319 | it->second.panel_->setDimensions(getArrowSizeX(dist), getArrowSizeY(dist)); |
---|
| 320 | |
---|
| 321 | // Switch between top, bottom, left and right position of the arrow at the screen border |
---|
| 322 | if (pos.x < pos.y) |
---|
| 323 | { |
---|
| 324 | if (pos.y > -pos.x) |
---|
| 325 | { |
---|
| 326 | // Top |
---|
| 327 | float position = pos.x / pos.y + 1.0f; |
---|
| 328 | it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 0.0f); |
---|
| 329 | it->second.panel_->setUV(0.5f, 0.0f, 1.0f, 0.5f); |
---|
| 330 | it->second.text_->setLeft((position - textLength) * 0.5f); |
---|
| 331 | it->second.text_->setTop(it->second.panel_->getHeight()); |
---|
| 332 | } |
---|
| 333 | else |
---|
| 334 | { |
---|
| 335 | // Left |
---|
| 336 | float position = pos.y / pos.x + 1.0f; |
---|
| 337 | it->second.panel_->setPosition(0.0f, (position - it->second.panel_->getWidth()) * 0.5f); |
---|
| 338 | it->second.panel_->setUV(0.0f, 0.0f, 0.5f, 0.5f); |
---|
| 339 | it->second.text_->setLeft(it->second.panel_->getWidth() + 0.01f); |
---|
| 340 | it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f); |
---|
| 341 | } |
---|
| 342 | } |
---|
[7163] | 343 | else |
---|
| 344 | { |
---|
[9348] | 345 | if (pos.y < -pos.x) |
---|
| 346 | { |
---|
| 347 | // Bottom |
---|
| 348 | float position = -pos.x / pos.y + 1.0f; |
---|
| 349 | it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 1.0f - it->second.panel_->getHeight()); |
---|
| 350 | it->second.panel_->setUV(0.0f, 0.5f, 0.5f, 1.0f ); |
---|
| 351 | it->second.text_->setLeft((position - textLength) * 0.5f); |
---|
| 352 | it->second.text_->setTop(1.0f - it->second.panel_->getHeight() - it->second.text_->getCharHeight()); |
---|
| 353 | } |
---|
| 354 | else |
---|
| 355 | { |
---|
| 356 | // Right |
---|
| 357 | float position = -pos.y / pos.x + 1.0f; |
---|
| 358 | it->second.panel_->setPosition(1.0f - it->second.panel_->getWidth(), (position - it->second.panel_->getHeight()) * 0.5f); |
---|
| 359 | it->second.panel_->setUV(0.5f, 0.5f, 1.0f, 1.0f); |
---|
| 360 | it->second.text_->setLeft(1.0f - it->second.panel_->getWidth() - textLength - 0.01f); |
---|
| 361 | it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f); |
---|
| 362 | } |
---|
[7163] | 363 | } |
---|
| 364 | } |
---|
[1580] | 365 | else |
---|
| 366 | { |
---|
[9348] | 367 | // Object is in view |
---|
[7163] | 368 | |
---|
[10258] | 369 | //calculate the health of the actual selected radarViewable (relativHealthScale: while (0) is no health left, (1) is the initial health) |
---|
[11023] | 370 | const Pawn* pawnPtr = orxonox_cast<const Pawn*>(it->first->getWorldEntity()); |
---|
| 371 | if (pawnPtr) |
---|
| 372 | { |
---|
| 373 | float health = pawnPtr->getHealth(); |
---|
| 374 | float initHealth = pawnPtr->getMaxHealth(); |
---|
| 375 | float relativHealthScale = health/initHealth; |
---|
[10258] | 376 | |
---|
[11023] | 377 | //integer values from 0 to 10 (0 is no health and 10 is full health) |
---|
| 378 | int discreteHealthScale = (int)(10*relativHealthScale); |
---|
[10258] | 379 | |
---|
[11023] | 380 | //calculate the HealthLevel (= OponentLevel or Strength) there are 5 Levels |
---|
| 381 | //Level 1, Level 2,... , Level 5 |
---|
| 382 | int HealthLevel = 1; |
---|
[10258] | 383 | |
---|
[11023] | 384 | if(initHealth < 200) |
---|
| 385 | HealthLevel = 1; |
---|
| 386 | if(200 <= initHealth && initHealth < 500) |
---|
| 387 | HealthLevel = 2; |
---|
| 388 | if(500 <= initHealth && initHealth < 1000) |
---|
| 389 | HealthLevel = 3; |
---|
| 390 | if(1000 <= initHealth && initHealth < 2500) |
---|
| 391 | HealthLevel = 4; |
---|
| 392 | if(2500 <= initHealth) |
---|
| 393 | HealthLevel = 5; |
---|
[10258] | 394 | |
---|
[11023] | 395 | // Change material only if outOfView changed |
---|
| 396 | if (it->second.wasOutOfView_) |
---|
| 397 | { |
---|
| 398 | //it->second.health_->setMaterialName(TextureGenerator::getMaterialName("bar2_1.png", it->first->getRadarObjectColour())); |
---|
| 399 | it->second.health_->setMaterialName(TextureGenerator::getMaterialName("barSquare.png", it->first->getRadarObjectColour())); |
---|
| 400 | it->second.health_->setDimensions(this->healthMarkerSize_ * this->getActualSize().x , 0.75f*this->healthMarkerSize_ * this->getActualSize().y); |
---|
[10258] | 401 | |
---|
[11023] | 402 | // because as soon as relative health drops below 10% (0.1) the descrete value is 0 but as long as the |
---|
| 403 | // spaceship is still intact there should be at least one part of the bar left. |
---|
| 404 | if(1<=discreteHealthScale){ |
---|
| 405 | it->second.health_->setTiling((float)discreteHealthScale , 1 ,0); |
---|
| 406 | it->second.health_->setDimensions(this->healthMarkerSize_ * this->getActualSize().x *0.1f*discreteHealthScale, 0.75f*this->healthMarkerSize_ * this->getActualSize().y); |
---|
| 407 | } |
---|
[10258] | 408 | |
---|
[11023] | 409 | //healthLevel |
---|
| 410 | it->second.healthLevel_->setMaterialName(TextureGenerator::getMaterialName("barSquare.png", it->first->getRadarObjectColour())); |
---|
| 411 | it->second.healthLevel_->setDimensions(this->healthLevelMarkerSize_ * this->getActualSize().x , 0.75f*this->healthLevelMarkerSize_ * this->getActualSize().y); |
---|
| 412 | it->second.healthLevel_->setTiling((float)HealthLevel , 1 ,0); |
---|
| 413 | it->second.healthLevel_->setDimensions(this->healthLevelMarkerSize_ * this->getActualSize().x *0.1f*HealthLevel, 0.25f*this->healthLevelMarkerSize_ * this->getActualSize().y); |
---|
| 414 | } |
---|
[10258] | 415 | |
---|
[11023] | 416 | // sets Position and Dimensions (amount) health |
---|
| 417 | it->second.health_->setUV(0.0f, 0.0f, 1.0f, 1.0f); |
---|
| 418 | it->second.health_->setLeft((pos.x + 0.975f - it->second.panel_->getWidth()) * 0.5f); |
---|
| 419 | it->second.health_->setTop((-pos.y + 1.04f - it->second.panel_->getHeight()) * 0.5f); |
---|
[10258] | 420 | |
---|
| 421 | // because as soon as relative health drops below 10% (0.1) the descrete value is 0 but as long as the |
---|
| 422 | // spaceship is still intact there should be at least one part of the bar left. |
---|
| 423 | if(1<=discreteHealthScale){ |
---|
| 424 | it->second.health_->setTiling((float)discreteHealthScale , 1 ,0); |
---|
| 425 | it->second.health_->setDimensions(this->healthMarkerSize_ * this->getActualSize().x *0.1f*discreteHealthScale, 0.75f*this->healthMarkerSize_ * this->getActualSize().y); |
---|
| 426 | } |
---|
| 427 | |
---|
[11023] | 428 | //sets Position and Dimensions (level) of healthLevel |
---|
| 429 | it->second.healthLevel_->setMaterialName(TextureGenerator::getMaterialName("barSquare.png", it->first->getRadarObjectColour())); |
---|
| 430 | it->second.healthLevel_->setUV(0.0f, 0.0f, 1.0f, 1.0f); |
---|
| 431 | it->second.healthLevel_->setLeft((pos.x + 0.975f - it->second.panel_->getWidth()) * 0.5f); |
---|
| 432 | it->second.healthLevel_->setTop((-pos.y + 1.125f - it->second.panel_->getHeight()) * 0.5f); |
---|
[10258] | 433 | |
---|
| 434 | it->second.healthLevel_->setTiling((float)HealthLevel , 1 ,0); |
---|
| 435 | it->second.healthLevel_->setDimensions(this->healthLevelMarkerSize_ * this->getActualSize().x *0.1f*HealthLevel, 0.25f*this->healthLevelMarkerSize_ * this->getActualSize().y); |
---|
[11023] | 436 | |
---|
| 437 | // Make sure the overlays are shown |
---|
| 438 | it->second.health_->show(); |
---|
| 439 | it->second.healthLevel_->show(); |
---|
[7163] | 440 | } |
---|
[9348] | 441 | |
---|
[10258] | 442 | |
---|
[11023] | 443 | // Change material only if outOfView changed |
---|
| 444 | if (it->second.wasOutOfView_) |
---|
| 445 | { |
---|
| 446 | //it->second.panel_->setMaterialName("Orxonox/NavTDC"); |
---|
| 447 | it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("tdc.png", it->first->getRadarObjectColour())); |
---|
| 448 | it->second.panel_->setDimensions(this->navMarkerSize_ * this->getActualSize().x, this->navMarkerSize_ * this->getActualSize().y); |
---|
| 449 | it->second.target_->setDimensions(this->aimMarkerSize_ * this->getActualSize().x, this->aimMarkerSize_ * this->getActualSize().y); |
---|
| 450 | it->second.wasOutOfView_ = false; |
---|
[10258] | 451 | } |
---|
| 452 | |
---|
[9348] | 453 | // Position marker |
---|
| 454 | it->second.panel_->setUV(0.0f, 0.0f, 1.0f, 1.0f); |
---|
| 455 | it->second.panel_->setLeft((pos.x + 1.0f - it->second.panel_->getWidth()) * 0.5f); |
---|
| 456 | it->second.panel_->setTop((-pos.y + 1.0f - it->second.panel_->getHeight()) * 0.5f); |
---|
| 457 | |
---|
| 458 | // Position text |
---|
| 459 | it->second.text_->setLeft((pos.x + 1.0f + it->second.panel_->getWidth()) * 0.5f); |
---|
| 460 | it->second.text_->setTop((-pos.y + 1.0f + it->second.panel_->getHeight()) * 0.5f); |
---|
[9526] | 461 | |
---|
| 462 | // Make sure the overlays are shown |
---|
| 463 | it->second.panel_->show(); |
---|
| 464 | it->second.text_->show(); |
---|
| 465 | |
---|
| 466 | // Target marker |
---|
| 467 | const Pawn* pawn = dynamic_cast<const Pawn*>(it->first->getWorldEntity()); |
---|
| 468 | /* Pawn* humanPawn = HumanController::getLocalControllerEntityAsPawn();*/ |
---|
| 469 | if(!it->second.selected_ |
---|
| 470 | || it->first->getRVVelocity().squaredLength() == 0 |
---|
| 471 | || pawn == NULL |
---|
| 472 | /* TODO : improve getTeam in such a way that it works |
---|
| 473 | * || humanPawn == NULL |
---|
| 474 | * || pawn->getTeam() == humanPawn->getTeam()*/) |
---|
| 475 | { |
---|
| 476 | // don't show marker for not selected enemies nor if the selected doesn't move |
---|
| 477 | it->second.target_->hide(); |
---|
| 478 | } |
---|
| 479 | else // object is selected and moves |
---|
| 480 | { |
---|
| 481 | // get the aim position |
---|
[10291] | 482 | const Vector3& targetPos = this->toAimPosition(it->first); |
---|
[9526] | 483 | // Transform to screen coordinates |
---|
[10291] | 484 | Vector3 screenPos = camTransform * targetPos; |
---|
[9526] | 485 | // Check if the target marker is in view too |
---|
| 486 | if(screenPos.z > 1 || screenPos.x < -1.0 || screenPos.x > 1.0 |
---|
| 487 | || screenPos.y < -1.0 || screenPos.y > 1.0) |
---|
| 488 | { |
---|
| 489 | it->second.target_->hide(); |
---|
| 490 | } |
---|
| 491 | else |
---|
| 492 | { |
---|
| 493 | it->second.target_->setLeft((screenPos.x + 1.0f - it->second.target_->getWidth()) * 0.5f); |
---|
| 494 | it->second.target_->setTop((-screenPos.y + 1.0f - it->second.target_->getHeight()) * 0.5f); |
---|
| 495 | it->second.target_->show(); |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | |
---|
[1411] | 499 | } |
---|
| 500 | } |
---|
[9348] | 501 | else // do not display on HUD |
---|
[9526] | 502 | |
---|
[1580] | 503 | { |
---|
[10258] | 504 | it->second.health_->hide(); |
---|
| 505 | it->second.healthLevel_->hide(); |
---|
[9348] | 506 | it->second.panel_->hide(); |
---|
| 507 | it->second.text_->hide(); |
---|
[9526] | 508 | it->second.target_->hide(); |
---|
[9348] | 509 | } |
---|
| 510 | } |
---|
[9526] | 511 | |
---|
| 512 | this->closestTarget_ = false; |
---|
| 513 | this->nextTarget_ = false; |
---|
[9348] | 514 | } |
---|
[7163] | 515 | |
---|
[9348] | 516 | /** Overridden method of OrxonoxOverlay. |
---|
[9526] | 517 | @details |
---|
| 518 | Usually the entire overlay scales with scale(). |
---|
| 519 | Here we obviously have to adjust this. |
---|
| 520 | */ |
---|
[9348] | 521 | void HUDNavigation::sizeChanged() |
---|
| 522 | { |
---|
| 523 | // Use size to compensate for aspect ratio if enabled. |
---|
| 524 | float xScale = this->getActualSize().x; |
---|
| 525 | float yScale = this->getActualSize().y; |
---|
[7163] | 526 | |
---|
[9348] | 527 | for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it) |
---|
[1580] | 528 | { |
---|
[10258] | 529 | if (it->second.health_ != NULL) |
---|
| 530 | it->second.health_->setDimensions(this->healthMarkerSize_ * xScale, this->healthMarkerSize_ * yScale); |
---|
| 531 | if (it->second.healthLevel_ != NULL) |
---|
| 532 | it->second.healthLevel_->setDimensions(this->healthLevelMarkerSize_ * xScale, this->healthLevelMarkerSize_ * yScale); |
---|
[9348] | 533 | if (it->second.panel_ != NULL) |
---|
| 534 | it->second.panel_->setDimensions(this->navMarkerSize_ * xScale, this->navMarkerSize_ * yScale); |
---|
| 535 | if (it->second.text_ != NULL) |
---|
[10292] | 536 | it->second.text_->setCharHeight(this->textSize_ * yScale); |
---|
[9526] | 537 | if (it->second.target_ != NULL) |
---|
| 538 | it->second.target_->setDimensions(this->aimMarkerSize_ * xScale, this->aimMarkerSize_ * yScale); |
---|
[7163] | 539 | } |
---|
| 540 | } |
---|
[1566] | 541 | |
---|
[9348] | 542 | void HUDNavigation::addObject(RadarViewable* object) |
---|
[7163] | 543 | { |
---|
[9348] | 544 | if (showObject(object) == false) |
---|
[9526] | 545 | return; |
---|
[7163] | 546 | |
---|
[9348] | 547 | if (this->activeObjectList_.size() >= this->markerLimit_) |
---|
[9526] | 548 | if (object == NULL) |
---|
| 549 | return; |
---|
[7163] | 550 | |
---|
[9348] | 551 | // Object hasn't been added yet (we know that) |
---|
| 552 | assert(this->activeObjectList_.find(object) == this->activeObjectList_.end()); |
---|
[7163] | 553 | |
---|
[9348] | 554 | // Scales used for dimensions and text size |
---|
| 555 | float xScale = this->getActualSize().x; |
---|
| 556 | float yScale = this->getActualSize().y; |
---|
[7163] | 557 | |
---|
[9348] | 558 | // Create everything needed to display the object on the radar and add it to the map |
---|
[7163] | 559 | |
---|
[10258] | 560 | // Create health |
---|
| 561 | Ogre::PanelOverlayElement* health = static_cast<Ogre::PanelOverlayElement*>( Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDNavigation_healthMarker_" + getUniqueNumberString())); |
---|
| 562 | //panel->setMaterialName("Orxonox/NavTDC"); |
---|
| 563 | health->setMaterialName(TextureGenerator::getMaterialName("barSquare.png", object->getRadarObjectColour())); |
---|
| 564 | health->setDimensions(this->healthMarkerSize_ * xScale, this->healthMarkerSize_ * yScale); |
---|
| 565 | //panel->setColour(object->getRadarObjectColour()); |
---|
| 566 | |
---|
| 567 | // Create healthLevel |
---|
| 568 | Ogre::PanelOverlayElement* healthLevel = static_cast<Ogre::PanelOverlayElement*>( Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDNavigation_healthLevelMarker_" + getUniqueNumberString())); |
---|
| 569 | //panel->setMaterialName("Orxonox/NavTDC"); |
---|
| 570 | health->setMaterialName(TextureGenerator::getMaterialName("barSquare.png", object->getRadarObjectColour())); |
---|
| 571 | health->setDimensions(this->healthLevelMarkerSize_ * xScale, this->healthLevelMarkerSize_ * yScale); |
---|
| 572 | |
---|
| 573 | |
---|
[9348] | 574 | // Create arrow/marker |
---|
| 575 | Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>( Ogre::OverlayManager::getSingleton() |
---|
[9526] | 576 | .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString())); |
---|
[9348] | 577 | //panel->setMaterialName("Orxonox/NavTDC"); |
---|
| 578 | panel->setMaterialName(TextureGenerator::getMaterialName("tdc.png", object->getRadarObjectColour())); |
---|
| 579 | panel->setDimensions(this->navMarkerSize_ * xScale, this->navMarkerSize_ * yScale); |
---|
| 580 | //panel->setColour(object->getRadarObjectColour()); |
---|
[7163] | 581 | |
---|
[9526] | 582 | // Create target marker |
---|
| 583 | Ogre::PanelOverlayElement* target = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() |
---|
| 584 | .createOverlayElement("Panel", "HUDNavigation_targetMarker_" + getUniqueNumberString())); |
---|
| 585 | target->setMaterialName(TextureGenerator::getMaterialName("target.png", object->getRadarObjectColour())); |
---|
| 586 | target->setDimensions(this->aimMarkerSize_ * xScale, this->aimMarkerSize_ * yScale); |
---|
| 587 | |
---|
| 588 | // Create text |
---|
[9348] | 589 | Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>( Ogre::OverlayManager::getSingleton() |
---|
[9526] | 590 | .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString())); |
---|
[9348] | 591 | text->setFontName(this->fontName_); |
---|
[10292] | 592 | text->setCharHeight(this->textSize_ * yScale); |
---|
[9348] | 593 | text->setColour(object->getRadarObjectColour()); |
---|
[7163] | 594 | |
---|
[10258] | 595 | health->hide(); |
---|
| 596 | healthLevel->hide(); |
---|
[9348] | 597 | panel->hide(); |
---|
[9526] | 598 | target->hide(); |
---|
[9348] | 599 | text->hide(); |
---|
[7163] | 600 | |
---|
[9526] | 601 | ObjectInfo tempStruct = |
---|
[10258] | 602 | { health, healthLevel, panel, target, text, false, false, false}; |
---|
[9348] | 603 | this->activeObjectList_[object] = tempStruct; |
---|
[7163] | 604 | |
---|
[10258] | 605 | this->background_->addChild(health); |
---|
| 606 | this->background_->addChild(healthLevel); |
---|
[9348] | 607 | this->background_->addChild(panel); |
---|
[9526] | 608 | this->background_->addChild(target); |
---|
[9348] | 609 | this->background_->addChild(text); |
---|
[7163] | 610 | |
---|
[9348] | 611 | this->sortedObjectList_.push_front(std::make_pair(object, (unsigned int)0)); |
---|
| 612 | } |
---|
[7163] | 613 | |
---|
[9348] | 614 | void HUDNavigation::removeObject(RadarViewable* viewable) |
---|
[1580] | 615 | { |
---|
[9348] | 616 | std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(viewable); |
---|
[7163] | 617 | |
---|
[9348] | 618 | if (this->activeObjectList_.find(viewable) != this->activeObjectList_.end()) |
---|
| 619 | { |
---|
| 620 | // Detach overlays |
---|
[10258] | 621 | this->background_->removeChild(it->second.health_->getName()); |
---|
| 622 | this->background_->removeChild(it->second.healthLevel_->getName()); |
---|
[9348] | 623 | this->background_->removeChild(it->second.panel_->getName()); |
---|
[9526] | 624 | this->background_->removeChild(it->second.target_->getName()); |
---|
[9348] | 625 | this->background_->removeChild(it->second.text_->getName()); |
---|
| 626 | // Properly destroy the overlay elements (do not use delete!) |
---|
[10258] | 627 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.health_); |
---|
| 628 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.healthLevel_); |
---|
[9348] | 629 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.panel_); |
---|
[9526] | 630 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.target_); |
---|
[9348] | 631 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.text_); |
---|
| 632 | // Remove from the list |
---|
| 633 | this->activeObjectList_.erase(viewable); |
---|
| 634 | } |
---|
[7163] | 635 | |
---|
[9348] | 636 | for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt) |
---|
| 637 | { |
---|
| 638 | if ((listIt->first) == viewable) |
---|
| 639 | { |
---|
| 640 | this->sortedObjectList_.erase(listIt); |
---|
| 641 | break; |
---|
| 642 | } |
---|
| 643 | } |
---|
[1410] | 644 | } |
---|
[1590] | 645 | |
---|
[9348] | 646 | void HUDNavigation::objectChanged(RadarViewable* viewable) |
---|
[1590] | 647 | { |
---|
[9348] | 648 | // TODO: niceification neccessary ;) |
---|
| 649 | removeObject(viewable); |
---|
| 650 | addObject(viewable); |
---|
| 651 | } |
---|
[7163] | 652 | |
---|
[9348] | 653 | bool HUDNavigation::showObject(RadarViewable* rv) |
---|
| 654 | { |
---|
| 655 | if (rv == orxonox_cast<RadarViewable*>(this->getOwner())) |
---|
[9526] | 656 | return false; |
---|
[9348] | 657 | assert(rv->getWorldEntity()); |
---|
| 658 | if (rv->getWorldEntity()->isVisible() == false || rv->getRadarVisibility() == false) |
---|
[9526] | 659 | return false; |
---|
[9348] | 660 | return true; |
---|
[1590] | 661 | } |
---|
[7163] | 662 | |
---|
[9348] | 663 | void HUDNavigation::changedOwner() |
---|
[7163] | 664 | { |
---|
[9348] | 665 | const std::set<RadarViewable*>& respawnObjects = this->getOwner()->getScene()->getRadar()->getRadarObjects(); |
---|
| 666 | for (std::set<RadarViewable*>::const_iterator it = respawnObjects.begin(); it != respawnObjects.end(); ++it) |
---|
| 667 | { |
---|
| 668 | if (!(*it)->isHumanShip_) |
---|
[9526] | 669 | this->addObject(*it); |
---|
[9348] | 670 | } |
---|
[7163] | 671 | } |
---|
[9526] | 672 | |
---|
[10291] | 673 | Vector3 HUDNavigation::toAimPosition(RadarViewable* target) const |
---|
[9526] | 674 | { |
---|
| 675 | Vector3 wePosition = HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition(); |
---|
| 676 | Vector3 targetPosition = target->getRVWorldPosition(); |
---|
| 677 | Vector3 targetSpeed = target->getRVVelocity(); |
---|
| 678 | |
---|
[10291] | 679 | return getPredictedPosition(wePosition, this->currentMunitionSpeed_, targetPosition, targetSpeed); |
---|
[9526] | 680 | } |
---|
| 681 | |
---|
| 682 | void HUDNavigation::selectClosestTarget() |
---|
| 683 | { |
---|
| 684 | if(HUDNavigation::localHUD_s) |
---|
| 685 | { |
---|
| 686 | HUDNavigation::localHUD_s->closestTarget_ = true; |
---|
| 687 | } |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | void HUDNavigation::selectNextTarget() |
---|
| 691 | { |
---|
| 692 | if(HUDNavigation::localHUD_s) |
---|
| 693 | { |
---|
| 694 | HUDNavigation::localHUD_s->nextTarget_ = true; |
---|
| 695 | } |
---|
| 696 | } |
---|
[7163] | 697 | } |
---|