Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9259


Ignore:
Timestamp:
May 28, 2012, 4:47:47 PM (12 years ago)
Author:
landauf
Message:

refactored HUDEnemyHealthBar: now it uses ControllableEntity::getTarget() instead of searching the target itself

Location:
code/trunk/src/modules
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/overlays/hud/HUDEnemyHealthBar.cc

    r9016 r9259  
    2323 *      Matthias Spalinger
    2424 *   Co-authors:
    25  *      ...
     25 *      Fabian 'x3n' Landau
    2626 *
    2727 */
     
    2929#include "HUDEnemyHealthBar.h"
    3030
    31 #include <OgreCamera.h>
    32 
    33 #include "util/Convert.h"
    3431#include "core/ConfigValueIncludes.h"
    35 #include "core/CoreIncludes.h"
    36 #include "core/XMLPort.h"
    37 #include "infos/PlayerInfo.h"
    38 #include "overlays/OverlayGroup.h"
    39 #include "CameraManager.h"
    40 #include "graphics/Camera.h"
    41 #include "util/Math.h"
    42 #include "HUDNavigation.h"
    43 #include "core/input/InputManager.h"
    44 #include "controllers/HumanController.h"
    45 #include "core/GraphicsManager.h"
    46 #include "Scene.h"
    47 #include "Radar.h"
    48 #include "controllers/NewHumanController.h"
     32#include "worldentities/pawns/Pawn.h"
    4933
    5034namespace orxonox
     
    5741
    5842        this->setConfigValues();
    59         setSensibility(0.1f);
    6043        this->owner_ = 0;
    61         markerLimit_ = 3; //TODO connect with markerLimit_ from the settings / from HUDNavigation.cc
    62         currentYaw = 0;
    63         currentPitch = 0;
    64 
    65         this->getOverlayText()->setCaption("");
    6644    }
    6745
    6846    HUDEnemyHealthBar::~HUDEnemyHealthBar()
    6947    {
    70         sortedObjectList_.clear();
    7148    }
    7249
     
    7653    }
    7754
    78     void HUDEnemyHealthBar::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     55    void HUDEnemyHealthBar::tick(float dt)
    7956    {
    80         SUPER(HUDEnemyHealthBar, XMLPort, xmlelement, mode);
     57        this->updateTarget();
    8158
    82         XMLPortParam ( HUDEnemyHealthBar, "sensibility", setSensibility, getSensibility, xmlelement, mode );
     59        SUPER(HUDEnemyHealthBar, tick, dt);
    8360    }
    8461
    85     bool compareDist ( std::pair<RadarViewable*, unsigned int > a, std::pair<RadarViewable*, unsigned int > b )
     62    void HUDEnemyHealthBar::updateTarget()
    8663    {
    87         return a.second<b.second;
    88     }
    89 
    90     void HUDEnemyHealthBar::tick(float dt)
    91     {
    92         if (!useEnemyBar_){
    93             this->setValue(0); //TODO hide it instead of setting it to 0
    94             this->getOverlayText()->setCaption("");
    95             return;
     64        Pawn* pawn = NULL;
     65        if (this->owner_ && this->useEnemyBar_)
     66        {
     67            // Get the owner's current target (target is usually a Model)
     68            WorldEntity* target = this->owner_->getTarget();
     69            // Find the Pawn that belongs to this target (if any)
     70            while (target && !target->isA(Class(Pawn)))
     71                target = target->getParent();
     72            pawn = orxonox_cast<Pawn*>(target);
     73            // Don't show the HealthBar if the pawn is invisible
     74            if (pawn && !pawn->isVisible())
     75                pawn = NULL;
    9676        }
    97 
    98         Camera* cam = CameraManager::getInstance().getActiveCamera();
    99         if ( cam == NULL )
    100             return;
    101         const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix();
    102 
    103         unsigned int markerCount_ = 0;
    104 
    105         for ( sortedList::iterator listIt = sortedObjectList_.begin(); listIt != sortedObjectList_.end(); ++listIt )
    106         {
    107             listIt->second = ( int ) ( ( listIt->first->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition() ).length() + 0.5f );
    108         }
    109    
    110         sortedObjectList_.sort ( compareDist );
    111 
    112         for ( sortedList::iterator listIt = sortedObjectList_.begin(); listIt != sortedObjectList_.end(); ++markerCount_, ++listIt )
    113         {
    114             if ( markerCount_ < markerLimit_ )
    115             {       
    116                 // Transform to screen coordinates and reverse x-axis
    117                 Vector3 pos = (camTransform * listIt->first->getRVWorldPosition());
    118                 pos.x = -pos.x;               
    119 
    120                 // get mouse position
    121                 if(this->getOwner() && dynamic_cast<ControllableEntity*>(this->getOwner())->getController() && dynamic_cast<NewHumanController*>(dynamic_cast<ControllableEntity*>(this->getOwner())->getController()))
    122                 {
    123                     currentYaw = dynamic_cast<NewHumanController*>(dynamic_cast<ControllableEntity*>(this->getOwner())->getController())->getCurrentYaw();
    124                     currentPitch = dynamic_cast<NewHumanController*>(dynamic_cast<ControllableEntity*>(this->getOwner())->getController())->getCurrentPitch();
    125                 }
    126                 // Compare cursor position to object position
    127                 if ( fabs(pos.x - currentYaw) < sens_ && fabs(pos.y - currentPitch) < sens_ )
    128                 {
    129                     this->owner_ = orxonox_cast<Pawn*>(listIt->first);
    130                     break;
    131                 }
    132             }
    133             this->owner_ = 0;
    134         }
    135 
    136 
    137 
    138         if (this->owner_)
    139         {
    140             this->setValue(this->owner_->getHealth() / this->owner_->getInitialHealth());
    141             this->getOverlayText()->setCaption(multi_cast<std::string>(static_cast<int>(this->owner_->getHealth())));
    142         }
    143         else
    144         {
    145             this->setValue(0);      //TODO hide it instead of setting it to zero
    146             this->getOverlayText()->setCaption("");
    147         }
    148 
    149         if (this->getTextUseBarColour())
    150             this->getOverlayText()->setColour(this->getCurrentBarColour());
    151     }
    152 
    153     void HUDEnemyHealthBar::addObject ( RadarViewable* object )
    154     {
    155         if( showObject(object)==false )
    156             return;
    157    
    158         if ( sortedObjectList_.size() >= markerLimit_ )
    159             if ( object == NULL )
    160                 return;
    161    
    162         sortedObjectList_.push_front ( std::make_pair ( object, ( unsigned int ) 0 ) );
    163 
    164         //remove duplicates
    165         sortedObjectList_.unique();
    166     }
    167 
    168     bool HUDEnemyHealthBar::showObject(RadarViewable* rv)
    169     {
    170         if ( rv == dynamic_cast<RadarViewable*> ( this->getOwner() ) )
    171             return false;
    172         assert( rv->getWorldEntity() );
    173         if ( rv->getWorldEntity()->isVisible()==false || rv->getRadarVisibility()==false )
    174             return false;
    175         return true;
    176     }
    177 
    178     void HUDEnemyHealthBar::removeObject ( RadarViewable* viewable )
    179     {
    180         for ( sortedList::iterator listIt = sortedObjectList_.begin(); listIt != sortedObjectList_.end(); ++listIt )
    181         {
    182             if ( (listIt->first) == viewable )
    183             {
    184                 sortedObjectList_.erase ( listIt );
    185                 break;
    186             }
    187    
    188         }
    189 
    190     }
    191 
    192     void HUDEnemyHealthBar::objectChanged(RadarViewable* viewable)
    193     {
    194         // TODO: niceification neccessary - and while you're at it: the same function exists in HUDNavigation.cc ;)
    195         removeObject(viewable);
    196         addObject(viewable);
     77        // Set the pawn as owner of the HealthBar
     78        this->setHealthBarOwner(pawn);
     79        this->setVisible(pawn != NULL);
    19780    }
    19881
    19982    void HUDEnemyHealthBar::changedOwner()
    20083    {
    201    
    202         const std::set<RadarViewable*>& respawnObjects = this->getOwner()->getScene()->getRadar()->getRadarObjects();
    203         for ( std::set<RadarViewable*>::const_iterator it = respawnObjects.begin(); it != respawnObjects.end(); ++it )
    204         {
    205             if ( ! ( *it )->isHumanShip_ )
    206                 this->addObject ( *it );
    207         }
     84        SUPER(HUDEnemyHealthBar, changedOwner);
     85
     86        this->owner_ = orxonox_cast<ControllableEntity*>(this->getOwner());
     87        this->updateTarget();
    20888    }
    209 
    21089}
  • code/trunk/src/modules/overlays/hud/HUDEnemyHealthBar.h

    r9016 r9259  
    2323 *      Matthias Spalinger
    2424 *   Co-authors:
    25  *      ...
     25 *      Fabian 'x3n' Landau
    2626 *
    2727 */
     
    3030#define _HUDEnemyHealthBar_H__
    3131
    32 #include "interfaces/RadarViewable.h"
    33 #include "worldentities/pawns/Pawn.h"
    34 
    3532#include "HUDHealthBar.h"
    36 #include "interfaces/RadarListener.h"
    3733
    3834namespace orxonox
    3935{
    40     class _OverlaysExport HUDEnemyHealthBar : public HUDHealthBar, public RadarListener
     36    class _OverlaysExport HUDEnemyHealthBar : public HUDHealthBar
    4137    {
    4238        public:
    4339            HUDEnemyHealthBar(BaseObject* creator);
    4440            virtual ~HUDEnemyHealthBar();
     41
    4542            void setConfigValues();
    46 
    47             bool compareDistance ( std::pair<RadarViewable*, unsigned int > a, std::pair<RadarViewable*, unsigned int > b );
    48 
    49             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    5043            virtual void tick(float dt);
    51 
    52             //RadarListener interface
    53             void addObject ( RadarViewable* object );
    54             void removeObject ( RadarViewable* viewable );
    55             void objectChanged(RadarViewable* viewable);
    56             virtual void radarTick ( float dt ) {}
    57             inline float getRadarSensitivity() const
    58                 { return 1.0f; }
    5944
    6045            void changedOwner();
    6146
    6247        private:
     48            void updateTarget();
    6349
    64             void setSensibility (float sense){
    65                 this->sens_ = sense;}
    66             float getSensibility(){
    67                 return this->sens_;}
    68 
    69             bool showObject(RadarViewable* rv);
    70        
    71             typedef std::list < std::pair<RadarViewable*, unsigned int > > sortedList;
    72             sortedList sortedObjectList_;
    73 
    74             Pawn* owner_;
    75             float sens_;
     50            ControllableEntity* owner_;
    7651            bool useEnemyBar_;
    77             unsigned int markerLimit_;
    78 
    79             float currentYaw;
    80             float currentPitch;
    8152    };
    8253}
  • code/trunk/src/modules/overlays/hud/HUDHealthBar.h

    r9016 r9259  
    110110                { return this->textoverlay_->getSpaceWidth(); }
    111111
    112             inline void setOverlayText(SmartPtr<OverlayText> textoverlay)
    113                 { this->textoverlay_ = textoverlay; }
    114             inline SmartPtr<OverlayText> getOverlayText() const
    115                 {return this->textoverlay_; }
     112            inline void setHealthBarOwner(Pawn* owner)
     113                { this->owner_ = owner; }
    116114
    117115        private:
  • code/trunk/src/modules/pong/PongScore.cc

    r9258 r9259  
    130130            {
    131131                if (this->bShowName_ && this->bShowScore_ && player1_ != NULL)
    132                         output1 = name1 + " - " + score1;
     132                    output1 = name1 + " - " + score1;
    133133                else if (this->bShowScore_)
    134                         output1 = score1;
     134                    output1 = score1;
    135135                else if (this->bShowName_)
    136                         output1 = name1;
     136                    output1 = name1;
    137137            }
    138138
Note: See TracChangeset for help on using the changeset viewer.