95 lines
2.9 KiB
C#
95 lines
2.9 KiB
C#
using System;
|
|
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
|
using NaughtyAttributes;
|
|
using UnityEngine;
|
|
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);
|
|
|
|
[SelectionBase]
|
|
public class PuzzleState : ModuleState
|
|
{
|
|
public event PuzzleEventHandler PuzzleEvent;
|
|
|
|
protected PuzzleModule Module { get; private 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;
|
|
|
|
protected virtual void Start()
|
|
{
|
|
OnPuzzleEvent(PuzzleEventType.Restarted);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
#region Debug Buttons
|
|
|
|
[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);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
public static PuzzleState FromState(ModuleState state)
|
|
{
|
|
if (state is PuzzleState puzzleState)
|
|
{
|
|
return puzzleState;
|
|
}
|
|
|
|
throw new WrongTypeException(typeof(PuzzleState), state.GetType(), typeof(ModuleState));
|
|
}
|
|
}
|
|
} |