RedLightPlayer.cs
using Sandbox;
public sealed class RedLightPlayer : Component
{
public enum PlayerState
{
Playing,
GameOver,
Win
}
[Property]
public RedLightGreenLight GameController { get; set; }
[Property]
public SpawnPoint SpawnPoint { get; set; }
public PlayerState State { get; set; } = PlayerState.Playing;
Vector3 previousPosition;
protected override void OnStart()
{
previousPosition = WorldPosition;
}
protected override void OnUpdate()
{
if ( State == PlayerState.GameOver )
{
previousPosition = WorldPosition;
return;
}
if ( State != PlayerState.Playing )
return;
if ( GameController.State == RedLightGreenLight.GameState.Red && State != PlayerState.Win )
{
if ( WorldPosition != previousPosition )
{
Log.Info( "PLAYER MOVED!" );
State = PlayerState.GameOver;
WorldPosition = SpawnPoint.WorldPosition;
}
}
previousPosition = WorldPosition;
}
}