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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $Workfile: $
// $NoKeywords: $
//===========================================================================//
#include "cbase.h"
#include "c_basetempentity.h"
#include "particle_simple3d.h"
#include "tier1/KeyValues.h"
#include "toolframework_client.h"
#include "fx.h"
#include "tier0/vprof.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define PI 3.14159265359
#define GLASS_SHARD_MIN_LIFE 2
#define GLASS_SHARD_MAX_LIFE 5
#define GLASS_SHARD_NOISE 0.3
#define GLASS_SHARD_GRAVITY 500
#define GLASS_SHARD_DAMPING 0.3
#include "clienteffectprecachesystem.h"
CLIENTEFFECT_REGISTER_BEGIN( PrecacheEffectGlassShatter )
CLIENTEFFECT_MATERIAL( "effects/fleck_glass1" )
CLIENTEFFECT_MATERIAL( "effects/fleck_glass2" )
CLIENTEFFECT_MATERIAL( "effects/fleck_tile1" )
CLIENTEFFECT_MATERIAL( "effects/fleck_tile2" )
CLIENTEFFECT_REGISTER_END()
//###################################################
// > C_TEShatterSurface
//###################################################
class C_TEShatterSurface : public C_BaseTempEntity
{
public:
DECLARE_CLASS( C_TEShatterSurface, C_BaseTempEntity );
DECLARE_CLIENTCLASS();
C_TEShatterSurface( void );
~C_TEShatterSurface( void );
virtual void PostDataUpdate( DataUpdateType_t updateType );
private:
// Recording
void RecordShatterSurface( );
public:
Vector m_vecOrigin;
QAngle m_vecAngles;
Vector m_vecForce;
Vector m_vecForcePos;
float m_flWidth;
float m_flHeight;
float m_flShardSize;
PMaterialHandle m_pMaterialHandle;
int m_nSurfaceType;
byte m_uchFrontColor[3];
byte m_uchBackColor[3];
};
//------------------------------------------------------------------------------
// Networking
//------------------------------------------------------------------------------
IMPLEMENT_CLIENTCLASS_EVENT_DT(C_TEShatterSurface, DT_TEShatterSurface, CTEShatterSurface)
RecvPropVector( RECVINFO(m_vecOrigin)),
RecvPropVector( RECVINFO(m_vecAngles)),
RecvPropVector( RECVINFO(m_vecForce)),
RecvPropVector( RECVINFO(m_vecForcePos)),
RecvPropFloat( RECVINFO(m_flWidth)),
RecvPropFloat( RECVINFO(m_flHeight)),
RecvPropFloat( RECVINFO(m_flShardSize)),
RecvPropInt( RECVINFO(m_nSurfaceType)),
RecvPropInt( RECVINFO(m_uchFrontColor[0])),
RecvPropInt( RECVINFO(m_uchFrontColor[1])),
RecvPropInt( RECVINFO(m_uchFrontColor[2])),
RecvPropInt( RECVINFO(m_uchBackColor[0])),
RecvPropInt( RECVINFO(m_uchBackColor[1])),
RecvPropInt( RECVINFO(m_uchBackColor[2])),
END_RECV_TABLE()
//------------------------------------------------------------------------------
// Constructor, destructor
//------------------------------------------------------------------------------
C_TEShatterSurface::C_TEShatterSurface( void )
{
m_vecOrigin.Init();
m_vecAngles.Init();
m_vecForce.Init();
m_vecForcePos.Init();
m_flWidth = 16.0;
m_flHeight = 16.0;
m_flShardSize = 3;
m_nSurfaceType = SHATTERSURFACE_GLASS;
m_uchFrontColor[0] = 255;
m_uchFrontColor[1] = 255;
m_uchFrontColor[2] = 255;
m_uchBackColor[0] = 255;
m_uchBackColor[1] = 255;
m_uchBackColor[2] = 255;
}
C_TEShatterSurface::~C_TEShatterSurface()
{
}
//-----------------------------------------------------------------------------
// Recording
//-----------------------------------------------------------------------------
void C_TEShatterSurface::RecordShatterSurface( )
{
if ( !ToolsEnabled() )
return;
if ( clienttools->IsInRecordingMode() )
{
Color front( m_uchFrontColor[0], m_uchFrontColor[1], m_uchFrontColor[2], 255 );
Color back( m_uchBackColor[0], m_uchBackColor[1], m_uchBackColor[2], 255 );
KeyValues *msg = new KeyValues( "TempEntity" );
msg->SetInt( "te", TE_SHATTER_SURFACE );
msg->SetString( "name", "TE_ShatterSurface" );
msg->SetFloat( "time", gpGlobals->curtime );
msg->SetFloat( "originx", m_vecOrigin.x );
msg->SetFloat( "originy", m_vecOrigin.y );
msg->SetFloat( "originz", m_vecOrigin.z );
msg->SetFloat( "anglesx", m_vecAngles.x );
msg->SetFloat( "anglesy", m_vecAngles.y );
msg->SetFloat( "anglesz", m_vecAngles.z );
msg->SetFloat( "forcex", m_vecForce.x );
msg->SetFloat( "forcey", m_vecForce.y );
msg->SetFloat( "forcez", m_vecForce.z );
msg->SetFloat( "forceposx", m_vecForcePos.x );
msg->SetFloat( "forceposy", m_vecForcePos.y );
msg->SetFloat( "forceposz", m_vecForcePos.z );
msg->SetColor( "frontcolor", front );
msg->SetColor( "backcolor", back );
msg->SetFloat( "width", m_flWidth );
msg->SetFloat( "height", m_flHeight );
msg->SetFloat( "size", m_flShardSize );
msg->SetInt( "surfacetype", m_nSurfaceType );
ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
msg->deleteThis();
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void C_TEShatterSurface::PostDataUpdate( DataUpdateType_t updateType )
{
VPROF( "C_TEShatterSurface::PostDataUpdate" );
RecordShatterSurface();
CSmartPtr<CSimple3DEmitter> pGlassEmitter = CSimple3DEmitter::Create( "C_TEShatterSurface 1" );
pGlassEmitter->SetSortOrigin( m_vecOrigin );
Vector vecColor;
engine->ComputeLighting( m_vecOrigin, NULL, true, vecColor );
// HACK: Blend a little toward white to match the materials...
VectorLerp( vecColor, Vector( 1, 1, 1 ), 0.3, vecColor );
PMaterialHandle *hMaterial;
if (m_nSurfaceType == SHATTERSURFACE_GLASS)
{
hMaterial = g_Mat_Fleck_Glass;
}
else
{
hMaterial = g_Mat_Fleck_Tile;
}
// ---------------------------------------------------
// Figure out number of particles required to fill space
// ---------------------------------------------------
int nNumWide = m_flWidth / m_flShardSize;
int nNumHigh = m_flHeight / m_flShardSize;
Vector vWidthStep,vHeightStep;
AngleVectors(m_vecAngles,NULL,&vWidthStep,&vHeightStep);
vWidthStep *= m_flShardSize;
vHeightStep *= m_flShardSize;
// ---------------------
// Create glass shards
// ----------------------
Vector vCurPos = m_vecOrigin;
vCurPos.x += 0.5*m_flShardSize;
vCurPos.z += 0.5*m_flShardSize;
float flMinSpeed = 9999999999.0f;
float flMaxSpeed = 0;
Particle3D *pParticle = NULL;
for (int width=0;width<nNumWide;width++)
{
for (int height=0;height<nNumHigh;height++)
{
pParticle = (Particle3D *) pGlassEmitter->AddParticle( sizeof(Particle3D), hMaterial[random->RandomInt(0,1)], vCurPos );
Vector vForceVel = Vector(0,0,0);
if (random->RandomInt(0, 3) != 0)
{
float flForceDistSqr = (vCurPos - m_vecForcePos).LengthSqr();
vForceVel = m_vecForce;
if (flForceDistSqr > 0 )
{
vForceVel *= ( 40.0f / flForceDistSqr );
}
}
if (pParticle)
{
pParticle->m_flLifeRemaining = random->RandomFloat(GLASS_SHARD_MIN_LIFE,GLASS_SHARD_MAX_LIFE);
pParticle->m_vecVelocity = vForceVel;
pParticle->m_vecVelocity += RandomVector(-25,25);
pParticle->m_uchSize = m_flShardSize + random->RandomFloat(-0.5*m_flShardSize,0.5*m_flShardSize);
pParticle->m_vAngles = m_vecAngles;
pParticle->m_flAngSpeed = random->RandomFloat(-400,400);
pParticle->m_uchFrontColor[0] = (byte)(m_uchFrontColor[0] * vecColor.x );
pParticle->m_uchFrontColor[1] = (byte)(m_uchFrontColor[1] * vecColor.y );
pParticle->m_uchFrontColor[2] = (byte)(m_uchFrontColor[2] * vecColor.z );
pParticle->m_uchBackColor[0] = (byte)(m_uchBackColor[0] * vecColor.x );
pParticle->m_uchBackColor[1] = (byte)(m_uchBackColor[1] * vecColor.y );
pParticle->m_uchBackColor[2] = (byte)(m_uchBackColor[2] * vecColor.z );
}
// Keep track of min and max speed for collision detection
float flForceSpeed = vForceVel.Length();
if (flForceSpeed > flMaxSpeed)
{
flMaxSpeed = flForceSpeed;
}
if (flForceSpeed < flMinSpeed)
{
flMinSpeed = flForceSpeed;
}
vCurPos += vHeightStep;
}
vCurPos -= nNumHigh*vHeightStep;
vCurPos += vWidthStep;
}
// --------------------------------------------------
// Set collision parameters
// --------------------------------------------------
Vector vMoveDir = m_vecForce;
VectorNormalize(vMoveDir);
pGlassEmitter->m_ParticleCollision.Setup( m_vecOrigin, &vMoveDir, GLASS_SHARD_NOISE,
flMinSpeed, flMaxSpeed, GLASS_SHARD_GRAVITY, GLASS_SHARD_DAMPING );
}
void TE_ShatterSurface( IRecipientFilter& filter, float delay,
const Vector* pos, const QAngle* angle, const Vector* vForce, const Vector* vForcePos,
float width, float height, float shardsize, ShatterSurface_t surfacetype,
int front_r, int front_g, int front_b, int back_r, int back_g, int back_b)
{
// Major hack to simulate receiving network message
__g_C_TEShatterSurface.m_vecOrigin = *pos;
__g_C_TEShatterSurface.m_vecAngles = *angle;
__g_C_TEShatterSurface.m_vecForce = *vForce;
__g_C_TEShatterSurface.m_vecForcePos = *vForcePos;
__g_C_TEShatterSurface.m_flWidth = width;
__g_C_TEShatterSurface.m_flHeight = height;
__g_C_TEShatterSurface.m_flShardSize = shardsize;
__g_C_TEShatterSurface.m_nSurfaceType = surfacetype;
__g_C_TEShatterSurface.m_uchFrontColor[0] = front_r;
__g_C_TEShatterSurface.m_uchFrontColor[1] = front_g;
__g_C_TEShatterSurface.m_uchFrontColor[2] = front_b;
__g_C_TEShatterSurface.m_uchBackColor[0] = back_r;
__g_C_TEShatterSurface.m_uchBackColor[1] = back_g;
__g_C_TEShatterSurface.m_uchBackColor[2] = back_b;
__g_C_TEShatterSurface.PostDataUpdate( DATA_UPDATE_CREATED );
}
void TE_ShatterSurface( IRecipientFilter& filter, float delay, KeyValues *pKeyValues )
{
Vector vecOrigin, vecForce, vecForcePos;
QAngle angles;
vecOrigin.x = pKeyValues->GetFloat( "originx" );
vecOrigin.y = pKeyValues->GetFloat( "originy" );
vecOrigin.z = pKeyValues->GetFloat( "originz" );
angles.x = pKeyValues->GetFloat( "anglesx" );
angles.y = pKeyValues->GetFloat( "anglesy" );
angles.z = pKeyValues->GetFloat( "anglesz" );
vecForce.x = pKeyValues->GetFloat( "forcex" );
vecForce.y = pKeyValues->GetFloat( "forcey" );
vecForce.z = pKeyValues->GetFloat( "forcez" );
vecForcePos.x = pKeyValues->GetFloat( "forceposx" );
vecForcePos.y = pKeyValues->GetFloat( "forceposy" );
vecForcePos.z = pKeyValues->GetFloat( "forceposz" );
Color front = pKeyValues->GetColor( "frontcolor" );
Color back = pKeyValues->GetColor( "backcolor" );
float flWidth = pKeyValues->GetFloat( "width" );
float flHeight = pKeyValues->GetFloat( "height" );
float flSize = pKeyValues->GetFloat( "size" );
ShatterSurface_t nSurfaceType = (ShatterSurface_t)pKeyValues->GetInt( "surfacetype" );
TE_ShatterSurface( filter, 0.0f, &vecOrigin, &angles, &vecForce, &vecForcePos,
flWidth, flHeight, flSize, nSurfaceType, front.r(), front.g(), front.b(),
back.r(), back.g(), back.b() );
}
|