Abstract component representing a timed effect attached to a Paddle. It finds the Paddle in its component hierarchy, calls OnApply when started, counts down Duration with a TimeUntil, and calls OnRemoved when destroyed or expired.
using Sandbox;
namespace Breakout;
/// <summary>
/// Base class for a timed effect living on the paddle (such as the wide-paddle boost). It runs
/// OnApply when added, then removes itself after Duration seconds, calling OnRemoved on its way out.
/// </summary>
public abstract class PaddleEffect : Component
{
[Property] public float Duration { get; set; } = 8f;
protected Paddle Paddle { get; private set; }
private TimeUntil expires;
protected override void OnStart()
{
Paddle = Components.Get<Paddle>( FindMode.EverythingInSelfAndAncestors );
if ( Paddle is null )
{
Destroy();
return;
}
expires = Duration;
OnApply();
}
protected override void OnUpdate()
{
if ( Duration > 0f && expires )
Destroy();
}
protected override void OnDestroy() => OnRemoved();
/// <summary>
/// Called once when the effect is added. Override to start the effect.
/// </summary>
protected virtual void OnApply() { }
/// <summary>
/// Called once when the effect expires or is removed. Override to undo the effect.
/// </summary>
protected virtual void OnRemoved() { }
}