71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
|
using NaughtyAttributes;
|
|
|
|
namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|
{
|
|
public enum DoorEventType
|
|
{
|
|
Locked, Unlocked, Connected, ExitedFrom
|
|
}
|
|
|
|
public delegate void DoorEventHandler(DoorModule source, DoorEventType e);
|
|
|
|
public class DoorState : ModuleState
|
|
{
|
|
public event DoorEventHandler DoorEvent;
|
|
|
|
protected DoorModule Module { get; set; }
|
|
public bool Unlocked
|
|
{
|
|
get => _unlocked;
|
|
private set
|
|
{
|
|
var type =
|
|
!_unlocked && value ? Some<DoorEventType>.Of(DoorEventType.Unlocked)
|
|
: _unlocked && !value ? Some<DoorEventType>.Of(DoorEventType.Locked)
|
|
: None<DoorEventType>.New();
|
|
_unlocked = value;
|
|
type.Match(some: OnDoorEvent);
|
|
}
|
|
}
|
|
|
|
private bool _unlocked;
|
|
|
|
protected void OnDoorEvent(DoorEventType type)
|
|
{
|
|
Logger.Log($"{Module} has been {type}", LogType.PuzzleFlow);
|
|
|
|
DoorEvent?.Invoke(Module, type);
|
|
}
|
|
|
|
public override void SetModule(Module module)
|
|
{
|
|
Module = DoorModule.FromModule(module);
|
|
}
|
|
|
|
public void Connect() => OnDoorEvent(DoorEventType.Connected);
|
|
|
|
#region Debug Buttons
|
|
|
|
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
|
internal void Unlock() => Unlocked = true;
|
|
|
|
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
|
internal void Lock() => Unlocked = false;
|
|
|
|
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
|
internal void ExitFrom() => OnDoorEvent(DoorEventType.ExitedFrom);
|
|
|
|
#endregion
|
|
|
|
public static DoorState FromState(ModuleState state)
|
|
{
|
|
if (state is DoorState doorState)
|
|
{
|
|
return doorState;
|
|
}
|
|
|
|
throw new WrongTypeException(typeof(DoorState), state.GetType(), typeof(ModuleState));
|
|
}
|
|
}
|
|
} |