- Home /
A list of enemies and players speed to determine turn order
I have some scripts for these players (4 of them, practically identical) and an enemy script. I want to make a list of who is fastest and then have them take their turns down the list. But my enemies and players aren't all game objects. so how should I get them all together?
This is a game where the battle system happens in the blink of an eye. so I don't want to create any unnecessary objects that will be gone in a split second. unless that would fix it.
public void PBattle(List<GameObject> _party, List<TheEnemy> _enemies)
{
foreach(GameObject player in _party)
{
//Get speed
}
}
should I make a general class and have them all inherit from it and all it does is have a method in there that gets their speed and returns it? Or should I just have a method that checks everyone's speed every time but I set a counter after each time I use the method. So once it's used and the first person goes it adds 1 to the counter the next time around it sees there's 1 person who went and so it goes to the next one who has the fastest speed.
One last way could be, that it checks the first party members speed, then checks it against all the enemies, then if the player is faster he will have his turn, then we go to the next player and check his speed against all the enemies, say the enemy's speed is higher, so it will have its turn. then we go back to that same player who didn't go yet and have him check to see if he's faster than the rest of the enemies until he can go and so on with the other players. the only problem would be keeping track of who has had their turn and what code would be able to detect that. (including enemies)
Answer by Alanisaac · Dec 23, 2017 at 05:43 PM
I wouldn't use a base class here, but rather use an interface. This would allow you to have both player and enemy objects implement the interface to check speed without additional objects. For this example, I'm assuming that your speed is represented by a simple integer. Below, I'm only showing the enemy classes implementing the interface, but the player/party objects can do the same thing. You can also keep the PBattle(...) method using two lists, but all the objects should be ICanBattle
which will let you check speed. You can add other things to ICanBattle
that are common between your players and enemies that are important to your battle system. I don't know what your game is like, but a simple example would be something like health/HP.
public interface ICanBattle
{
int Speed { get; }
}
// ---------------------------
public class TheEnemy : ICanBattle
{
public int Speed { get; set; }
// the rest of the class ...
}
// ---------------------------
public void PBattle(List<ICanBattle> _partyAndEnemiesInOneList)
{
foreach(ICanBattle battler in _partyAndEnemiesInOneList)
{
var speed = battler.Speed;
}
}
Answer by jag45 · Dec 25, 2017 at 08:23 PM
The problem I have now is I don't know how to add my party exactly to the list. I have a game object for each class script, then I have just one script for enemies that are not attached to any game object. Since all my class scripts are going to be the character themselves, should i just have 1 game object with 4 components of each one of the classes? one object for the class scripts and one object for the enemy scripts. Here is some code i have
public interface ICanBattle
{
int Speed { get; }
}
one of the classes
public class TheAttacker : MonoBehaviour, ICanBattle
{
public string _name = "Bob";
public List<RPGStat> stats = new List<RPGStat>();
public TheAttacker()
{
stats.Add(new RPGStat("Level", 1));
stats.Add(new RPGStat("XP", 0));
SetStatTempValue("XP", 20);
stats.Add(new RPGStat("HP", 90));
stats.Add(new RPGStat("MP", 15));
stats.Add(new RPGStat("Strength", 10));
stats.Add(new RPGStat("Intellect", 4));
stats.Add(new RPGStat("Defense", 6));
stats.Add(new RPGStat("Speed", 9));
stats.Add(new RPGStat("HPMod", 10));
stats.Add(new RPGStat("MPMod", 5));
stats.Add(new RPGStat("StrMod", 1));
stats.Add(new RPGStat("IntelMod", 1));
stats.Add(new RPGStat("DefMod", 1));
stats.Add(new RPGStat("SpeMod", 1));
}
public int Speed
{
get
{
return GetStatBaseValue("Speed");
}
}
public int GetStatBaseValue(string name)
{
foreach(RPGStat stat in stats)
{
if(stat.GetName() == name)
{
return stat.GetBaseValue();
}
}
return 0;
}
the controller that initiates everything
public class Controller : MonoBehaviour
{
public List<GameObject> party = new List<GameObject>();
public GameObject attacker, tank, mage, healer;
public GameObject enemyCont;
public int money = 0;
// Use this for initialization
void Start ()
{
enemyCont = GameObject.Find("EnemyContainer");
attacker = GameObject.Find("Attacker");
tank = GameObject.Find("Tank");
mage = GameObject.Find("Mage");
healer = GameObject.Find("Healer");
attacker.AddComponent<TheAttacker>();
tank.AddComponent<TheTank>();
mage.AddComponent<TheMage>();
healer.AddComponent<TheHealer>();
party.Add(attacker);
//add this code where you aquire these characters
party.Add(tank);
party.Add(mage);
party.Add(healer);
//tank.SetActive(false);
//mage.SetActive(false);
//healer.SetActive(false);
}
public void StartBattle()
{
this.GetComponent<BattleController>().SBattle(party);
}
}
the battle controller. to set up the enemies and set the attack order (attacker order could be determined in the battle class)
public class BattleController : MonoBehaviour
{
public List<ICanBattle> battleOrder = new List<ICanBattle>();
private TheAttacker attacker;
private int numEnemies;
public void SBattle(List<GameObject> party)
{
numEnemies = Random.Range(1,4);
for(int i = 0; i < numEnemies; i++)
{
// ADD ENEMIES HERE TO GAME OBJECT OR LIST
//_enemyCont.AddComponent<TheEnemy>();
//battleOrder.Add(TheEnemy enemy2 = new TheEnemy());
}
foreach (GameObject player in party)
{
if (player.name == "Attacker")
{
attacker = player.GetComponent<TheAttacker>();
Debug.Log("inside");
battleOrder.Add(attacker);
}
/*else if (playerClass.name == "Tank")
{
battleOrder.Add(playerClass.GetComponent<TheTank>());
}
else if (playerClass.name == "Mage")
{
battleOrder.Add(playerClass.GetComponent<TheMage>());
}
else if (playerClass.name == "Healer")
{
battleOrder.Add(playerClass.GetComponent<TheHealer>());
}*/
}
Debug.Log(battleOrder.Count);
//battleOrder.Sort();
//Debug.Log(battleOrder.ToString());
/*foreach (ICanBattle battler in battleOrder)
{
var speed = battler.Speed;
}*/
gameObject.AddComponent<Battle>();
//gameObject.GetComponent<Battle>().PBattle(party);
}
}
ive been trying to make this work for a couple hours and i still cant get it haha
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Add GameObject to List using Timer 1 Answer
Update list on mouse click 1 Answer
Adding a prefab to gameObject at a certain position in runtime 0 Answers