139 lines
4.0 KiB
C#
139 lines
4.0 KiB
C#
using CharacterSystem;
|
|
using PrizeSystem;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
using UnityEngine.UI;
|
|
|
|
public class CharacterSelectionPanel : MonoBehaviour, IControlable
|
|
{
|
|
public event Action<Character> OnCharacterSelected;
|
|
public event Action OnExitRequested;
|
|
|
|
[SerializeField, FormerlySerializedAs("CharacterNameLabel")] private TextMeshProUGUI _characterNameLabel;
|
|
[SerializeField, FormerlySerializedAs("CharacterIcon")] private Image _characterIcon;
|
|
[SerializeField] private RectTransform _prizeTable;
|
|
[SerializeField] private PrizeView _prizeViewPrefab;
|
|
[SerializeField, FormerlySerializedAs("buttons")] private List<MSignalButton> _buttons;
|
|
[SerializeField] private MSignalButton _selectButton;
|
|
[SerializeField] private MSignalButton _exitButton;
|
|
|
|
private Character[] _characters;
|
|
private int _characterIndex;
|
|
private int _buttonIndex;
|
|
|
|
public int CharactersCount => _characters?.Length ?? 0;
|
|
public Character SelectedCharacter => _characters[_characterIndex];
|
|
public bool ControlsEnabled { get; set; } = true;
|
|
private MSignalButton SelectedButton => _buttons[_buttonIndex];
|
|
|
|
private void Awake()
|
|
{
|
|
_selectButton.OnClick += SelectCharacter;
|
|
_exitButton.OnClick += RequestExit;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_characters = CharactersController.GetCharactersArray();
|
|
Open();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_selectButton.OnClick -= SelectCharacter;
|
|
_exitButton.OnClick -= RequestExit;
|
|
}
|
|
|
|
public void Open()
|
|
{
|
|
if (_buttons == null || _buttons.Count == 0)
|
|
return;
|
|
|
|
SelectedButton.DeselectAnimation();
|
|
_buttonIndex = 0;
|
|
SelectedButton.SelectAnimation();
|
|
|
|
if (_characters == null || _characters.Length == 0)
|
|
return;
|
|
|
|
_characterIndex = Mathf.Clamp(_characterIndex, 0, _characters.Length - 1);
|
|
DisplaySelectedCharacter();
|
|
}
|
|
|
|
public void SwitchCharacter(int direction)
|
|
{
|
|
if (direction == 0 || CharactersCount == 0)
|
|
return;
|
|
|
|
_characterIndex = (_characterIndex + direction + _characters.Length) % _characters.Length;
|
|
DisplaySelectedCharacter();
|
|
}
|
|
|
|
public void SwitchButton(int direction)
|
|
{
|
|
if (direction == 0)
|
|
return;
|
|
|
|
int newIndex = Mathf.Clamp(_buttonIndex + direction, 0, _buttons.Count - 1);
|
|
if (newIndex == _buttonIndex)
|
|
return;
|
|
|
|
SelectedButton.DeselectAnimation();
|
|
_buttonIndex = newIndex;
|
|
SelectedButton.SelectAnimation();
|
|
}
|
|
|
|
public void UpdateControls()
|
|
{
|
|
if (CharactersCount == 0)
|
|
return;
|
|
|
|
Vector2Int axes = In.GetAxis(JoystickSide.Both, InteractMode.Discrete);
|
|
SwitchCharacter(axes.x);
|
|
SwitchButton(-axes.y);
|
|
SelectedButton.TakeInput(In.GetAction(JoystickSide.Both, InteractMode.Continious));
|
|
}
|
|
|
|
private void SelectCharacter()
|
|
{
|
|
OnCharacterSelected?.Invoke(SelectedCharacter);
|
|
}
|
|
|
|
private void RequestExit()
|
|
{
|
|
OnExitRequested?.Invoke();
|
|
}
|
|
|
|
private void DisplaySelectedCharacter()
|
|
{
|
|
_characterNameLabel.text = SelectedCharacter.characterName;
|
|
_characterIcon.sprite = SelectedCharacter.visualModule.icon;
|
|
|
|
ClearTable();
|
|
PrizeSet prizeSet = PrizeSystemManager.PrizeContainer.Sets[SelectedCharacter.CharacterIndex];
|
|
DisplayPrizeSet(prizeSet);
|
|
}
|
|
|
|
private void ClearTable()
|
|
{
|
|
for (int i = _prizeTable.childCount - 1; i >= 0; i--)
|
|
{
|
|
Transform child = _prizeTable.GetChild(i);
|
|
child.SetParent(null, false);
|
|
Destroy(child.gameObject);
|
|
}
|
|
}
|
|
|
|
private void DisplayPrizeSet(PrizeSet prizeSet)
|
|
{
|
|
foreach (Prize prize in prizeSet.Prizes)
|
|
{
|
|
PrizeView view = Instantiate(_prizeViewPrefab, _prizeTable);
|
|
view.SetPrize(prize);
|
|
}
|
|
}
|
|
}
|