summaryrefslogtreecommitdiff
path: root/game/client/tf2/hud_damageindicator.cpp
blob: 40cbc556af0ee8327c0aae260088a9531f70ff98 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Hud element that indicates the direction of damage taken by the player
//
//=============================================================================//
#include "cbase.h"
#include "hud.h"
#include "text_message.h"
#include "hud_macros.h"
#include "iclientmode.h"
#include "view.h"
#include <KeyValues.h>
#include <vgui_controls/AnimationController.h>
#include <vgui/ISurface.h>
#include "VGuiMatSurface/IMatSystemSurface.h"
#include "materialsystem/imaterial.h"
#include "materialsystem/imesh.h"
#include "materialsystem/imaterialvar.h"
#include "IEffects.h"
#include "hudelement.h"

using namespace vgui;

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
class CHudDamageIndicator : public CHudElement, public vgui::Panel
{
	DECLARE_CLASS_SIMPLE( CHudDamageIndicator, vgui::Panel );
public:
	CHudDamageIndicator( const char *pElementName );
	void Init( void );
	void VidInit( void );
	void Reset( void );
	virtual bool ShouldDraw( void );

	// Handler for our message
	void MsgFunc_Damage( bf_read &msg );

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

	// Painting
	void GetDamagePosition( const Vector &vecDelta, float flRadius, int *xpos, int *ypos, float *flRotation );
	void DrawDamageIndicator(int x0, int y0, int x1, int y1, float alpha, float flRotation );

private:
	// Indication times

	CPanelAnimationVarAliasType( float, m_flMinimumWidth, "MinimumWidth", "10", "proportional_float" );
	CPanelAnimationVarAliasType( float, m_flMaximumWidth, "MaximumWidth", "100", "proportional_float" );
	CPanelAnimationVarAliasType( float, m_flMinimumHeight, "MinimumHeight", "20", "proportional_float" );
	CPanelAnimationVarAliasType( float, m_flMaximumHeight, "MaximumHeight", "100", "proportional_float" );
	CPanelAnimationVarAliasType( float, m_flStartRadius, "StartRadius", "140", "proportional_float" );
	CPanelAnimationVarAliasType( float, m_flEndRadius, "EndRadius", "120", "proportional_float" );

	CPanelAnimationVar( float, m_iMaximumDamage, "MaximumDamage", "50" );
	CPanelAnimationVar( float, m_flMinimumTime, "MinimumTime", "1" );
	CPanelAnimationVar( float, m_flMaximumTime, "MaximumTime", "6" );
	CPanelAnimationVar( float, m_flTravelTime, "TravelTime", ".1" );
	CPanelAnimationVar( float, m_flFadeOutPercentage, "FadeOutPercentage", "0.7" );
	CPanelAnimationVar( float, m_flNoise, "Noise", "0.1" );

	// List of damages we've taken
	struct damage_t
	{
		int		iScale;
		float	flLifeTime;
		float	flStartTime;
		Vector	vecDelta;	// Damage origin relative to the player
	};
	CUtlVector<damage_t>	m_vecDamages;
};

DECLARE_HUDELEMENT( CHudDamageIndicator );
DECLARE_HUD_MESSAGE( CHudDamageIndicator, Damage );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CHudDamageIndicator::CHudDamageIndicator( const char *pElementName ) :
	CHudElement( pElementName ), BaseClass(NULL, "DamageIndicator")
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );

	SetHiddenBits( HIDEHUD_HEALTH );
}

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

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudDamageIndicator::Reset( void )
{
	m_vecDamages.Purge();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudDamageIndicator::VidInit( void )
{
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudDamageIndicator::OnThink()
{
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CHudDamageIndicator::ShouldDraw( void )
{
	if ( !CHudElement::ShouldDraw() )
		return false;

	// Don't draw if we don't have any damage to indicate
	if ( !m_vecDamages.Count() )
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Convert a damage position in world units to the screen's units
//-----------------------------------------------------------------------------
void CHudDamageIndicator::GetDamagePosition( const Vector &vecDelta, float flRadius, int *xpos, int *ypos, float *flRotation )
{
	// Player Data
	Vector playerPosition = MainViewOrigin();
	QAngle playerAngles = MainViewAngles();

	Vector forward, right, up(0,0,1);
	AngleVectors (playerAngles, &forward, NULL, NULL );
	forward.z = 0;
	VectorNormalize(forward);
	CrossProduct( up, forward, right );
	float front = DotProduct(vecDelta, forward);
	float side = DotProduct(vecDelta, right);
	*xpos = flRadius * -side;
	*ypos = flRadius * -front;

	// Get the rotation (yaw)
	*flRotation = atan2(*xpos,*ypos) + M_PI;
	*flRotation *= 180 / M_PI;

	float yawRadians = -(*flRotation) * M_PI / 180.0f;
	float ca = cos( yawRadians );
	float sa = sin( yawRadians );
				 
	// Rotate it around the circle
	*xpos = (int)((ScreenWidth() / 2) + (flRadius * sa));
	*ypos = (int)((ScreenHeight() / 2) - (flRadius * ca));
}

//-----------------------------------------------------------------------------
// Purpose: Draw a single damage indicator
//-----------------------------------------------------------------------------
void CHudDamageIndicator::DrawDamageIndicator(int x0, int y0, int x1, int y1, float alpha, float flRotation )
{
	IMaterial *pMat = materials->FindMaterial( "hud/health/indicator", TEXTURE_GROUP_VGUI );
	CMatRenderContextPtr pRenderContext( materials );
	IMesh *pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, pMat );

	// Get the corners, since they're being rotated
	int wide = x1 - x0;
	int tall = y1 - y0;
	Vector2D vecCorners[4];
	Vector2D center( x0 + (wide * 0.5f), y0 + (tall * 0.5f) );
	float yawRadians = -flRotation * M_PI / 180.0f;
	Vector2D axis[2];
	axis[0].x = cos(yawRadians);
	axis[0].y = sin(yawRadians);
	axis[1].x = -axis[0].y;
	axis[1].y = axis[0].x;
	Vector2DMA( center, -0.5f * wide, axis[0], vecCorners[0] );
	Vector2DMA( vecCorners[0], -0.5f * tall, axis[1], vecCorners[0] );
	Vector2DMA( vecCorners[0], wide, axis[0], vecCorners[1] );
	Vector2DMA( vecCorners[1], tall, axis[1], vecCorners[2] );
	Vector2DMA( vecCorners[0], tall, axis[1], vecCorners[3] );

	// Draw the sucker
	CMeshBuilder meshBuilder;
	meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );

	int iAlpha = alpha * 255;
	meshBuilder.Color4ub( 255,255,255, iAlpha );
	meshBuilder.TexCoord2f( 0,0,0 );
	meshBuilder.Position3f( vecCorners[0].x,vecCorners[0].y,0 );
	meshBuilder.AdvanceVertex();

	meshBuilder.Color4ub( 255,255,255, iAlpha );
	meshBuilder.TexCoord2f( 0,1,0 );
	meshBuilder.Position3f( vecCorners[1].x,vecCorners[1].y,0 );
	meshBuilder.AdvanceVertex();

	meshBuilder.Color4ub( 255,255,255, iAlpha );
	meshBuilder.TexCoord2f( 0,1,1 );
	meshBuilder.Position3f( vecCorners[2].x,vecCorners[2].y,0 );
	meshBuilder.AdvanceVertex();

	meshBuilder.Color4ub( 255,255,255, iAlpha );
	meshBuilder.TexCoord2f( 0,0,1 );
	meshBuilder.Position3f( vecCorners[3].x,vecCorners[3].y,0 );
	meshBuilder.AdvanceVertex();

	meshBuilder.End();
	pMesh->Draw();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudDamageIndicator::Paint()
{
	// Iterate backwards, because we might remove them as we go
	int iSize = m_vecDamages.Count();
	for (int i = iSize-1; i >= 0; i--)
	{
		// Scale size to the damage
		int clampedDamage = clamp( m_vecDamages[i].iScale, 0, m_iMaximumDamage );

		int iWidth = RemapVal(clampedDamage, 0, m_iMaximumDamage, m_flMinimumWidth, m_flMaximumWidth) * 0.5;
		int iHeight = RemapVal(clampedDamage, 0, m_iMaximumDamage, m_flMinimumHeight, m_flMaximumHeight) * 0.5;

		// Find the place to draw it
		int xpos, ypos;
		float flRotation;
		float flTimeSinceStart = ( gpGlobals->curtime - m_vecDamages[i].flStartTime );
		float flRadius = RemapVal( MIN( flTimeSinceStart, m_flTravelTime ), 0, m_flTravelTime, m_flStartRadius, m_flEndRadius );
		GetDamagePosition( m_vecDamages[i].vecDelta, flRadius, &xpos, &ypos, &flRotation );

		// Calculate life left
		float flLifeLeft = ( m_vecDamages[i].flLifeTime - gpGlobals->curtime );
		if ( flLifeLeft > 0 )
		{
			float flPercent = flTimeSinceStart / (m_vecDamages[i].flLifeTime - m_vecDamages[i].flStartTime);
			float alpha;
			if ( flPercent <= m_flFadeOutPercentage )
			{
				alpha = 1.0;
			}
			else
			{
				alpha = 1.0 - RemapVal( flPercent, m_flFadeOutPercentage, 1.0, 0.0, 1.0 );
			}
			DrawDamageIndicator( xpos-iWidth, ypos-iHeight, xpos+iWidth, ypos+iHeight, alpha, flRotation );
		}
		else
		{
			m_vecDamages.Remove(i);
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Message handler for Damage message
//-----------------------------------------------------------------------------
void CHudDamageIndicator::MsgFunc_Damage( bf_read &msg )
{
	damage_t damage;
	damage.iScale = msg.ReadByte();
	if ( damage.iScale > m_iMaximumDamage )
	{
		damage.iScale = m_iMaximumDamage;
	}
	Vector vecOrigin;
	vecOrigin.x = msg.ReadFloat();
	vecOrigin.y = msg.ReadFloat();
	vecOrigin.z = msg.ReadFloat();
	damage.flStartTime = gpGlobals->curtime;
	damage.flLifeTime = gpGlobals->curtime + RemapVal(damage.iScale, 0, m_iMaximumDamage, m_flMinimumTime, m_flMaximumTime);

	if ( vecOrigin == vec3_origin )
	{
		vecOrigin = MainViewOrigin();
	}

	damage.vecDelta = (vecOrigin - MainViewOrigin());
	VectorNormalize( damage.vecDelta );

	// Add some noise
	damage.vecDelta[0] += random->RandomFloat( -m_flNoise, m_flNoise );
	damage.vecDelta[1] += random->RandomFloat( -m_flNoise, m_flNoise );
	damage.vecDelta[2] += random->RandomFloat( -m_flNoise, m_flNoise );
	VectorNormalize( damage.vecDelta );

	m_vecDamages.AddToTail( damage );
}

//-----------------------------------------------------------------------------
// Purpose: hud scheme settings
//-----------------------------------------------------------------------------
void CHudDamageIndicator::ApplySchemeSettings(vgui::IScheme *pScheme)
{
	BaseClass::ApplySchemeSettings(pScheme);
	SetPaintBackgroundEnabled(false);

	// set our size
	int screenWide, screenTall;
	int x, y;
	GetPos(x, y);
	GetHudSize(screenWide, screenTall);
	SetBounds(0, y, screenWide, screenTall - y);
}