Files

146 lines
4.3 KiB
C#
Raw Permalink Normal View History

2023-09-13 15:09:50 +03:00
using System.Collections;
2022-08-18 19:07:10 +03:00
using TMPro;
2021-09-11 22:46:46 +02:00
using UnityEngine;
2022-08-11 14:44:52 +03:00
using UnityEngine.InputSystem;
2022-08-09 16:02:14 +03:00
using static UnityEngine.InputSystem.InputAction;
2021-09-11 22:46:46 +02:00
public class PlayerIcon : MonoBehaviour
{
2022-10-07 17:31:42 +03:00
[SerializeField] private PlayersMenu _playersMenu = default;
[SerializeField] private PlayerInput _playerInput = default;
[SerializeField] private TextMeshProUGUI _controllerText = default;
2023-09-22 16:54:55 +03:00
[SerializeField] private TextMeshProUGUI _numberText = default;
[SerializeField] private GameObject _leftArrow = default;
[SerializeField] private GameObject _rightArrow = default;
[SerializeField] private Color[] _colors = default;
2022-10-07 17:31:42 +03:00
private RectTransform _rectTransform;
private Audio _audio;
2023-09-13 15:09:50 +03:00
private GameObject _cpuText;
2022-10-07 17:31:42 +03:00
private bool _isMovenentInUse;
2023-09-22 16:54:55 +03:00
private int _number;
private static int LastNumber;
2022-10-07 17:31:42 +03:00
public PlayerInput PlayerInput { get { return _playerInput; } private set { } }
2022-08-11 19:28:14 +03:00
2021-09-11 22:46:46 +02:00
2022-10-07 17:31:42 +03:00
private void Awake()
{
_audio = GetComponent<Audio>();
_rectTransform = GetComponent<RectTransform>();
}
2021-09-11 22:46:46 +02:00
2022-10-07 17:31:42 +03:00
public void SetController()
{
gameObject.SetActive(true);
if (_playerInput.devices.Count > 0)
{
2023-09-22 16:54:55 +03:00
if (_number == 0)
{
LastNumber++;
_number = LastNumber;
_numberText.text = _number.ToString();
_numberText.color = _colors[_number - 1];
}
2022-10-07 17:31:42 +03:00
if (_playerInput.devices[0].displayName == "Keyboard")
_controllerText.text = "Keyboard";
else
_controllerText.text = "Controller";
}
else
gameObject.SetActive(false);
}
2022-08-18 20:04:56 +03:00
2022-10-07 17:31:42 +03:00
public void Movement(CallbackContext callbackContext)
{
if (gameObject.activeInHierarchy)
{
float movement = callbackContext.ReadValue<Vector2>().x;
if (movement != 0.0f)
if (!_isMovenentInUse)
{
_isMovenentInUse = true;
if (movement > 0.0f)
{
2023-09-22 16:54:55 +03:00
_audio.Sound("Selected").Play();
2023-09-13 15:09:50 +03:00
if (_rectTransform.parent == _playersMenu.PlayerGroups[0])
2022-10-07 17:31:42 +03:00
Center();
2023-09-22 16:54:55 +03:00
else
2023-08-29 17:42:32 +03:00
MoveRight();
2022-10-07 17:31:42 +03:00
}
else if (movement < 0.0f)
{
2023-09-22 16:54:55 +03:00
_audio.Sound("Selected").Play();
2023-09-13 15:09:50 +03:00
if (_rectTransform.parent == _playersMenu.PlayerGroups[2])
2022-10-07 17:31:42 +03:00
Center();
2023-09-22 16:54:55 +03:00
else
2023-08-29 17:42:32 +03:00
MoveLeft();
2022-10-07 17:31:42 +03:00
}
}
if (movement == 0.0f)
_isMovenentInUse = false;
}
}
2022-08-15 01:58:40 +03:00
2023-08-29 17:42:32 +03:00
public void MoveRight()
{
2023-09-22 16:54:55 +03:00
if (_playersMenu.PlayerGroups[2].childCount > 0)
return;
_rightArrow.SetActive(false);
2023-08-29 17:42:32 +03:00
_playersMenu.CpuTextRight.SetActive(false);
2023-09-13 15:09:50 +03:00
_cpuText = _playersMenu.CpuTextRight;
2023-09-10 12:07:02 +03:00
_rectTransform.SetParent(_playersMenu.PlayerGroups[2]);
2023-08-29 17:42:32 +03:00
}
public void MoveLeft()
{
2023-09-22 16:54:55 +03:00
if (_playersMenu.PlayerGroups[0].childCount > 0)
return;
_leftArrow.gameObject.SetActive(false);
2023-08-29 17:42:32 +03:00
_playersMenu.CpuTextLeft.SetActive(false);
2023-09-13 15:09:50 +03:00
_cpuText = _playersMenu.CpuTextLeft;
2023-09-10 12:07:02 +03:00
_rectTransform.SetParent(_playersMenu.PlayerGroups[0]);
2023-08-29 17:42:32 +03:00
}
2023-09-22 16:54:55 +03:00
public void ConfirmQuickAssign()
2022-10-07 17:31:42 +03:00
{
2023-09-22 16:54:55 +03:00
_playersMenu.CpuTextLeft.SetActive(false);
_audio.Sound("Selected").Play();
_rectTransform.SetParent(_playersMenu.PlayerGroups[0]);
_cpuText = _playersMenu.CpuTextLeft;
2022-10-07 17:31:42 +03:00
}
2022-07-12 23:24:00 +03:00
2022-10-07 17:31:42 +03:00
public void Center()
{
if (gameObject.activeSelf)
{
2023-09-13 15:09:50 +03:00
if (_cpuText != null)
{
_cpuText.SetActive(true);
_cpuText = null;
}
2023-09-22 16:54:55 +03:00
_leftArrow.SetActive(true);
_rightArrow.SetActive(true);
2023-09-10 12:07:02 +03:00
_rectTransform.SetParent(_playersMenu.PlayerGroups[1]);
2023-09-22 16:54:55 +03:00
_rectTransform.SetSiblingIndex(_number - 1);
2022-10-07 17:31:42 +03:00
}
}
2023-09-22 16:54:55 +03:00
public void CheckCPU()
{
if (_cpuText != null)
{
_cpuText.SetActive(true);
_cpuText = null;
}
}
2023-12-05 04:01:01 +02:00
private void OnDisable()
{
LastNumber = 0;
}
2023-09-22 16:54:55 +03:00
private void OnEnable()
{
2023-10-03 13:38:22 +03:00
Center();
2023-09-22 16:54:55 +03:00
}
2023-10-03 13:38:22 +03:00
}