using EscapeRoomEngine.Engine.Runtime.Modules; using EscapeRoomEngine.Engine.Runtime.Modules.State; using EscapeRoomEngine.Engine.Runtime.Utilities; using NaughtyAttributes; using Station46.Modules.Dispenser.Scripts; using Station46.Scripts; using UnityEngine; using UnityEngine.XR.Interaction.Toolkit; namespace Station46.Modules.Steles.Scripts { public class AlignSteles : StatePuzzle { private static readonly int SlotIn = Animator.StringToHash("Slot In"); public float accuracy = 0.1f; public float sensitivity = 0.001f; public Vector3 movementScaling = Vector3.one; [BoxGroup("Internal")] [SerializeField] private Transform target; [BoxGroup("Internal")] [SerializeField] private AlignStele steleX, steleY, steleZ; [BoxGroup("Internal")] [SerializeField] private Animator animator; private DispenserOrb _orb; private Vector3 _previousPosition; protected override void Start() { base.Start(); PuzzleEvent += (_, type) => { // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault switch (type) { case PuzzleEventType.Restarted: _orb.GetComponent().enabled = true; _orb.Color = EscapeRoomEngine.Engine.Runtime.Engine.Theme.puzzleColor.hdr; break; case PuzzleEventType.Solved: _orb.transform.position = target.position; steleX.Displacement = 0; steleY.Displacement = 0; steleZ.Displacement = 0; _orb.Color = EscapeRoomEngine.Engine.Runtime.Engine.Theme.solvedColor.hdr; _orb.GetComponent().enabled = false; animator.SetTrigger(SlotIn); break; } }; } private void Update() { if (!Solved) { var position = _orb.transform.position - target.position; steleX.Displacement = position.x * movementScaling.x; steleY.Displacement = position.y * movementScaling.y; steleZ.Displacement = position.z * movementScaling.z; SetState(0, Mathf.RoundToInt(steleX.Displacement / accuracy), false); SetState(1, Mathf.RoundToInt(steleY.Displacement / accuracy), false); SetState(2, Mathf.RoundToInt(steleZ.Displacement / accuracy), true); if (_orb.Color != EscapeRoomEngine.Engine.Runtime.Engine.Theme.solvedColor.hdr) { _orb.Color = (position - _previousPosition).magnitude < sensitivity ? EscapeRoomEngine.Engine.Runtime.Engine.Theme.puzzleColor.hdr : EscapeRoomEngine.Engine.Runtime.Engine.Theme.activeColor.hdr; _previousPosition = position; } } } public override void SetModule(Module module) { base.SetModule(module); // The holes require a related dispenser module var firstRelatedModule = Module.relatedModules[0]; if (firstRelatedModule.State is DispenserOrbGroup orbGroup) { _orb = orbGroup.GetComponentInChildren(); } else { throw new EngineException("Align steles was not assigned a related dispenser orb."); } } } }