Ok… I’ve looked into this quite a bit and haven’t really found a solution at all.
I’m trying to achieve something along the lines of a Final Fantasy style battle system. And am not really sure how to achieve this. Here is some pseudo-code.
Determine the amount of characters on the battle field;
Start all subsequent timers -- then:
if(Player's Turn)
Display Player's options;
if(Enemy's Turn)
Run A* to determine action;
Continue timers;
if(All enemies or players are dead)
End round;
I know I’m going to need to get an array or list of all active characters in the scene and start their timers. Now, here is the part that gets me… do I need to implement a Finite State Machine or use coroutines to pause the other timers after one of the timers has filled up completely? If you guys wouldn’t mind sharing some pseudo-code or pointing me in the right direction with some links/tutorials for broadening my knowledge on how to achieve this type of effect, I would greatly appreciate it. I just want to learn. Thanks ahead of time.
I think you’d be best off creating a Battle-class that you instantiate for each battle. Alternatively you could use a singleton, but because your game is not in a constant state of battle, I do not recommend a singleton. You seem to have outlined the Battle-class pretty well in your snippet.
My code is in C#, but UnityScript has the same logic. To control the timers, you could have the Battle-class run a coroutine that loops over each player and adds to its timer. If the timer if full, give that player his turn.
The timer-code could look like this:
/*Because the battle is its own class, you could have different kinds of battles;
ones with slow timers, or with no timers... maybe with constant damage to all
players. Just do the required modifications in this class.*/
public class Battle{
//The timers aren't a feature of the character. They only exist in the battle.
Dictionary<Character, float> timer;
private IEnumerator BattleUpdate(){
while(true){
foreach (KeyValuePair<Character,float> kvp in timer){
//add to each timer
if (kvp.Value > kvp.Key.timeToAllowTurn){
yield return kvp.Key.DoTurn(); //wait until turn is completed
kvp.Value = 0f;
}
}
yield return null;
}
}
}
If the battle system is done this way, each character only needs to know its own battle behavior (DoTurn) and not care about timers or other players or anything. If you need to allow more than Characters in the Battle, then make Character an abstract class, or use an interface.
Once you’re done with the battle, just Destroy the object like any other.