Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/Radar.cc @ 10886

Last change on this file since 10886 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 6.8 KB
RevLine 
[1818]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 *      Reto Grieder
24 *   Co-authors:
25 *      Felix Schulthess
26 *
27 */
28
29/**
[3196]30@file
31@brief
[1818]32*/
33
34#include "Radar.h"
[3196]35
[1818]36#include <cassert>
[3196]37
38//#include "util/Math.h"
[10624]39#include "core/CoreIncludes.h"
[9667]40#include "core/object/ObjectList.h"
[7284]41#include "core/command/ConsoleCommand.h"
[3196]42#include "interfaces/RadarListener.h"
[6417]43#include "controllers/HumanController.h"
44#include "worldentities/pawns/Pawn.h"
[1818]45
46namespace orxonox
47{
[10624]48    RegisterAbstractClass(Radar).inheritsFrom<Tickable>();
[1818]49
50    Radar::Radar()
[6417]51        : itFocus_(0)
52        , focus_(0)
[1818]53        , objectTypeCounter_(0)
54    {
55        // TODO: make this mapping configurable. Maybe there's a possibility with self configured
56        //       configValues..
57        this->objectTypes_["Asteroid"] = RadarViewable::Dot;
58        this->objectTypes_["SpaceShip"] = RadarViewable::Square;
59        this->objectTypes_["AsdfQwerty"] = RadarViewable::Triangle;
60
61        /*WorldEntity* object;
62        object = new WorldEntity();
63        object->setPosition(2000.0, 0.0, 0.0);
64        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
65        object = new WorldEntity();
66        object->setPosition(0.0, 2000.0, 0.0);
67        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
68        object = new WorldEntity();
69        object->setPosition(0.0, 0.0, 2000.0);
70        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
71        object = new WorldEntity();
72        object->setPosition(10000.0,16000.0,0.0);
73        addRadarObject(object);*/
74
75    }
76
77    Radar::~Radar()
78    {
79    }
80
[7163]81    void Radar::addRadarObject(RadarViewable* rv)
82    {
83        assert( this->radarObjects_.find(rv) == this->radarObjects_.end() );
84        this->radarObjects_.insert(rv);
85        // iterate through all radarlisteners and notify them
86        for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
87        {
88            (*itListener)->addObject(rv);
89        }
90    }
91
92    void Radar::removeRadarObject(RadarViewable* rv)
93    {
94        assert( this->radarObjects_.find(rv) != this->radarObjects_.end() );
95        this->radarObjects_.erase(rv);
96        // iterate through all radarlisteners and notify them
97        for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
98        {
99            (*itListener)->removeObject(rv);
100        }
101    }
102
[1818]103    const RadarViewable* Radar::getFocus()
104    {
[5929]105        if (this->itFocus_)
106            return *(this->itFocus_);
107        else
108            return 0;
[1818]109    }
110
[6417]111    RadarViewable::Shape Radar::addObjectDescription(const std::string& name)
[1818]112    {
113        std::map<std::string, RadarViewable::Shape>::iterator it = this->objectTypes_.find(name);
114        if (it == this->objectTypes_.end())
115            return this->objectTypes_[name] = RadarViewable::Square; // default, configure!!
116        else
117            return this->objectTypes_[name];
118    }
119
120
121    void Radar::tick(float dt)
122    {
[2662]123        SUPER(Radar, tick, dt);
124
[5929]125        if (this->itFocus_ && (this->focus_ != *(this->itFocus_)))
[1818]126        {
127            // focus object was deleted, release focus
128            this->focus_ = 0;
129            this->itFocus_ = 0;
130        }
131
132        for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
133        {
134            (*itListener)->radarTick(dt);
135        }
136    }
137
138    void Radar::cycleFocus()
139    {
[6417]140        if (ObjectList<RadarViewable>::begin() == ObjectList<RadarViewable>::end())
[1818]141        {
142            // list is empty
143            this->itFocus_ = 0;
144            this->focus_ = 0;
145        }
[6417]146
147        else if (HumanController::getLocalControllerEntityAsPawn())
[1818]148        {
[6417]149            Vector3 localPosition = HumanController::getLocalControllerEntityAsPawn()->getWorldPosition();
[1818]150            Vector3 targetPosition = localPosition;
[6417]151            if (this->itFocus_ && *(this->itFocus_))
[2662]152                targetPosition = this->itFocus_->getRVWorldPosition();
[1818]153
[6417]154            // find the closest object further away than targetPosition
[1818]155            float currentDistance = localPosition.squaredDistance(targetPosition);
156            float nextDistance = FLT_MAX;
157            float minimumDistance = FLT_MAX;
158            ObjectList<RadarViewable>::iterator itFallback = 0;
159
160            for (ObjectList<RadarViewable>::iterator it = ObjectList<RadarViewable>::begin(); it; ++it)
161            {
[6417]162                if (*it == static_cast<RadarViewable*>(HumanController::getLocalControllerEntityAsPawn()))
[1818]163                    continue;
[2662]164
165                float targetDistance = localPosition.squaredDistance((*it)->getRVWorldPosition());
[1818]166                if (targetDistance > currentDistance && targetDistance < nextDistance)
167                {
168                    this->itFocus_ = it;
169                    nextDistance = targetDistance;
170                }
171                if (targetDistance < minimumDistance)
172                {
173                    itFallback = it;
174                    minimumDistance = targetDistance;
175                }
176            }
177
178            if (nextDistance == FLT_MAX)
179            {
180                // we already had the furthest object
181                this->itFocus_ = itFallback;
182                this->focus_ = *itFallback;
183            }
184            else
185            {
186                this->focus_ = *(this->itFocus_);
187            }
188        }
189    }
190
191    void Radar::releaseFocus()
192    {
193        this->itFocus_ = 0;
194        this->focus_ = 0;
195    }
196
197    void Radar::listObjects() const
198    {
[8858]199        orxout(debug_output) << "List of RadarObjects:" << endl;
[1818]200        // iterate through all Radar Objects
201        unsigned int i = 0;
202        for (ObjectList<RadarViewable>::iterator it = ObjectList<RadarViewable>::begin(); it; ++it, ++i)
203        {
[8858]204            orxout(debug_output) << i++ << ": " << (*it)->getRVWorldPosition() << endl;
[1818]205        }
206    }
[7163]207
208    void Radar::radarObjectChanged(RadarViewable* rv)
209    {
210        for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
211        {
212          (*itListener)->objectChanged(rv);
213        }
214    }
[1818]215}
Note: See TracBrowser for help on using the repository browser.