refactor modules
This commit is contained in:
112
Assets/Station46/Modules/Hexagon/Scripts/Hexagon.cs
Normal file
112
Assets/Station46/Modules/Hexagon/Scripts/Hexagon.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using EscapeRoomEngine.Engine.Runtime.Modules.State;
|
||||
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
||||
using JetBrains.Annotations;
|
||||
using NaughtyAttributes;
|
||||
using Station46.Runtime.Puzzle_A;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Station46.Runtime.Puzzle_B
|
||||
{
|
||||
[Serializable]
|
||||
internal struct ButtonAction
|
||||
{
|
||||
public SymbolButton button;
|
||||
public List<int> stateIndices;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The main component for the hexagon module.
|
||||
/// </summary>
|
||||
public class Hexagon : StatePuzzle
|
||||
{
|
||||
[Tooltip("How many seconds the buttons should be disabled until another input is accepted.")]
|
||||
[SerializeField] [Min(0)]
|
||||
private float buttonCooldown;
|
||||
[ValidateInput("CorrectRotatorCount")]
|
||||
public List<Rotator> rotators;
|
||||
[InfoBox("Every button action will toggle the states at the given indices between 0 and 90 degrees.")]
|
||||
[SerializeField]
|
||||
private List<ButtonAction> buttonActions;
|
||||
[BoxGroup("Internal")] [SerializeField]
|
||||
private List<Emission> lights;
|
||||
[BoxGroup("Internal")] [SerializeField]
|
||||
private Crystal crystal;
|
||||
|
||||
private float _previousPress = -1;
|
||||
|
||||
protected override void Start()
|
||||
{
|
||||
base.Start();
|
||||
|
||||
PuzzleEvent += (_, type) =>
|
||||
{
|
||||
// ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
|
||||
switch (type)
|
||||
{
|
||||
case PuzzleEventType.Restarted:
|
||||
var color = EscapeRoomEngine.Engine.Runtime.Engine.Theme.puzzleColor;
|
||||
crystal.Color = color;
|
||||
lights.ForEach(emission => emission.color = color.hdr);
|
||||
rotators.ForEach(rotator => rotator.Emission.color = color.hdr);
|
||||
buttonActions.ForEach(action => action.button.Enable());
|
||||
break;
|
||||
case PuzzleEventType.Solved:
|
||||
color = EscapeRoomEngine.Engine.Runtime.Engine.Theme.solvedColor;
|
||||
crystal.Color = color;
|
||||
lights.ForEach(emission => emission.color = color.hdr);
|
||||
rotators.ForEach(rotator => rotator.Emission.color = color.hdr);
|
||||
buttonActions.ForEach(action => action.button.Disable());
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
foreach (var buttonAction in buttonActions)
|
||||
{
|
||||
buttonAction.button.ButtonEvent += (_, type) =>
|
||||
{
|
||||
if (type == ButtonEventType.Pressed && _previousPress + buttonCooldown <= Time.time)
|
||||
{
|
||||
Switch(buttonAction);
|
||||
|
||||
_previousPress = Time.time;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
crystal.Active = true;
|
||||
lights.ForEach(emission => emission.active = true);
|
||||
rotators.ForEach(rotator => rotator.Emission.active = true);
|
||||
}
|
||||
|
||||
protected override void SetState(int index, int value, bool checkSolution)
|
||||
{
|
||||
base.SetState(index, value, checkSolution);
|
||||
|
||||
for (var i = 0; i < stateCount; i++)
|
||||
{
|
||||
rotators[i].angle = states[i];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Switch all rotators specified by a given action.
|
||||
/// </summary>
|
||||
private void Switch(ButtonAction action)
|
||||
{
|
||||
for (var i = 0; i < action.stateIndices.Count; i++)
|
||||
{
|
||||
var stateIndex = action.stateIndices[i];
|
||||
var current = OrientationExtensions.FromAngle(states[stateIndex]);
|
||||
current = current == Orientation.North ? current.Rotated(1) : current.Rotated(-1);
|
||||
|
||||
// set state but only check solution on the last change
|
||||
SetState(stateIndex, current.AngleInt(), i == action.stateIndices.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
[UsedImplicitly] // used for field validation
|
||||
private bool CorrectRotatorCount(List<Rotator> list) => list != null && list.Count == stateCount;
|
||||
}
|
||||
}
|
||||
3
Assets/Station46/Modules/Hexagon/Scripts/Hexagon.cs.meta
Normal file
3
Assets/Station46/Modules/Hexagon/Scripts/Hexagon.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8b80d4bcd2f6465189d94b993e13a034
|
||||
timeCreated: 1669688216
|
||||
36
Assets/Station46/Modules/Hexagon/Scripts/Rotator.cs
Normal file
36
Assets/Station46/Modules/Hexagon/Scripts/Rotator.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Station46.Runtime.Puzzle_B
|
||||
{
|
||||
/// <summary>
|
||||
/// The rotator component used by the hexagon module.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Emission))]
|
||||
public class Rotator : MonoBehaviour
|
||||
{
|
||||
[Tooltip("The angle this rotator should rotate towards.")]
|
||||
[Range(0, 359)]
|
||||
public int angle;
|
||||
[Tooltip("How quickly to rotate towards the target angle.")]
|
||||
[Range(0, 1)]
|
||||
public float speed;
|
||||
|
||||
public Emission Emission { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Emission = GetComponent<Emission>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
angle %= 360;
|
||||
var angleDifference = angle - transform.localEulerAngles.y;
|
||||
if (Math.Abs(angleDifference) >= 0.1)
|
||||
{
|
||||
transform.Rotate(0, angleDifference*Mathf.Pow(speed, 2), 0, Space.Self);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Station46/Modules/Hexagon/Scripts/Rotator.cs.meta
Normal file
11
Assets/Station46/Modules/Hexagon/Scripts/Rotator.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c4596ccc5a8dcfb4294f3932f43e1847
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user