using System; using System.Runtime.CompilerServices; 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; protected PuzzleModule Module { get; private set; } public bool Solved { get => _solved; private set { var type = !_solved && value ? Some.Of(PuzzleEventType.Solved) : _solved && !value ? Some.Of(PuzzleEventType.Restarted) : None.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) { Module = PuzzleModule.FromModule(module); } [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); } } public static PuzzleState FromState(ModuleState state) { if (state is PuzzleState puzzleState) { return puzzleState; } throw new WrongTypeException(typeof(PuzzleState), state.GetType(), typeof(ModuleState)); } } }