using CharacterSystem; using PrizeSystem; using System; using UnityEngine; public class CharacterPrizeConfigurator : MonoBehaviour, IControlable { public event Action OnClosed; [SerializeField] private CharacterSelectionPanel _characterSelection; [SerializeField] private PrizesSelector _prizesSelector; private IControlable _activePanel; public bool ControlsEnabled { get; set; } = true; private void Awake() { _characterSelection.OnCharacterSelected += OpenPrizeSelector; _characterSelection.OnExitRequested += SaveAndClose; _prizesSelector.OnConfirmed += OpenCharacterSelection; } private void OnEnable() { OpenCharacterSelection(); } private void Start() { OpenCharacterSelection(); } private void OnDestroy() { _characterSelection.OnCharacterSelected -= OpenPrizeSelector; _characterSelection.OnExitRequested -= SaveAndClose; _prizesSelector.OnConfirmed -= OpenCharacterSelection; } public void Open() { if (gameObject.activeSelf) { OpenCharacterSelection(); return; } gameObject.SetActive(true); } public void UpdateControls() { if (_activePanel != null && _activePanel.ControlsEnabled) _activePanel.UpdateControls(); } private void OpenPrizeSelector(CharacterDefinition character) { _characterSelection.gameObject.SetActive(false); _prizesSelector.Open(character); _activePanel = _prizesSelector; SetSelfAsControlable(); } private void OpenCharacterSelection() { _prizesSelector.gameObject.SetActive(false); _characterSelection.gameObject.SetActive(true); _characterSelection.Open(); _activePanel = _characterSelection; SetSelfAsControlable(); } private void SaveAndClose() { PrizeSerializer.SaveToFile(); PrizeSystemManager.SavePrizes(); if (ControlsPerformer.Instance != null) ControlsPerformer.Instance.SetControlable(null); gameObject.SetActive(false); OnClosed?.Invoke(); } private void SetSelfAsControlable() { if (ControlsPerformer.Instance != null) ControlsPerformer.Instance.SetControlable(this); } }