Im trying to make a turn based system where character speed determines the order in which characters attack. Im fairly new to unity and i tried looking around and fiddling with the code but i cant seem to figure out how to make a list getting my characters on screen and then creating a list of them in order of highest speed.
Hey!
Creating a turn based system is a bit of work so I can only try to outline a basic idea.
I would start by giving each character that takes part in the turn based process a custom script like:
public class TurnTaker : MonoBehaviour
{
public int GetSpeed()
{
//ask some AgentSkills script about the speed and return it.
return 0;
}
}
Next the simplest thing to do would be to have some sort of TurnBasedSystem script:
public class TurnBasedSystem : MonoBehaviour
{
Queue<TurnTaker> turnQueue = new Queue<TurnTaker>();
public void CreateAQueue()
{
//Get all turn takers
TurnTaker[] turnTakers = FindObjectsOfType<TurnTaker>();
//Sort it by speed from highest speed to lowest speed
turnTakers = turnTakers.OrderByDescending((val) => val.GetSpeed()).ToArray();
//Additional sorting to ensure that player and enemy moves interchangably ?
//Create a queue that will allow you to get the correct character first
turnQueue = new Queue<TurnTaker>(turnTakers);
}
//Check if turn is done
public bool IsTurnDone() => turnQueue.Count <= 0;
//get the next character to move
public TurnTaker GetNextCharacter()
{
if (IsTurnDone())
return null;
return turnQueue.Dequeue();
}
}
Here we call CreateAQueue() method when we want to get all the characters and prepare them for movement - at the start of a new turn.
Next all we need to do is to call GetNextCharacter() to get the TurnTaker that should go next. Having it you can set it as an active character or call on it some method.
Next all you need to do is to ask at the end of the movement IsTurnDone() - if it is then create a new queue and if not GetNextCharacter().
Of course you would add to it IsFightWon() or some check if there is even a point of taking the next turn.
Basically that is the idea of how you would create this kind of system in code.
I hope it helps!