126 lines
3.3 KiB
C#
126 lines
3.3 KiB
C#
using CharacterSystem;
|
|
using PrizeSystem;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class CharacterSelectionPanel : MonoBehaviour, IControlable
|
|
{
|
|
public event Action<Character> OnCharacterSelected;
|
|
public event Action OnExitRequested;
|
|
|
|
[SerializeField] TextMeshProUGUI CharacterNameLabel;
|
|
[SerializeField] Image CharacterIcon;
|
|
[SerializeField] AudioClip ClickSound;
|
|
|
|
[SerializeField] RectTransform _prizeTable;
|
|
[SerializeField] PrizeView _prizeViewPrefab;
|
|
|
|
|
|
private Character[] characters;
|
|
private int characterIndex = 0;
|
|
|
|
[SerializeField] private List<MSignalButton> buttons;
|
|
[SerializeField] private MSignalButton _selectButton;
|
|
[SerializeField] private MSignalButton _exitButton;
|
|
|
|
private int buttonIndex = 0;
|
|
|
|
public int CharactersCount => characters.Length;
|
|
public Character SelectedCharacter => characters[characterIndex];
|
|
|
|
public bool ControlsEnabled { get; set; }
|
|
private MSignalButton SelectedButton => buttons[buttonIndex];
|
|
|
|
private void Update()
|
|
{
|
|
UpdateControls();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_selectButton.OnClick += _selectButton_OnClick;
|
|
_exitButton.OnClick += _exitButton_OnClick;
|
|
|
|
characters = CharactersController.GetCharactersArray();
|
|
characterIndex = 0;
|
|
DisplaySelectedCharacter();
|
|
|
|
buttonIndex = 0;
|
|
SelectedButton.SelectAnimation();
|
|
DisplaySelectedCharacter();
|
|
}
|
|
|
|
|
|
private void _selectButton_OnClick()
|
|
{
|
|
OnCharacterSelected?.Invoke(SelectedCharacter);
|
|
}
|
|
|
|
private void _exitButton_OnClick()
|
|
{
|
|
OnExitRequested?.Invoke();
|
|
}
|
|
|
|
public void SwitchCharacter(int direction)
|
|
{
|
|
if (direction == 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();
|
|
}
|
|
|
|
private void DisplaySelectedCharacter()
|
|
{
|
|
CharacterNameLabel.text = SelectedCharacter.characterName;
|
|
CharacterIcon.sprite = SelectedCharacter.visualModule.icon;
|
|
|
|
ClearTable();
|
|
|
|
var set = PrizeSystemManager.PrizeContainer.Sets[SelectedCharacter.CharacterIndex];
|
|
DisplayPrizeSet(set);
|
|
}
|
|
|
|
public void UpdateControls()
|
|
{
|
|
int dir_h = In.GetAxis(Axis.Horizontal, JoystickSide.Both);
|
|
SwitchCharacter(dir_h);
|
|
|
|
int dir_v = -In.GetAxis(Axis.Vertical, JoystickSide.Both);
|
|
SwitchButton(dir_v);
|
|
|
|
SelectedButton.TakeInput(In.GetButtonAnyPlayer(PlayerAction.action));
|
|
}
|
|
|
|
private void ClearTable()
|
|
{
|
|
for (int i = _prizeTable.childCount - 1; i >= 0; i--)
|
|
{
|
|
var go = _prizeTable.GetChild(i).gameObject;
|
|
Destroy(go);
|
|
}
|
|
}
|
|
|
|
private void DisplayPrizeSet(PrizeSet prizeSet)
|
|
{
|
|
foreach (var item in prizeSet.Prizes)
|
|
{
|
|
var view = Instantiate(_prizeViewPrefab, _prizeTable);
|
|
view.SetPrize(item);
|
|
}
|
|
}
|
|
} |