summaryrefslogtreecommitdiff
path: root/game/client/tf2/c_obj_powerpack.cpp
blob: edbfcfd38a4d343937c47e0acd3550762370b082 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "c_baseobject.h"
#include "ObjectControlPanel.h"
#include "tf_shareddefs.h"
#include "tempent.h"
#include "c_te_legacytempents.h"
#include "iviewrender_beams.h"
#include "beamdraw.h"
#include "view.h"

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

#define NUM_POWERPACK_GLOWS		6

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
class C_ObjectPowerPack : public C_BaseObject
{
	DECLARE_CLASS( C_ObjectPowerPack, C_BaseObject );
public:
	DECLARE_CLIENTCLASS();

	C_ObjectPowerPack();
	~C_ObjectPowerPack();
	int SocketsLeft() const { return (MAX_OBJECTS_PER_PACK - m_iObjectsAttached); }

	// Since we have material proxies to show building amount, don't offset origin
	virtual bool	OffsetObjectOrigin( Vector& origin )
	{
		return false;
	}

	virtual void	OnGoActive( void );
	virtual void	OnGoInactive( void );
	void			RemoveGlows( void );
	virtual void	ClientThink( void );
	virtual int		DrawModel( int flags );

private:
	int					m_iObjectsAttached;
	int					m_iGlowModelIndex;
	C_LocalTempEntity	*m_pGlowSprites[ NUM_POWERPACK_GLOWS ];

	// Jacob's laddder
	Beam_t				*m_pJacobsLadderBeam;
	float				m_flJacobsLeftPoint;
	float				m_flJacobsRightPoint;
	Vector				m_vecJacobsStart;
	Vector				m_vecJacobsEnd;
	CMaterialReference	m_hJacobsPointMaterial;

private:
	C_ObjectPowerPack( const C_ObjectPowerPack & ); // not defined, not accessible
};

IMPLEMENT_CLIENTCLASS_DT(C_ObjectPowerPack, DT_ObjectPowerPack, CObjectPowerPack)
	RecvPropInt( RECVINFO(m_iObjectsAttached) ),
END_RECV_TABLE()

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_ObjectPowerPack::C_ObjectPowerPack()
{
	for ( int i = 0; i < NUM_POWERPACK_GLOWS; i++ )
	{
		m_pGlowSprites[i] = NULL;
	}

	m_iGlowModelIndex = PrecacheModel( "effects/human_object_glow.vmt" ); 
	m_pJacobsLadderBeam = NULL;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_ObjectPowerPack::~C_ObjectPowerPack( void )
{
	RemoveGlows();
}

//-----------------------------------------------------------------------------
// Purpose: We've just gone active
//-----------------------------------------------------------------------------
void C_ObjectPowerPack::OnGoActive( void )
{
	// Turn on our glows
	for ( int i = 0; i < NUM_POWERPACK_GLOWS; i++ )
	{
		// Find the attachment point
		int iAttachment = LookupAttachment( VarArgs("glow_%d",(i+1)) );
		Vector vecOrigin;
		QAngle vecAngles;
		if ( GetAttachment( iAttachment, vecOrigin, vecAngles ) )
		{
			Vector vecForward;
			AngleVectors( vecAngles, &vecForward );
			m_pGlowSprites[i] = tempents->TempSprite( vecOrigin, vec3_origin, 0.35, m_iGlowModelIndex, kRenderTransAdd, 0, 0.5, 1, FTENT_PERSIST | FTENT_NEVERDIE | FTENT_BEOCCLUDED, vecForward );
		}
	}

	m_flJacobsLeftPoint = 0;
	m_flJacobsRightPoint = 0;
	m_hJacobsPointMaterial.Init( "sprites/blueflare2", TEXTURE_GROUP_CLIENT_EFFECTS );
}

//-----------------------------------------------------------------------------
// Purpose: We've just gone inactive
//-----------------------------------------------------------------------------
void C_ObjectPowerPack::OnGoInactive( void )
{
	// Turn off our glows
	RemoveGlows();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_ObjectPowerPack::RemoveGlows( void )
{
	for ( int i = 0; i < NUM_POWERPACK_GLOWS; i++ )
	{
		if ( m_pGlowSprites[i] )
		{
			m_pGlowSprites[i]->die = 0;
			m_pGlowSprites[i] = NULL;
		}
	}

	// Stop the jacob's ladder
	if ( m_pJacobsLadderBeam )
	{
		m_pJacobsLadderBeam->flags &= ~FBEAM_FOREVER;
		m_pJacobsLadderBeam->die = gpGlobals->curtime;
		m_pJacobsLadderBeam = NULL;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_ObjectPowerPack::ClientThink( void )
{
	// Create the jacob's ladder
	if ( !m_pJacobsLadderBeam )
	{
		BeamInfo_t beamInfo;
		beamInfo.m_vecStart.Init();
		beamInfo.m_vecEnd.Init();
		beamInfo.m_pszModelName = "sprites/physbeam.vmt";
		beamInfo.m_flHaloScale = 0.0f;
		beamInfo.m_flLife = 0.0f;
		beamInfo.m_flWidth = 8.0f;
		beamInfo.m_flEndWidth = 4.0f;
		beamInfo.m_flFadeLength = 0.0f;
		beamInfo.m_flAmplitude = 20.0f;
		beamInfo.m_flBrightness = 255.0f;
		beamInfo.m_flSpeed = 0.0f;
		beamInfo.m_nStartFrame = 0;
		beamInfo.m_flFrameRate = 0.0f;
		beamInfo.m_flRed = 206.0f;
		beamInfo.m_flGreen = 181.0f;
		beamInfo.m_flBlue = 127.0f;
		beamInfo.m_nSegments = 5;
		beamInfo.m_bRenderable = true;
		m_pJacobsLadderBeam = beams->CreateBeamPoints( beamInfo );
	}

	// Update the position of the jacob's ladder
	BeamInfo_t beamInfo;
	QAngle vecAngle;
	int iAttachment;

	// Setup a color reflecting the amount of power being used
	color32 color;
	color.r = 206;
	color.g = 182;
	color.b = 127;
	color.a = 255;

	// Tesla Effect
	Vector vecRightTop, vecRightBottom;
	Vector vecLeftTop, vecLeftBottom;
	iAttachment = LookupAttachment( "Tesla_ll" );
	GetAttachment( iAttachment, vecLeftBottom, vecAngle );
	iAttachment = LookupAttachment( "Tesla_ul" );
	GetAttachment( iAttachment, vecLeftTop, vecAngle );
	iAttachment = LookupAttachment( "Tesla_lr" );
	GetAttachment( iAttachment, vecRightBottom, vecAngle );
	iAttachment = LookupAttachment( "Tesla_ur" );
	GetAttachment( iAttachment, vecRightTop, vecAngle );

	float flSpeed = 0.02;

	m_flJacobsLeftPoint += random->RandomFloat( flSpeed * 0.25, flSpeed * 2);
	m_flJacobsRightPoint += random->RandomFloat( flSpeed * 0.25, flSpeed * 2);

	// If they've both hit the end, break the ladder
	if ( m_flJacobsLeftPoint >= 1.0f && m_flJacobsRightPoint >= 1.0f )
	{
		// Snap!
		m_flJacobsLeftPoint = 0.0f; 
		m_flJacobsRightPoint = 0.0f; 
	}
	else if ( m_flJacobsLeftPoint > 1.0f ) 
	{ 
		// Only the left point's made it
		m_flJacobsLeftPoint = 1.0f; 
	}
	else if ( m_flJacobsRightPoint > 1.0f ) 
	{ 
		// Only the right point's made it
		m_flJacobsRightPoint = 1.0f; 
	}

	Vector vecLeft = vecLeftTop - vecLeftBottom;
	Vector vecRight = vecRightTop - vecRightBottom;
	m_vecJacobsStart = vecLeftBottom + ( m_flJacobsLeftPoint * vecLeft );
	m_vecJacobsEnd = vecRightBottom + ( m_flJacobsRightPoint * vecRight );

	beamInfo.m_vecStart = m_vecJacobsStart;
	beamInfo.m_vecEnd = m_vecJacobsEnd;
	beamInfo.m_flRed = color.r;
	beamInfo.m_flGreen = color.g;
	beamInfo.m_flBlue = color.b;
	beams->UpdateBeamInfo( m_pJacobsLadderBeam, beamInfo );	
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int C_ObjectPowerPack::DrawModel( int flags )
{
	if ( BaseClass::DrawModel( flags ) )
	{
		if ( ShouldBeActive() )
		{
			// Get the distance to the view
			float flDistance = (GetAbsOrigin() - MainViewOrigin()).LengthSqr();
			if ( flDistance < (1024 * 1024) )
			{
				// Draw a sprite at the tips.
				color32 color;
				color.r = 255;
				color.g = 255;
				color.b = 255;
				color.a = 255;
				float flSize = 25.0f;
				materials->Bind( m_hJacobsPointMaterial, this );
				DrawSprite( m_vecJacobsStart, flSize, flSize, color );
				DrawSprite( m_vecJacobsEnd, flSize, flSize, color );
			}
		}

		return true;
	}

	return false;
}

//-----------------------------------------------------------------------------
// Control screen 
//-----------------------------------------------------------------------------
class CPowerPackControlPanel : public CObjectControlPanel
{
	DECLARE_CLASS( CPowerPackControlPanel, CObjectControlPanel );

public:
	CPowerPackControlPanel( vgui::Panel *parent, const char *panelName );
	virtual bool Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData );
	virtual void OnTick();

private:
	vgui::Label *m_pSocketsLabel;
};


DECLARE_VGUI_SCREEN_FACTORY( CPowerPackControlPanel, "powerpack_control_panel" );


//-----------------------------------------------------------------------------
// Constructor: 
//-----------------------------------------------------------------------------
CPowerPackControlPanel::CPowerPackControlPanel( vgui::Panel *parent, const char *panelName )
	: BaseClass( parent, "CPowerPackControlPanel" ) 
{
}


//-----------------------------------------------------------------------------
// Initialization 
//-----------------------------------------------------------------------------
bool CPowerPackControlPanel::Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData )
{
	m_pSocketsLabel = new vgui::Label( GetActivePanel(), "SocketReadout", "" );

	if (!BaseClass::Init(pKeyValues, pInitData))
		return false;

	return true;
}


//-----------------------------------------------------------------------------
// Frame-based update
//-----------------------------------------------------------------------------
void CPowerPackControlPanel::OnTick()
{
	BaseClass::OnTick();

	C_BaseObject *pObj = GetOwningObject();
	if (!pObj)
		return;

	Assert( dynamic_cast<C_ObjectPowerPack*>(pObj) );
	C_ObjectPowerPack *pPowerPack = static_cast<C_ObjectPowerPack*>(pObj);

	char buf[256];
	int nSocketsLeft = pPowerPack->SocketsLeft();
	if (nSocketsLeft > 0)
	{
		Q_snprintf( buf, sizeof( buf ), "%d sockets left", pPowerPack->SocketsLeft() );
	}
	else
	{
		Q_strncpy( buf, "No sockets left", sizeof( buf ) );
	}

	m_pSocketsLabel->SetText( buf );
}