2026-01-21 20:27:45 +05:30
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Tilemaps;
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public struct TileData
|
|
|
|
|
{
|
2026-01-26 19:48:55 +05:30
|
|
|
public PlayerType playerType;
|
2026-01-21 20:27:45 +05:30
|
|
|
public Transform playerFinishingTileParent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TilesManager : MonoBehaviour, IBootLoader, IDataLoader, IBase
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private Tile[] generalTiles;
|
|
|
|
|
[SerializeField] private TileData[] tileDatas;
|
|
|
|
|
|
|
|
|
|
private GameplayManager gameplayManager;
|
|
|
|
|
|
2026-01-30 23:27:56 +05:30
|
|
|
private Dictionary<PlayerType, List<Tile>> finishingTileDataPairs = new Dictionary<PlayerType, List<Tile>>();
|
2026-01-21 20:27:45 +05:30
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
InterfaceManager.Instance.RegisterInterface<TilesManager>(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitializeData()
|
|
|
|
|
{
|
|
|
|
|
gameplayManager = InterfaceManager.Instance.GetInterfaceInstance<GameplayManager>();
|
2026-01-30 22:06:51 +05:30
|
|
|
}
|
2026-01-21 20:27:45 +05:30
|
|
|
|
2026-01-30 22:06:51 +05:30
|
|
|
public void InitTilesData()
|
|
|
|
|
{
|
2026-01-31 20:33:50 +05:30
|
|
|
finishingTileDataPairs = new Dictionary<PlayerType, List<Tile>>();
|
2026-01-21 20:27:45 +05:30
|
|
|
foreach (var tileData in tileDatas)
|
|
|
|
|
{
|
|
|
|
|
if (gameplayManager.PlayerTypesCollection.Contains(tileData.playerType))
|
|
|
|
|
{
|
2026-01-30 23:27:56 +05:30
|
|
|
finishingTileDataPairs.Add(tileData.playerType, new List<Tile>());
|
2026-01-21 20:27:45 +05:30
|
|
|
foreach (Transform child in tileData.playerFinishingTileParent)
|
2026-01-30 23:27:56 +05:30
|
|
|
finishingTileDataPairs[tileData.playerType].Add(child.GetComponent<Tile>());
|
2026-01-21 20:27:45 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 19:48:55 +05:30
|
|
|
public int GetFinishingTileDataLength(PlayerType playerType) => finishingTileDataPairs[playerType].Count;
|
2026-01-22 20:50:41 +05:30
|
|
|
|
2026-01-21 20:27:45 +05:30
|
|
|
public int GetGeneralTilesLength() => generalTiles.Length;
|
|
|
|
|
|
|
|
|
|
public Tile RetrieveTileBasedOnIndex(int index)
|
|
|
|
|
{
|
2026-01-22 20:50:41 +05:30
|
|
|
Debug.Log($"RetrieveTileBasedOnIndex: Index: {index}");
|
2026-01-23 12:46:59 +05:30
|
|
|
|
|
|
|
|
Tile tile = index == generalTiles.Length ? generalTiles[0] : generalTiles[index];
|
|
|
|
|
|
|
|
|
|
return tile;
|
2026-01-21 20:27:45 +05:30
|
|
|
}
|
|
|
|
|
|
2026-01-30 23:27:56 +05:30
|
|
|
public Tile RetrieveFinishingTileBasedOnIndex(PlayerType playerType, int index)
|
2026-01-21 20:27:45 +05:30
|
|
|
{
|
|
|
|
|
return finishingTileDataPairs[playerType][index];
|
|
|
|
|
}
|
2026-01-30 23:27:56 +05:30
|
|
|
|
2026-02-02 20:12:00 +05:30
|
|
|
public int GetNextGeneralTileIndex(int currentTileIndex)
|
|
|
|
|
{
|
|
|
|
|
return currentTileIndex == GetGeneralTilesLength() - 1 ? 0 : currentTileIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetNextFinishingTileIndex(int currentTileIndex, PlayerType currentPlayerTypeTurn)
|
|
|
|
|
{
|
|
|
|
|
return currentTileIndex > GetFinishingTileDataLength(currentPlayerTypeTurn) - 1 ? 0 : currentTileIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-31 20:33:50 +05:30
|
|
|
public void ResetData()
|
2026-01-30 23:27:56 +05:30
|
|
|
{
|
2026-01-31 20:33:50 +05:30
|
|
|
finishingTileDataPairs.Clear();
|
2026-01-30 23:27:56 +05:30
|
|
|
}
|
2026-02-02 19:27:17 +05:30
|
|
|
|
2026-02-13 18:17:23 +05:30
|
|
|
// causes null ref cast exception while player is moving.
|
|
|
|
|
/*
|
2026-02-02 19:27:17 +05:30
|
|
|
public void ResetTileData(PlayerType playerType, int currentTileIndex, PlayerState playerState)
|
|
|
|
|
{
|
2026-02-02 19:49:16 +05:30
|
|
|
if (playerState == PlayerState.InFinishingPath || playerState == PlayerState.HasFinished)
|
2026-02-02 19:27:17 +05:30
|
|
|
{
|
|
|
|
|
Tile tile = RetrieveFinishingTileBasedOnIndex(playerType, currentTileIndex);
|
|
|
|
|
tile.ResetTileData();
|
|
|
|
|
}
|
|
|
|
|
else if (playerState == PlayerState.Moving)
|
|
|
|
|
{
|
|
|
|
|
Tile tile = RetrieveTileBasedOnIndex(currentTileIndex);
|
|
|
|
|
tile.ResetTileData();
|
|
|
|
|
}
|
|
|
|
|
else if (playerState == PlayerState.InSafeZone)
|
|
|
|
|
{
|
|
|
|
|
SafeTile tile = (SafeTile)RetrieveTileBasedOnIndex(currentTileIndex);
|
|
|
|
|
tile.ResetTileData();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-13 18:17:23 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public void ResetTileDatas()
|
|
|
|
|
{
|
|
|
|
|
foreach (var tile in generalTiles)
|
|
|
|
|
tile.ResetTileData();
|
|
|
|
|
|
|
|
|
|
foreach (var pair in finishingTileDataPairs)
|
|
|
|
|
{
|
|
|
|
|
foreach (var tile in finishingTileDataPairs[pair.Key])
|
|
|
|
|
{
|
|
|
|
|
tile.ResetTileData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-02 20:12:00 +05:30
|
|
|
|
|
|
|
|
public Vector3 GetAndInitPositionInsideSafeZone(PlayerPawn playerPawn, Tile targetTile, PlayerType currentPlayerTypeTurn)
|
|
|
|
|
{
|
|
|
|
|
Vector3 targetPosition;
|
|
|
|
|
SafeTile targetSafeTile = (SafeTile)targetTile;
|
|
|
|
|
Debug.Log($"targetSafeTile.PlayerTypesCount: {targetSafeTile.PlayerTypesCount}");
|
|
|
|
|
if (targetSafeTile.PlayerTypesCount == 1)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"targetSafeTile.ContainsPlayerType(currentPlayerTypeTurn): {targetSafeTile.ContainsPlayerType(currentPlayerTypeTurn)}");
|
|
|
|
|
if (!targetSafeTile.ContainsPlayerType(currentPlayerTypeTurn)) // means it is a new player type, the second one
|
|
|
|
|
{
|
|
|
|
|
PlayerType initialPlayerType = targetSafeTile.GetFirstPlayerType();
|
|
|
|
|
|
|
|
|
|
// rearrange already existing player from center position to it's saved transform
|
2026-02-04 12:32:18 +05:30
|
|
|
Debug.Log($"### GetAndInitPositionInsideSafeZone initialPlayerType: {initialPlayerType}");
|
2026-02-02 20:12:00 +05:30
|
|
|
var playerPawns = targetSafeTile.GetPlayerPawns(initialPlayerType);
|
2026-02-04 12:32:18 +05:30
|
|
|
Debug.Log($"### GetAndInitPositionInsideSafeZone playerPawns: {playerPawns.Count}");
|
|
|
|
|
|
2026-02-02 20:12:00 +05:30
|
|
|
foreach (var pawn in playerPawns)
|
|
|
|
|
{
|
|
|
|
|
var placementPoint = targetSafeTile.GetPlacementPoint(initialPlayerType);
|
2026-02-04 12:32:18 +05:30
|
|
|
Debug.Log($"### GetAndInitPositionInsideSafeZone placementPoint: {placementPoint.position}");
|
2026-02-02 20:12:00 +05:30
|
|
|
pawn.MoveToCustomTilePosition(placementPoint.position);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-04 12:32:18 +05:30
|
|
|
Debug.Log($"### GetAndInitPositionInsideSafeZone playerPawn: {playerPawn.name}, {playerPawn.PlayerType}");
|
|
|
|
|
Debug.Log($"### GetAndInitPositionInsideSafeZone currentPlayerTypeTurn: {currentPlayerTypeTurn}");
|
|
|
|
|
|
2026-02-02 20:12:00 +05:30
|
|
|
targetSafeTile.InitPlayerPawn(playerPawn, currentPlayerTypeTurn);
|
|
|
|
|
targetPosition = targetSafeTile.GetPlacementPoint(currentPlayerTypeTurn).position;
|
2026-02-04 12:32:18 +05:30
|
|
|
|
|
|
|
|
Debug.Log($"### GetAndInitPositionInsideSafeZone targetPosition: {targetPosition}");
|
2026-02-02 20:12:00 +05:30
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
targetSafeTile.InitPlayerPawn(playerPawn, currentPlayerTypeTurn);
|
|
|
|
|
targetPosition = targetTile.CenterPlacementPosition;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// TODO :: Check the data if it's consistent
|
|
|
|
|
Debug.Log($"targetSafeTile.ContainsPlayerType(currentPlayerTypeTurn): {targetSafeTile.ContainsPlayerType(currentPlayerTypeTurn)}");
|
|
|
|
|
if (!targetSafeTile.ContainsPlayerType(currentPlayerTypeTurn))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"targetSafeTile.PlayerTypesCount: {targetSafeTile.PlayerTypesCount}");
|
|
|
|
|
if (targetSafeTile.PlayerTypesCount < 1) // he is the only player that is being added to the safe zone
|
|
|
|
|
{
|
|
|
|
|
targetSafeTile.InitPlayerPawn(playerPawn, currentPlayerTypeTurn);
|
|
|
|
|
targetPosition = targetTile.CenterPlacementPosition;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
targetSafeTile.InitPlayerPawn(playerPawn, currentPlayerTypeTurn);
|
|
|
|
|
targetPosition = targetSafeTile.GetPlacementPoint(currentPlayerTypeTurn).position;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
targetSafeTile.InitPlayerPawn(playerPawn, currentPlayerTypeTurn);
|
|
|
|
|
targetPosition = targetSafeTile.GetPlacementPoint(currentPlayerTypeTurn).position;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return targetPosition;
|
|
|
|
|
}
|
2026-01-21 20:27:45 +05:30
|
|
|
}
|