chip-gameV3/Assets/Scripts/Common/GameFSM/BotThinkingState.cs

122 lines
3.5 KiB
C#

using System.Threading;
using UnityEngine;
public class BotThinkingState : IState
{
Thread calcThread;
int calculatedColumn = -1;
float startTime;
float timeToThink;
Animator animator;
float animLength;
DifficultyPreset preset;
public BotThinkingState(float timeToThink)
{
this.timeToThink = timeToThink;
}
public BotThinkingState(float timeToThink, DifficultyPreset preset)
{
this.timeToThink = timeToThink;
this.preset = preset;
}
public void enter()
{
//Установка пресета сложности
DifficultyPreset currentPreset = CharactersController.CurrentCharacter.CurrentDifficultyPreset;
//Debug.Log(currentPreset.Description);
if (DifficultyManager.Instance.dummyBots)
currentPreset = DifficultyManager.Instance.DummyPreset;
if (preset!= null)
{
currentPreset = preset;
}
int movesMade = Board.Instance.MovesCount;
currentPreset = currentPreset.GetPreset(movesMade);
Solver.SetPreset(currentPreset);
calcThread = new Thread(calc);
calcThread.Start();
startTime = Time.time;
if (DifficultyManager.Instance.dummyBots)
timeToThink = 0;
CharacterEventBus.Instance.OnBotTurn?.Invoke();
animator = CharactersController.CurrentCharacter.GetComponent<Animator>();
try{ animLength = CharactersController.CurrentCharacter.chipSpawningAnim.length; }
catch { }
}
public void exit()
{
}
public bool displayed = false;
bool animationStarted = false;
float startAnimTime;
public void update()
{
//подсчет времени
if(calculatedColumn!= -1 && !displayed)
{
//Debug.Log($"Думал: {Time.time - startTime}");
displayed = true;
}
if (calculatedColumn != -1 && Time.time - startTime > timeToThink)
{
if (!animationStarted)
{
animator.SetTrigger("Throw");
startAnimTime = Time.time;
animationStarted = true;
}
if (animationStarted && Time.time - startAnimTime > animLength)
{
//ставим фишку в расчитанную колонку (подготовка к следующему состоянию)
Vector2 targetPosition = Board.Instance.GetStickedPosition(calculatedColumn);
targetPosition += Vector2.up * 5;
GameManager.instance.currentChip.transform.position = targetPosition;
GameManager.instance.stateMachine.ChangeState(new ChipFallingState(0, calculatedColumn));
}
}
}
void calc()
{
GameManager gm = GameManager.instance;
bool OnBossByScore = gm.vsBotScore == gm.botsAmount - 1;
bool IsHardCommonDifficulty = SettingsManager.GetString(SettingKey.DIFFICULTY) == "Сложная";
bool IsUpperCashDifficulty = SettingsManager.GetString(SettingKey.DIFFICULTY_MODE) == "Заработок";
bool NeedSuperPreset = OnBossByScore && (IsHardCommonDifficulty || IsUpperCashDifficulty);
if (NeedSuperPreset)
{
calculatedColumn = SolverV2.FindBestMove(Board.Instance.GridToTeam(), GameManager.instance.currentPlayer);
}
else
{
calculatedColumn = Solver.FindBestMove(Board.Instance.GridToTeam(), GameManager.instance.currentPlayer);
}
}
}