Addition: Added game icon for recognising each player's turn. Fix: Added null checks for users timer system.
1411 lines
55 KiB
C#
1411 lines
55 KiB
C#
using TMPro;
|
|
using System;
|
|
using System.Linq;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using UnityEngine.Serialization;
|
|
|
|
public enum BotMove
|
|
{
|
|
FinishingPathMove = 0,
|
|
SafeMove = 1,
|
|
AttackMove = 2,
|
|
NormalMove = 3,
|
|
NoMoves = 4
|
|
}
|
|
|
|
public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
|
|
{
|
|
[SerializeField] private bool isDebugAITest = false;
|
|
[SerializeField] private bool isDebugPlayerTest = false;
|
|
|
|
public bool IsDebugAITest => isDebugAITest;
|
|
public bool IsDebugPlayerTest => isDebugPlayerTest;
|
|
|
|
[SerializeField] DiceRollHandler diceRollHandler;
|
|
|
|
[SerializeField] private int diceValue = 0;
|
|
[SerializeField] private float diceRollDelayForBot = 0.5f;
|
|
[SerializeField] private float diceRollDelayForUser = 0.5f;
|
|
[SerializeField] private int maxDiceSixRollCounter = 2;
|
|
[SerializeField] private int totalStepsForCharacter = 57;
|
|
[SerializeField] private int currentPlayerTurnMaxTime = 5;
|
|
[SerializeField] private int currentPlayerSelectionMaxTime = 5;
|
|
|
|
[SerializeField] private TextMeshProUGUI diceText;
|
|
|
|
#if UNITY_EDITOR
|
|
[SerializeField] private Transform pointerDebug;
|
|
[SerializeField] private MeshRenderer pointerMeshRend;
|
|
[SerializeField] private Material turnMat;
|
|
[SerializeField] private Material selectMat;
|
|
#endif
|
|
|
|
[SerializeField] private PlayerGameData[] playerGameDatas;
|
|
[SerializeField] private PlayerBaseHandler playerBaseHandler;
|
|
|
|
public PlayerBaseHandler PlayerBaseHandler => playerBaseHandler;
|
|
|
|
private PlayerType currentPlayerTypeTurn;
|
|
private TimerSystem currentPlayerTurnTimer;
|
|
private int currentPlayerTurnIndex = 0;
|
|
|
|
private List<PlayerType> allPlayerTypes = new List<PlayerType>();
|
|
private List<PlayerType> botTypesInGame = new List<PlayerType>();
|
|
|
|
private List<PlayerData> playerDatas = new List<PlayerData>();
|
|
|
|
|
|
private Dictionary<PlayerType, PlayerGameData> playerGameDatasDict = new Dictionary<PlayerType, PlayerGameData>();
|
|
private Dictionary<PlayerType, Dictionary<int, BotMove>> botRuntimeMovementData = new Dictionary<PlayerType, Dictionary<int, BotMove>>();
|
|
|
|
public TilesManager TilesManager
|
|
{
|
|
get; private set;
|
|
}
|
|
private UIManager uIManager;
|
|
public GameManager GameManager
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
private GameModeHandler gameModeHandler;
|
|
private SoundManager soundManager;
|
|
|
|
private int diceRolledValue;
|
|
|
|
private bool CanRollDiceAgain = false; // used for when you get a 6 or when you reach the finish point
|
|
private int diceSixRollCounter = 0;
|
|
|
|
private List<PlayerPawn> availPlayersToMove = new List<PlayerPawn>();
|
|
private bool canSwitchPlayer = true;
|
|
|
|
public bool CanRollDiceForUser
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
public int TotalPlayersInGame
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
public List<PlayerData> PlayerDatas => playerDatas;
|
|
public List<PlayerType> PlayerTypesCollection => allPlayerTypes;
|
|
|
|
public Action OnGameResumed = null;
|
|
|
|
public void Initialize()
|
|
{
|
|
InterfaceManager.Instance?.RegisterInterface<GameplayManager>(this);
|
|
DOTween.useSafeMode = false;
|
|
}
|
|
|
|
public void InitializeData()
|
|
{
|
|
TilesManager = InterfaceManager.Instance.GetInterfaceInstance<TilesManager>();
|
|
uIManager = InterfaceManager.Instance.GetInterfaceInstance<UIManager>();
|
|
GameManager = InterfaceManager.Instance.GetInterfaceInstance<GameManager>();
|
|
gameModeHandler = InterfaceManager.Instance.GetInterfaceInstance<GameModeHandler>();
|
|
soundManager = InterfaceManager.Instance.GetInterfaceInstance<SoundManager>();
|
|
}
|
|
|
|
public void InitPlayerTypesForPVP(List<PlayerType> types, List<string> names)
|
|
{
|
|
allPlayerTypes = new List<PlayerType>(types);
|
|
playerDatas = new List<PlayerData>();
|
|
|
|
TotalPlayersInGame = types.Count;
|
|
|
|
for (int i=0; i<types.Count; i++)
|
|
{
|
|
playerDatas.Add(new PlayerData
|
|
{
|
|
playerType = types[i],
|
|
playerName = names[i],
|
|
});
|
|
}
|
|
|
|
TilesManager.InitTilesData();
|
|
|
|
playerBaseHandler.InitPlayerTypes(allPlayerTypes);
|
|
InitCurrentGamePlayerInfo();
|
|
}
|
|
|
|
public void InitPlayerTypesForBotMatch(PlayerData selectedPlayerData, int botCount)
|
|
{
|
|
playerDatas = new List<PlayerData>();
|
|
botTypesInGame = new List<PlayerType>();
|
|
allPlayerTypes = new List<PlayerType>();
|
|
|
|
TotalPlayersInGame = botCount + 1;
|
|
AssignBotTypes(selectedPlayerData.playerType, botCount);
|
|
|
|
foreach (PlayerType playerType in Enum.GetValues(typeof(PlayerType)))
|
|
{
|
|
if (botTypesInGame.Contains(playerType))
|
|
{
|
|
allPlayerTypes.Add(playerType);
|
|
playerDatas.Add(new PlayerData { playerType = playerType, playerName = $"{playerType}" });
|
|
}
|
|
else if (selectedPlayerData.playerType == playerType)
|
|
{
|
|
allPlayerTypes.Add(selectedPlayerData.playerType);
|
|
playerDatas.Add(new PlayerData { playerType = selectedPlayerData.playerType, playerName = $"{selectedPlayerData.playerName}" });
|
|
}
|
|
}
|
|
|
|
TilesManager.InitTilesData();
|
|
playerBaseHandler.InitPlayerTypes(allPlayerTypes);
|
|
InitCurrentGamePlayerInfo();
|
|
InitBotRuntimeData();
|
|
AssignPlayerAndBotStates(selectedPlayerData.playerType);
|
|
|
|
// SetCanRollDiceForUser(!botTypesInGame.Contains(allPlayerTypes[0]));
|
|
if (botTypesInGame.Contains(allPlayerTypes[0]))
|
|
HandleDiceRollWithDelay();
|
|
}
|
|
|
|
private void HandleDiceRollWithDelay()
|
|
{
|
|
Invoke(nameof(RollDiceForBot), diceRollDelayForBot);
|
|
}
|
|
|
|
private void InitBotRuntimeData()
|
|
{
|
|
botRuntimeMovementData = new Dictionary<PlayerType, Dictionary<int, BotMove>>();
|
|
|
|
foreach (var botType in botTypesInGame)
|
|
{
|
|
PlayerGameData playerGameData = playerGameDatasDict[botType];
|
|
if (!botRuntimeMovementData.ContainsKey(botType))
|
|
{
|
|
botRuntimeMovementData.Add(botType, new Dictionary<int, BotMove>());
|
|
}
|
|
|
|
foreach (var pawn in playerGameData.playerPawnsDict)
|
|
{
|
|
botRuntimeMovementData[botType].Add(pawn.Value.PlayerId, BotMove.NoMoves);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AssignPlayerAndBotStates(PlayerType selectedPlayerType)
|
|
{
|
|
PlayerBase playerBase = playerBaseHandler.GetPlayerBase(selectedPlayerType);
|
|
playerBase.AssignBotState(PawnType.User);
|
|
|
|
foreach (PlayerType playerType in botTypesInGame)
|
|
{
|
|
playerBase = playerBaseHandler.GetPlayerBase(playerType);
|
|
playerBase.AssignBotState(PawnType.Bot);
|
|
}
|
|
}
|
|
|
|
private void AssignBotTypes(PlayerType selectedPlayerType, int botCount)
|
|
{
|
|
int addedBots = 0;
|
|
if (botCount == 1)
|
|
{
|
|
var playerType = (PlayerType)((int)selectedPlayerType + 2 < Enum.GetValues(typeof(PlayerType)).Length ?
|
|
(int)selectedPlayerType + 2 : (int)selectedPlayerType - 2);
|
|
botTypesInGame.Add(playerType);
|
|
}
|
|
else if (botCount == 2)
|
|
{
|
|
foreach (PlayerType enumType in Enum.GetValues(typeof(PlayerType)))
|
|
{
|
|
if (enumType == selectedPlayerType) continue;
|
|
|
|
botTypesInGame.Add(enumType);
|
|
addedBots++;
|
|
|
|
if (addedBots == botCount) break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (PlayerType enumType in Enum.GetValues(typeof(PlayerType)))
|
|
{
|
|
if (enumType == selectedPlayerType) continue;
|
|
|
|
botTypesInGame.Add(enumType);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UpdateCurrentPlayerTurn(PlayerType playerType)
|
|
{
|
|
playerBaseHandler.ShowSelectedPlayerBase(currentPlayerTypeTurn, false);
|
|
currentPlayerTypeTurn = playerType;
|
|
playerBaseHandler.ShowSelectedPlayerBase(currentPlayerTypeTurn, true);
|
|
}
|
|
|
|
private void UpdateDiceView()
|
|
{
|
|
diceRollHandler.DiceView.SetDiceButtonInteraction(true);
|
|
diceRollHandler.DiceView.ShowDefaultSprite();
|
|
UpdateResponseTimerForUser(currentPlayerMaxTime: currentPlayerTurnMaxTime, () => RollDiceForUser());
|
|
}
|
|
|
|
private void UpdateDiceViewForBot()
|
|
{
|
|
UpdateDiceView();
|
|
HandleDiceRollWithDelay();
|
|
}
|
|
|
|
private void UpdateResponseTimerForUser(int currentPlayerMaxTime, Action onComplete)
|
|
{
|
|
if (gameModeHandler.CurrentGameModeType == GameModeType.Bot)
|
|
{
|
|
bool isBotTurn = botTypesInGame.Contains(currentPlayerTypeTurn);
|
|
// SetCanRollDiceForUser(!isBotTurn); // TODO :: Need to change
|
|
if (isBotTurn)
|
|
{
|
|
currentPlayerTurnTimer?.KillTimer();
|
|
uIManager.UpdatePlayerTurnText(currentPlayerTypeTurn);
|
|
uIManager.UpdatePlayerTurnIcon(currentPlayerTypeTurn);
|
|
return;
|
|
}
|
|
}
|
|
// else
|
|
// SetCanRollDiceForUser(true); // TODO :: Need to change
|
|
|
|
uIManager.UpdatePlayerTurnText(currentPlayerTypeTurn, currentPlayerMaxTime);
|
|
uIManager.UpdatePlayerTurnIcon(currentPlayerTypeTurn);
|
|
|
|
if (currentPlayerTurnTimer == null)
|
|
currentPlayerTurnTimer = new TimerSystem();
|
|
|
|
currentPlayerTurnTimer.Init(currentPlayerMaxTime, onComplete: () =>
|
|
{
|
|
Debug.Log($"currentPlayerTurnTimer: HandleDiceViewForUser");
|
|
onComplete?.Invoke();
|
|
}, inProgress: (remTime) =>
|
|
{
|
|
uIManager.UpdatePlayerTurnText(currentPlayerTypeTurn, currentPlayerMaxTime - (int)remTime);
|
|
});
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (currentPlayerTurnTimer == null || !currentPlayerTurnTimer.IsInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!currentPlayerTurnTimer.IsTimerComplete)
|
|
currentPlayerTurnTimer.UpdateTimer(Time.deltaTime);
|
|
else
|
|
ResetCurrentPlayerTurnTimer();
|
|
}
|
|
|
|
private void ResetCurrentPlayerTurnTimer()
|
|
{
|
|
currentPlayerTurnTimer?.KillTimer();
|
|
}
|
|
|
|
public void InitCurrentGamePlayerInfo()
|
|
{
|
|
currentPlayerTurnIndex = 0;
|
|
UpdateCurrentPlayerTurn(allPlayerTypes[currentPlayerTurnIndex]);
|
|
SetCanRollDiceForUser(IsUsersTurn());
|
|
UpdateDiceView();
|
|
Debug.Log($"currentPlayerTypeTurn: {currentPlayerTypeTurn}");
|
|
|
|
#if UNITY_EDITOR
|
|
SetCurrentSelectedPointer();
|
|
#endif
|
|
|
|
// initialize the board based on the player types
|
|
playerGameDatasDict = new Dictionary<PlayerType, PlayerGameData>();
|
|
|
|
foreach (PlayerGameData playerGameData in playerGameDatas)
|
|
{
|
|
if (!allPlayerTypes.Contains(playerGameData.playerType))
|
|
continue;
|
|
|
|
Debug.Log($"playerGameData.playerType: {playerGameData.playerType}");
|
|
|
|
playerGameDatasDict.Add(
|
|
playerGameData.playerType,
|
|
new PlayerGameData
|
|
{
|
|
playerType = playerGameData.playerType,
|
|
startIndex = playerGameData.startIndex,
|
|
endIndex = playerGameData.endIndex,
|
|
playersParent = playerGameData.playersParent,
|
|
playerPawnsDict = new Dictionary<int, PlayerPawn>(),
|
|
totalPawnsInHome = playerGameData.totalPawnsInHome,
|
|
totalPawnsFinished = playerGameData.totalPawnsFinished
|
|
});
|
|
|
|
foreach (Transform playerPawnChild in playerGameData.playersParent)
|
|
{
|
|
if (!playerPawnChild.gameObject.activeInHierarchy) continue;
|
|
|
|
var pawn = playerPawnChild.GetComponent<PlayerPawn>();
|
|
playerGameDatasDict[playerGameData.playerType].playerPawnsDict.Add(pawn.PlayerId, pawn);
|
|
}
|
|
|
|
playerGameDatasDict[playerGameData.playerType].totalPawnsInHome = playerGameDatasDict[playerGameData.playerType].playerPawnsDict.Count;
|
|
}
|
|
}
|
|
|
|
public void SetPlayerSelectionStates(bool state, Predicate<PlayerState> skipPredicate = null)
|
|
{
|
|
foreach (var playerPawn in playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict)
|
|
{
|
|
if (skipPredicate != null && skipPredicate.Invoke(playerPawn.Value.GetPlayerState()))
|
|
{
|
|
playerPawn.Value.SetPlayerSelectionState(false);
|
|
continue;
|
|
}
|
|
|
|
playerPawn.Value.SetPlayerSelectionState(state);
|
|
}
|
|
}
|
|
|
|
public void OnDiceInteracted()
|
|
{
|
|
RollDiceForUser();
|
|
}
|
|
|
|
public void RollDiceForPlayer(int rolledVal)
|
|
{
|
|
OnDiceRolled(rolledVal);
|
|
OnNoMovesLeft();
|
|
}
|
|
|
|
// Summary :: Function will be called and effective when a dice is rolled and no selection of pawns is made.
|
|
private void OnNoMovesLeft()
|
|
{
|
|
if (canSwitchPlayer && !CanRollDiceAgain)
|
|
{
|
|
Debug.Log($"Switching player");
|
|
SwitchPlayer();
|
|
}
|
|
}
|
|
|
|
private bool CheckForMaxDiceRollAttempt()
|
|
{
|
|
if (diceSixRollCounter > maxDiceSixRollCounter) // TODO :: Reset after test
|
|
{
|
|
CanRollDiceAgain = false;
|
|
SwitchPlayer();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void RollDiceForBot(int rolledVal)
|
|
{
|
|
Debug.Log($"CallTest: RollDiceForBot");
|
|
|
|
// OnDiceRolled(diceValue == 0 ? UnityEngine.Random.Range(1, Ludo_3D_Constants.Max_Dice_Rolls + 1) : diceValue);
|
|
OnDiceRolled(rolledVal);
|
|
|
|
SelectPawnFromBotBase();
|
|
|
|
OnNoMovesLeft();
|
|
}
|
|
|
|
// TODO :: Call right after the dice is rolled with the animation
|
|
// What happens when you get a 6
|
|
private void SelectPawnFromBotBase()
|
|
{
|
|
if (canSwitchPlayer || availPlayersToMove.Count() < 1 && !CanRollDiceAgain || !botTypesInGame.Contains(currentPlayerTypeTurn))
|
|
{
|
|
Debug.Log($"returning from SelectPawnFromBotBase");
|
|
return; // Have a better check here
|
|
}
|
|
|
|
Debug.Log($"CallTest: SelectPawnFromBotBase: {currentPlayerTypeTurn}");
|
|
var botPawnsDictForCurrentPlayer = botRuntimeMovementData[currentPlayerTypeTurn]; // set the data inside this dict
|
|
|
|
int savedPlayerId = -1;
|
|
PlayerPawn pawn = null;
|
|
|
|
Debug.Log($"SelectPawnFromBotBase: availPlayers.Count(): {availPlayersToMove.Count()}, CanRollDiceAgain: {CanRollDiceAgain}");
|
|
|
|
InitActivePlayers();
|
|
#if UNITY_EDITOR
|
|
if (playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome == 0)
|
|
{
|
|
SetDisplayCountForAllAvailPlayers(true);
|
|
}
|
|
#endif
|
|
|
|
if (CanRollDiceAgain) // got a 6 roll value
|
|
{
|
|
pawn = playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Values.FirstOrDefault(pawn => pawn.GetPlayerState() == PlayerState.InHome);
|
|
|
|
if (pawn != null)
|
|
{
|
|
Debug.Log($"SelectedPawn: {pawn.name}");
|
|
OnPawnSelected(pawn);
|
|
return;
|
|
}
|
|
}
|
|
|
|
Debug.Log($"Before Iterating");
|
|
foreach (var playerPawn in availPlayersToMove)
|
|
{
|
|
int possibleSteps = 0;
|
|
Tile possibleTileData = null;
|
|
|
|
if (playerPawn.GetPlayerState() != PlayerState.InFinishingPath)
|
|
FindPossibleTileData(playerPawn, out possibleSteps, out possibleTileData);
|
|
|
|
if (playerPawn.GetPlayerState() == PlayerState.InFinishingPath || possibleSteps > playerGameDatasDict[currentPlayerTypeTurn].endIndex) // TODO :: have a better second check
|
|
{
|
|
Debug.Log($"AI playerPawn :: {playerPawn.name} :: {playerPawn.PlayerId} :: inFinishingPath :: canSelectPlayer: {playerPawn.CanSelectPlayer}");
|
|
if (playerPawn.CanSelectPlayer)
|
|
{
|
|
botPawnsDictForCurrentPlayer[playerPawn.PlayerId] = BotMove.FinishingPathMove;
|
|
savedPlayerId = playerPawn.PlayerId;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
botPawnsDictForCurrentPlayer[playerPawn.PlayerId] = BotMove.NoMoves;
|
|
}
|
|
}
|
|
else if (possibleTileData != null && possibleTileData.IsSafeZone)
|
|
{
|
|
Debug.Log($"AI playerPawn :: {playerPawn.name} :: {playerPawn.PlayerId} :: safeMove");
|
|
botPawnsDictForCurrentPlayer[playerPawn.PlayerId] = BotMove.SafeMove;
|
|
}
|
|
else if (possibleTileData != null && possibleTileData.HasPawnsAvailable && possibleTileData.CurrentHoldingPlayerType != playerPawn.PlayerType)
|
|
{
|
|
Debug.Log($"AI playerPawn :: {playerPawn.name} :: {playerPawn.PlayerId} :: attackMove");
|
|
botPawnsDictForCurrentPlayer[playerPawn.PlayerId] = BotMove.AttackMove;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"AI playerPawn :: {playerPawn.name} :: {playerPawn.PlayerId} :: normalMove");
|
|
botPawnsDictForCurrentPlayer[playerPawn.PlayerId] = BotMove.NormalMove;
|
|
}
|
|
}
|
|
|
|
List<int> playerIds = new List<int>();
|
|
|
|
if (savedPlayerId != -1)
|
|
Debug.Log($"SavedPlayerId: {savedPlayerId}, CanSelectPlayer: {playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict[savedPlayerId].CanSelectPlayer}");
|
|
if (savedPlayerId != -1 && playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict[savedPlayerId].CanSelectPlayer)
|
|
{
|
|
pawn = playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict[savedPlayerId]; // chances are when one of the character reaches towards the finishing point
|
|
}
|
|
else
|
|
{
|
|
if (botPawnsDictForCurrentPlayer.ContainsValue(BotMove.SafeMove) && botPawnsDictForCurrentPlayer.ContainsValue(BotMove.AttackMove))
|
|
{
|
|
InitPlayerIdsBasedOnState((val) => val == BotMove.AttackMove || val == BotMove.SafeMove);
|
|
|
|
savedPlayerId = playerIds[UnityEngine.Random.Range(0, playerIds.Count)];
|
|
pawn = playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict[savedPlayerId];
|
|
Debug.Log($"AI playerPawn :: {pawn.name} :: Getting savedplayer id");
|
|
}
|
|
else if (botPawnsDictForCurrentPlayer.ContainsValue(BotMove.SafeMove))
|
|
{
|
|
InitPawnBasedOnState(BotMove.SafeMove);
|
|
Debug.Log($"AI playerPawn :: {pawn.name} :: Getting savedplayer id");
|
|
}
|
|
else if (botPawnsDictForCurrentPlayer.ContainsValue(BotMove.AttackMove))
|
|
{
|
|
InitPawnBasedOnState(BotMove.AttackMove);
|
|
Debug.Log($"AI playerPawn :: {pawn.name} :: Getting savedplayer id");
|
|
}
|
|
else if (botPawnsDictForCurrentPlayer.ContainsValue(BotMove.NormalMove))
|
|
{
|
|
InitPawnBasedOnState(BotMove.NormalMove);
|
|
Debug.Log($"AI playerPawn :: {pawn.name} :: Getting savedplayer id");
|
|
}
|
|
}
|
|
|
|
if (pawn != null)
|
|
{
|
|
Debug.Log($"AI playerPawn :: {pawn.name} :: SelectedPawn: {pawn.name}");
|
|
OnPawnSelected(pawn);
|
|
}
|
|
else
|
|
{
|
|
if (CheckForMaxDiceRollAttempt())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (CanRollDiceAgain)
|
|
{
|
|
RollDiceForBot();
|
|
}
|
|
else
|
|
SwitchPlayer();
|
|
}
|
|
|
|
void InitPawnBasedOnState(BotMove botMove)
|
|
{
|
|
InitPlayerIdsBasedOnState((val) => val == botMove);
|
|
ReturnPlayerWithMaxSteps(ref savedPlayerId);
|
|
pawn = playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict[savedPlayerId];
|
|
}
|
|
|
|
void InitPlayerIdsBasedOnState(Predicate<BotMove> state)
|
|
{
|
|
foreach (var botPair in botPawnsDictForCurrentPlayer)
|
|
if (state.Invoke(botPair.Value))
|
|
playerIds.Add(botPair.Key);
|
|
}
|
|
|
|
void ReturnPlayerWithMaxSteps(ref int savedPlayerId)
|
|
{
|
|
int maxStepsTaken = -999;
|
|
foreach (var id in playerIds)
|
|
{
|
|
if (playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict[id].StepsTaken > maxStepsTaken)
|
|
{
|
|
maxStepsTaken = playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict[id].StepsTaken;
|
|
savedPlayerId = id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FindPossibleTileData(PlayerPawn playerPawn, out int possibleSteps, out Tile possibleTileData)
|
|
{
|
|
int lastStepGenTile = totalStepsForCharacter - TilesManager.GetFinishingTileDataLength(playerPawn.PlayerType);//playerGameDatasDict[playerPawn.PlayerType].endIndex;
|
|
int possibleTileIndex = playerPawn.CurrentTileIndex + diceRolledValue;
|
|
|
|
int lastTileIndex = TilesManager.GetGeneralTilesLength() - 1;
|
|
bool canGoingInsideFinishingPath = IsGoingInsideFinishingPath(playerPawn, out possibleSteps);
|
|
|
|
int index = canGoingInsideFinishingPath ? possibleSteps - lastStepGenTile - 1
|
|
: possibleTileIndex > lastTileIndex ? possibleTileIndex - lastTileIndex - 1 : possibleTileIndex; // case for addressing the scenario when going through the last index of general tiles.
|
|
|
|
Debug.Log($"possibleTileIndex: {possibleTileIndex}, lastStepGenTileIdx: {lastStepGenTile}");
|
|
Debug.Log($"index: {index}");
|
|
possibleTileData = canGoingInsideFinishingPath ? TilesManager.RetrieveFinishingTileBasedOnIndex(playerPawn.PlayerType, index) : TilesManager.RetrieveTileBasedOnIndex(index);
|
|
}
|
|
|
|
public bool IsGoingInsideFinishingPath(PlayerPawn playerPawn, out int possibleSteps)
|
|
{
|
|
possibleSteps = playerGameDatasDict[playerPawn.PlayerType].playerPawnsDict[playerPawn.PlayerId].StepsTaken + diceRolledValue;
|
|
return possibleSteps > TilesManager.GetGeneralTilesLength() - 1;
|
|
}
|
|
|
|
private bool HasNoPlayersTravelling() => Mathf.Abs(availPlayersToMove.Count - playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath) < 1;
|
|
|
|
private bool IsUsersTurn() => gameModeHandler.CurrentGameModeType != GameModeType.Bot ||
|
|
!botTypesInGame.Contains(currentPlayerTypeTurn);
|
|
|
|
public void OnDiceRolled(int rolledVal)
|
|
{
|
|
SetCanRollDiceForUser(false);
|
|
|
|
// add core dice logic here
|
|
Debug.Log($"Tile Index :: LUDO :: rolledVal: {rolledVal} :: {currentPlayerTypeTurn}");
|
|
diceRolledValue = rolledVal;
|
|
diceText.text = $"{diceRolledValue}";
|
|
|
|
if (!CanRollDiceAgain) // remove this check for showing arrow logic
|
|
{
|
|
#if UNITY_EDITOR
|
|
SetDisplayCountForAllAvailPlayers(true);
|
|
#endif
|
|
}
|
|
|
|
FilterAvailablePlayersToMove();
|
|
if (rolledVal == Ludo_3D_Constants.Max_Dice_Rolls)
|
|
{
|
|
canSwitchPlayer = false;
|
|
// provide option to select a pawn from the list
|
|
// also play a simple animation before selecting
|
|
CanRollDiceAgain = true;
|
|
diceSixRollCounter++;
|
|
|
|
if (IsUsersTurn())
|
|
{
|
|
Debug.Log($"availPlayersToMove.Count < 1: {availPlayersToMove.Count < 1} || HasNoPlayersTravelling(): {HasNoPlayersTravelling()}");
|
|
if (availPlayersToMove.Count < 1 || HasNoPlayersTravelling())
|
|
{
|
|
if (playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome > 0)
|
|
{
|
|
OnPawnSelected(playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Values
|
|
.FirstOrDefault(pawn => pawn.GetPlayerState() == PlayerState.InHome));
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome < 1 && CanMoveSoloPlayer())
|
|
{
|
|
SetPlayerSelectionStates(false);
|
|
OnPawnSelected(availPlayersToMove[0]);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
pointerMeshRend.material = selectMat;
|
|
#endif
|
|
|
|
Debug.Log($"### AreAllPawnsInFinishingPath");
|
|
if (AreAllPawnsInFinishingPath())
|
|
{
|
|
SetCanRollDiceForUser(IsUsersTurn());
|
|
UpdateResponseTimerForUser(currentPlayerSelectionMaxTime, () => RollDiceForUser());
|
|
CheckForMaxDiceRollAttempt();
|
|
|
|
return;
|
|
}
|
|
|
|
SetPlayerSelectionStates(true, (state) => state == PlayerState.InFinishingPath || state == PlayerState.HasFinished);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"before CustomAvailablePlayers: {availPlayersToMove.Count}");
|
|
|
|
Debug.Log($"CustomAvailablePlayers: {availPlayersToMove.Count}");
|
|
canSwitchPlayer = availPlayersToMove.Count < 1;
|
|
CanRollDiceAgain = false;
|
|
}
|
|
|
|
diceRollHandler.DiceView.SetDiceButtonInteraction(CanRollDiceAgain);
|
|
Debug.Log($"CanRollDiceAgain: {CanRollDiceAgain}, canSwitchPlayer: {canSwitchPlayer}");
|
|
|
|
if (IsUsersTurn())
|
|
{
|
|
Action onComplete = null;
|
|
if (CanRollDiceAgain && availPlayersToMove.Count < 1 &&
|
|
playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome > 0)
|
|
{
|
|
onComplete = () => OnPawnSelected(playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Values
|
|
.FirstOrDefault(pawn => pawn.GetPlayerState() == PlayerState.InHome));
|
|
|
|
UpdateResponseTimerForUser(currentPlayerSelectionMaxTime, () => onComplete?.Invoke());
|
|
}
|
|
else if (availPlayersToMove.Count > 0)
|
|
{
|
|
if (CanRollDiceAgain && playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome > 0)
|
|
{
|
|
onComplete = () => OnPawnSelected(playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Values
|
|
.FirstOrDefault(pawn => pawn.GetPlayerState() == PlayerState.InHome));
|
|
UpdateResponseTimerForUser(currentPlayerSelectionMaxTime, () => onComplete?.Invoke());
|
|
return;
|
|
}
|
|
|
|
if (CanMoveSoloPlayer())
|
|
{
|
|
SetPlayerSelectionStates(false);
|
|
OnPawnSelected(availPlayersToMove[0]);
|
|
return;
|
|
}
|
|
|
|
onComplete = () => OnPawnSelected(availPlayersToMove.OrderByDescending(pawn => pawn.StepsTaken).FirstOrDefault());
|
|
UpdateResponseTimerForUser(currentPlayerSelectionMaxTime, () => onComplete?.Invoke());
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FilterAvailablePlayersToMove()
|
|
{
|
|
List<int> indexesToRemove = new List<int>();
|
|
|
|
for (int i = 0; i < availPlayersToMove.Count; i++)
|
|
{
|
|
Debug.Log($"## playerPawn.GetPlayerState(): {availPlayersToMove[i].GetPlayerState()}");
|
|
|
|
if (availPlayersToMove[i].GetPlayerState() == PlayerState.InFinishingPath)
|
|
{
|
|
Debug.Log($"diceRolledValue: {diceRolledValue}, FinishingDataLen: {TilesManager.GetFinishingTileDataLength(currentPlayerTypeTurn)}, playerPawn.CurrentTileIndex: {availPlayersToMove[i].CurrentTileIndex}");
|
|
if (diceRolledValue <= TilesManager.GetFinishingTileDataLength(currentPlayerTypeTurn) - (availPlayersToMove[i].CurrentTileIndex + 1))
|
|
{
|
|
availPlayersToMove[i].SetPlayerSelectionState(true);
|
|
}
|
|
else
|
|
{
|
|
indexesToRemove.Add(i);
|
|
availPlayersToMove[i].SetPlayerSelectionState(false);
|
|
#if UNITY_EDITOR
|
|
availPlayersToMove[i].ShowPlayerCountCanvas(false);
|
|
#endif
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
availPlayersToMove[i].SetPlayerSelectionState(true);
|
|
}
|
|
|
|
for (int idx = indexesToRemove.Count - 1; idx >= 0; idx--)
|
|
availPlayersToMove.RemoveAt(idx);
|
|
}
|
|
|
|
private bool CanMoveSoloPlayer()
|
|
{
|
|
if (availPlayersToMove.Count == 1)
|
|
{
|
|
if (availPlayersToMove[0].GetPlayerState() == PlayerState.InSafeZone || availPlayersToMove[0].GetPlayerState() == PlayerState.Moving
|
|
|| availPlayersToMove[0].GetPlayerState() == PlayerState.InFinishingPath)
|
|
{
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void InitActivePlayers()
|
|
{
|
|
availPlayersToMove = new List<PlayerPawn>();
|
|
availPlayersToMove = playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Values.Select(pawn => pawn)
|
|
.Where(pawn => pawn.GetPlayerState() == PlayerState.InSafeZone ||
|
|
pawn.GetPlayerState() == PlayerState.Moving ||
|
|
pawn.GetPlayerState() == PlayerState.InFinishingPath).ToList();
|
|
}
|
|
|
|
private bool AreAllPawnsInFinishingPath()
|
|
{
|
|
bool areAllPawnsInFinishingPath = false;
|
|
foreach (var pawn in playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict)
|
|
{
|
|
if (pawn.Value.GetPlayerState() == PlayerState.HasFinished) continue;
|
|
|
|
if (pawn.Value.GetPlayerState() == PlayerState.InFinishingPath)
|
|
{
|
|
areAllPawnsInFinishingPath = true;
|
|
continue;
|
|
}
|
|
|
|
areAllPawnsInFinishingPath = false;
|
|
break;
|
|
}
|
|
|
|
return areAllPawnsInFinishingPath;
|
|
}
|
|
|
|
private void UpdatePlayerState(PlayerPawn playerPawn, PlayerState playerState)
|
|
{
|
|
if (!playerPawn)
|
|
{
|
|
// Debug.LogError($"Player pawn is null");
|
|
return;
|
|
}
|
|
|
|
Debug.Log($"#### UpdatePlayerState ");
|
|
playerGameDatasDict[playerPawn.PlayerType].playerPawnsDict[playerPawn.PlayerId] = playerPawn;
|
|
playerGameDatasDict[playerPawn.PlayerType].playerPawnsDict[playerPawn.PlayerId].SetPlayerState(playerState);
|
|
playerPawn.SetPlayerState(playerState);
|
|
}
|
|
|
|
private void CheckDiceRollForBot()
|
|
{
|
|
if (CanRollDiceAgain)
|
|
{
|
|
RollDiceForBot();
|
|
}
|
|
}
|
|
|
|
private void RollDiceForUser()
|
|
{
|
|
ResetCurrentPlayerTurnTimer();
|
|
InitActivePlayers();
|
|
bool canUsePawnsFromHome = CanUsePawnsFromHome();
|
|
diceRollHandler.HandleDiceViewForUser(
|
|
hasNoActionOnRoll: canUsePawnsFromHome,
|
|
playerBase: canUsePawnsFromHome ? PlayerBaseHandler.GetPlayerBase(currentPlayerTypeTurn) : null);
|
|
}
|
|
|
|
private void RollDiceForBot()
|
|
{
|
|
ResetCurrentPlayerTurnTimer();
|
|
InitActivePlayers();
|
|
HandleDiceRollForBot();
|
|
}
|
|
|
|
/***
|
|
* Summary:
|
|
* Scenario where the user/bot has no other move when some of the pawns/characters out of home
|
|
* are in finishing path and doesn't have a move from dice rolled value
|
|
*/
|
|
private bool CanUsePawnsFromHome()
|
|
{
|
|
return playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome > 0 && HasNoPlayersTravelling();
|
|
}
|
|
|
|
private void HandleDiceRollForBot()
|
|
{
|
|
if (isDebugAITest)
|
|
diceRollHandler.HandleDiceViewForBot((rollVal) => RollDiceForBot(rollVal),
|
|
diceValue == 0 ? UnityEngine.Random.Range(1, Ludo_3D_Constants.Max_Dice_Rolls + 1) : diceValue);
|
|
else
|
|
{
|
|
bool canUsePawnsFromHome = CanUsePawnsFromHome();
|
|
diceRollHandler.HandleDiceViewForBot(
|
|
(rollVal) => RollDiceForBot(rollVal),
|
|
hasNoActionOnRoll: canUsePawnsFromHome,
|
|
playerBase: canUsePawnsFromHome ? PlayerBaseHandler.GetPlayerBase(currentPlayerTypeTurn) : null);
|
|
}
|
|
}
|
|
|
|
public void OnPawnSelected(PlayerPawn selectedPawn)
|
|
{
|
|
// TODO :: Hide the selectable characters indicator here
|
|
SetPlayerSelectionStates(false);
|
|
if (IsUsersTurn()) currentPlayerTurnTimer.KillTimer();
|
|
|
|
PlayerGameData playerGameData = playerGameDatasDict[currentPlayerTypeTurn];
|
|
Debug.Log($"playerPawn.GetPlayerState(): {selectedPawn.GetPlayerState()}");
|
|
|
|
#if UNITY_EDITOR
|
|
selectedPawn.ShowPlayerCountCanvas(false);
|
|
#endif
|
|
if (selectedPawn.GetPlayerState() == PlayerState.InHome)
|
|
{
|
|
Tile targetTile = TilesManager.RetrieveTileBasedOnIndex(playerGameData.startIndex);
|
|
|
|
selectedPawn.MoveToTile(
|
|
TilesManager.GetAndInitPositionInsideSafeZone(selectedPawn, targetTile, currentPlayerTypeTurn),
|
|
onComplete: () =>
|
|
{
|
|
playerGameDatasDict[playerGameData.playerType].totalPawnsInHome--;
|
|
UpdatePlayerState(selectedPawn, PlayerState.InSafeZone);
|
|
#if UNITY_EDITOR
|
|
ShowUpdatedPlayerCountOnTile(selectedPawn);
|
|
#endif
|
|
if (CheckForMaxDiceRollAttempt())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (selectedPawn.IsBotPlayer)
|
|
CheckDiceRollForBot();
|
|
else
|
|
{
|
|
SetCanRollDiceForUser(IsUsersTurn());
|
|
UpdateResponseTimerForUser(currentPlayerMaxTime: currentPlayerTurnMaxTime, () => RollDiceForUser());
|
|
}
|
|
|
|
}, playerGameData.startIndex);
|
|
|
|
return;
|
|
}
|
|
else if (selectedPawn.GetPlayerState() == PlayerState.InFinishingPath)
|
|
{
|
|
Tile currentSittingTile = TilesManager.RetrieveFinishingTileBasedOnIndex(selectedPawn.PlayerType, selectedPawn.CurrentTileIndex);
|
|
currentSittingTile.ResetPlayerPawn(selectedPawn);
|
|
|
|
#if UNITY_EDITOR
|
|
if (currentSittingTile.HasPawnsAvailable)
|
|
{
|
|
var playerPawns = currentSittingTile.GetPlayerPawns();
|
|
foreach (var pawn in playerPawns)
|
|
ShowUpdatedPlayerCountOnTile(pawn);
|
|
}
|
|
#endif
|
|
|
|
ApplyFinishingPathLogic(selectedPawn);
|
|
}
|
|
else if (selectedPawn.CurrentTileIndex == playerGameDatasDict[currentPlayerTypeTurn].endIndex)
|
|
{
|
|
TilesManager.RetrieveTileBasedOnIndex(selectedPawn.CurrentTileIndex).ResetPlayerPawn(selectedPawn);
|
|
playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath++;
|
|
ApplyFinishingPathLogic(selectedPawn);
|
|
}
|
|
else if (selectedPawn.GetPlayerState() == PlayerState.InSafeZone || selectedPawn.GetPlayerState() == PlayerState.Moving)
|
|
{
|
|
// move based on the dice value
|
|
Debug.Log($"Tile Index :: currentTileIndex: {selectedPawn.CurrentTileIndex}");
|
|
int nextTileIdx = TilesManager.GetNextGeneralTileIndex(selectedPawn.CurrentTileIndex);
|
|
int targetIdx = selectedPawn.CurrentTileIndex + diceRolledValue;
|
|
|
|
if (nextTileIdx == 0)
|
|
targetIdx = (targetIdx - selectedPawn.CurrentTileIndex) - 1;
|
|
|
|
Tile currentSittingTile = TilesManager.RetrieveTileBasedOnIndex(selectedPawn.CurrentTileIndex);
|
|
if (currentSittingTile.IsSafeZone)
|
|
{
|
|
SafeTile currentSittingSafeTile = (SafeTile)currentSittingTile;
|
|
currentSittingSafeTile.UpdateSafeZonePlayerData(currentPlayerTypeTurn, selectedPawn);
|
|
|
|
if (currentSittingSafeTile.PlayerTypesCount == 1) // && currentSittingSafeTile.ContainsPlayerType(selectedPawn.PlayerType))
|
|
{
|
|
var playerPawnsTest = currentSittingSafeTile.GetFirstPlayerPawns();
|
|
foreach (var pawn in playerPawnsTest)
|
|
pawn.MoveToCustomTilePosition(currentSittingSafeTile.CenterPlacementPosition);
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
if (currentSittingSafeTile.ContainsPlayerType(currentPlayerTypeTurn))
|
|
{
|
|
var playerPawns = currentSittingSafeTile.GetPlayerPawns(currentPlayerTypeTurn);
|
|
foreach (var pawn in playerPawns)
|
|
ShowUpdatedPlayerCountOnTile(pawn);
|
|
}
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
currentSittingTile.ResetPlayerPawn(selectedPawn);
|
|
#if UNITY_EDITOR
|
|
if (currentSittingTile.HasPawnsAvailable)
|
|
{
|
|
var playerPawns = currentSittingTile.GetPlayerPawns();
|
|
foreach (var pawn in playerPawns)
|
|
ShowUpdatedPlayerCountOnTile(pawn);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
MoveThroughTiles(selectedPawn, nextTileIdx, targetIndex: targetIdx);
|
|
}
|
|
}
|
|
|
|
private void ApplyFinishingPathLogic(PlayerPawn playerPawn)
|
|
{
|
|
int finishingPathIndex = TilesManager.GetNextFinishingTileIndex(playerPawn.CurrentTileIndex, playerPawn.PlayerType);
|
|
int targetIdx = finishingPathIndex + diceRolledValue > TilesManager.GetFinishingTileDataLength(currentPlayerTypeTurn) - 1 ?
|
|
TilesManager.GetFinishingTileDataLength(currentPlayerTypeTurn) - 1 : finishingPathIndex + diceRolledValue;
|
|
|
|
Debug.Log($"TargetIdx: {targetIdx}, finishingPathIndex: {finishingPathIndex}");
|
|
|
|
MoveThroughFinishingPath(playerPawn, finishingPathIndex, targetIdx);
|
|
}
|
|
|
|
// TODO :: move to tiles manager
|
|
|
|
|
|
private void SwitchPlayer(PlayerPawn playerPawn = null)
|
|
{
|
|
Debug.Log($"CallTest: SwitchPlayer");
|
|
if (playerPawn)
|
|
UpdatePlayerState(playerPawn, TilesManager.RetrieveTileBasedOnIndex(playerPawn.CurrentTileIndex).IsSafeZone ? PlayerState.InSafeZone : PlayerState.Moving);
|
|
|
|
if (!CanRollDiceAgain)
|
|
{
|
|
Debug.Log($"currentPlayerTurn: {currentPlayerTypeTurn}");
|
|
Debug.Log($"currentPlayerTurnIndex: {currentPlayerTurnIndex}");
|
|
|
|
Debug.Log($"before SwitchPlayer availPlayers: {availPlayersToMove.Count}, playerPawn: {playerPawn}");
|
|
|
|
InitActivePlayers();
|
|
#if UNITY_EDITOR
|
|
SetDisplayCountForAllAvailPlayers(false);
|
|
#endif
|
|
Debug.Log($"after SwitchPlayer availPlayers: {availPlayersToMove.Count}, playerPawn: {playerPawn}");
|
|
Debug.Log($"after allPlayerTypes.Count: {allPlayerTypes.Count}");
|
|
|
|
if (allPlayerTypes.Count == 0)
|
|
{
|
|
Debug.LogError($"GAME IS OVER");
|
|
return;
|
|
}
|
|
|
|
currentPlayerTurnIndex = allPlayerTypes.FindIndex(type => type == currentPlayerTypeTurn);
|
|
if (currentPlayerTypeTurn == allPlayerTypes[allPlayerTypes.Count - 1])
|
|
{
|
|
currentPlayerTurnIndex = 0;
|
|
}
|
|
else
|
|
{
|
|
currentPlayerTurnIndex++;
|
|
}
|
|
|
|
soundManager.PlayGameSoundClip(SoundType.Turn);
|
|
UpdateCurrentPlayerTurn(allPlayerTypes[currentPlayerTurnIndex]);
|
|
|
|
Debug.Log($"IsUsersTurn: {IsUsersTurn()}");
|
|
if (IsUsersTurn())
|
|
{
|
|
SetCanRollDiceForUser(true);
|
|
Invoke(nameof(UpdateDiceView), diceRollDelayForUser);
|
|
}
|
|
else
|
|
{
|
|
SetCanRollDiceForUser(false);
|
|
currentPlayerTurnTimer?.KillTimer();
|
|
Invoke(nameof(UpdateDiceViewForBot), diceRollDelayForBot);
|
|
}
|
|
|
|
diceSixRollCounter = 0;
|
|
diceText.text = $"{0}";
|
|
}
|
|
// else
|
|
// {
|
|
// UpdateResponseTimerForUser(currentPlayerMaxTime: currentPlayerTurnMaxTime, () => RollDiceForUser());
|
|
//
|
|
// if (gameModeHandler.CurrentGameModeType == GameModeType.Bot && botTypesInGame.Contains(currentPlayerTypeTurn)) // TODO :: Double check calling of the function
|
|
// {
|
|
// Debug.Log($"Invoking RollDiceForBot");
|
|
// HandleDiceRollWithDelay();
|
|
// }
|
|
// }
|
|
|
|
Debug.Log($"currentPlayerTurnIndex: {currentPlayerTurnIndex}");
|
|
Debug.Log($"CurrentPlayerTurn: {currentPlayerTypeTurn}");
|
|
|
|
#if UNITY_EDITOR
|
|
SetCurrentSelectedPointer();
|
|
pointerMeshRend.material = turnMat;
|
|
// pointerMeshRend.materials[0] = turnMat;
|
|
#endif
|
|
}
|
|
|
|
private void OnCanRollDiceAgain(PlayerPawn playerPawn = null)
|
|
{
|
|
if (playerPawn)
|
|
UpdatePlayerState(playerPawn, TilesManager.RetrieveTileBasedOnIndex(playerPawn.CurrentTileIndex).IsSafeZone ? PlayerState.InSafeZone : PlayerState.Moving);
|
|
|
|
UpdateResponseTimerForUser(currentPlayerMaxTime: currentPlayerTurnMaxTime, () => RollDiceForUser());
|
|
|
|
if (gameModeHandler.CurrentGameModeType == GameModeType.Bot && botTypesInGame.Contains(currentPlayerTypeTurn)) // TODO :: Double check calling of the function
|
|
{
|
|
Debug.Log($"Invoking RollDiceForBot");
|
|
HandleDiceRollWithDelay();
|
|
}
|
|
}
|
|
|
|
private void MoveThroughTiles(PlayerPawn playerPawn, int index, int targetIndex)
|
|
{
|
|
Tile nextTile = TilesManager.RetrieveTileBasedOnIndex(index);
|
|
Vector3 targetPosition = nextTile.CenterPlacementPosition;
|
|
|
|
Debug.Log($"Tile Index :: nextIndex: {index}, targetIndex: {targetIndex}, nextTileName: {nextTile.name}");
|
|
|
|
if (index == targetIndex) // if the target index is the safe zone only then apply the logic for rearranging pawns
|
|
{
|
|
Tile targetTile = TilesManager.RetrieveTileBasedOnIndex(targetIndex);
|
|
if (targetTile.IsSafeZone)
|
|
{
|
|
targetPosition = TilesManager.GetAndInitPositionInsideSafeZone(playerPawn, targetTile, currentPlayerTypeTurn);
|
|
}
|
|
}
|
|
|
|
Debug.Log($"tile targetPosition: {targetPosition}");
|
|
#if UNITY_EDITOR
|
|
playerPawn.ShowPlayerCountCanvas(false); // TODO :: Check if call can be removed
|
|
#endif
|
|
|
|
playerPawn.MoveToTile(
|
|
targetPosition,
|
|
onComplete: () =>
|
|
{
|
|
diceRolledValue--;
|
|
Debug.Log($"DiceRolledValue: {diceRolledValue}");
|
|
if (diceRolledValue > 0)
|
|
{
|
|
int nextTileIndex = TilesManager.GetNextGeneralTileIndex(playerPawn.CurrentTileIndex);
|
|
Debug.Log($"currentTileIndex: {playerPawn.CurrentTileIndex}, nextTileIndex: {nextTileIndex}, targetIndex: {targetIndex}");
|
|
|
|
if (playerPawn.GetPlayerState() == PlayerState.InFinishingPath || index == playerGameDatasDict[currentPlayerTypeTurn].endIndex)
|
|
{
|
|
// MoveThroughTiles(playerPawn, index, targetIndex);
|
|
Debug.Log($"TargetIdx: {targetIndex - index}");
|
|
if (index == playerGameDatasDict[currentPlayerTypeTurn].endIndex)
|
|
playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath++;
|
|
|
|
CheckForGamePause(() => MoveThroughFinishingPath(playerPawn, 0, targetIndex - index));
|
|
}
|
|
else if (nextTileIndex <= targetIndex)
|
|
{
|
|
if (nextTileIndex == 0)
|
|
targetIndex = (targetIndex - playerPawn.CurrentTileIndex) - 1;
|
|
|
|
CheckForGamePause(() => MoveThroughTiles(playerPawn, nextTileIndex, targetIndex));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// TODO :: Improve this logic, use a collection
|
|
Debug.Log($"nextTile.IsSafeZone: {nextTile.IsSafeZone}");
|
|
|
|
if (CanRollDiceAgain)
|
|
{
|
|
#if UNITY_EDITOR
|
|
ShowUpdatedPlayerCountOnTile(playerPawn);
|
|
#endif
|
|
}
|
|
|
|
if (!nextTile.IsSafeZone)
|
|
{
|
|
Debug.Log($"nextTile.HasPawnsAvailable: {nextTile.HasPawnsAvailable}");
|
|
if (nextTile.HasPawnsAvailable && playerPawn.PlayerType != nextTile.CurrentHoldingPlayerType)
|
|
{
|
|
Debug.Log($"nextTile.PlayerPawn: {nextTile.CurrentHoldingPlayerType}, {nextTile.transform.name}");
|
|
Debug.Log($"nextTile.TotalPawnsInTile: {nextTile.TotalPawnsInTile}");
|
|
// play animation for moving back to base.
|
|
// TODO :: Send existing pawn back to base.
|
|
// means there's already a pawn there, move him back to the base.
|
|
int counter = nextTile.TotalPawnsInTile;
|
|
for (int i = counter; i > 0; i--)
|
|
{
|
|
var pawn = nextTile.GetPlayerPawn();
|
|
playerGameDatasDict[pawn.PlayerType].totalPawnsInHome++;
|
|
playerBaseHandler.SendPlayerToHome(pawn);
|
|
}
|
|
|
|
if (CheckForMaxDiceRollAttempt())
|
|
{
|
|
nextTile.InitPlayerPawn(playerPawn, currentPlayerTypeTurn);
|
|
// UpdatePlayerCountOnTile(playerPawn, true);
|
|
|
|
return;
|
|
}
|
|
|
|
CanRollDiceAgain = true;
|
|
}
|
|
|
|
nextTile.InitPlayerPawn(playerPawn, currentPlayerTypeTurn);
|
|
#if UNITY_EDITOR
|
|
ShowUpdatedPlayerCountOnTile(playerPawn);
|
|
#endif
|
|
|
|
if (CheckForMaxDiceRollAttempt())
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (CheckForMaxDiceRollAttempt())
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!CanRollDiceAgain) SwitchPlayer(playerPawn);
|
|
else
|
|
{
|
|
if (IsUsersTurn())
|
|
{
|
|
UpdateResponseTimerForUser(currentPlayerMaxTime: currentPlayerTurnMaxTime, () => RollDiceForUser());
|
|
SetCanRollDiceForUser(IsUsersTurn());
|
|
}
|
|
|
|
OnCanRollDiceAgain(playerPawn);
|
|
diceRollHandler.DiceView.SetDiceButtonInteraction(true);
|
|
}
|
|
}
|
|
},
|
|
index);
|
|
}
|
|
|
|
public void CheckForGamePause(Action onComplete)
|
|
{
|
|
Debug.Log($"CheckForGamePause: {GameManager.CurrentGameState == GameState.IsPaused}");
|
|
if ((botTypesInGame == null || !botTypesInGame.Contains(currentPlayerTypeTurn)) &&
|
|
GameManager.CurrentGameState == GameState.IsPaused)
|
|
{
|
|
OnGameResumed = onComplete;
|
|
}
|
|
else
|
|
{
|
|
onComplete?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void MoveThroughFinishingPath(PlayerPawn playerPawn, int index, int targetIndex)
|
|
{
|
|
UpdatePlayerState(playerPawn, PlayerState.InFinishingPath);
|
|
|
|
playerPawn.MoveToTile(
|
|
TilesManager.RetrieveFinishingTileBasedOnIndex(currentPlayerTypeTurn, index).transform.position,
|
|
onComplete: () =>
|
|
{
|
|
diceRolledValue--;
|
|
|
|
Debug.Log($"DiceRolledValue: {diceRolledValue}");
|
|
if (diceRolledValue > 0)
|
|
{
|
|
int tileIndex = TilesManager.GetNextFinishingTileIndex(playerPawn.CurrentTileIndex, playerPawn.PlayerType);
|
|
Debug.Log($"tileIndex: {tileIndex}, targetIndex: {targetIndex}");
|
|
if (tileIndex <= targetIndex)
|
|
{
|
|
CheckForGamePause(() => MoveThroughFinishingPath(playerPawn, tileIndex, targetIndex));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (playerPawn.CurrentTileIndex == TilesManager.GetFinishingTileDataLength(currentPlayerTypeTurn) - 1)
|
|
{
|
|
OnGameResumed = null;
|
|
Tile tile = TilesManager.RetrieveFinishingTileBasedOnIndex(playerPawn.PlayerType, playerPawn.CurrentTileIndex);
|
|
tile.InitPlayerPawn(playerPawn, playerPawn.PlayerType);
|
|
// ShowUpdatedPlayerCountOnTile(playerPawn);
|
|
|
|
UpdatePlayerState(playerPawn, PlayerState.HasFinished);
|
|
playerPawn.SetPlayerSelectionState(false);
|
|
|
|
playerGameDatasDict[currentPlayerTypeTurn].totalPawnsFinished++;
|
|
playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath--;
|
|
|
|
Debug.Log($"playerGameDatasDict[currentPlayerTypeTurn].totalPawnsFinished: {playerGameDatasDict[currentPlayerTypeTurn].totalPawnsFinished}");
|
|
Debug.Log($"playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Count: {playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Count}");
|
|
if (playerGameDatasDict[currentPlayerTypeTurn].totalPawnsFinished == playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Count)
|
|
{
|
|
CanRollDiceAgain = false;
|
|
|
|
var playerTypeToRemove = currentPlayerTypeTurn;
|
|
allPlayerTypes.ForEach(type => Debug.Log($"before allPlayerTypes: {type}"));
|
|
SwitchPlayer();
|
|
allPlayerTypes.ForEach(type => Debug.Log($"after allPlayerTypes: {type}"));
|
|
|
|
if (allPlayerTypes.Contains(playerTypeToRemove))
|
|
{
|
|
allPlayerTypes.Remove(playerTypeToRemove);
|
|
playerDatas.FirstOrDefault(data => data.playerType == playerTypeToRemove).ranking = TotalPlayersInGame - allPlayerTypes.Count;
|
|
}
|
|
|
|
if (allPlayerTypes.Count == 1)
|
|
{
|
|
// Game is over
|
|
var lastUnfinishingPlayerType = allPlayerTypes[0];
|
|
allPlayerTypes.RemoveAt(0);
|
|
playerDatas.FirstOrDefault(data => data.playerType == lastUnfinishingPlayerType).ranking = TotalPlayersInGame - allPlayerTypes.Count;
|
|
|
|
// Show Game Over panel
|
|
GameManager.OnGameStateChanged(GameState.GameOver);
|
|
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CanRollDiceAgain = true;
|
|
if (playerPawn.IsBotPlayer)
|
|
CheckDiceRollForBot();
|
|
else
|
|
{
|
|
SetCanRollDiceForUser(true);
|
|
UpdateResponseTimerForUser(currentPlayerMaxTime: currentPlayerTurnMaxTime, () => RollDiceForUser());
|
|
}
|
|
|
|
diceRollHandler.DiceView.SetDiceButtonInteraction(CanRollDiceAgain);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// activate here
|
|
TilesManager.RetrieveFinishingTileBasedOnIndex(currentPlayerTypeTurn, playerPawn.CurrentTileIndex).InitPlayerPawn(playerPawn, currentPlayerTypeTurn);
|
|
|
|
if (CheckForMaxDiceRollAttempt())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (CanRollDiceAgain)
|
|
{
|
|
SetCanRollDiceForUser(IsUsersTurn());
|
|
OnCanRollDiceAgain();
|
|
#if UNITY_EDITOR
|
|
ShowUpdatedPlayerCountOnTile(playerPawn);
|
|
#endif
|
|
}
|
|
else
|
|
SwitchPlayer();
|
|
}
|
|
}
|
|
},
|
|
index);
|
|
}
|
|
|
|
private void SetCanRollDiceForUser(bool state)
|
|
{
|
|
CanRollDiceForUser = state;
|
|
Debug.Log($"CAnRollDiceForUser: {CanRollDiceForUser}");
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void ShowUpdatedPlayerCountOnTile(PlayerPawn playerPawn)
|
|
{
|
|
Tile tile = playerPawn.GetPlayerState() == PlayerState.InFinishingPath ?
|
|
TilesManager.RetrieveFinishingTileBasedOnIndex(playerPawn.PlayerType, playerPawn.CurrentTileIndex)
|
|
: TilesManager.RetrieveTileBasedOnIndex(playerPawn.CurrentTileIndex);
|
|
|
|
playerPawn.ShowPlayerCountCanvas(true);
|
|
|
|
int count = 0;
|
|
if (tile.IsSafeZone)
|
|
{
|
|
count = (tile as SafeTile).GetPlayerPawnsCountInTile(playerPawn.PlayerType);
|
|
}
|
|
else
|
|
{
|
|
count = tile.TotalPawnsInTile;
|
|
}
|
|
|
|
Debug.Log($"ShowUpdatedPlayerCountOnTile: {count}");
|
|
|
|
playerPawn.PlayerIndicatorCanvas.SetPlayerCount(count);
|
|
}
|
|
|
|
private void SetCurrentSelectedPointer()
|
|
{
|
|
var tempPos = playerBaseHandler.GetPlayerBase(currentPlayerTypeTurn).transform.position;
|
|
pointerDebug.position = new Vector3(tempPos.x, 3f, tempPos.z);
|
|
}
|
|
|
|
public void SetDisplayCountForAllAvailPlayers(bool state)
|
|
{
|
|
if (state)
|
|
{
|
|
availPlayersToMove.ForEach(pawn => ShowUpdatedPlayerCountOnTile(pawn));
|
|
}
|
|
else
|
|
{
|
|
availPlayersToMove.ForEach(pawn => pawn.ShowPlayerCountCanvas(false));
|
|
}
|
|
}
|
|
#endif
|
|
|
|
public void ResetTileDatasForPlayers()
|
|
{
|
|
// causes null ref cast exception while player is moving.
|
|
// foreach (PlayerData data in playerDatas)
|
|
// {
|
|
// var pawns = playerGameDatasDict[data.playerType].playerPawnsDict.Values;
|
|
//
|
|
// foreach (var pawn in pawns)
|
|
// {
|
|
// if (pawn.GetPlayerState() == PlayerState.InHome) continue;
|
|
//
|
|
// TilesManager.ResetTileData(pawn.PlayerType, pawn.CurrentTileIndex, pawn.GetPlayerState());
|
|
// }
|
|
// }
|
|
|
|
TilesManager.ResetTileDatas();
|
|
}
|
|
|
|
public void ResetData()
|
|
{
|
|
Debug.Log($"ResetOnSessionEnd()");
|
|
diceRollHandler.DiceView.ResetOnSessionEnd();
|
|
|
|
ResetGameRestartData();
|
|
|
|
currentPlayerTurnTimer?.KillTimer();
|
|
currentPlayerTurnTimer = null;
|
|
OnGameResumed = null;
|
|
playerDatas = null;
|
|
allPlayerTypes = null;
|
|
|
|
playerGameDatasDict = null;
|
|
playerDatas = null;
|
|
availPlayersToMove = null;
|
|
|
|
botTypesInGame = null;
|
|
botRuntimeMovementData = null;
|
|
}
|
|
|
|
public void ResetGameRestartData()
|
|
{
|
|
currentPlayerTurnIndex = 0;
|
|
diceSixRollCounter = 0;
|
|
}
|
|
}
|