Esempio n. 1
0
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : scale - 
//			attachmentIndex - 
//			bOneFrame - 
//-----------------------------------------------------------------------------
void FX_MuzzleEffectAttached( 
	float scale, 
	ClientEntityHandle_t hEntity, 
	int attachmentIndex, 
	unsigned char *pFlashColor,
	bool bOneFrame )
{
	VPROF_BUDGET( "FX_MuzzleEffect", VPROF_BUDGETGROUP_PARTICLE_RENDERING );

	// If the material isn't available, let's not do anything.
	if ( g_Mat_SMG_Muzzleflash[0] == NULL )
	{
		return;
	}
	
	CSmartPtr<CLocalSpaceEmitter> pSimple = CLocalSpaceEmitter::Create( "MuzzleFlash", hEntity, attachmentIndex );
	Assert( pSimple );
	if ( pSimple == NULL )
		return;
	
	// Lock our bounding box
	pSimple->GetBinding().SetBBox( -( Vector( 16, 16, 16 ) * scale ), ( Vector( 16, 16, 16 ) * scale ) );
	
	SimpleParticle *pParticle;
	Vector			forward(1,0,0), offset;

	float flScale = random->RandomFloat( scale-0.25f, scale+0.25f );

	if ( flScale < 0.5f )
	{
		flScale = 0.5f;
	}
	else if ( flScale > 8.0f )
	{
		flScale = 8.0f;
	}

	//
	// Flash
	//

	int i;
	for ( i = 1; i < 9; i++ )
	{
		offset = (forward * (i*2.0f*scale));

		pParticle = (SimpleParticle *) pSimple->AddParticle( sizeof( SimpleParticle ), g_Mat_SMG_Muzzleflash[random->RandomInt(0,3)], offset );
			
		if ( pParticle == NULL )
			return;

		pParticle->m_flLifetime		= 0.0f;
		pParticle->m_flDieTime		= bOneFrame ? 0.0001f : 0.1f;

		pParticle->m_vecVelocity.Init();

		if ( !pFlashColor )
		{
			pParticle->m_uchColor[0]	= 255;
			pParticle->m_uchColor[1]	= 255;
			pParticle->m_uchColor[2]	= 255;
		}
		else
		{
			pParticle->m_uchColor[0]	= pFlashColor[0];
			pParticle->m_uchColor[1]	= pFlashColor[1];
			pParticle->m_uchColor[2]	= pFlashColor[2];
		}

		pParticle->m_uchStartAlpha	= 255;
		pParticle->m_uchEndAlpha	= 128;

		pParticle->m_uchStartSize	= (random->RandomFloat( 6.0f, 9.0f ) * (12-(i))/9) * flScale;
		pParticle->m_uchEndSize		= pParticle->m_uchStartSize;
		pParticle->m_flRoll			= random->RandomInt( 0, 360 );
		pParticle->m_flRollDelta	= 0.0f;
	}


	if ( !ToolsEnabled() )
		return;

	if ( !clienttools->IsInRecordingMode() )
		return;

	C_BaseEntity *pEnt = ClientEntityList().GetBaseEntityFromHandle( hEntity );
	if ( pEnt )
	{
		pEnt->RecordToolMessage();
	}

	// NOTE: Particle system destruction message will be sent by the particle effect itself.
	int nId = pSimple->AllocateToolParticleEffectId();

	KeyValues *msg = new KeyValues( "OldParticleSystem_Create" );
	msg->SetString( "name", "FX_MuzzleEffectAttached" );
	msg->SetInt( "id", nId );
	msg->SetFloat( "time", gpGlobals->curtime );

	KeyValues *pEmitter = msg->FindKey( "DmeSpriteEmitter", true );
	pEmitter->SetInt( "count", 9 );
	pEmitter->SetFloat( "duration", 0 );
	pEmitter->SetString( "material", "effects/muzzleflash2" ); // FIXME - create DmeMultiMaterialSpriteEmitter to support the 4 materials of muzzleflash
	pEmitter->SetInt( "active", true );

	KeyValues *pInitializers = pEmitter->FindKey( "initializers", true );

	KeyValues *pPosition = pInitializers->FindKey( "DmeLinearAttachedPositionInitializer", true );
	pPosition->SetPtr( "entindex", (void*)pEnt->entindex() );
	pPosition->SetInt( "attachmentIndex", attachmentIndex );
	pPosition->SetFloat( "linearOffsetX", 2.0f * scale );

	// TODO - create a DmeConstantLifetimeInitializer
	KeyValues *pLifetime = pInitializers->FindKey( "DmeRandomLifetimeInitializer", true );
	pLifetime->SetFloat( "minLifetime", bOneFrame ? 1.0f / 24.0f : 0.1f );
	pLifetime->SetFloat( "maxLifetime", bOneFrame ? 1.0f / 24.0f : 0.1f );

	KeyValues *pVelocity = pInitializers->FindKey( "DmeConstantVelocityInitializer", true );
	pVelocity->SetFloat( "velocityX", 0.0f );
	pVelocity->SetFloat( "velocityY", 0.0f );
	pVelocity->SetFloat( "velocityZ", 0.0f );

	KeyValues *pRoll = pInitializers->FindKey( "DmeRandomRollInitializer", true );
	pRoll->SetFloat( "minRoll", 0.0f );
	pRoll->SetFloat( "maxRoll", 360.0f );

	// TODO - create a DmeConstantRollSpeedInitializer
	KeyValues *pRollSpeed = pInitializers->FindKey( "DmeRandomRollSpeedInitializer", true );
	pRollSpeed->SetFloat( "minRollSpeed", 0.0f );
	pRollSpeed->SetFloat( "maxRollSpeed", 0.0f );

	// TODO - create a DmeConstantColorInitializer
	KeyValues *pColor = pInitializers->FindKey( "DmeRandomInterpolatedColorInitializer", true );
	Color color( pFlashColor ? pFlashColor[ 0 ] : 255, pFlashColor ? pFlashColor[ 1 ] : 255, pFlashColor ? pFlashColor[ 2 ] : 255, 255 );
	pColor->SetColor( "color1", color );
	pColor->SetColor( "color2", color );

	// TODO - create a DmeConstantAlphaInitializer
	KeyValues *pAlpha = pInitializers->FindKey( "DmeRandomAlphaInitializer", true );
	pAlpha->SetInt( "minStartAlpha", 255 );
	pAlpha->SetInt( "maxStartAlpha", 255 );
	pAlpha->SetInt( "minEndAlpha", 128 );
	pAlpha->SetInt( "maxEndAlpha", 128 );

	// size = rand(6..9) * indexed(12/9..4/9) * flScale = rand(6..9) * ( 4f + f * i )
	KeyValues *pSize = pInitializers->FindKey( "DmeMuzzleFlashSizeInitializer", true );
	float f = flScale / 9.0f;
	pSize->SetFloat( "indexedBase", 4.0f * f );
	pSize->SetFloat( "indexedDelta", f );
	pSize->SetFloat( "minRandomFactor", 6.0f );
	pSize->SetFloat( "maxRandomFactor", 9.0f );

/*
	KeyValues *pUpdaters = pEmitter->FindKey( "updaters", true );

	pUpdaters->FindKey( "DmePositionVelocityUpdater", true );
	pUpdaters->FindKey( "DmeRollUpdater", true );
	pUpdaters->FindKey( "DmeAlphaLinearUpdater", true );
	pUpdaters->FindKey( "DmeSizeUpdater", true );
*/
	ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
	msg->deleteThis();
}