- Home /
Im not sure if i should use a list or queue for a battle sequence
I created a game for an assignment in school, however, I have trouble trying to move on to the next enemy after the first one is killed. I have created prefabs of each enemy. My end goal is after defeating the enemy, it will destroy the prefab and instantiate the next one until all enemies are defeated.
public enum BattleTurn { STARTFIGHT, PLAYERTURN, ENEMYTURN, NEXTENEMY, GAMEWON, GAMELOST } public class Combat : MonoBehaviour { public BattleTurn turn;
public GameObject Player;
public GameObject Enemy1;
/*public GameObject Enemy2;
public GameObject Enemy3;
public GameObject Enemy4;
public GameObject Enemy5;
public GameObject Enemy6;
public GameObject Enemy7;*/
Character playerUnit;
Character enemyUnit;
public Transform PlayerStats;
public Transform EnemyStats;
public Text Dialogue;
public GameHUD playerHUD;
public GameHUD enemyHUD;
// Start is called before the first frame update
void Start() //When the game starts it sets the state to the StartFight
{
turn = BattleTurn.STARTFIGHT;
Debug.Log("Game Starts");
StartCoroutine(StartBattle());
}
IEnumerator StartBattle() //When the game is ran, it creates a player and enemy
{
GameObject playerUI = Instantiate(Player, PlayerStats);
playerUnit = playerUI.GetComponent<Character>();
GameObject enemyUI = Instantiate(Enemy1, EnemyStats);
enemyUnit = enemyUI.GetComponent<Character>();
/*
GameObject enemyUI = Instantiate([enemy in list/queue], EnemyStats);
enemyUnit = enemyUI.GetComponent<Character>();
*/
Dialogue.text = "A " + enemyUnit.characterName + " Appears";
playerHUD.SetHUD(playerUnit);
enemyHUD.SetHUD(enemyUnit);
yield return new WaitForSeconds(2f);
//after creating the characters, it sets to the player's turn
turn = BattleTurn.PLAYERTURN;
StartPlayersTurn();
}
void StartPlayersTurn() // This sets the text to your move so the player can attack
{
Dialogue.text = " Your Move ";
}
public void OnAttackButton() //When the attack button is clicked, it starts the playerattack function
{
if (turn != BattleTurn.PLAYERTURN)
return;
StartCoroutine(PlayerAttack());
}
IEnumerator PlayerAttack() //When the player uses the attack button it will do damage based on the players damage to the enemy
{
bool isDead = enemyUnit.TakeDamage(playerUnit.damage);
enemyHUD.SetHP(enemyUnit.CurrentHp);
Dialogue.text = "You did " + playerUnit.damage;
yield return new WaitForSeconds(2f);
//this checks if the enemy is dead, and changes the states based on it
if (isDead)
{
turn = BattleTurn.NEXTENEMY;
EndBattle();
}
else
{
turn = BattleTurn.ENEMYTURN;
StartCoroutine(EnemyTurn());
}
/*
else(if all enemies are defeated){
turn=BattleTurn.GAMEWON
}
*/
}
IEnumerator EnemyTurn()
{
Dialogue.text = enemyUnit.characterName + "Attacks You";
yield return new WaitForSeconds(1f);
bool isDead = playerUnit.TakeDamage(enemyUnit.damage);
playerHUD.SetHP(playerUnit.CurrentHp);
yield return new WaitForSeconds(1f);
if (isDead)
{
turn = BattleTurn.GAMELOST;
EndBattle();
}else
{
turn = BattleTurn.PLAYERTURN;
StartPlayersTurn();
}
}
void EndBattle()
{
if(turn == BattleTurn.NEXTENEMY)
{
Dialogue.text = "You Move On To The Next Enemy";
StartCoroutine(NextFight());
}
else if(turn == BattleTurn.GAMEWON)
{
Dialogue.text = "You Have Won";
}else if(turn == BattleTurn.GAMELOST)
{
Dialogue.text = "You Have Lost";
}
}
IEnumerator NextFight()
{
Dialogue.text = "Another enemy enters";
//Destroy(monster);
//Get next monster in list/queue
yield return new WaitForSeconds(2f);
}
}
Comment
you shouldn't destroy and instantiate, look into pooling.
Your answer
![](https://koobas.hobune.stream/wayback/20220613041446im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# - Is it possible to sync a Queue to a List, or is there a better option? 1 Answer
Creating List of Player 1 Answer
Strange behavior with map connectivity 1 Answer
public Queue 2 Answers