You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Services.Lobbies.Models;
using UnityEngine;
namespace LobbyScripts
{
public class LobbyPlayerList : MonoBehaviour
{
[SerializeField] private LobbyPlayerData _lobbyPlayer;
[SerializeField] private Transform _parent;
private List<LobbyPlayerData> _players;
public List<LobbyPlayerData> PlayerList => _players;
private void Awake() => _players ??= new List<LobbyPlayerData>();
private void OnEnable() => GameLobby.OnLobbyCreated += (_,_) => ClearLobby();
public void PlayerJoined(Player player, Lobby lobby, bool isHost)
{
var playerData = Instantiate(_lobbyPlayer, _parent);
playerData.SetPlayerData(player, lobby, isHost);
if(_players.Contains(playerData)) return;
_players.Add(playerData);
}
public void RemovePlayer(string playerID)
{
Debug.Log($"Main Id: {playerID}");
foreach (var playerData in _players)
{
Debug.Log(playerData.PlayerID);
}
var player = _players.FirstOrDefault(player => player.PlayerID == playerID);
if (player == null) return;
if (!_players.Contains(player)) return;
_players.Remove(player);
Destroy(player.gameObject);
}
public void ClearLobby()
{
if (_players.Count > 0)
{
foreach (var player in _players)
{
if(player == null ) continue;
Destroy(player.gameObject);
}
_players.Clear();
}
}
}
}