RizzeBattleRoyale/Assets/TPSBR/Scripts/UI/MenuViews/UIAgentSelectionView.cs
2025-10-18 03:26:16 +05:00

276 lines
10 KiB
C#

using System;
using TMPro;
using UnityEngine;
using Cinemachine;
using System.Collections;
namespace TPSBR.UI
{
public class UIAgentSelectionView : UICloseView
{
[SerializeField] private CinemachineVirtualCamera _camera;
[SerializeField] private UIList _agentList;
[SerializeField] private UIButton _selectButton;
[SerializeField] private TextMeshProUGUI _agentName;
[SerializeField] private TextMeshProUGUI _agentDescription;
[SerializeField] private string _agentNameFormat = "{0}";
[SerializeField] private UIBehaviour _selectedAgentGroup;
[SerializeField] private UIBehaviour _selectedEffect;
[SerializeField] private AudioSetup _selectedSound;
[SerializeField] private float _closeDelayAfterSelection = 0.5f;
private string _previewAgent;
private const string ShopKey = "Characters";
protected override void OnInitialize()
{
base.OnInitialize();
_agentList.SelectionChanged += OnSelectionChanged;
_agentList.UpdateContent += OnListUpdateContent;
_selectButton.onClick.AddListener(OnSelectButton);
}
private void OnListUpdateContent(int index, MonoBehaviour content)
{
StartCoroutine(OnListUpdateContentRoutine(index,content));
}
IEnumerator OnListUpdateContentRoutine(int index, MonoBehaviour content)
{
yield return new WaitForSeconds(0.2f);
var behaviour = content as UIBehaviour;
var setup = Context.Settings.Agent.Agents[index];
behaviour.Image.sprite = setup.Icon;
bool owned = index == 0 || ShopUnlocks.IsUnlocked(setup.ID, ShopKey);
var item = content.GetComponentInParent<UIListItem>();
if (item != null) item.SetLocked(!owned); // lockGO active when NOT bought
}
protected override void OnOpen()
{
base.OnOpen();
_camera.enabled = true;
_selectedEffect.SetActive(false);
var agents = Context.Settings.Agent.Agents;
if (agents != null && agents.Length > 0)
ShopUnlocks.EnsureUnlocked(agents[0].ID, ShopKey); // first is free
_previewAgent = Context.PlayerData.AgentID;
_agentList.Refresh(agents.Length, false);
UpdateAgent();
}
protected override void OnClose()
{
_camera.enabled = false;
Context.PlayerPreview.ShowAgent(Context.PlayerData.AgentID);
base.OnClose();
}
protected override void OnDeinitialize()
{
_agentList.SelectionChanged -= OnSelectionChanged;
_agentList.UpdateContent -= OnListUpdateContent;
_selectButton.onClick.RemoveListener(OnSelectButton);
base.OnDeinitialize();
}
private void OnSelectionChanged(int index)
{
var setup = Context.Settings.Agent.Agents[index];
bool owned = index == 0 || ShopUnlocks.IsUnlocked(setup.ID, ShopKey);
if (!owned) return; // block selecting locked
_previewAgent = setup.ID;
UpdateAgent();
}
private void OnSelectButton()
{
bool isSame = Context.PlayerData.AgentID == _previewAgent;
if (isSame == false)
{
Context.PlayerData.AgentID = _previewAgent;
_selectedEffect.SetActive(false);
_selectedEffect.SetActive(true);
PlaySound(_selectedSound);
UpdateAgent();
Invoke("CloseWithBack", _closeDelayAfterSelection);
}
else
{
CloseWithBack();
}
}
private void UpdateAgent()
{
var agentSetups = Context.Settings.Agent.Agents;
_agentList.Selection = Array.FindIndex(agentSetups, t => t.ID == _previewAgent);
if (_agentList.Selection < 0)
{
int firstOwned = Array.FindIndex(agentSetups, t => ShopUnlocks.IsUnlocked(t.ID, ShopKey));
_agentList.Selection = firstOwned >= 0 ? firstOwned : 0;
_previewAgent = agentSetups[_agentList.Selection].ID;
}
if (_previewAgent.HasValue() == false)
{
Context.PlayerPreview.HideAgent();
_agentName.text = string.Empty;
_agentDescription.text = string.Empty;
_selectButton.interactable = false;
}
else
{
var setup = Context.Settings.Agent.GetAgentSetup(_previewAgent);
Context.PlayerPreview.ShowAgent(_previewAgent);
_agentName.text = string.Format(_agentNameFormat, setup.DisplayName);
_agentDescription.text = setup.Description;
_selectButton.interactable = ShopUnlocks.IsUnlocked(_previewAgent, ShopKey);
}
_selectedAgentGroup.SetActive(_previewAgent == Context.PlayerData.AgentID);
}
}
}
//using System;
//using TMPro;
//using UnityEngine;
//using Cinemachine;
//using System.Collections.Generic;
//using System.Collections;
//namespace TPSBR.UI
//{
// public class UIAgentSelectionView : UICloseView
// {
// [SerializeField] private CinemachineVirtualCamera _camera;
// [SerializeField] private UIList _agentList;
// [SerializeField] private UIButton _selectButton;
// [SerializeField] private TextMeshProUGUI _agentName;
// [SerializeField] private TextMeshProUGUI _agentDescription;
// [SerializeField] private string _agentNameFormat = "{0}";
// [SerializeField] private UIBehaviour _selectedAgentGroup;
// [SerializeField] private UIBehaviour _selectedEffect;
// [SerializeField] private AudioSetup _selectedSound;
// [SerializeField] private float _closeDelayAfterSelection = 0.5f;
// private string _previewAgent;
// private const string ShopKey = "Characters";
// protected override void OnInitialize()
// {
// base.OnInitialize();
// _agentList.SelectionChanged += OnSelectionChanged;
// _agentList.UpdateContent += OnListUpdateContent;
// _selectButton.onClick.AddListener(OnSelectButton);
// }
// private void OnListUpdateContent(int index, MonoBehaviour content)
// {
// StartCoroutine(OnListUpdateContentRoutine(index, content));
// }
// IEnumerator OnListUpdateContentRoutine(int index, MonoBehaviour content)
// {
// yield return new WaitForSeconds(0.1f);
// var behaviour = content as UIBehaviour;
// var setup = Context.Settings.Agent.Agents[index];
// behaviour.Image.sprite = setup.Icon;
// bool unlocked = index == 0 || ShopUnlocks.IsUnlocked(setup.ID, ShopKey);
// var item = content.GetComponentInParent<UIListItem>();
// if (item != null) item.SetLocked(!unlocked);
// }
// protected override void OnOpen()
// {
// base.OnOpen();
// _camera.enabled = true;
// _selectedEffect.SetActive(false);
// var agents = Context.Settings.Agent.Agents;
// if (agents != null && agents.Length > 0)
// ShopUnlocks.EnsureUnlocked(agents[0].ID, ShopKey);
// _previewAgent = Context.PlayerData.AgentID;
// _agentList.Refresh(agents.Length, false);
// UpdateAgent();
// }
// protected override void OnClose()
// {
// _camera.enabled = false;
// Context.PlayerPreview.ShowAgent(Context.PlayerData.AgentID);
// base.OnClose();
// }
// protected override void OnDeinitialize()
// {
// _agentList.SelectionChanged -= OnSelectionChanged;
// _agentList.UpdateContent -= OnListUpdateContent;
// _selectButton.onClick.RemoveListener(OnSelectButton);
// base.OnDeinitialize();
// }
// private void OnSelectionChanged(int index)
// {
// var setup = Context.Settings.Agent.Agents[index];
// bool unlocked = index == 0 || ShopUnlocks.IsUnlocked(setup.ID, ShopKey);
// if (!unlocked) return;
// _previewAgent = setup.ID;
// UpdateAgent();
// }
// private void OnSelectButton()
// {
// bool isSame = Context.PlayerData.AgentID == _previewAgent;
// if (isSame == false)
// {
// Context.PlayerData.AgentID = _previewAgent;
// _selectedEffect.SetActive(false);
// _selectedEffect.SetActive(true);
// PlaySound(_selectedSound);
// UpdateAgent();
// Invoke("CloseWithBack", _closeDelayAfterSelection);
// }
// else
// {
// CloseWithBack();
// }
// }
// private void UpdateAgent()
// {
// var agentSetups = Context.Settings.Agent.Agents;
// _agentList.Selection = Array.FindIndex(agentSetups, t => t.ID == _previewAgent);
// if (_agentList.Selection < 0)
// {
// int firstUnlocked = Array.FindIndex(agentSetups, t => ShopUnlocks.IsUnlocked(t.ID, ShopKey));
// _agentList.Selection = firstUnlocked >= 0 ? firstUnlocked : 0;
// _previewAgent = agentSetups[_agentList.Selection].ID;
// }
// if (_previewAgent.HasValue() == false)
// {
// Context.PlayerPreview.HideAgent();
// _agentName.text = string.Empty;
// _agentDescription.text = string.Empty;
// _selectButton.interactable = false;
// }
// else
// {
// var setup = Context.Settings.Agent.GetAgentSetup(_previewAgent);
// Context.PlayerPreview.ShowAgent(_previewAgent);
// _agentName.text = string.Format(_agentNameFormat, setup.DisplayName);
// _agentDescription.text = setup.Description;
// _selectButton.interactable = ShopUnlocks.IsUnlocked(_previewAgent, ShopKey);
// }
// _selectedAgentGroup.SetActive(_previewAgent == Context.PlayerData.AgentID);
// }
// }
//}