129 lines
4.0 KiB
C#
129 lines
4.0 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 = CharacterController.CurrentCharacter.CurrentDifficultyPreset;
|
|
//Debug.Log(currentPreset.Description);
|
|
if (DifficultyManager.Instance.dummyBots)
|
|
currentPreset = DifficultyManager.Instance.DummyPreset;
|
|
|
|
if (preset!= null)
|
|
{
|
|
currentPreset = preset;
|
|
}
|
|
|
|
|
|
int movesMade = GameManager.instance.GetMovesCount();
|
|
currentPreset = currentPreset.GetPreset(movesMade);
|
|
//Debug.Log("Ходов: " + movesMade + " Пресет: " + currentPreset.Description);
|
|
Solver.SetPreset(currentPreset);
|
|
|
|
|
|
calcThread = new Thread(calc);
|
|
calcThread.Start();
|
|
startTime = Time.time;
|
|
if (DifficultyManager.Instance.dummyBots)
|
|
timeToThink = 0;
|
|
|
|
CharacterEventBus.Instance.OnBotTurn?.Invoke();
|
|
animator = CharacterController.CurrentCharacter.GetComponent<Animator>();
|
|
|
|
try{ animLength = CharacterController.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)
|
|
{
|
|
//ставим фишку в расчитанную колонку (подготовка к следующему состоянию)
|
|
GameManager.instance.currentColumnIndex = calculatedColumn;
|
|
float targetY = GameManager.instance.startY;
|
|
float targetX = GameManager.instance.slotLeftTop.position.x +
|
|
GameManager.instance.currentColumnIndex *
|
|
GameManager.instance.stepX;
|
|
Vector2 targetPosition = new Vector2(targetX, targetY);
|
|
|
|
GameManager.instance.currentChip.transform.position = new Vector2(targetX, targetY + 5);
|
|
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("Difficulty", "Средняя") == "Сложная";
|
|
bool IsUpperCashDifficulty = SettingsManager.GetString("BaseBotDifficulty", "Повышенная") == "Повышенная";
|
|
|
|
bool NeedSuperPreset = OnBossByScore && (IsHardCommonDifficulty || IsUpperCashDifficulty);
|
|
if (NeedSuperPreset)
|
|
{
|
|
calculatedColumn = SolverV2.FindBestMove(GameManager.fieldModel.FieldArray, GameManager.instance.currentPlayer);
|
|
}
|
|
else
|
|
{
|
|
calculatedColumn = Solver.FindBestMove(GameManager.fieldModel.FieldArray, GameManager.instance.currentPlayer);
|
|
}
|
|
|
|
}
|
|
}
|