aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/vgui_imagehealthpanel.cpp
blob: 69119cb2c1a8e87a3b4d9c7ff57c0ae1c69794ea (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: This is a panel which is rendered image on top of an entity
//
// $Revision: $
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "vgui_imagehealthpanel.h"
#include "commanderoverlay.h"
#include <KeyValues.h>
#include "mapdata.h"

bool IsLocalPlayerInTactical( void );

//-----------------------------------------------------------------------------
// Class factory
//-----------------------------------------------------------------------------

DECLARE_OVERLAY_FACTORY( CEntityImageHealthPanel, "image_health" );


//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CEntityImageHealthPanel::CEntityImageHealthPanel( vgui::Panel *parent, const char *panelName )
: BaseClass( parent, "CEntityImageHealthPanel" )
{
	SetPaintBackgroundEnabled( false );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CEntityImageHealthPanel::~CEntityImageHealthPanel()
{
	if ( m_pImagePanel )
	{
		delete m_pImagePanel;
		m_pImagePanel = NULL;
	}
}

//-----------------------------------------------------------------------------
// initialization
//-----------------------------------------------------------------------------
bool CEntityImageHealthPanel::Init( KeyValues* pInitData, C_BaseEntity* pEntity )
{
	if ( !pInitData )
		return false;
	if ( !BaseClass::Init( pInitData, pEntity ) )
		return false;

	m_CommanderHealthBar = NULL;
	m_NormalHealthBar = NULL;
	m_ResourceLevelBar = NULL;
	m_pImagePanel = NULL;

	// Health bar when we're in commander view
	KeyValues *pHealth = pInitData->FindKey("health_commander");
	if ( pHealth )
	{
		m_CommanderHealthBar = new CHealthBarPanel();
		if (!m_CommanderHealthBar->Init( pHealth ))
			return false;
		m_CommanderHealthBar->SetParent( this );
	}

	// Health bar when we're in normal view
	pHealth = pInitData->FindKey("health_normal");
	if ( pHealth )
	{
		m_NormalHealthBar = new CHealthBarPanel();
		if (!m_NormalHealthBar->Init( pHealth ))
			return false;
		m_NormalHealthBar->SetParent( this );
	}

	// Resource bar for collectors
	KeyValues *pResources = pInitData->FindKey("resource_level");
	if ( pResources )
	{
		m_ResourceLevelBar = new CHealthBarPanel();
		if (!m_ResourceLevelBar->Init( pResources ))
			return false;
		m_ResourceLevelBar->SetParent( this );
	}

	KeyValues *pImage = pInitData->FindKey("Image");
	if ( pImage )
	{
		m_pImagePanel = new CEntityTeamImagePanel( this, "CEntityTeamImagePanel" );
		if ( !m_pImagePanel->Init( pImage, GetEntity() ) )
			return false;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Should we draw?.
//-----------------------------------------------------------------------------
bool CEntityImageHealthPanel::ShouldDraw( void )
{
	// Always draw health
	return true;
}

//-----------------------------------------------------------------------------
// called when we're ticked...
//-----------------------------------------------------------------------------
void CEntityImageHealthPanel::OnTick()
{
	// tick the entity panel
	BaseClass::OnTick();

	C_BaseEntity* pBaseEntity = GetEntity();
	if (!pBaseEntity)
		return;
	// Don't draw if I'm not visible in the tactical map
	if ( MapData().IsEntityVisibleToTactical( pBaseEntity ) == false )
		return;

	if ( m_CommanderHealthBar )
		m_CommanderHealthBar->SetHealth( (float)pBaseEntity->GetHealth() / (float)pBaseEntity->GetMaxHealth() );
	if ( m_NormalHealthBar )
		m_NormalHealthBar->SetHealth( (float)pBaseEntity->GetHealth() / (float)pBaseEntity->GetMaxHealth() );

	// Hide the health bar we don't want to see
	C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();

	if ( pPlayer && (pBaseEntity->GetTeamNumber() != pPlayer->GetTeamNumber()) )
	{
		if ( m_CommanderHealthBar )
			m_CommanderHealthBar->SetVisible( false );
		if ( m_NormalHealthBar ) 
			m_NormalHealthBar->SetVisible( false );
		if ( m_ResourceLevelBar )
			m_ResourceLevelBar->SetVisible( false );
		if ( m_pImagePanel )
			m_pImagePanel->SetVisible( false );
	}
	else if ( IsLocalPlayerInTactical() )
	{
		if ( m_CommanderHealthBar )
			m_CommanderHealthBar->SetVisible( true );
		if ( m_NormalHealthBar ) 
			m_NormalHealthBar->SetVisible( false );
		if ( m_ResourceLevelBar )
			m_ResourceLevelBar->SetVisible( true );
		if ( m_pImagePanel )
			m_pImagePanel->SetVisible( true );
	}
	else
	{
		if ( m_CommanderHealthBar )
			m_CommanderHealthBar->SetVisible( false );
		if ( m_NormalHealthBar ) 
			m_NormalHealthBar->SetVisible( true );
		if ( m_ResourceLevelBar )
			m_ResourceLevelBar->SetVisible( true );
		if ( m_pImagePanel )
			m_pImagePanel->SetVisible( false );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Compute the size of the panel based upon the commander's zoom level
//-----------------------------------------------------------------------------
void CEntityImageHealthPanel::ComputeAndSetSize( void )
{
	BaseClass::ComputeAndSetSize();

	// Now update the bars
	if ( m_CommanderHealthBar )
		m_CommanderHealthBar->SetSize( GetWide(), m_CommanderHealthBar->GetTall() );
	if ( m_NormalHealthBar ) 
		m_NormalHealthBar->SetSize( GetWide(), m_CommanderHealthBar->GetTall() );
	if ( m_ResourceLevelBar )
		m_ResourceLevelBar->SetSize( GetWide(), m_CommanderHealthBar->GetTall() );
}