Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/overlays/hud/HUDHealthBar.cc @ 2663

Last change on this file since 2663 was 2369, checked in by landauf, 16 years ago
  • Added a health bar
  • Some changes in CameraManager to handle the case when no camera exists after removing the last one, but this is still somehow buggy (freezes and later crashes reproducible but inexplicable after a few respawns)
  • Added PawnManager to handle destruction of Pawns without using delete within tick()
  • Property svn:eol-style set to native
File size: 4.9 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "HUDHealthBar.h"
31
32#include <OgreOverlayManager.h>
33#include <OgrePanelOverlayElement.h>
34
35#include "core/CoreIncludes.h"
36#include "core/XMLPort.h"
37#include "objects/worldentities/pawns/Pawn.h"
38#include "objects/items/Engine.h"
39#include "overlays/OverlayGroup.h"
40#include "util/Convert.h"
41
42namespace orxonox
43{
44    CreateFactory(HUDHealthBar);
45
46    HUDHealthBar::HUDHealthBar(BaseObject* creator) : HUDBar(creator)
47    {
48        RegisterObject(HUDHealthBar);
49
50        this->owner_ = 0;
51        this->bUseBarColour_ = false;
52
53        this->textoverlay_ = new OverlayText(this);
54
55        assert(this->textoverlay_);
56
57        this->textoverlay_->setCaption("");
58    }
59
60    HUDHealthBar::~HUDHealthBar()
61    {
62        if (this->isInitialized())
63            delete this->textoverlay_;
64    }
65
66    void HUDHealthBar::XMLPort(Element& xmlelement, XMLPort::Mode mode)
67    {
68        SUPER(HUDHealthBar, XMLPort, xmlelement, mode);
69
70        XMLPortParam(HUDHealthBar, "showtext",          setTextVisible,          getTextVisible,          xmlelement, mode).defaultValues(true);
71        XMLPortParam(HUDHealthBar, "textfont",          setTextFont,             getTextFont,             xmlelement, mode).defaultValues("Monofur");
72        XMLPortParam(HUDHealthBar, "textusebarcolour",  setTextUseBarColour,     getTextUseBarColour,     xmlelement, mode).defaultValues(false);
73        XMLPortParam(HUDHealthBar, "textcolour",        setTextColour,           getTextColour,           xmlelement, mode).defaultValues(ColourValue(1.0, 1.0, 1.0, 1.0));
74        XMLPortParam(HUDHealthBar, "textsize",          setTextSize,             getTextSize,             xmlelement, mode).defaultValues(1.0f);
75        XMLPortParam(HUDHealthBar, "textalign",         setTextAlignmentString,  getTextAlignmentString,  xmlelement, mode).defaultValues("left");
76        XMLPortParam(HUDHealthBar, "textoffset",        setTextOffset,           getTextOffset,           xmlelement, mode).defaultValues(Vector2::ZERO);
77        XMLPortParam(HUDHealthBar, "textpickpoint",     setTextPickPoint,        getTextPickPoint,        xmlelement, mode).defaultValues(Vector2::ZERO);
78        XMLPortParam(HUDHealthBar, "textrotation",      setTextRotation,         getTextRotation,         xmlelement, mode).defaultValues(0.0f);
79        XMLPortParam(HUDHealthBar, "textcorrectaspect", setTextAspectCorrection, getTextAspectCorrection, xmlelement, mode).defaultValues(true);
80        XMLPortParam(HUDHealthBar, "textspacewidth",    setTextSpaceWidth,       getTextSpaceWidth,       xmlelement, mode).defaultValues(true);
81    }
82
83    void HUDHealthBar::tick(float dt)
84    {
85        SUPER(HUDHealthBar, tick, dt);
86
87        if (this->owner_)
88        {
89            this->setValue(this->owner_->getHealth() / this->owner_->getInitialHealth());
90            this->textoverlay_->setCaption(convertToString((int)this->owner_->getHealth()));
91        }
92
93        if (this->bUseBarColour_)
94            this->textoverlay_->setColour(this->getCurrentBarColour());
95    }
96
97    void HUDHealthBar::changedOwner()
98    {
99        SUPER(HUDHealthBar, changedOwner);
100
101        this->owner_ = dynamic_cast<Pawn*>(this->getOwner());
102    }
103
104    void HUDHealthBar::changedOverlayGroup()
105    {
106        SUPER(HUDHealthBar, changedOverlayGroup);
107
108        this->getOverlayGroup()->addElement(this->textoverlay_);
109    }
110
111    void HUDHealthBar::changedVisibility()
112    {
113        SUPER(HUDHealthBar, changedVisibility);
114
115        this->textoverlay_->setVisible(this->isVisible());
116    }
117
118    void HUDHealthBar::changedName()
119    {
120        SUPER(HUDHealthBar, changedName);
121
122        this->textoverlay_->setName(this->getName() + "text");
123    }
124
125    void HUDHealthBar::setTextColour(const ColourValue& colour)
126    {
127        this->textColour_ = colour;
128        if (!this->bUseBarColour_)
129            this->textoverlay_->setColour(colour);
130    }
131
132    void HUDHealthBar::setTextUseBarColour(bool bUseBarColour)
133    {
134        this->bUseBarColour_ = bUseBarColour;
135        if (!bUseBarColour)
136            this->textoverlay_->setColour(this->textColour_);
137    }
138}
Note: See TracBrowser for help on using the repository browser.