Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/overlays/hud/HUDEnemyHealthBar.cc @ 9215

Last change on this file since 9215 was 9016, checked in by jo, 13 years ago

Merging presentation2011 branch to trunk. Please check for possible bugs.

File size: 7.0 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 *      Matthias Spalinger
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "HUDEnemyHealthBar.h"
30
31#include <OgreCamera.h>
32
33#include "util/Convert.h"
34#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"
49
50namespace orxonox
51{
52    CreateFactory(HUDEnemyHealthBar);
53
54    HUDEnemyHealthBar::HUDEnemyHealthBar(BaseObject* creator) : HUDHealthBar(creator)
55    {
56        RegisterObject(HUDEnemyHealthBar);
57
58        this->setConfigValues();
59        setSensibility(0.1f);
60        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("");
66    }
67
68    HUDEnemyHealthBar::~HUDEnemyHealthBar()
69    {
70        sortedObjectList_.clear();
71    }
72
73    void HUDEnemyHealthBar::setConfigValues()
74    {
75        SetConfigValue(useEnemyBar_, true);
76    }
77
78    void HUDEnemyHealthBar::XMLPort(Element& xmlelement, XMLPort::Mode mode)
79    {
80        SUPER(HUDEnemyHealthBar, XMLPort, xmlelement, mode);
81
82        XMLPortParam ( HUDEnemyHealthBar, "sensibility", setSensibility, getSensibility, xmlelement, mode );
83    }
84
85    bool compareDist ( std::pair<RadarViewable*, unsigned int > a, std::pair<RadarViewable*, unsigned int > b )
86    {
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;
96        }
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);
197    }
198
199    void HUDEnemyHealthBar::changedOwner()
200    {
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        }
208    }
209
210}
Note: See TracBrowser for help on using the repository browser.