Files
modular-vr/Assets/Engine/Runtime/Modules/PuzzleState.cs

92 lines
2.7 KiB
C#

using System;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using NaughtyAttributes;
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;
namespace EscapeRoomEngine.Engine.Runtime.Modules
{
public enum PuzzleEventType
{
Restarted, Solved, WrongInput
}
public static class PuzzleEventExtensions
{
public static string Description(this PuzzleEventType type, PuzzleModule module)
{
return type switch
{
PuzzleEventType.Restarted => $"{module} has been restarted",
PuzzleEventType.Solved => $"{module} has been solved",
PuzzleEventType.WrongInput => $"Wrong input for {module}",
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
}
}
public delegate void PuzzleEventHandler(PuzzleModule source, PuzzleEventType e);
public class PuzzleState : ModuleState
{
public event PuzzleEventHandler PuzzleEvent;
public EngineTheme theme;
private new PuzzleModule Module { get; set; }
public bool Solved
{
get => _solved;
private set
{
var type =
!_solved && value ? Some<PuzzleEventType>.Of(PuzzleEventType.Solved)
: _solved && !value ? Some<PuzzleEventType>.Of(PuzzleEventType.Restarted)
: None<PuzzleEventType>.New();
_solved = value;
type.Match(some: OnPuzzleEvent);
}
}
private bool _solved;
private void OnPuzzleEvent(PuzzleEventType type)
{
Logger.Log(type.Description(Module), LogType.PuzzleFlow);
PuzzleEvent?.Invoke(Module, type);
}
public override void SetModule(Module module)
{
if (module is PuzzleModule puzzleModule)
{
Module = puzzleModule;
}
else
{
throw new Exception($"Tried to set wrong type of module ({module.GetType()} instead of PuzzleModule)");
}
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
public void Solve()
{
Solved = true;
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
public void Restart()
{
Solved = false;
}
[Button("Trigger Wrong Input", EButtonEnableMode.Playmode)]
public void WrongInput()
{
if (!Solved)
{
OnPuzzleEvent(PuzzleEventType.WrongInput);
}
}
}
}