Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11481 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 6.7 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()
[11071]51        : itFocus_(nullptr)
52        , focus_(nullptr)
[1818]53        , objectTypeCounter_(0)
54    {
55        // TODO: make this mapping configurable. Maybe there's a possibility with self configured
56        //       configValues..
[11071]57        this->objectTypes_["Asteroid"] = RadarViewable::Shape::Dot;
58        this->objectTypes_["SpaceShip"] = RadarViewable::Shape::Square;
59        this->objectTypes_["AsdfQwerty"] = RadarViewable::Shape::Triangle;
[1818]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
[11071]86        for (RadarListener* listener : ObjectList<RadarListener>())
[7163]87        {
[11071]88            listener->addObject(rv);
[7163]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
[11071]97        for (RadarListener* listener : ObjectList<RadarListener>())
[7163]98        {
[11071]99            listener->removeObject(rv);
[7163]100        }
101    }
102
[1818]103    const RadarViewable* Radar::getFocus()
104    {
[5929]105        if (this->itFocus_)
106            return *(this->itFocus_);
107        else
[11071]108            return nullptr;
[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())
[11071]115            return this->objectTypes_[name] = RadarViewable::Shape::Square; // default, configure!!
[1818]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
[11071]128            this->focus_ = nullptr;
129            this->itFocus_ = nullptr;
[1818]130        }
131
[11071]132        for (RadarListener* listener : ObjectList<RadarListener>())
[1818]133        {
[11071]134            listener->radarTick(dt);
[1818]135        }
136    }
137
138    void Radar::cycleFocus()
139    {
[11071]140        ObjectList<RadarViewable> listRadarViewable;
141        if (listRadarViewable.size() == 0)
[1818]142        {
143            // list is empty
[11071]144            this->itFocus_ = nullptr;
145            this->focus_ = nullptr;
[1818]146        }
[6417]147
148        else if (HumanController::getLocalControllerEntityAsPawn())
[1818]149        {
[6417]150            Vector3 localPosition = HumanController::getLocalControllerEntityAsPawn()->getWorldPosition();
[1818]151            Vector3 targetPosition = localPosition;
[6417]152            if (this->itFocus_ && *(this->itFocus_))
[2662]153                targetPosition = this->itFocus_->getRVWorldPosition();
[1818]154
[6417]155            // find the closest object further away than targetPosition
[1818]156            float currentDistance = localPosition.squaredDistance(targetPosition);
157            float nextDistance = FLT_MAX;
158            float minimumDistance = FLT_MAX;
[11071]159            ObjectList<RadarViewable>::iterator itFallback = nullptr;
[1818]160
[11071]161            for (ObjectList<RadarViewable>::iterator it = listRadarViewable.begin(); it; ++it)
[1818]162            {
[6417]163                if (*it == static_cast<RadarViewable*>(HumanController::getLocalControllerEntityAsPawn()))
[1818]164                    continue;
[2662]165
166                float targetDistance = localPosition.squaredDistance((*it)->getRVWorldPosition());
[1818]167                if (targetDistance > currentDistance && targetDistance < nextDistance)
168                {
169                    this->itFocus_ = it;
170                    nextDistance = targetDistance;
171                }
172                if (targetDistance < minimumDistance)
173                {
174                    itFallback = it;
175                    minimumDistance = targetDistance;
176                }
177            }
178
179            if (nextDistance == FLT_MAX)
180            {
181                // we already had the furthest object
182                this->itFocus_ = itFallback;
183                this->focus_ = *itFallback;
184            }
185            else
186            {
187                this->focus_ = *(this->itFocus_);
188            }
189        }
190    }
191
192    void Radar::releaseFocus()
193    {
[11071]194        this->itFocus_ = nullptr;
195        this->focus_ = nullptr;
[1818]196    }
197
198    void Radar::listObjects() const
199    {
[8858]200        orxout(debug_output) << "List of RadarObjects:" << endl;
[1818]201        // iterate through all Radar Objects
202        unsigned int i = 0;
[11071]203        for (RadarViewable* viewable : ObjectList<RadarViewable>())
[1818]204        {
[11071]205            orxout(debug_output) << i++ << ": " << viewable->getRVWorldPosition() << endl;
206            ++i;
[1818]207        }
208    }
[7163]209
210    void Radar::radarObjectChanged(RadarViewable* rv)
211    {
[11071]212        for (RadarListener* listener : ObjectList<RadarListener>())
[7163]213        {
[11071]214            listener->objectChanged(rv);
[7163]215        }
216    }
[1818]217}
Note: See TracBrowser for help on using the repository browser.