Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1742 was 1625, checked in by rgrieder, 16 years ago

merged hud branch back to trunk

  • Property svn:eol-style set to native
File size: 6.5 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 "OrxonoxStableHeaders.h"
35#include "Radar.h"
36#include <float.h>
37#include "objects/WorldEntity.h"
38#include "objects/SpaceShip.h"
39#include "core/CoreIncludes.h"
40#include "core/ConsoleCommand.h"
41#include "RadarListener.h"
42
43namespace orxonox
44{
45    SetConsoleCommand(Radar, cycleNavigationFocus, true).setAccessLevel(AccessLevel::User);
46    SetConsoleCommand(Radar, releaseNavigationFocus, true).setAccessLevel(AccessLevel::User);
47
48    Radar* Radar::instance_s = 0;
49
50    Radar::Radar()
51        : focus_(0)
52        , objectTypeCounter_(0)
53    {
54        assert(instance_s == 0);
55        instance_s = this;
56
57        // TODO: make this mapping configurable. Maybe there's a possibility with self configured
58        //       configValues..
59        this->objectTypes_["Asteroid"] = RadarViewable::Dot;
60        this->objectTypes_["SpaceShip"] = RadarViewable::Square;
61        this->objectTypes_["AsdfQwerty"] = RadarViewable::Triangle;
62
63        /*WorldEntity* object;
64        object = new WorldEntity();
65        object->setPosition(2000.0, 0.0, 0.0);
66        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
67        object = new WorldEntity();
68        object->setPosition(0.0, 2000.0, 0.0);
69        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
70        object = new WorldEntity();
71        object->setPosition(0.0, 0.0, 2000.0);
72        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
73        object = new WorldEntity();
74        object->setPosition(10000.0,16000.0,0.0);
75        addRadarObject(object);*/
76
77    }
78
79    Radar::~Radar()
80    {
81        instance_s = 0;
82    }
83
84    const RadarViewable* Radar::getFocus()
85    {
86        return *(this->itFocus_);
87    }
88
89    RadarViewable::Shape Radar::addObjectDescription(const std::string name)
90    {
91        std::map<std::string, RadarViewable::Shape>::iterator it = this->objectTypes_.find(name);
92        if (it == this->objectTypes_.end())
93            return this->objectTypes_[name] = RadarViewable::Square; // default, configure!!
94        else
95            return this->objectTypes_[name];
96    }
97
98
99    void Radar::tick(float dt)
100    {
101        if (this->focus_ != *(this->itFocus_))
102        {
103            // focus object was deleted, release focus
104            this->focus_ = 0;
105            this->itFocus_ = 0;
106        }
107
108        for (Iterator<RadarListener> itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
109        {
110            (*itListener)->radarTick(dt);
111
112            for (Iterator<RadarViewable> itElement = ObjectList<RadarViewable>::begin(); itElement; ++itElement)
113            {
114                if ((*itElement) != SpaceShip::getLocalShip() && (*itListener)->getRadarSensitivity() > (*itElement)->getRadarObjectCamouflage())
115                    (*itListener)->displayObject(*itElement, *itElement == this->focus_);
116            }
117        }
118    }
119
120    void Radar::cycleFocus()
121    {
122        if (*(ObjectList<RadarViewable>::begin()) == 0)
123        {
124            // list is empty
125            this->itFocus_ = 0;
126            this->focus_ = 0;
127        }
128        else
129        {
130            Vector3 localPosition = SpaceShip::getLocalShip()->getPosition();
131            Vector3 targetPosition = localPosition;
132            if (*(this->itFocus_))
133                targetPosition = this->itFocus_->getWorldPosition();
134
135            // find the closed object further away than targetPosition
136            float currentDistance = localPosition.squaredDistance(targetPosition);
137            float nextDistance = FLT_MAX;
138            float minimumDistance = FLT_MAX;
139            Iterator<RadarViewable> itFallback = 0;
140
141            for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::begin(); it; ++it)
142            {
143                if (*it == SpaceShip::getLocalShip())
144                    continue;
145
146                float targetDistance = localPosition.squaredDistance((*it)->getWorldPosition());
147                if (targetDistance > currentDistance && targetDistance < nextDistance)
148                {
149                    this->itFocus_ = it;
150                    nextDistance = targetDistance;
151                }
152                if (targetDistance < minimumDistance)
153                {
154                    itFallback = it;
155                    minimumDistance = targetDistance;
156                }
157            }
158
159            if (nextDistance == FLT_MAX)
160            {
161                // we already had the furthest object
162                this->itFocus_ = itFallback;
163                this->focus_ = *itFallback;
164            }
165            else
166            {
167                this->focus_ = *(this->itFocus_);
168            }
169        }
170    }
171
172    void Radar::releaseFocus()
173    {
174        this->itFocus_ = 0;
175        this->focus_ = 0;
176    }
177
178    void Radar::listObjects() const
179    {
180        COUT(3) << "List of RadarObjects:\n";
181        // iterate through all Radar Objects
182        unsigned int i = 0;
183        for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::start(); it; ++it, ++i)
184        {
185            COUT(3) << i++ << ": " << (*it)->getWorldPosition() << std::endl;
186        }
187    }
188
189
190    /*static*/ Radar& Radar::getInstance()
191    {
192        assert(instance_s);
193        return *instance_s;
194    }
195
196    /*static*/ void Radar::cycleNavigationFocus()
197    {
198        // avoid using getInstance because of the assert().
199        // User might call this fuction even if HUDNavigation doesn't exist.
200        if (instance_s)
201            instance_s->cycleFocus();
202    }
203
204    /*static*/ void Radar::releaseNavigationFocus()
205    {
206        // avoid using getInstance because of the assert().
207        // User might call this fuction even if HUDNavigation doesn't exist.
208        if (instance_s)
209            instance_s->releaseFocus();
210    }
211}
Note: See TracBrowser for help on using the repository browser.