Files
modular-vr/Assets/Desert/Runtime/Puzzle A/Terminal.cs
2022-11-21 20:38:48 +01:00

129 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
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.Desert.Runtime.Puzzle_A
{
[RequireComponent(typeof(Animator))]
public class Terminal : CyclicStepPuzzle
{
private static readonly int LightFlash = Animator.StringToHash("Light Flash");
[BoxGroup("Internal")] [Required] public Emission terminalLight;
[BoxGroup("Internal")] public List<SymbolButton> symbols;
private int _activeSymbol, _lastPressedSymbol;
private Animator _animator;
private Ball _ball;
private void Awake()
{
_animator = GetComponent<Animator>();
}
protected override void Start()
{
base.Start();
PuzzleEvent += (_, type) =>
{
switch (type)
{
case PuzzleEventType.Restarted:
_ball.Active = true;
terminalLight.color = theme.puzzleColor;
symbols.ForEach(symbol => symbol.Enable());
break;
case PuzzleEventType.Solved:
_ball.Active = false;
terminalLight.color = theme.solvedColor;
symbols.ForEach(symbol => symbol.Disable());
break;
case PuzzleEventType.WrongInput:
_animator.SetTrigger(LightFlash);
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
};
symbols.ForEach(symbol =>
{
symbol.ButtonEvent += (_, type) =>
{
if (type == ButtonEventType.Pressed)
{
PressedSymbol(symbol.symbolNumber);
}
};
});
_ball.ring.symbols.ForEach(symbol =>
{
symbol.SymbolEvent += type =>
_activeSymbol = type == SymbolEventType.Activated ? symbol.symbolNumber : -1;
});
}
public override void SetModule(Module module)
{
base.SetModule(module);
var firstRelatedModule = Module.relatedModules[0];
if (firstRelatedModule.State is Ball ball)
{
_ball = ball;
}
else
{
throw new EngineException("Terminal was not assigned a related Ball.");
}
TurnOnLights();
}
// required in animation
internal void TurnOnLights()
{
terminalLight.active = true;
_ball.StatusLight = true;
}
// required in animation
internal void TurnOffLights()
{
terminalLight.active = false;
_ball.StatusLight = false;
}
internal void PressedSymbol(int number)
{
if (!Solved)
{
Logger.Log($"Pressed symbol {number} on {this}", LogType.PuzzleDetail);
if (number == _activeSymbol)
{
CheckStep(number);
}
else
{
WrongInput();
}
}
}
#region Debug Buttons
[Button(enabledMode: EButtonEnableMode.Playmode)] public void PressSymbol0() => PressedSymbol(0);
[Button(enabledMode: EButtonEnableMode.Playmode)] public void PressSymbol1() => PressedSymbol(1);
[Button(enabledMode: EButtonEnableMode.Playmode)] public void PressSymbol2() => PressedSymbol(2);
#endregion
}
}