| 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 | *      Yuning Chai | 
|---|
| 24 | *      Felix Schulthess | 
|---|
| 25 | *   Co-authors: | 
|---|
| 26 | *      Reto Grieder | 
|---|
| 27 | * | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | #include "OrxonoxStableHeaders.h" | 
|---|
| 31 | #include "HUDRadar.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include <OgreOverlayManager.h> | 
|---|
| 34 | #include <OgrePanelOverlayElement.h> | 
|---|
| 35 |  | 
|---|
| 36 | #include "util/Math.h" | 
|---|
| 37 | #include "util/String.h" | 
|---|
| 38 | #include "core/ConsoleCommand.h" | 
|---|
| 39 | #include "core/CoreIncludes.h" | 
|---|
| 40 | #include "core/XMLPort.h" | 
|---|
| 41 | #include "objects/Radar.h" | 
|---|
| 42 | #include "objects/worldentities/WorldEntity.h" | 
|---|
| 43 | #include "objects/worldentities/pawns/Pawn.h" | 
|---|
| 44 | #include "tools/TextureGenerator.h" | 
|---|
| 45 |  | 
|---|
| 46 | namespace orxonox | 
|---|
| 47 | { | 
|---|
| 48 | CreateFactory(HUDRadar); | 
|---|
| 49 |  | 
|---|
| 50 | HUDRadar::HUDRadar(BaseObject* creator) | 
|---|
| 51 | : OrxonoxOverlay(creator) | 
|---|
| 52 | { | 
|---|
| 53 | RegisterObject(HUDRadar); | 
|---|
| 54 |  | 
|---|
| 55 | this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
| 56 | .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString())); | 
|---|
| 57 | this->marker_->setMaterialName("Orxonox/RadarMarker"); | 
|---|
| 58 | this->overlay_->add2D(this->marker_); | 
|---|
| 59 | this->marker_->hide(); | 
|---|
| 60 |  | 
|---|
| 61 | this->setRadarSensitivity(1.0f); | 
|---|
| 62 | this->setHalfDotSizeDistance(3000.0f); | 
|---|
| 63 | this->setMaximumDotSize(0.1f); | 
|---|
| 64 |  | 
|---|
| 65 | this->shapeMaterials_[RadarViewable::Dot]      = "RadarDot.tga"; | 
|---|
| 66 | this->shapeMaterials_[RadarViewable::Triangle] = "RadarSquare.tga"; | 
|---|
| 67 | this->shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga"; | 
|---|
| 68 |  | 
|---|
| 69 | this->owner_ = 0; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | HUDRadar::~HUDRadar() | 
|---|
| 73 | { | 
|---|
| 74 | if (this->isInitialized()) | 
|---|
| 75 | { | 
|---|
| 76 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_); | 
|---|
| 77 | for (std::vector<Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.begin(); | 
|---|
| 78 | it != this->radarDots_.end(); ++it) | 
|---|
| 79 | { | 
|---|
| 80 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(*it); | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode) | 
|---|
| 86 | { | 
|---|
| 87 | SUPER(HUDRadar, XMLPort, xmlElement, mode); | 
|---|
| 88 |  | 
|---|
| 89 | XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode); | 
|---|
| 90 | XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode); | 
|---|
| 91 | XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked) | 
|---|
| 95 | { | 
|---|
| 96 | if (object == (RadarViewable*)this->owner_) | 
|---|
| 97 | return; | 
|---|
| 98 |  | 
|---|
| 99 | const WorldEntity* wePointer = object->getWorldEntity(); | 
|---|
| 100 |  | 
|---|
| 101 | // Just to be sure that we actually have a WorldEntity. | 
|---|
| 102 | // We could do a dynamic_cast, but that would be a lot slower. | 
|---|
| 103 | if (!wePointer || !this->owner_) | 
|---|
| 104 | { | 
|---|
| 105 | if (!wePointer) | 
|---|
| 106 | CCOUT(2) << "Cannot display a non-WorldEntitiy on the radar" << std::endl; | 
|---|
| 107 | if (!this->owner_) | 
|---|
| 108 | CCOUT(2) << "No owner defined" << std::endl; | 
|---|
| 109 | return; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | // try to find a panel already created | 
|---|
| 113 | Ogre::PanelOverlayElement* panel; | 
|---|
| 114 | //std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object); | 
|---|
| 115 | if (itRadarDots_ == this->radarDots_.end()) | 
|---|
| 116 | { | 
|---|
| 117 | // we have to create a new entry | 
|---|
| 118 | panel = static_cast<Ogre::PanelOverlayElement*>( | 
|---|
| 119 | Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString())); | 
|---|
| 120 | radarDots_.push_back(panel); | 
|---|
| 121 | // get right material | 
|---|
| 122 | panel->setMaterialName(TextureGenerator::getMaterialName( | 
|---|
| 123 | shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour())); | 
|---|
| 124 | this->overlay_->add2D(panel); | 
|---|
| 125 | this->itRadarDots_ = this->radarDots_.end(); | 
|---|
| 126 | } | 
|---|
| 127 | else | 
|---|
| 128 | { | 
|---|
| 129 | panel = *itRadarDots_; | 
|---|
| 130 | ++itRadarDots_; | 
|---|
| 131 | std::string materialName = TextureGenerator::getMaterialName( | 
|---|
| 132 | shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()); | 
|---|
| 133 | if (materialName != panel->getMaterialName()) | 
|---|
| 134 | panel->setMaterialName(materialName); | 
|---|
| 135 | } | 
|---|
| 136 | panel->show(); | 
|---|
| 137 |  | 
|---|
| 138 | // set size to fit distance... | 
|---|
| 139 | float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length(); | 
|---|
| 140 | // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda) | 
|---|
| 141 | float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance); | 
|---|
| 142 | panel->setDimensions(size, size); | 
|---|
| 143 |  | 
|---|
| 144 | // calc position on radar... | 
|---|
| 145 | Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition()); | 
|---|
| 146 | coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture | 
|---|
| 147 | panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5); | 
|---|
| 148 |  | 
|---|
| 149 | if (bIsMarked) | 
|---|
| 150 | { | 
|---|
| 151 | this->marker_->show(); | 
|---|
| 152 | this->marker_->setDimensions(size * 1.5, size * 1.5); | 
|---|
| 153 | this->marker_->setPosition((1.0 + coord.x - size * 1.5) * 0.5, (1.0 - coord.y - size * 1.5) * 0.5); | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | void HUDRadar::radarTick(float dt) | 
|---|
| 158 | { | 
|---|
| 159 | for (itRadarDots_ = radarDots_.begin(); itRadarDots_ != radarDots_.end(); ++itRadarDots_) | 
|---|
| 160 | (*itRadarDots_)->hide(); | 
|---|
| 161 | this->itRadarDots_ = this->radarDots_.begin(); | 
|---|
| 162 | this->marker_->hide(); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | void HUDRadar::changedOwner() | 
|---|
| 166 | { | 
|---|
| 167 | SUPER(HUDRadar, changedOwner); | 
|---|
| 168 |  | 
|---|
| 169 | this->owner_ = dynamic_cast<Pawn*>(this->getOwner()); | 
|---|
| 170 | } | 
|---|
| 171 | } | 
|---|