Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai2/src/modules/overlays/hud/HUDRadar.cc @ 8875

Last change on this file since 8875 was 8875, checked in by jo, 13 years ago

Radar fix: pawns were not displayed when their visibility/activity changed from 'invisible' to 'visible'.

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
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 "HUDRadar.h"
31
32#include <OgreOverlayManager.h>
33#include <OgrePanelOverlayElement.h>
34
35#include "util/Math.h"
36#include "util/StringUtils.h"
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39#include "tools/TextureGenerator.h"
40#include "worldentities/ControllableEntity.h"
41#include "Scene.h"
42#include "Radar.h"
43
44namespace orxonox
45{
46    CreateFactory(HUDRadar);
47
48    HUDRadar::HUDRadar(BaseObject* creator)
49        : OrxonoxOverlay(creator)
50    {
51        RegisterObject(HUDRadar);
52
53        this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
54            .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString()));
55        this->marker_->setMaterialName("Orxonox/RadarMarker");
56        this->overlay_->add2D(this->marker_);
57        this->marker_->hide();
58
59        this->setRadarSensitivity(1.0f);
60        this->setHalfDotSizeDistance(3000.0f);
61        this->setMaximumDotSize(0.1f);
62
63        this->shapeMaterials_[RadarViewable::Dot]      = "RadarDot.png";
64        this->shapeMaterials_[RadarViewable::Triangle] = "RadarTriangle.png";
65        this->shapeMaterials_[RadarViewable::Square]   = "RadarSquare.png";
66        this->setDetectionLimit( 10000.0f );
67        this->owner_ = 0;
68    }
69
70    HUDRadar::~HUDRadar()
71    {
72        if (this->isInitialized())
73        {
74            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
75            for (std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it = this->radarObjects_.begin();
76                it != this->radarObjects_.end(); ++it)
77            {
78                Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second);
79            }
80        }
81    }
82
83    void HUDRadar::XMLPort(Element& xmlelement, XMLPort::Mode mode)
84    {
85        SUPER(HUDRadar, XMLPort, xmlelement, mode);
86
87        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlelement, mode);
88        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlelement, mode);
89        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlelement, mode);
90    }
91
92    void HUDRadar::addObject(RadarViewable* object)
93    {
94        if (object == dynamic_cast<RadarViewable*>(this->owner_))
95            return;
96        if( showObject(object) == false ) //do not show objects that are "invisible" or "radar invisible"
97            return;
98
99        // Make sure the object hasn't been added yet
100        assert( this->radarObjects_.find(object) == this->radarObjects_.end() );
101
102        // Create everything needed to display the object on the radar and add it to the map
103        Ogre::PanelOverlayElement* panel;
104        panel = static_cast<Ogre::PanelOverlayElement*>(
105            Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString()));
106        this->overlay_->add2D(panel);
107        // get right material
108        panel->setMaterialName(TextureGenerator::getMaterialName(
109            shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()));
110        this->radarObjects_[object] = panel;
111    }
112
113    void HUDRadar::removeObject(RadarViewable* object)
114    {
115        // If object was added at all then remove it
116        std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it;
117        it = this->radarObjects_.find( object );
118        if( it != this->radarObjects_.end() )
119        {
120            Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second);
121            this->radarObjects_.erase(it);
122        }
123    }
124
125    void HUDRadar::objectChanged( RadarViewable* rv )
126    {// The new implementation behaves more precisely, since inactive RadarViewables are not displayed anymore.
127        this->removeObject(rv);
128        this->addObject(rv);
129    }
130
131    void HUDRadar::gatherObjects()
132    {
133        const std::set<RadarViewable*>& objectSet = this->getCreator()->getScene()->getRadar()->getRadarObjects();
134        std::set<RadarViewable*>::const_iterator it;
135        for( it=objectSet.begin(); it!=objectSet.end(); ++it )
136            this->addObject(*it);
137    }
138
139    void HUDRadar::radarTick(float dt)
140    {
141        // Make sure the owner of the radar was defined
142        if( !this->owner_ )
143            return;
144
145        this->marker_->hide();      // in case that no object is in focus
146        // get the focus object
147        Radar* radar = this->getOwner()->getScene()->getRadar();
148        const RadarViewable* focusObject = radar->getFocus();
149
150        // update the distances for all objects
151        std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it;
152        for( it = this->radarObjects_.begin(); it != this->radarObjects_.end(); ++it )
153        {
154            // Make sure the object really is a WorldEntity
155            const WorldEntity* wePointer = it->first->getWorldEntity();
156            if( !wePointer )
157            {
158                CCOUT(0) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
159                assert(0);
160            }
161            bool isFocus = (it->first == focusObject);
162            // set size to fit distance...
163            float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length();
164            // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
165            float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
166            it->second->setDimensions(size, size);
167
168            // calc position on radar...
169            Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition());
170            coord *= math::pi / 3.5f; // small adjustment to make it fit the texture
171            it->second->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f);
172            if( distance < detectionLimit_ || detectionLimit_ < 0 )
173                it->second->show();
174            else
175                it->second->hide();
176
177            // if this object is in focus, then set the focus marker
178            if (isFocus)
179            {
180                this->marker_->setDimensions(size * 1.5f, size * 1.5f);
181                this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f);
182                this->marker_->show();
183            }
184        }
185    }
186
187    bool HUDRadar::showObject(RadarViewable* rv)
188    {
189        if ( rv == dynamic_cast<RadarViewable*> ( this->getOwner() ) )
190            return false;
191        assert( rv->getWorldEntity() );
192        if ( rv->getWorldEntity()->isVisible()==false || rv->getRadarVisibility()==false )
193            return false;
194        return true;
195    }
196
197
198    void HUDRadar::changedOwner()
199    {
200        SUPER(HUDRadar, changedOwner);
201
202        this->owner_ = orxonox_cast<ControllableEntity*>(this->getOwner());
203        assert(this->radarObjects_.size() == 0);
204        this->gatherObjects();
205    }
206}
Note: See TracBrowser for help on using the repository browser.