Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SpaceRaceAI_HS16/src/orxonox/Radar.cc @ 11436

Last change on this file since 11436 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
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 *      Reto Grieder
24 *   Co-authors:
25 *      Felix Schulthess
26 *
27 */
28
29/**
30@file
31@brief
32*/
33
34#include "Radar.h"
35
36#include <cassert>
37
38//#include "util/Math.h"
39#include "core/CoreIncludes.h"
40#include "core/object/ObjectList.h"
41#include "core/command/ConsoleCommand.h"
42#include "interfaces/RadarListener.h"
43#include "controllers/HumanController.h"
44#include "worldentities/pawns/Pawn.h"
45
46namespace orxonox
47{
48    RegisterAbstractClass(Radar).inheritsFrom<Tickable>();
49
50    Radar::Radar()
51        : itFocus_(nullptr)
52        , focus_(nullptr)
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::Shape::Dot;
58        this->objectTypes_["SpaceShip"] = RadarViewable::Shape::Square;
59        this->objectTypes_["AsdfQwerty"] = RadarViewable::Shape::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
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 (RadarListener* listener : ObjectList<RadarListener>())
87        {
88            listener->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 (RadarListener* listener : ObjectList<RadarListener>())
98        {
99            listener->removeObject(rv);
100        }
101    }
102
103    const RadarViewable* Radar::getFocus()
104    {
105        if (this->itFocus_)
106            return *(this->itFocus_);
107        else
108            return nullptr;
109    }
110
111    RadarViewable::Shape Radar::addObjectDescription(const std::string& name)
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::Shape::Square; // default, configure!!
116        else
117            return this->objectTypes_[name];
118    }
119
120
121    void Radar::tick(float dt)
122    {
123        SUPER(Radar, tick, dt);
124
125        if (this->itFocus_ && (this->focus_ != *(this->itFocus_)))
126        {
127            // focus object was deleted, release focus
128            this->focus_ = nullptr;
129            this->itFocus_ = nullptr;
130        }
131
132        for (RadarListener* listener : ObjectList<RadarListener>())
133        {
134            listener->radarTick(dt);
135        }
136    }
137
138    void Radar::cycleFocus()
139    {
140        ObjectList<RadarViewable> listRadarViewable;
141        if (listRadarViewable.size() == 0)
142        {
143            // list is empty
144            this->itFocus_ = nullptr;
145            this->focus_ = nullptr;
146        }
147
148        else if (HumanController::getLocalControllerEntityAsPawn())
149        {
150            Vector3 localPosition = HumanController::getLocalControllerEntityAsPawn()->getWorldPosition();
151            Vector3 targetPosition = localPosition;
152            if (this->itFocus_ && *(this->itFocus_))
153                targetPosition = this->itFocus_->getRVWorldPosition();
154
155            // find the closest object further away than targetPosition
156            float currentDistance = localPosition.squaredDistance(targetPosition);
157            float nextDistance = FLT_MAX;
158            float minimumDistance = FLT_MAX;
159            ObjectList<RadarViewable>::iterator itFallback = nullptr;
160
161            for (ObjectList<RadarViewable>::iterator it = listRadarViewable.begin(); it; ++it)
162            {
163                if (*it == static_cast<RadarViewable*>(HumanController::getLocalControllerEntityAsPawn()))
164                    continue;
165
166                float targetDistance = localPosition.squaredDistance((*it)->getRVWorldPosition());
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    {
194        this->itFocus_ = nullptr;
195        this->focus_ = nullptr;
196    }
197
198    void Radar::listObjects() const
199    {
200        orxout(debug_output) << "List of RadarObjects:" << endl;
201        // iterate through all Radar Objects
202        unsigned int i = 0;
203        for (RadarViewable* viewable : ObjectList<RadarViewable>())
204        {
205            orxout(debug_output) << i++ << ": " << viewable->getRVWorldPosition() << endl;
206            ++i;
207        }
208    }
209
210    void Radar::radarObjectChanged(RadarViewable* rv)
211    {
212        for (RadarListener* listener : ObjectList<RadarListener>())
213        {
214            listener->objectChanged(rv);
215        }
216    }
217}
Note: See TracBrowser for help on using the repository browser.