[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" |
---|
[1609] | 39 | #include "RadarViewable.h" |
---|
[1502] | 40 | |
---|
[1283] | 41 | namespace orxonox |
---|
| 42 | { |
---|
[1604] | 43 | CreateFactory(HUDRadar); |
---|
[1609] | 44 | CreateFactory(RadarColour); |
---|
| 45 | CreateFactory(RadarShape); |
---|
[1604] | 46 | |
---|
| 47 | HUDRadar* HUDRadar::instance_s = 0; |
---|
| 48 | |
---|
[1302] | 49 | using namespace Ogre; |
---|
[1283] | 50 | |
---|
[1604] | 51 | HUDRadar::HUDRadar() |
---|
| 52 | : background_(0) |
---|
| 53 | { |
---|
| 54 | RegisterObject(HUDRadar); |
---|
| 55 | |
---|
| 56 | assert(instance_s == 0); |
---|
| 57 | instance_s = this; |
---|
[1302] | 58 | } |
---|
[1283] | 59 | |
---|
[1604] | 60 | HUDRadar::~HUDRadar() |
---|
| 61 | { |
---|
| 62 | if (this->isInitialized()) |
---|
| 63 | { |
---|
| 64 | if (this->background_) |
---|
| 65 | OverlayManager::getSingleton().destroyOverlayElement(this->background_); |
---|
[1609] | 66 | while (this->radarDots_.size() > 0) |
---|
| 67 | { |
---|
| 68 | OverlayManager::getSingleton().destroyOverlayElement(this->radarDots_[this->radarDots_.size() - 1]); |
---|
| 69 | this->radarDots_.pop_back(); |
---|
| 70 | } |
---|
[1604] | 71 | } |
---|
| 72 | |
---|
| 73 | instance_s = 0; |
---|
[1302] | 74 | } |
---|
[1283] | 75 | |
---|
[1604] | 76 | void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
| 77 | { |
---|
| 78 | OrxonoxOverlay::XMLPort(xmlElement, mode); |
---|
[1283] | 79 | |
---|
[1609] | 80 | XMLPortObject(HUDRadar, RadarColour, "shapes", addShape, getShape, xmlElement, mode, false, true); |
---|
| 81 | |
---|
[1604] | 82 | if (mode == XMLPort::LoadObject) |
---|
| 83 | { |
---|
| 84 | background_ = (Ogre::PanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Background"); |
---|
| 85 | background_->setMaterialName("Orxonox/Radar"); |
---|
| 86 | overlay_->add2D(background_); |
---|
[1339] | 87 | |
---|
[1609] | 88 | // create an array of all possible materials |
---|
| 89 | unsigned int iMaterial = 0; |
---|
| 90 | for (std::map<unsigned int, RadarShape*>::const_iterator itShape = shapes_.begin(); itShape != shapes_.end(); ++itShape) |
---|
| 91 | { |
---|
| 92 | Ogre::MaterialPtr originalMaterial = (Ogre::MaterialPtr)(Ogre::MaterialManager::getSingleton().getByName((*itShape).second->getAttribute())); |
---|
| 93 | for (std::map<unsigned int, RadarColour*>::const_iterator itColour = colours_.begin(); itColour != colours_.end(); ++itColour) |
---|
| 94 | { |
---|
| 95 | Ogre::MaterialPtr newMaterial = originalMaterial->clone((*itShape).second->getAttribute() + convertToString(iMaterial++)); |
---|
| 96 | newMaterial->getTechnique(0)->getPass(0)->setAmbient((*itColour).second->getAttribute()); |
---|
| 97 | materialNames_[(*itShape).first + ((*itColour).first << 8)] = newMaterial->getName(); |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | /*WorldEntity* object; |
---|
[1604] | 102 | object = new WorldEntity(); |
---|
| 103 | object->setPosition(2000.0, 0.0, 0.0); |
---|
| 104 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 105 | object = new WorldEntity(); |
---|
| 106 | object->setPosition(0.0, 2000.0, 0.0); |
---|
| 107 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 108 | object = new WorldEntity(); |
---|
| 109 | object->setPosition(0.0, 0.0, 2000.0); |
---|
| 110 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 111 | object = new WorldEntity(); |
---|
| 112 | object->setPosition(10000.0,16000.0,0.0); |
---|
[1609] | 113 | addRadarObject(object);*/ |
---|
[1604] | 114 | } |
---|
[1609] | 115 | } |
---|
[1302] | 116 | |
---|
[1609] | 117 | void HUDRadar::addColour(RadarColour* colour) |
---|
| 118 | { |
---|
| 119 | this->colours_[colour->getIndex()] = colour; |
---|
[1314] | 120 | } |
---|
| 121 | |
---|
[1609] | 122 | RadarColour* HUDRadar::getColour(unsigned int index) const |
---|
[1604] | 123 | { |
---|
[1609] | 124 | if (index < this->colours_.size()) |
---|
[1564] | 125 | { |
---|
[1609] | 126 | std::map<unsigned int, RadarColour*>::const_iterator it = colours_.begin(); |
---|
| 127 | for (unsigned int i = 0; i != index; ++it, ++i) |
---|
| 128 | ; |
---|
| 129 | return (*it).second; |
---|
[1502] | 130 | } |
---|
[1609] | 131 | else |
---|
| 132 | return 0; |
---|
[1339] | 133 | } |
---|
[1310] | 134 | |
---|
[1609] | 135 | void HUDRadar::addShape(RadarShape* shape) |
---|
[1604] | 136 | { |
---|
[1609] | 137 | this->shapes_[shape->getIndex()] = shape; |
---|
[1407] | 138 | } |
---|
[1604] | 139 | |
---|
[1609] | 140 | RadarShape* HUDRadar::getShape(unsigned int index) const |
---|
[1604] | 141 | { |
---|
[1609] | 142 | if (index < this->shapes_.size()) |
---|
| 143 | { |
---|
| 144 | std::map<unsigned int, RadarShape*>::const_iterator it = shapes_.begin(); |
---|
| 145 | for (unsigned int i = 0; i != index; ++it, ++i) |
---|
| 146 | ; |
---|
| 147 | return (*it).second; |
---|
| 148 | } |
---|
| 149 | else |
---|
| 150 | return 0; |
---|
[1604] | 151 | } |
---|
| 152 | |
---|
[1609] | 153 | void HUDRadar::tick(float dt) |
---|
[1604] | 154 | { |
---|
[1609] | 155 | // iterate through all RadarObjects |
---|
| 156 | unsigned int i = 0; |
---|
| 157 | for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::start(); it; ++it, ++i) |
---|
[1604] | 158 | { |
---|
[1609] | 159 | if ((*it)->isVisibleOnRadar()) |
---|
[1604] | 160 | { |
---|
[1609] | 161 | WorldEntity* object = (*it)->getWorldEntity(); |
---|
| 162 | // Just to be sure that we actually have a WorldEntity |
---|
| 163 | // We could do a dynamic_cast, but that's a lot slower |
---|
| 164 | assert(object); |
---|
[1604] | 165 | |
---|
[1609] | 166 | // set size to fit distance... |
---|
| 167 | float size = 1.0/((object->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length()); |
---|
| 168 | size = clamp(size * 100.0f, 0.02f, 0.12f); |
---|
| 169 | if (i == radarDots_.size()) |
---|
| 170 | { |
---|
| 171 | // we have to create a new panel |
---|
| 172 | radarDots_.push_back(static_cast<Ogre::PanelOverlayElement*>( |
---|
| 173 | Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + convertToString(i)))); |
---|
| 174 | } |
---|
| 175 | radarDots_[i]->setDimensions(size, size); |
---|
| 176 | |
---|
| 177 | // calc position on radar... |
---|
| 178 | Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), object->getWorldPosition()); |
---|
| 179 | coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture |
---|
| 180 | radarDots_[i]->setPosition((1.0 + coord.x) * 0.5, (1.0 - coord.y) * 0.5); |
---|
| 181 | |
---|
| 182 | // apply the right material |
---|
| 183 | RadarPoint description = (*it)->getDescription(); |
---|
| 184 | radarDots_[i]->setMaterialName(materialNames_[description.shape_ + (description.colour_ << 8)]); |
---|
[1604] | 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
[1609] | 189 | void HUDRadar::listObjects() |
---|
| 190 | { |
---|
| 191 | COUT(3) << "List of RadarObjects:\n"; |
---|
| 192 | // iterate through all Radar Objects |
---|
| 193 | unsigned int i = 0; |
---|
| 194 | for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::start(); it; ++it, ++i) |
---|
| 195 | { |
---|
| 196 | COUT(3) << i++ << ": " << (*it)->getWorldEntity()->getWorldPosition() << std::endl; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
[1604] | 200 | /*static*/ HUDRadar& HUDRadar::getInstance() |
---|
| 201 | { |
---|
| 202 | assert(instance_s); |
---|
| 203 | return *instance_s; |
---|
| 204 | } |
---|
[1283] | 205 | } |
---|