summaryrefslogtreecommitdiff
path: root/game/client/hl1/hl1_hud_health.cpp
blob: 601081d840713242a2c3c3cc555b7161755d58b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
//
// Health.cpp
//
// implementation of CHudHealth class
//
#include "cbase.h"
#include "hud.h"
#include "hud_macros.h"
#include "view.h"

#include "iclientmode.h"

#include "hl1_hud_numbers.h"

#include <KeyValues.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui_controls/Panel.h>

using namespace vgui;

#include "hudelement.h"

#include "convar.h"

#define INIT_HEALTH -1

#define FADE_TIME	100
#define MIN_ALPHA	100	

//-----------------------------------------------------------------------------
// Purpose: Health panel
//-----------------------------------------------------------------------------
class CHudHealth : public CHudElement, public CHL1HudNumbers
{
	DECLARE_CLASS_SIMPLE( CHudHealth, CHL1HudNumbers );

public:
	CHudHealth( const char *pElementName );

	void			Init( void );
	void			VidInit( void );
	void			Reset( void );
	void			OnThink();
	void			MsgFunc_Damage(bf_read &msg);

private:
	void	Paint( void );
	void	ApplySchemeSettings(vgui::IScheme *pScheme);

private:
	CHudTexture		*icon_cross;
	int				m_iHealth;
	float			m_flFade;
	int				m_bitsDamage;
};	

DECLARE_HUDELEMENT( CHudHealth );
DECLARE_HUD_MESSAGE( CHudHealth, Damage );

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudHealth::CHudHealth( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudHealth")
{
	SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudHealth::Init()
{
	HOOK_HUD_MESSAGE( CHudHealth, Damage );
	Reset();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudHealth::Reset()
{
	m_iHealth		= INIT_HEALTH;
	m_flFade			= 0;
	m_bitsDamage	= 0;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudHealth::VidInit()
{
	Reset();
	BaseClass::VidInit();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudHealth::OnThink()
{
	int x = 0;
	C_BasePlayer *local = C_BasePlayer::GetLocalPlayer();
	if ( local )
	{
		// Never below zero
		x = MAX( local->GetHealth(), 0 );
	}

	// Only update the fade if we've changed health
	if ( x == m_iHealth )
	{
		return;
	}

	m_flFade = FADE_TIME;
	m_iHealth = x;
}

void CHudHealth::Paint()
{
	Color	clrHealth;
	int		a;
	int		x;
	int		y;

	BaseClass::Paint();

	if ( !icon_cross )
	{
		icon_cross = gHUD.GetIcon( "cross" );
	}

	if ( !icon_cross )
	{
		return;
	}

	// Has health changed? Flash the health #
	if ( m_flFade )
	{
		m_flFade -= ( gpGlobals->frametime * 20 );
		if ( m_flFade <= 0 )
		{
			a = MIN_ALPHA;
			m_flFade = 0;
		}
		else
		{
			// Fade the health number back to dim
			a = MIN_ALPHA +  ( m_flFade / FADE_TIME ) * 128;
		}
	}
	else
	{
		a = MIN_ALPHA;
	}

	// If health is getting low, make it bright red
	if ( m_iHealth <= 15 )
		a = 255;
		
	if (m_iHealth > 25)
	{
		int r, g, b, nUnused;

		(gHUD.m_clrYellowish).GetColor( r, g, b, nUnused );
		clrHealth.SetColor( r, g, b, a );
	}
	else
	{
		clrHealth.SetColor( 250, 0, 0, a );
	}

	int nFontWidth	= GetNumberFontWidth();
	int nFontHeight	= GetNumberFontHeight();
	int nCrossWidth	= icon_cross->Width();

	x = nCrossWidth / 2;
	y = GetTall() - ( nFontHeight * 1.5 );

	icon_cross->DrawSelf( x, y, clrHealth );

	x = nCrossWidth + ( nFontWidth / 2 );

	x = DrawHudNumber( x, y, m_iHealth, clrHealth );

	x += nFontWidth / 2;

	int iHeight	= nFontHeight;
	int iWidth	= nFontWidth / 10;

	clrHealth.SetColor( 255, 160, 0, a  );
	vgui::surface()->DrawSetColor( clrHealth );
	vgui::surface()->DrawFilledRect( x, y, x + iWidth, y + iHeight );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudHealth::MsgFunc_Damage(bf_read &msg)
{
	msg.ReadByte();	// armor
	msg.ReadByte();	// health
	msg.ReadLong();	// damage bits

	Vector vecFrom;

	vecFrom.x = msg.ReadBitCoord();
	vecFrom.y = msg.ReadBitCoord();
	vecFrom.z = msg.ReadBitCoord();
}

void CHudHealth::ApplySchemeSettings(vgui::IScheme *pScheme)
{
	BaseClass::ApplySchemeSettings(pScheme);
	SetPaintBackgroundEnabled(false);
}