using System ;
using System.Collections.Generic ;
using Unity.BossRoom.Gameplay.GameplayObjects.Character ;
using Unity.BossRoom.Gameplay.GameState ;
using TMPro ;
using UnityEngine ;
using UnityEngine.UI ;
namespace Unity.BossRoom.Gameplay.UI
{
public class UICharSelectPlayerSeat : MonoBehaviour
{
[SerializeField]
private GameObject m_InactiveStateVisuals ;
[SerializeField]
private GameObject m_ActiveStateVisuals ;
[SerializeField]
private Transform m_PlayerNumberHolderParent ; // Parent for player images
[SerializeField]
private Transform m_PlayerNameHolderParent ; // Parent for player names
[SerializeField]
private GameObject m_PlayerNumberTemplate ; // Prefab template for player images
[SerializeField]
private GameObject m_PlayerNameTemplate ; // Prefab template for player names
[SerializeField]
private Image m_Glow ;
[SerializeField]
private Image m_Checkbox ;
[SerializeField]
private Button m_Button ;
[SerializeField]
private Animator m_Animator ;
[SerializeField]
private string m_AnimatorTriggerWhenLockedIn = "LockedIn" ;
[SerializeField]
private string m_AnimatorTriggerWhenUnlocked = "Unlocked" ;
[SerializeField]
private CharacterTypeEnum m_CharacterClass ;
private int m_SeatIndex ;
private List < int > m_PlayerNumbers = new List < int > ( ) ;
private List < string > m_PlayerNames = new List < string > ( ) ;
private NetworkCharSelection . SeatState m_State ;
private bool m_IsDisabled ;
public static event Action < int , int > OnPlayerSwitchedProfile ; // Event for player switching
public void Initialize ( int seatIndex )
{
m_SeatIndex = seatIndex ;
m_State = NetworkCharSelection . SeatState . Inactive ;
m_PlayerNumbers . Clear ( ) ;
m_PlayerNames . Clear ( ) ;
ConfigureStateGraphics ( ) ;
}
public bool IsLocked ( )
{
return m_State = = NetworkCharSelection . SeatState . LockedIn & & m_PlayerNumbers . Count > 0 ;
}
/// <summary>
/// Sets the state of the seat and updates the UI for the selected players.
/// </summary>
public void SetState ( NetworkCharSelection . SeatState state , int playerNumber , string playerName )
{
if ( state = = NetworkCharSelection . SeatState . Inactive )
{
RemovePlayer ( playerNumber ) ;
}
else
{
OnPlayerSwitchedProfile ? . Invoke ( playerNumber , m_SeatIndex ) ; // Notify other seats
AddPlayer ( playerNumber , playerName ) ;
}
m_State = state ;
ConfigureStateGraphics ( ) ;
}
/// <summary>
/// Adds a player's number and name to the seat.
/// </summary>
public void AddPlayer ( int playerNumber , string playerName )
{
if ( ! m_PlayerNumbers . Contains ( playerNumber ) )
{
m_PlayerNumbers . Add ( playerNumber ) ;
m_PlayerNames . Add ( playerName ) ;
}
ConfigureStateGraphics ( ) ;
}
/// <summary>
/// Removes a player's number and name from the seat.
/// </summary>
public void RemovePlayer ( int playerNumber )
{
int index = m_PlayerNumbers . IndexOf ( playerNumber ) ;
if ( index > = 0 )
{
m_PlayerNumbers . RemoveAt ( index ) ;
m_PlayerNames . RemoveAt ( index ) ;
}
ConfigureStateGraphics ( ) ;
}
private void ConfigureStateGraphics ( )
{
// Clear existing player images and names.
foreach ( Transform child in m_PlayerNumberHolderParent )
{
Destroy ( child . gameObject ) ;
}
foreach ( Transform child in m_PlayerNameHolderParent )
{
Destroy ( child . gameObject ) ;
}
if ( m_State = = NetworkCharSelection . SeatState . Inactive | | m_PlayerNumbers . Count = = 0 )
{
m_InactiveStateVisuals . SetActive ( true ) ;
m_ActiveStateVisuals . SetActive ( false ) ;
m_Glow . gameObject . SetActive ( false ) ;
m_Checkbox . gameObject . SetActive ( false ) ;
return ;
}
m_InactiveStateVisuals . SetActive ( false ) ;
m_ActiveStateVisuals . SetActive ( true ) ;
float playerNumberYPosition = 0f ; // Starting Y-position for player images.
float playerNameYPosition = 0f ; // Starting Y-position for player names.
for ( int i = 0 ; i < m_PlayerNumbers . Count ; i + + )
{
// Instantiate and position player number (image).
var playerImageObj = Instantiate ( m_PlayerNumberTemplate , m_PlayerNumberHolderParent ) ;
var playerImage = playerImageObj . GetComponent < Image > ( ) ;
playerImage . sprite = ClientCharSelectState . Instance . m_IdentifiersForEachPlayerNumber [ m_PlayerNumbers [ i ] ] . Indicator ;
// Set position of player image with Y-offset of +60 units.
RectTransform imageRect = playerImageObj . GetComponent < RectTransform > ( ) ;
imageRect . anchoredPosition = new Vector2 ( imageRect . anchoredPosition . x , playerNumberYPosition ) ;
playerNumberYPosition + = 60f ; // Move upward by 60 units for the next image.
// Instantiate and position player name (text).
var playerNameObj = Instantiate ( m_PlayerNameTemplate , m_PlayerNameHolderParent ) ;
var playerNameText = playerNameObj . GetComponent < TextMeshProUGUI > ( ) ;
playerNameText . text = m_PlayerNames [ i ] ;
playerNameText . color = ClientCharSelectState . Instance . m_IdentifiersForEachPlayerNumber [ m_PlayerNumbers [ i ] ] . Color ;
// Set position of player name with Y-offset of -20 units.
RectTransform nameRect = playerNameObj . GetComponent < RectTransform > ( ) ;
nameRect . anchoredPosition = new Vector2 ( nameRect . anchoredPosition . x , playerNameYPosition ) ;
playerNameYPosition - = 20f ; // Move downward by 20 units for the next name.
}
if ( m_State = = NetworkCharSelection . SeatState . LockedIn )
{
m_Glow . color = ClientCharSelectState . Instance . m_IdentifiersForEachPlayerNumber [ m_PlayerNumbers [ 0 ] ] . Color ;
m_Glow . gameObject . SetActive ( true ) ;
m_Checkbox . gameObject . SetActive ( true ) ;
PlayLockAnim ( ) ;
}
else
{
m_Glow . gameObject . SetActive ( false ) ;
m_Checkbox . gameObject . SetActive ( false ) ;
PlayUnlockAnim ( ) ;
}
}
private void PlayLockAnim ( )
{
if ( m_Animator )
{
m_Animator . ResetTrigger ( m_AnimatorTriggerWhenUnlocked ) ;
m_Animator . SetTrigger ( m_AnimatorTriggerWhenLockedIn ) ;
}
}
private void PlayUnlockAnim ( )
{
if ( m_Animator )
{
m_Animator . ResetTrigger ( m_AnimatorTriggerWhenLockedIn ) ;
m_Animator . SetTrigger ( m_AnimatorTriggerWhenUnlocked ) ;
}
}
public void SetDisableInteraction ( bool disable )
{
m_IsDisabled = disable ;
}
public void OnClicked ( )
{
ClientCharSelectState . Instance . OnPlayerClickedSeat ( m_SeatIndex ) ;
}
private void OnEnable ( )
{
OnPlayerSwitchedProfile + = HandlePlayerSwitched ;
}
private void OnDisable ( )
{
OnPlayerSwitchedProfile - = HandlePlayerSwitched ;
}
/// <summary>
/// Removes the player from this seat if they select another.
/// </summary>
private void HandlePlayerSwitched ( int playerNumber , int newSeatIndex )
{
if ( newSeatIndex ! = m_SeatIndex )
{
RemovePlayer ( playerNumber ) ;
}
}
}
}
//using System;
//using Unity.BossRoom.Gameplay.GameplayObjects.Character;
//using Unity.BossRoom.Gameplay.GameState;
//using TMPro;
//using UnityEngine;
//using UnityEngine.UI;
//namespace Unity.BossRoom.Gameplay.UI
//{
// /// <summary>
// /// Controls one of the eight "seats" on the character-select screen (the boxes along the bottom).
// /// </summary>
// public class UICharSelectPlayerSeat : MonoBehaviour
// {
// [SerializeField]
// private GameObject m_InactiveStateVisuals;
// [SerializeField]
// private GameObject m_ActiveStateVisuals;
// [SerializeField]
// private Image m_PlayerNumberHolder;
// [SerializeField]
// private TextMeshProUGUI m_PlayerNameHolder;
// [SerializeField]
// private Image m_Glow;
// [SerializeField]
// private Image m_Checkbox;
// [SerializeField]
// private Button m_Button;
// [SerializeField]
// private Animator m_Animator;
// [SerializeField]
// private string m_AnimatorTriggerWhenLockedIn = "LockedIn";
// [SerializeField]
// private string m_AnimatorTriggerWhenUnlocked = "Unlocked";
// [SerializeField]
// private CharacterTypeEnum m_CharacterClass;
// // just a way to designate which seat we are -- the leftmost seat on the lobby UI is index 0, the next one is index 1, etc.
// private int m_SeatIndex;
// // playerNumber of who is sitting in this seat right now. 0-based; e.g. this is 0 for Player 1, 1 for Player 2, etc. Meaningless when m_State is Inactive (and in that case it is set to -1 for clarity)
// private int m_PlayerNumber;
// // the last SeatState we were assigned
// private NetworkCharSelection.SeatState m_State;
// // once this is true, we're never clickable again!
// private bool m_IsDisabled;
// public void Initialize(int seatIndex)
// {
// m_SeatIndex = seatIndex;
// m_State = NetworkCharSelection.SeatState.Inactive;
// m_PlayerNumber = -1;
// ConfigureStateGraphics();
// }
// public void SetState(NetworkCharSelection.SeatState state, int playerIndex, string playerName)
// {
// if (state == m_State && playerIndex == m_PlayerNumber)
// return; // no actual changes
// m_State = state;
// m_PlayerNumber = playerIndex;
// m_PlayerNameHolder.text = playerName;
// if (m_State == NetworkCharSelection.SeatState.Inactive)
// m_PlayerNumber = -1;
// ConfigureStateGraphics();
// }
// public bool IsLocked()
// {
// return m_State == NetworkCharSelection.SeatState.LockedIn;
// }
// public void SetDisableInteraction(bool disable)
// {
// Debug.Log("Ali Interactable False");
// //m_Button.interactable = !disable;
// m_IsDisabled = disable;
// if (!disable)
// {
// // if we were locked move to unlocked state
// PlayUnlockAnim();
// }
// }
// private void PlayLockAnim()
// {
// if (m_Animator)
// {
// m_Animator.ResetTrigger(m_AnimatorTriggerWhenUnlocked);
// m_Animator.SetTrigger(m_AnimatorTriggerWhenLockedIn);
// }
// }
// private void PlayUnlockAnim()
// {
// if (m_Animator)
// {
// m_Animator.ResetTrigger(m_AnimatorTriggerWhenLockedIn);
// m_Animator.SetTrigger(m_AnimatorTriggerWhenUnlocked);
// }
// }
// private void ConfigureStateGraphics()
// {
// if (m_State == NetworkCharSelection.SeatState.Inactive)
// {
// m_InactiveStateVisuals.SetActive(true);
// m_ActiveStateVisuals.SetActive(false);
// m_Glow.gameObject.SetActive(false);
// m_Checkbox.gameObject.SetActive(false);
// m_PlayerNameHolder.gameObject.SetActive(false);
// Debug.Log("Ali Interactable False 2");
// //m_Button.interactable = !m_IsDisabled;
// PlayUnlockAnim();
// }
// else // either active or locked-in... these states are visually very similar
// {
// m_InactiveStateVisuals.SetActive(false);
// m_PlayerNumberHolder.sprite = ClientCharSelectState.Instance.m_IdentifiersForEachPlayerNumber[m_PlayerNumber].Indicator;
// m_ActiveStateVisuals.SetActive(true);
// m_PlayerNameHolder.gameObject.SetActive(true);
// m_PlayerNameHolder.color = ClientCharSelectState.Instance.m_IdentifiersForEachPlayerNumber[m_PlayerNumber].Color;
// //m_Button.interactable = !m_IsDisabled;
// Debug.Log("Ali Interactable False 3");
// if (m_State == NetworkCharSelection.SeatState.LockedIn)
// {
// m_Glow.color = ClientCharSelectState.Instance.m_IdentifiersForEachPlayerNumber[m_PlayerNumber].Color;
// m_Glow.gameObject.SetActive(true);
// m_Checkbox.gameObject.SetActive(true);
// Debug.Log("Ali Interactable False 4");
// //m_Button.interactable = false;
// PlayLockAnim();
// }
// else
// {
// m_Glow.gameObject.SetActive(false);
// m_Checkbox.gameObject.SetActive(false);
// PlayUnlockAnim();
// }
// }
// }
// // Called directly by Button in UI
// public void OnClicked()
// {
// ClientCharSelectState.Instance.OnPlayerClickedSeat(m_SeatIndex);
// }
// }
//}