A component for an arena floor tile. It stores the tile's initial position, can be told to fall at a configurable speed, highlights itself by tinting the ModelRenderer, and can be reset back to its initial position.
using Sandbox;
public sealed class ArenaTile : Component
{
private Vector3 initialPosition;
public bool IsFalling { get; private set; } = false;
[Property] public float FallSpeed { get; set; } = 500f;
private ModelRenderer modelRenderer;
private Color originalColor = Color.White;
protected override void OnStart()
{
initialPosition = WorldPosition;
modelRenderer = Components.Get<ModelRenderer>();
if ( modelRenderer.IsValid() )
{
originalColor = modelRenderer.Tint;
}
}
public void HighlightWarning( bool enable )
{
if ( !modelRenderer.IsValid() ) return;
modelRenderer.Tint = enable ? Color.Red : originalColor;
}
public void Drop()
{
IsFalling = true;
}
protected override void OnUpdate()
{
if ( IsFalling )
{
WorldPosition += Vector3.Down * FallSpeed * Time.Delta;
}
}
public void ResetTile()
{
IsFalling = false;
WorldPosition = initialPosition;
HighlightWarning( false );
}
}