summaryrefslogtreecommitdiff
path: root/game/client/tf/tf_hud_bowcharge.cpp
blob: c1d3d256cfdc381283166507239a01bddb555ea5 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================

#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "c_tf_player.h"
#include "iclientmode.h"
#include "ienginevgui.h"
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include <vgui/IVGui.h>
#include <vgui_controls/EditablePanel.h>
#include <vgui_controls/ProgressBar.h>
#include "tf_weaponbase.h"
#include "c_tf_projectile_arrow.h"

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

using namespace vgui;

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
class CHudBowChargeMeter : public CHudElement, public EditablePanel
{
	DECLARE_CLASS_SIMPLE( CHudBowChargeMeter, EditablePanel );

public:
	CHudBowChargeMeter( const char *pElementName );

	virtual void	ApplySchemeSettings( IScheme *scheme );
	virtual bool	ShouldDraw( void );
	virtual void	OnTick( void );
	virtual void	Init( void );
	virtual void	FireGameEvent( IGameEvent *event );

private:
	vgui::ContinuousProgressBar *m_pChargeMeter;
};

DECLARE_HUDELEMENT( CHudBowChargeMeter );

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

	m_pChargeMeter = new ContinuousProgressBar( this, "ChargeMeter" );

	SetHiddenBits( HIDEHUD_MISCSTATUS );

	vgui::ivgui()->AddTickSignal( GetVPanel() );

	RegisterForRenderGroup( "inspect_panel" );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudBowChargeMeter::ApplySchemeSettings( IScheme *pScheme )
{
	// load control settings...
	LoadControlSettings( "resource/UI/HudBowCharge.res" );

	BaseClass::ApplySchemeSettings( pScheme );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudBowChargeMeter::Init( void )
{
	ListenForGameEvent( "arrow_impact" );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudBowChargeMeter::FireGameEvent( IGameEvent *event )
{
	if ( !event )
		return;

	const char *pszEventName = event->GetName();

	if ( FStrEq( pszEventName, "arrow_impact" ) )
	{
		int attachedEntity = event->GetInt( "attachedEntity" );
		C_BaseFlex *pFlex = dynamic_cast<C_BaseFlex*>( ClientEntityList().GetEnt( attachedEntity ) );
		if ( !pFlex )
			return;

		// Create a client side arrow and have it attach itself.
		C_TFProjectile_Arrow *pArrow = new C_TFProjectile_Arrow;
		if ( !pArrow )
			return;

		int boneIndexAttached = event->GetInt( "boneIndexAttached" );
		Vector bonePosition( 
			event->GetFloat( "bonePositionX"),
			event->GetFloat( "bonePositionY"),
			event->GetFloat( "bonePositionZ") );
		QAngle boneAngles( 
			event->GetFloat( "boneAnglesX"),
			event->GetFloat( "boneAnglesY"),
			event->GetFloat( "boneAnglesZ") );

		const char* pszModelName = NULL;
		int type = event->GetInt( "projectileType" );
		float flScale = 1.0f;

		switch ( type )
		{
		case TF_PROJECTILE_STICKY_BALL:
			pszModelName = g_pszArrowModels[MODEL_SNOWBALL];
			break;
		case TF_PROJECTILE_ARROW:
			pszModelName = g_pszArrowModels[MODEL_ARROW_REGULAR];
			break;
		case TF_PROJECTILE_BUILDING_REPAIR_BOLT:
			pszModelName = g_pszArrowModels[MODEL_ARROW_BUILDING_REPAIR];
			break;
		case TF_PROJECTILE_FESTIVE_ARROW:
			pszModelName = g_pszArrowModels[MODEL_FESTIVE_ARROW_REGULAR];
			break;
		case TF_PROJECTILE_HEALING_BOLT:
#ifdef STAGING_ONLY
		case TF_PROJECTILE_MILK_BOLT:
#endif
			{
				pszModelName = g_pszArrowModels[MODEL_SYRINGE];
				// pull the syringe back slightly
				Vector vForward;
				AngleVectors( boneAngles, &vForward );
				bonePosition = bonePosition - (vForward * 6.0f);
				flScale = 1.6f;
			}
			break;
		case TF_PROJECTILE_FESTIVE_HEALING_BOLT:
			{
				pszModelName = g_pszArrowModels[MODEL_FESTIVE_HEALING_BOLT];
				// pull the syringe back slightly
				Vector vForward;
				AngleVectors( boneAngles, &vForward );
				bonePosition = bonePosition - ( vForward * 1.0f );
				flScale = 1.4f;
			}
			break;
		case TF_PROJECTILE_BREAD_MONSTER:
		case TF_PROJECTILE_BREADMONSTER_JARATE:
		case TF_PROJECTILE_BREADMONSTER_MADMILK:
			{
				pszModelName = g_pszArrowModels[MODEL_BREAD_MONSTER];
				// pull the syringe back slightly
				Vector vForward;
				AngleVectors( boneAngles, &vForward );
				bonePosition = bonePosition - ( vForward * 1.0f );
				flScale = 2.5f;
				pArrow->SetLifeTime( 10.0f );
				if ( event->GetBool( "isCrit" ) )
				{
					flScale = RandomFloat( 3.0f, 5.0f );
				}
				break;
			}
#ifdef STAGING_ONLY
		case TF_PROJECTILE_THROWING_KNIFE:
			{
				pszModelName = g_pszArrowModels[MODEL_THROWING_KNIFE];
				// pull the syringe back slightly
				Vector vForward;
				AngleVectors( boneAngles, &vForward );
				//bonePosition = bonePosition + ( vForward * 5.0f );
				//bonePosition = ( vForward * -7.0f ) + Vector(0, 0, 2);
				//flScale = 5.0f;
				break;
			}
		case TF_PROJECTILE_SNIPERBULLET:
			pszModelName = g_pszArrowModels[MODEL_SYRINGE];
			break;
#endif // STAGING_ONLY
		default:
			Warning( " Unsupported Projectile type on event arrow_impact - %d", type );
			return;
		}
		
		pArrow->InitializeAsClientEntity( pszModelName, RENDER_GROUP_OPAQUE_ENTITY );
		pArrow->SetModelScale( flScale );

		CTFPlayer *pPlayer = ToTFPlayer( ClientEntityList().GetEnt( event->GetInt( "shooter" ) ) );
		if ( pPlayer )
		{
			pArrow->m_nSkin = ( pPlayer->GetTeamNumber() == TF_TEAM_BLUE ) ? 1 : 0;
		}

		pArrow->AttachEntityToBone( pFlex, boneIndexAttached, bonePosition, boneAngles );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CHudBowChargeMeter::ShouldDraw( void )
{
	C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();

	if ( !pPlayer || !pPlayer->IsPlayerClass( TF_CLASS_SNIPER ) || !pPlayer->IsAlive() )
	{
		return false;
	}

	CTFWeaponBase *pWpn = pPlayer->GetActiveTFWeapon();

	if ( !pWpn )
	{
		return false;
	}

	int iWeaponID = pWpn->GetWeaponID();

	if ( iWeaponID != TF_WEAPON_COMPOUND_BOW )
	{
		return false;
	}

	return CHudElement::ShouldDraw();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudBowChargeMeter::OnTick( void )
{
	C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();

	if ( !pPlayer )
		return;

	CTFWeaponBase *pWpn = pPlayer->GetActiveTFWeapon();
	ITFChargeUpWeapon *pChargeupWeapon = dynamic_cast< ITFChargeUpWeapon *>( pWpn );

	if ( !pWpn || !pChargeupWeapon )
		return;

	if ( m_pChargeMeter )
	{
		float flChargeMaxTime = pChargeupWeapon->GetChargeMaxTime();

		if ( flChargeMaxTime != 0 )
		{
			float flChargeBeginTime = pChargeupWeapon->GetChargeBeginTime();

			if ( flChargeBeginTime > 0 )
			{
				float flTimeCharged = MAX( 0, gpGlobals->curtime - flChargeBeginTime );
				flTimeCharged = MIN( flTimeCharged, 1.f );
				float flPercentCharged = MIN( 1.0, flTimeCharged / flChargeMaxTime );

				m_pChargeMeter->SetProgress( flPercentCharged );
			}
			else
			{
				m_pChargeMeter->SetProgress( 0.0f );
			}
		}
	}
}