Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 20, 2008, 12:05:12 AM (16 years ago)
Author:
rgrieder
Message:
  • Radar now working like before
  • but more of a svn save..

There is class called Radar which takes care of the focus and all objects that can be displayed on a Radar.
The actual visual implementation is in HUDRadar.
To make a object radar viewable, simply implement RadarViewable and set the WorldEntitiy pointer in the constructor (assertation will fail otherwise!).
You can also set a camouflage value between 0 and 1 that tells how good a radar can see an object.
The HUDRadar (or another one) on the other side has a sensitivity between 0 and 1. So only if the sensitivity is higher than the camouflage value, the object gets displayed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/hud/src/orxonox/Radar.cc

    r1609 r1613  
    3434#include "OrxonoxStableHeaders.h"
    3535#include "Radar.h"
     36#include <float.h>
    3637#include "objects/WorldEntity.h"
    37 #include "overlays/hud/RadarViewable.h"
    38 
     38#include "objects/SpaceShip.h"
    3939#include "core/CoreIncludes.h"
    4040//#include "core/ConfigValueIncludes.h"
     
    4747    }
    4848
     49    SetConsoleCommand(Radar, cycleNavigationFocus, true).setAccessLevel(AccessLevel::User);
     50    SetConsoleCommand(Radar, releaseNavigationFocus, true).setAccessLevel(AccessLevel::User);
    4951
    5052    Radar* Radar::instance_s = 0;
    5153
    5254    Radar::Radar()
     55        : focus_(0)
     56        , objectTypeCounter_(0)
    5357    {
    5458        assert(instance_s == 0);
    5559        instance_s = this;
     60
     61        // TODO: make this mapping configurable. Maybe there's a possibility with self configured
     62        //       configValues..
     63        this->objectTypes_["Asteroid"] = RadarViewable::Dot;
     64        this->objectTypes_["SpaceShip"] = RadarViewable::Square;
     65        this->objectTypes_["AsdfQwerty"] = RadarViewable::Triangle;
     66
     67        /*WorldEntity* object;
     68        object = new WorldEntity();
     69        object->setPosition(2000.0, 0.0, 0.0);
     70        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
     71        object = new WorldEntity();
     72        object->setPosition(0.0, 2000.0, 0.0);
     73        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
     74        object = new WorldEntity();
     75        object->setPosition(0.0, 0.0, 2000.0);
     76        addRadarObject(object, ColourValue(0.5, 0, 0, 1));
     77        object = new WorldEntity();
     78        object->setPosition(10000.0,16000.0,0.0);
     79        addRadarObject(object);*/
     80
    5681    }
    5782
     
    6186    }
    6287
    63     Radar& Radar::getInstance()
     88    /*void Radar::unregisterObject(RadarViewable* object)
     89    {
     90        if (this->focus_ == object)
     91            this->focus_ = 0;
     92        // TODO: check for focus
     93    }*/
     94
     95    const RadarViewable* Radar::getFocus()
     96    {
     97        return *(this->itFocus_);
     98    }
     99
     100    RadarViewable::Shape Radar::addObjectDescription(const std::string name)
     101    {
     102        std::map<std::string, RadarViewable::Shape>::iterator it = this->objectTypes_.find(name);
     103        if (it == this->objectTypes_.end())
     104            return this->objectTypes_[name] = RadarViewable::Square; // default, configure!!
     105        else
     106            return this->objectTypes_[name];
     107    }
     108
     109
     110    void Radar::tick(float dt)
     111    {
     112        if (this->focus_ != *(this->itFocus_))
     113        {
     114            // focus object was deleted, release focus
     115            this->focus_ = 0;
     116            this->itFocus_ = 0;
     117        }
     118
     119        for (Iterator<RadarListener> itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
     120        {
     121            for (Iterator<RadarViewable> itElement = ObjectList<RadarViewable>::begin(); itElement; ++itElement)
     122            {
     123                if ((*itElement) != SpaceShip::getLocalShip() && (*itListener)->getRadarSensitivity() > (*itElement)->getRadarObjectCamouflage())
     124                    (*itListener)->displayObject(*itElement, *itElement == this->focus_);
     125            }
     126
     127            (*itListener)->radarTick(dt);
     128
     129            if (this->focus_ == 0)
     130                (*itListener)->hideMarker();
     131        }
     132    }
     133
     134    void Radar::cycleFocus()
     135    {
     136        if (*(ObjectList<RadarViewable>::begin()) == 0)
     137        {
     138            // list is empty
     139            this->itFocus_ = 0;
     140            this->focus_ = 0;
     141        }
     142        else
     143        {
     144            Vector3 localPosition = SpaceShip::getLocalShip()->getPosition();
     145            Vector3 targetPosition = localPosition;
     146            if (*(this->itFocus_))
     147                targetPosition = this->itFocus_->getWorldPosition();
     148
     149            // find the closed object further away than targetPosition
     150            float currentDistance = localPosition.squaredDistance(targetPosition);
     151            float nextDistance = FLT_MAX;
     152            float minimumDistance = FLT_MAX;
     153            Iterator<RadarViewable> itFallback = 0;
     154
     155            for (Iterator<RadarViewable> it = ObjectList<RadarViewable>::begin(); it; ++it)
     156            {
     157                if (*it == SpaceShip::getLocalShip())
     158                    continue;
     159
     160                float targetDistance = localPosition.squaredDistance((*it)->getWorldPosition());
     161                if (targetDistance > currentDistance && targetDistance < nextDistance)
     162                {
     163                    this->itFocus_ = it;
     164                    nextDistance = targetDistance;
     165                }
     166                if (targetDistance < minimumDistance)
     167                {
     168                    itFallback = it;
     169                    minimumDistance = targetDistance;
     170                }
     171            }
     172
     173            if (nextDistance == FLT_MAX)
     174            {
     175                // we already had the furthest object
     176                this->itFocus_ = itFallback;
     177                this->focus_ = *itFallback;
     178            }
     179            else
     180            {
     181                this->focus_ = *(this->itFocus_);
     182            }
     183        }
     184    }
     185
     186    void Radar::releaseFocus()
     187    {
     188        this->itFocus_ = 0;
     189        this->focus_ = 0;
     190    }
     191
     192
     193    /*static*/ Radar& Radar::getInstance()
    64194    {
    65195        assert(instance_s);
     
    67197    }
    68198
    69     void Radar::unregisterObject(RadarViewable* object)
    70     {
    71         // TODO: check for focus
    72     }
    73 
    74     void Radar::tick(float dt)
    75     {
    76         for (Iterator<RadarListener> itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
    77         {
    78             for (Iterator<RadarViewable> itElement = ObjectList<RadarViewable>::begin(); itElement; ++itElement)
    79             {
    80                 if ((*itListener)->getRadarSensitivity() > (*itElement)->getRadarObjectVisibility())
    81                     (*itListener)->displayObject(*itElement);
    82             }
    83         }
     199    /*static*/ void Radar::cycleNavigationFocus()
     200    {
     201        // avoid using getInstance because of the assert().
     202        // User might call this fuction even if HUDNavigation doesn't exist.
     203        if (instance_s)
     204            instance_s->cycleFocus();
     205    }
     206
     207    /*static*/ void Radar::releaseNavigationFocus()
     208    {
     209        // avoid using getInstance because of the assert().
     210        // User might call this fuction even if HUDNavigation doesn't exist.
     211        if (instance_s)
     212            instance_s->releaseFocus();
    84213    }
    85214}
Note: See TracChangeset for help on using the changeset viewer.