I’m struggling making an RPG turn-based battle system (à la early Final Fantasy), and I’m really trying to take an object-oriented approach. I have a CombatManager class attached to an object that takes all the combatants, sorts them according to each combatants’ Speed stat, and tells each combatant when he can act. However, I’ve run into a problem on that last part: How do I tell the CombatManager to stop its job for a moment so the combatant can do his job? If the combatant is the player, the program will pause and wait for input, and this could take many frames, during which the CombatManager shouldn’t do anything. Telling the player to act is simple enough (just call the player’s Act() method), but how do I pause the CombatManager then resume it when the player is done?
Upon request, here is my CombatManager. There is still much work to be done on it.
public class CombatManager : MonoBehaviour
{
private List<Combatant> combatants;
private Queue<Combatant> combatantsQueue;
private bool isRunning;
private void Awake()
{
combatants = new List<Combatant>();
combatantsQueue = new Queue<Combatant>();
isRunning = false;
}
private void Update()
{
while(isRunning)
{
// Run the combat.
// Dequeue a combatant and tell it to act.
// Add it back to the queue (at the end).
// When it's done acting, CombatManager should be notified so it can dequeue the next combatant.
// If a combatant is unable to act (such as when he dies), remove him from the queue.
// Other combatants may revive him, in which case add him back to the queue.
}
}
public void AddCombatant(Combatant combatant)
{
combatants.Add(combatant);
}
public void SortCombatants()
{
// Sort combatants by speed, highest first
combatants = combatants.OrderByDescending(c=>c.Speed).ToList();
foreach(var c in combatants)
{
// Add each combatant to the queue, starting with the fastest.
combatantsQueue.Enqueue(c);
}
}
public void StartCombat()
{
SortCombatants();
isRunning = true;
}
}
PS - Any general tips on creating an RPG turn-based battle system would be greatly appreciated, as well. I’m having a tough time conceptualizing the design taking into consideration the game loop.