Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.