Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6886 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

  • Actual GUI with settings, etc.
  • Improved space ship steering (human interaction)
  • Rocket fire and more particle effects
  • Advanced sound framework
  • Property svn:eol-style set to native
File size: 6.0 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"
[1818]39#include "core/ConsoleCommand.h"
[3196]40#include "core/ObjectList.h"
41#include "interfaces/RadarListener.h"
[6417]42#include "controllers/HumanController.h"
43#include "worldentities/pawns/Pawn.h"
[1818]44
45namespace orxonox
46{
47
48    Radar::Radar()
[6417]49        : itFocus_(0)
50        , focus_(0)
[1818]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    {
[5929]81        if (this->itFocus_)
82            return *(this->itFocus_);
83        else
84            return 0;
[1818]85    }
86
[6417]87    RadarViewable::Shape Radar::addObjectDescription(const std::string& name)
[1818]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    {
[2662]99        SUPER(Radar, tick, dt);
100
[5929]101        if (this->itFocus_ && (this->focus_ != *(this->itFocus_)))
[1818]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            {
[3064]114                if ((*itElement)->getRadarVisibility())
115                    if ((*itListener)->getRadarSensitivity() > (*itElement)->getRadarObjectCamouflage())
116                        (*itListener)->displayObject(*itElement, *itElement == this->focus_);
[1818]117            }
118        }
119    }
120
121    void Radar::cycleFocus()
122    {
[6417]123        if (ObjectList<RadarViewable>::begin() == ObjectList<RadarViewable>::end())
[1818]124        {
125            // list is empty
126            this->itFocus_ = 0;
127            this->focus_ = 0;
128        }
[6417]129
130        else if (HumanController::getLocalControllerEntityAsPawn())
[1818]131        {
[6417]132            Vector3 localPosition = HumanController::getLocalControllerEntityAsPawn()->getWorldPosition();
[1818]133            Vector3 targetPosition = localPosition;
[6417]134            if (this->itFocus_ && *(this->itFocus_))
[2662]135                targetPosition = this->itFocus_->getRVWorldPosition();
[1818]136
[6417]137            // find the closest object further away than targetPosition
[1818]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            {
[6417]145                if (*it == static_cast<RadarViewable*>(HumanController::getLocalControllerEntityAsPawn()))
[1818]146                    continue;
[2662]147
148                float targetDistance = localPosition.squaredDistance((*it)->getRVWorldPosition());
[1818]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        {
[2662]187            COUT(3) << i++ << ": " << (*it)->getRVWorldPosition() << std::endl;
[1818]188        }
189    }
190}
Note: See TracBrowser for help on using the repository browser.