Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/Radar.cc @ 7208

Last change on this file since 7208 was 6394, checked in by rgrieder, 15 years ago

std::string tweaks:

  • Declared BLANKSTRING in UtilPrereqs.h as well (removed obsolete StringUtils.h includes to avoid dependencies)
  • Using BLANKSTRING if const std::string& return type is possible
  • Replaced a few (const) std::string arguments with const std::string&
  • if (str == "") —> if (str.empty())
  • std::string msg = name + "adsf"; —> const std::string& msg = name + "asdf";
  • std::string asdf = object→getFooBar(); —> const std::string& asdf = object→getFooBar();
  • std::string asdf = "asdf"; —> std::string asdf("asdf");
  • ostream << "."; and name + "." —> ostream << '.'; and name + '.'
  • str = ""; —> str.clear()
  • std::string asdf = ""; —> std::string asdf;
  • asdf_ = ""; (in c'tor) —> delete line
  • Property svn:eol-style set to native
File size: 6.0 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/ConsoleCommand.h"
40#include "core/ObjectList.h"
41#include "interfaces/RadarListener.h"
42#include "controllers/HumanController.h"
43#include "worldentities/pawns/Pawn.h"
44
45namespace orxonox
46{
47
48    Radar::Radar()
49        : itFocus_(0)
50        , focus_(0)
51        , objectTypeCounter_(0)
52    {
53        // TODO: make this mapping configurable. Maybe there's a possibility with self configured
54        //       configValues..
55        this->objectTypes_["Asteroid"] = RadarViewable::Dot;
56        this->objectTypes_["SpaceShip"] = RadarViewable::Square;
57        this->objectTypes_["AsdfQwerty"] = RadarViewable::Triangle;
58
59        /*WorldEntity* object;
60        object = new WorldEntity();
61        object->setPosition(2000.0, 0.0, 0.0);
62        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
63        object = new WorldEntity();
64        object->setPosition(0.0, 2000.0, 0.0);
65        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
66        object = new WorldEntity();
67        object->setPosition(0.0, 0.0, 2000.0);
68        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
69        object = new WorldEntity();
70        object->setPosition(10000.0,16000.0,0.0);
71        addRadarObject(object);*/
72
73    }
74
75    Radar::~Radar()
76    {
77    }
78
79    const RadarViewable* Radar::getFocus()
80    {
81        if (this->itFocus_)
82            return *(this->itFocus_);
83        else
84            return 0;
85    }
86
87    RadarViewable::Shape Radar::addObjectDescription(const std::string& name)
88    {
89        std::map<std::string, RadarViewable::Shape>::iterator it = this->objectTypes_.find(name);
90        if (it == this->objectTypes_.end())
91            return this->objectTypes_[name] = RadarViewable::Square; // default, configure!!
92        else
93            return this->objectTypes_[name];
94    }
95
96
97    void Radar::tick(float dt)
98    {
99        SUPER(Radar, tick, dt);
100
101        if (this->itFocus_ && (this->focus_ != *(this->itFocus_)))
102        {
103            // focus object was deleted, release focus
104            this->focus_ = 0;
105            this->itFocus_ = 0;
106        }
107
108        for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
109        {
110            (*itListener)->radarTick(dt);
111
112            for (ObjectList<RadarViewable>::iterator itElement = ObjectList<RadarViewable>::begin(); itElement; ++itElement)
113            {
114                if ((*itElement)->getRadarVisibility())
115                    if ((*itListener)->getRadarSensitivity() > (*itElement)->getRadarObjectCamouflage())
116                        (*itListener)->displayObject(*itElement, *itElement == this->focus_);
117            }
118        }
119    }
120
121    void Radar::cycleFocus()
122    {
123        if (ObjectList<RadarViewable>::begin() == ObjectList<RadarViewable>::end())
124        {
125            // list is empty
126            this->itFocus_ = 0;
127            this->focus_ = 0;
128        }
129
130        else if (HumanController::getLocalControllerEntityAsPawn())
131        {
132            Vector3 localPosition = HumanController::getLocalControllerEntityAsPawn()->getWorldPosition();
133            Vector3 targetPosition = localPosition;
134            if (this->itFocus_ && *(this->itFocus_))
135                targetPosition = this->itFocus_->getRVWorldPosition();
136
137            // find the closest object further away than targetPosition
138            float currentDistance = localPosition.squaredDistance(targetPosition);
139            float nextDistance = FLT_MAX;
140            float minimumDistance = FLT_MAX;
141            ObjectList<RadarViewable>::iterator itFallback = 0;
142
143            for (ObjectList<RadarViewable>::iterator it = ObjectList<RadarViewable>::begin(); it; ++it)
144            {
145                if (*it == static_cast<RadarViewable*>(HumanController::getLocalControllerEntityAsPawn()))
146                    continue;
147
148                float targetDistance = localPosition.squaredDistance((*it)->getRVWorldPosition());
149                if (targetDistance > currentDistance && targetDistance < nextDistance)
150                {
151                    this->itFocus_ = it;
152                    nextDistance = targetDistance;
153                }
154                if (targetDistance < minimumDistance)
155                {
156                    itFallback = it;
157                    minimumDistance = targetDistance;
158                }
159            }
160
161            if (nextDistance == FLT_MAX)
162            {
163                // we already had the furthest object
164                this->itFocus_ = itFallback;
165                this->focus_ = *itFallback;
166            }
167            else
168            {
169                this->focus_ = *(this->itFocus_);
170            }
171        }
172    }
173
174    void Radar::releaseFocus()
175    {
176        this->itFocus_ = 0;
177        this->focus_ = 0;
178    }
179
180    void Radar::listObjects() const
181    {
182        COUT(3) << "List of RadarObjects:\n";
183        // iterate through all Radar Objects
184        unsigned int i = 0;
185        for (ObjectList<RadarViewable>::iterator it = ObjectList<RadarViewable>::begin(); it; ++it, ++i)
186        {
187            COUT(3) << i++ << ": " << (*it)->getRVWorldPosition() << std::endl;
188        }
189    }
190}
Note: See TracBrowser for help on using the repository browser.