[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
[1502] | 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 | * |
---|
[1502] | 22 | * Author: |
---|
| 23 | * Yuning Chai |
---|
| 24 | * Co-authors: |
---|
| 25 | * Felix Schulthess |
---|
| 26 | * |
---|
| 27 | */ |
---|
[1283] | 28 | |
---|
[1502] | 29 | #include "OrxonoxStableHeaders.h" |
---|
[1604] | 30 | #include "HUDRadar.h" |
---|
[1283] | 31 | |
---|
[1604] | 32 | #include <assert.h> |
---|
[1502] | 33 | #include <OgreOverlayManager.h> |
---|
[1609] | 34 | #include <OgreMaterialManager.h> |
---|
[1502] | 35 | |
---|
| 36 | #include "core/ConsoleCommand.h" |
---|
| 37 | #include "objects/SpaceShip.h" |
---|
[1604] | 38 | #include "objects/WorldEntity.h" |
---|
[1613] | 39 | #include "tools/TextureGenerator.h" |
---|
[1609] | 40 | #include "RadarViewable.h" |
---|
[1502] | 41 | |
---|
[1283] | 42 | namespace orxonox |
---|
| 43 | { |
---|
[1604] | 44 | CreateFactory(HUDRadar); |
---|
[1609] | 45 | CreateFactory(RadarShape); |
---|
[1604] | 46 | |
---|
[1302] | 47 | using namespace Ogre; |
---|
[1283] | 48 | |
---|
[1604] | 49 | HUDRadar::HUDRadar() |
---|
| 50 | : background_(0) |
---|
[1613] | 51 | , marker_(0) |
---|
| 52 | , sensitivity_(1.0f) |
---|
[1604] | 53 | { |
---|
| 54 | RegisterObject(HUDRadar); |
---|
[1302] | 55 | } |
---|
[1283] | 56 | |
---|
[1604] | 57 | HUDRadar::~HUDRadar() |
---|
| 58 | { |
---|
| 59 | if (this->isInitialized()) |
---|
| 60 | { |
---|
[1613] | 61 | /*if (this->background_) |
---|
[1604] | 62 | OverlayManager::getSingleton().destroyOverlayElement(this->background_); |
---|
[1609] | 63 | while (this->radarDots_.size() > 0) |
---|
| 64 | { |
---|
| 65 | OverlayManager::getSingleton().destroyOverlayElement(this->radarDots_[this->radarDots_.size() - 1]); |
---|
| 66 | this->radarDots_.pop_back(); |
---|
[1613] | 67 | }*/ |
---|
[1604] | 68 | } |
---|
[1302] | 69 | } |
---|
[1283] | 70 | |
---|
[1604] | 71 | void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
| 72 | { |
---|
| 73 | OrxonoxOverlay::XMLPort(xmlElement, mode); |
---|
[1283] | 74 | |
---|
[1613] | 75 | if (mode == XMLPort::LoadObject) |
---|
| 76 | { |
---|
| 77 | this->sensitivity_ = 1.0f; |
---|
| 78 | this->halfDotSizeDistance_ = 3000.0f; |
---|
| 79 | this->maximumDotSize_ = 0.1; |
---|
| 80 | } |
---|
[1609] | 81 | |
---|
[1613] | 82 | XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode); |
---|
| 83 | XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode); |
---|
| 84 | XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode); |
---|
| 85 | |
---|
| 86 | shapeMaterials_[RadarViewable::Dot] = "RadarSquare.tga"; |
---|
| 87 | shapeMaterials_[RadarViewable::Triangle] = "RadarSquare.tga"; |
---|
| 88 | shapeMaterials_[RadarViewable::Square] = "RadarSquare.tga"; |
---|
| 89 | |
---|
[1604] | 90 | if (mode == XMLPort::LoadObject) |
---|
| 91 | { |
---|
| 92 | background_ = (Ogre::PanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Background"); |
---|
| 93 | background_->setMaterialName("Orxonox/Radar"); |
---|
| 94 | overlay_->add2D(background_); |
---|
[1339] | 95 | |
---|
[1613] | 96 | marker_ = (Ogre::PanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Marker"); |
---|
| 97 | marker_->setMaterialName("Orxonox/RadarMarker"); |
---|
| 98 | overlay_->add2D(marker_); |
---|
| 99 | marker_->hide(); |
---|
[1604] | 100 | } |
---|
[1609] | 101 | } |
---|
[1302] | 102 | |
---|
[1613] | 103 | /*void HUDRadar::addShape(RadarShape* shape) |
---|
[1609] | 104 | { |
---|
[1613] | 105 | this->shapes_[shape->getIndex()] = shape; |
---|
[1314] | 106 | } |
---|
| 107 | |
---|
[1613] | 108 | RadarShape* HUDRadar::getShape(unsigned int index) const |
---|
[1604] | 109 | { |
---|
[1613] | 110 | if (index < this->shapes_.size()) |
---|
[1564] | 111 | { |
---|
[1613] | 112 | std::map<unsigned int, RadarShape*>::const_iterator it = shapes_.begin(); |
---|
[1609] | 113 | for (unsigned int i = 0; i != index; ++it, ++i) |
---|
| 114 | ; |
---|
| 115 | return (*it).second; |
---|
[1502] | 116 | } |
---|
[1609] | 117 | else |
---|
| 118 | return 0; |
---|
[1613] | 119 | }*/ |
---|
[1310] | 120 | |
---|
[1613] | 121 | void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked) |
---|
[1604] | 122 | { |
---|
[1613] | 123 | const WorldEntity* wePointer = object->getWorldEntity(); |
---|
[1604] | 124 | |
---|
[1613] | 125 | // Just to be sure that we actually have a WorldEntity |
---|
| 126 | // We could do a dynamic_cast, but that's a lot slower |
---|
| 127 | if (!wePointer) |
---|
[1609] | 128 | { |
---|
[1613] | 129 | CCOUT(4) << "Cannot display a non-WorldEntitiy on the radar" << std::endl; |
---|
| 130 | return; |
---|
[1609] | 131 | } |
---|
[1613] | 132 | |
---|
| 133 | /*static int counter = 0; |
---|
| 134 | if (++counter < 1120 && counter > 120) |
---|
| 135 | { |
---|
| 136 | // we have to create a new entry |
---|
| 137 | Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>( |
---|
| 138 | Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr())); |
---|
| 139 | // get right material |
---|
| 140 | panel->setMaterialName("Orxonox/RadarSquare"); |
---|
| 141 | panel->setDimensions(0.03, 0.03); |
---|
| 142 | panel->setPosition((1.0 + (rand() & 0xFF) / 256.0 - 0.001) * 0.5, (1.0 - (rand() & 0xFF) / 256.0 - 0.001) * 0.5); |
---|
| 143 | panel->show(); |
---|
| 144 | this->overlay_->add2D(panel); |
---|
| 145 | this->overlay_->show(); |
---|
| 146 | }*/ |
---|
| 147 | |
---|
| 148 | // try to find a panel already created |
---|
| 149 | Ogre::PanelOverlayElement* panel; |
---|
| 150 | std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object); |
---|
| 151 | if (it == this->radarDots_.end()) |
---|
| 152 | { |
---|
| 153 | // we have to create a new entry |
---|
| 154 | panel = static_cast<Ogre::PanelOverlayElement*>( |
---|
| 155 | Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr())); |
---|
| 156 | radarDots_[object] = panel; |
---|
| 157 | // get right material |
---|
| 158 | panel->setMaterialName(TextureGenerator::getMaterialName( |
---|
| 159 | shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour())); |
---|
| 160 | this->overlay_->add2D(panel); |
---|
| 161 | } |
---|
[1609] | 162 | else |
---|
[1613] | 163 | panel = (*it).second; |
---|
| 164 | |
---|
| 165 | // set size to fit distance... |
---|
| 166 | float distance = (wePointer->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length(); |
---|
| 167 | // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda) |
---|
| 168 | float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance); |
---|
| 169 | panel->setDimensions(size, size); |
---|
| 170 | |
---|
| 171 | // calc position on radar... |
---|
| 172 | Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), wePointer->getWorldPosition()); |
---|
| 173 | coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture |
---|
| 174 | panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5); |
---|
| 175 | |
---|
| 176 | if (bIsMarked) |
---|
| 177 | { |
---|
| 178 | this->marker_->show(); |
---|
| 179 | this->marker_->setDimensions(size * 1.2, size * 1.2); |
---|
| 180 | this->marker_->setPosition((1.0 + coord.x - size * 1.2) * 0.5, (1.0 - coord.y - size * 1.2) * 0.5); |
---|
| 181 | } |
---|
[1604] | 182 | } |
---|
| 183 | |
---|
[1613] | 184 | void HUDRadar::radarTick(float dt) |
---|
[1604] | 185 | { |
---|
[1613] | 186 | } |
---|
| 187 | |
---|
| 188 | /*void HUDRadar::tick(float dt) |
---|
| 189 | { |
---|
[1609] | 190 | // iterate through all RadarObjects |
---|
| 191 | unsigned int i = 0; |
---|
| 192 | for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::start(); it; ++it, ++i) |
---|
[1604] | 193 | { |
---|
[1609] | 194 | if ((*it)->isVisibleOnRadar()) |
---|
[1604] | 195 | { |
---|
| 196 | } |
---|
| 197 | } |
---|
[1613] | 198 | }*/ |
---|
[1604] | 199 | |
---|
[1613] | 200 | /*void HUDRadar::listObjects() |
---|
[1609] | 201 | { |
---|
| 202 | COUT(3) << "List of RadarObjects:\n"; |
---|
| 203 | // iterate through all Radar Objects |
---|
| 204 | unsigned int i = 0; |
---|
| 205 | for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::start(); it; ++it, ++i) |
---|
| 206 | { |
---|
| 207 | COUT(3) << i++ << ": " << (*it)->getWorldEntity()->getWorldPosition() << std::endl; |
---|
| 208 | } |
---|
[1613] | 209 | }*/ |
---|
[1283] | 210 | } |
---|