LightningParticleEffect.cs

A client-side particle effect component that controls a lightning-style effect. It exposes ParticleEffect, ParticleSpriteRenderer, and ParticleRingEmitter properties, handles a timed charge/delay cycle, and provides RPCs to reset the effect and toggle visibility.

using System;
using Sandbox;

public class LightningParticleEffect : Component
{
	[Property] public ParticleEffect ParticleEffect { get; set; }
	[Property] public ParticleSpriteRenderer ParticleRenderer { get; set; }
	[Property] public ParticleRingEmitter RingEmitter { get; set; }

	private const float CHARGE_TIME = 3f;
	private const float DELAY = 0.5f;

	private TimeSince _timeSinceReset;

	protected override void OnStart()
	{
		base.OnStart();

		_timeSinceReset = 0f;
	}

	[Rpc.Broadcast]
	public void ResetEffect()
	{
		if ( !IsProxy ) // only for client effect
			return;

		if ( !ParticleRenderer.Enabled )
			return;

		_timeSinceReset = 0f;

		RingEmitter.Rate = 500;
		RingEmitter.Radius = 30f;
		ParticleEffect.Alpha = 0f;
	}

	protected override void OnUpdate()
	{
		if ( !IsProxy ) // only for client effect
			return;

		if ( _timeSinceReset < DELAY )
			return;

		float progress = Utils.Map( _timeSinceReset, 0f, DELAY + CHARGE_TIME, 0f, 1f );

		RingEmitter.Rate = 500;
		RingEmitter.Radius = Utils.Map( progress, 0f, 1f, 30f, 0f, EasingType.SineIn );
		ParticleEffect.Alpha = Utils.Map( progress, 0f, 1f, 0f, 1f, EasingType.ExpoIn );
	}

	[Rpc.Broadcast]
	public void SetVisible( bool visible )
	{
		ParticleRenderer.Enabled = visible;
	}
}