Ludo-3D/Assets/Scripts/Tile/SafeTile.cs

90 lines
3.4 KiB
C#
Raw Normal View History

2026-01-23 12:46:59 +05:30
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
public class SafeTile : Tile
{
[SerializeField] private Transform centerPlacementPoint;
[SerializeField] private Transform[] placementPoints;
protected Queue<Transform> placementQueue = new Queue<Transform>();
2026-01-26 19:48:55 +05:30
protected Dictionary<PlayerType, PlayerTileData> playerTypesDict = new Dictionary<PlayerType, PlayerTileData>();
2026-01-23 12:46:59 +05:30
public int PlayerTypesCount => playerTypesDict.Count;
public bool HasMoreThanOnePlayerType => playerTypesDict.Count > 1;
2026-01-26 19:48:55 +05:30
public List<PlayerPawn> GetPlayerPawns(PlayerType playerType) => playerTypesDict[playerType].playerPawns.Values.ToList();
2026-01-23 12:46:59 +05:30
2026-01-26 19:48:55 +05:30
public bool ContainsPlayerType(PlayerType playerType) => playerTypesDict.ContainsKey(playerType);
2026-01-23 12:46:59 +05:30
private void Awake()
{
foreach (var placement in placementPoints)
{
placementQueue.Enqueue(placement);
}
}
2026-01-26 19:48:55 +05:30
public override void InitPlayerPawn(PlayerPawn playerPawn, PlayerType playerType)
2026-01-23 12:46:59 +05:30
{
// PlayerPawn = playerPawn;
if (!playerTypesDict.ContainsKey(playerType))
{
playerTypesDict.Add(
playerType,
new PlayerTileData { playerCount = 1,
playerPawns = new Dictionary<int, PlayerPawn>()
});
playerTypesDict[playerType].playerPawns.Add(playerPawn.PlayerId, playerPawn);
playerTypesDict[playerType].commonPlacementTransform = placementQueue.Dequeue();
Debug.Log($"targetSafeTile. Adding player {playerType} {playerTypesDict[playerType].playerCount} tileName: {name}");
}
else
{
playerTypesDict[playerType].playerCount++;
Debug.Log($"targetSafeTile. Adding player {playerType} {playerTypesDict[playerType].playerCount}, tileName: {name}");
playerTypesDict[playerType].playerPawns.Add(playerPawn.PlayerId, playerPawn);
}
}
2026-01-26 19:48:55 +05:30
public void UpdateSafeZonePlayerData(PlayerType playerType, PlayerPawn playerPawn)
2026-01-23 12:46:59 +05:30
{
if (playerTypesDict.ContainsKey(playerType))
{
if (playerTypesDict[playerType].playerCount > 0)
{
Debug.Log($"targetSafeTile. Removing player {playerType} {playerTypesDict[playerType].playerCount}, tileName: {name}");
playerTypesDict[playerType].playerCount--;
Debug.Log($"targetSafeTile. Removing player {playerType} after: {playerTypesDict[playerType].playerCount}");
playerTypesDict[playerType].playerPawns.Remove(playerPawn.PlayerId);
if (playerTypesDict[playerType].playerCount == 0)
{
placementQueue.Enqueue(playerTypesDict[playerType].commonPlacementTransform);
playerTypesDict.Remove(playerType);
Debug.Log($"targetSafeTile. Removing player {playerType}");
lastOccupiedIndex--;
}
}
}
}
2026-01-26 19:48:55 +05:30
public PlayerType GetFirstPlayerType() => playerTypesDict.Keys.FirstOrDefault();
2026-01-23 12:46:59 +05:30
2026-01-26 19:48:55 +05:30
public int GetPlayerPawnsCountInTile(PlayerType playerType)
2026-01-23 12:46:59 +05:30
{
return playerTypesDict[playerType].playerPawns.Count;
}
2026-01-26 19:48:55 +05:30
public PlayerPawn IterateAndGetPlayerPawn(PlayerType playerType, int index)
2026-01-23 12:46:59 +05:30
{
return playerTypesDict[playerType].playerPawns[index + 1];
}
2026-01-26 19:48:55 +05:30
public Transform GetPlacementPoint(PlayerType playerType)
2026-01-23 12:46:59 +05:30
{
return playerTypesDict[playerType].commonPlacementTransform;
}
}