/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Yuning Chai * Felix Schulthess * Co-authors: * Reto Grieder * */ #include "OrxonoxStableHeaders.h" #include "HUDRadar.h" #include #include #include "util/Math.h" #include "core/ConsoleCommand.h" #include "objects/SpaceShip.h" #include "objects/WorldEntity.h" #include "tools/TextureGenerator.h" #include "Radar.h" namespace orxonox { CreateFactory(HUDRadar); using namespace Ogre; HUDRadar::HUDRadar() : marker_(0) { RegisterObject(HUDRadar); } HUDRadar::~HUDRadar() { if (this->isInitialized()) { if (this->marker_) OverlayManager::getSingleton().destroyOverlayElement(this->marker_); for (std::vector::iterator it = this->radarDots_.begin(); it != this->radarDots_.end(); ++it) { OverlayManager::getSingleton().destroyOverlayElement(*it); } } } void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode) { OrxonoxOverlay::XMLPort(xmlElement, mode); if (mode == XMLPort::LoadObject) { this->sensitivity_ = 1.0f; this->halfDotSizeDistance_ = 3000.0f; this->maximumDotSize_ = 0.1; } XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode); XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode); XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode); shapeMaterials_[RadarViewable::Dot] = "RadarSquare.tga"; shapeMaterials_[RadarViewable::Triangle] = "RadarSquare.tga"; shapeMaterials_[RadarViewable::Square] = "RadarSquare.tga"; if (mode == XMLPort::LoadObject) { marker_ = (Ogre::PanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Marker"); marker_->setMaterialName("Orxonox/RadarMarker"); overlay_->add2D(marker_); marker_->hide(); } } void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked) { const WorldEntity* wePointer = object->getWorldEntity(); // Just to be sure that we actually have a WorldEntity // We could do a dynamic_cast, but that's a lot slower if (!wePointer) { CCOUT(4) << "Cannot display a non-WorldEntitiy on the radar" << std::endl; return; } // try to find a panel already created Ogre::PanelOverlayElement* panel; //std::map::iterator it = this->radarDots_.find(object); if (itRadarDots_ == this->radarDots_.end()) { // we have to create a new entry panel = static_cast( Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr())); radarDots_.push_back(panel); // get right material panel->setMaterialName(TextureGenerator::getMaterialName( shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour())); this->overlay_->add2D(panel); this->itRadarDots_ = this->radarDots_.end(); } else { panel = *itRadarDots_; ++itRadarDots_; std::string materialName = TextureGenerator::getMaterialName( shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour()); if (materialName != panel->getMaterialName()) panel->setMaterialName(materialName); } // set size to fit distance... float distance = (wePointer->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length(); // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda) float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance); panel->setDimensions(size, size); // calc position on radar... Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), wePointer->getWorldPosition()); coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5); if (bIsMarked) { this->marker_->show(); this->marker_->setDimensions(size * 1.5, size * 1.5); this->marker_->setPosition((1.0 + coord.x - size * 1.5) * 0.5, (1.0 - coord.y - size * 1.5) * 0.5); } } void HUDRadar::radarTick(float dt) { this->itRadarDots_ = this->radarDots_.begin(); this->marker_->hide(); } }