Estou com um problema no meu código no sistema de batalha por turnos, quando o primeiro inimigo é derrotado, não está aparecendo os botões, deveria atualizar os botões de ações do player, mas não está funcionando corretamente.
Aqui os métodos no meu BattleStateMachine que cria os botões, primeiro esse dos inimigos:
public void EnemyButtons()
{
Debug.Log("CHAMOU O ENEMYBUTTONS()!");
// Cria botões apenas para inimigos que ainda não têm botão
foreach (GameObject enemy in enemiesInBattle)
{
// Obtém o nome do inimigo a partir do componente EnemyStateMachine
string enemyName = enemy.GetComponent<EnemyStateMachine>().ENEMY.firstName;
// Verifica se já existe um botão com esse nome na lista de botões
bool buttonExists = enemyButtons.Any(b =>
b.transform.Find("TextTargetEnemyButton").GetComponent<TMP_Text>().text == enemyName);
if (buttonExists)
{
// Se já existir, passa para o próximo inimigo
continue;
}
// Cria um novo botão de seleção de inimigo
GameObject newButton = Instantiate(targetEnemyButton);
// Obtém o componente EnemySelectButton do novo botão
EnemySelectButton button = newButton.GetComponent<EnemySelectButton>();
// Define o texto do botão com o nome do inimigo
TMP_Text buttonText = newButton.transform.Find("TextTargetEnemyButton").GetComponent<TMP_Text>();
buttonText.text = enemyName;
// Associa o prefab do inimigo ao botão
button.EnemyPrefab = enemy;
// Adiciona o botão como filho do objeto alvo no layout
newButton.transform.SetParent(targetSpacer, false);
// Adiciona o novo botão à lista de botões
enemyButtons.Add(newButton);
}
}
E tem esse que cria os botões de ação quando inicia a batalha:
public void CreateAttackButtons()
{
//attack option button
GameObject attackActionButton = Instantiate(actionButton) as GameObject;
TMP_Text textAttackActionButton = attackActionButton.transform.Find("TextActionButton").gameObject.GetComponent<TMP_Text>();
textAttackActionButton.text = "Attack";
attackActionButton.GetComponent<Button>().onClick.AddListener(() => Input1());
attackActionButton.transform.SetParent(actionSpacer, false);
actionButtons.Add(attackActionButton);
//skill option button
GameObject skillsActionButton = Instantiate(actionButton) as GameObject;
TMP_Text textSkillsActionButton = skillsActionButton.transform.Find("TextActionButton").gameObject.GetComponent<TMP_Text>();
textSkillsActionButton.text = "Skills";
skillsActionButton.GetComponent<Button>().onClick.AddListener(() => Input3());
skillsActionButton.transform.SetParent(actionSpacer, false);
actionButtons.Add(skillsActionButton);
//defend option button
GameObject defendActionButton = Instantiate(actionButton) as GameObject;
TMP_Text textDefendActionButton = defendActionButton.transform.Find("TextActionButton").gameObject.GetComponent<TMP_Text>();
textDefendActionButton.text = "Defend";
defendActionButton.GetComponent<Button>().onClick.AddListener(() => Input5());
defendActionButton.transform.SetParent(actionSpacer, false);
actionButtons.Add(defendActionButton);
//magic skills buttons
if (herosInAction[0].GetComponent<HeroStateMachine>().HERO.magicSkills.Count > 0)
{
foreach (BaseAttack magicSkill in herosInAction[0].GetComponent<HeroStateMachine>().HERO.magicSkills)
{
GameObject magicSkillsButton = Instantiate(skillsButton) as GameObject;
TMP_Text textMagicSkillsButton = magicSkillsButton.transform.Find("TextSkillsButton").gameObject.GetComponent<TMP_Text>();
textMagicSkillsButton.text = magicSkill.attackName;
AttackButton ATB = magicSkillsButton.GetComponent<AttackButton>();
ATB.magicAttackToPerform = magicSkill;
magicSkillsButton.transform.SetParent(skillsSpacer, false);
if (herosInAction[0].GetComponent<HeroStateMachine>().HERO.curMP < magicSkill.attackCost)
{
magicSkillsButton.GetComponent<Button>().interactable = false;
}
actionButtons.Add(magicSkillsButton);
}
}
else
{
skillsActionButton.GetComponent<Button>().interactable = false;
}
}
E aqui o método quando o inimigo é derrotado no script EnemyStateMachine:
case TurnState.DEAD:
// Remove o inimigo das listas
BSM.enemiesInBattle.Remove(this.gameObject);
BSM.performList.RemoveAll(turn => turn.attackerGameObject == this.gameObject);
BSM.ReassignTargets(this.gameObject);
// Desativa selector e animação de morte
Selector.SetActive(false);
animator.Play("Die");
// Limpa botões de ação dos heróis para evitar referência quebrada
BSM.ClearAttackPanel();
// Se ainda existirem inimigos, atualiza os botões
if (BSM.enemiesInBattle.Count > 0)
{
BSM.EnemyButtons();
Debug.Log("TEM INIMIGO VIVO!");
}
// Muda o estado da batalha
BSM.battleStates = BattleStateMachine.PerformAction.CHECKALIVE;
break;