- Home /
Active Turn-Based Battle System Help
So I have this turn-based battle system. So far, it's working pretty well and each "hero" can choose an action, this action gets added to a list and the first one in that list gets processed. However, one of the requirement of the game is that each action has a certain delay before it's processed, meaning that if playerA choose to take actionX with a delay of 12 seconds and playerB choose to take actionY with a delay of 4 seconds, if actionX still has more than 4 seconds to go, actionY will become the next action to be processed. Also, right now, actionX has to be completed (and 12 seconds need to pass) before actionY "timer" starts... which is not good as each "timer" should update in parallel...
.
I have an idea about how to reorder the actors list when I add a new one to it, based on the time left before each action. But I have absolutely no clue how to process everything in parallel and make it so that all the "timers" update at the same time instead of one after the other...
public class BattleStateMachine : MonoBehaviour {
public enum BattleStates {
INITIALIZE,
WAITING,
PROCESS_ACTION,
IDLE,
CHECK_IF_ALIVE,
WON,
LOST
}
public BattleStates currState;
public List<Actor> actors = new List<Actor>(); //List of actors, which contains the details of the action taken by the battler
// Update is called once per frame
void Update() {
switch(currState) {
case BattleStates.INITIALIZE: {
currState = BattleStates.WAITING;
break;
}
case BattleStates.WAITING: {
if(actors.Count > 0)
currState = BattleStates.PROCESS_ACTION;
break;
}
case BattleStates.PROCESS_ACTION: {
HeroStateMachine hsm = actors[0].Origin.GetComponent<HeroStateMachine>();
hsm.target = actors[0].Target;
hsm.currState = HeroStateMachine.TurnStates.ACTION;
break;
}
}
}
}
public class Actor : MonoBehaviour
{
public string ActorType;
public GameObject Origin;
public string OriginName;
public GameObject Target;
public string ActionType;
public BaseAction ChosenAction;
}
public class HeroStateMachine : MonoBehaviour {
public BattleStateMachine bsm;
public enum TurnStates {
WAITING,
ADDTOLIST,
IDLE,
ACTION,
DEAD
}
public TurnStates currState;
void Update () {
switch(currState) {
case TurnStates.WAITING: {
UpdateATBGauge();
break;
}
case TurnStates.ADDTOLIST: {
bsm.heroesToManage.Add(this.gameObject);
currState = TurnStates.IDLE;
break;
}
case TurnStates.IDLE: {
break;
}
case TurnStates.ACTION: {
StartCoroutine(TimeForAction());
break;
}
}
}
private IEnumerator TimeForAction() {
if(actionStarted)
yield break;
actionStarted = true;
yield return new WaitForSeconds(bsm.actors[0].ChosenAction.ActionDelay);
DoDamage();
bsm.actors.RemoveAt(0);
bsm.currState = BattleStateMachine.BattleStates.WAITING;
actionStarted = false;
statsPanel.progressGauge.value = 0f;
currState = TurnStates.WAITING;
}
}
Oh masters of Unity (Or C# in general), if you know of a way, please help me because I've been stuck on this issue for a lot longer that I would admit...
Thanks,
A lot of the code about how the Actor instance is built doesn't appear here but since the BattleStateMachine is pushing 600 lines, I didn't include anything that wasn't related to the question and had no effect on the turn order.
Answer by ItsaMeTuni · Oct 30, 2017 at 11:47 PM
Create a counter for each of the actions (a float, not an int). Then on Update add Time.deltaTime to all counters ( counter += Time.deltaTime
) and if a counter value exceeds the time limit (which would be 12 seconds or 4 seconds, if(counter >= timeLimit)
) it means that the action is finished. I didn't read your code because I don't have time to do it right now (sorry), but I think that you can adapt what I said to your needs.
Answer by Schaezar · Oct 31, 2017 at 11:02 AM
Someone answered my question on reddit but it's very similar to what you said here. Add ActionTime to the BaseAction class and a function to calculate it's value whenever it's added to the actors list:
public class BaseAction
{
public string Name;
public float ActionDelay;
public float ActionTime;
public void CalculateActionTime()
{
ActionTime = Time.time + ActionDelay;
}
}
Modify Actor like so :
public class Actor
{
public string ActorType;
public GameObject Origin;
public string OriginName;
public GameObject Target;
public string ActionType;
public BaseAction ChosenAction;
public BattleStateMachine BSM;
public void AddToActionQueue()
{
ChosenAction.CalculateActionTime();
BSM.actors.Add(this);
}
public void ExecuteAndRemoveFromQueue()
{
HeroStateMachine hsm = Origin.GetComponent<HeroStateMachine>();
hsm.currState = HeroStateMachine.TurnStates.ACTION;
hsm.chosenAction = ChosenAction;
hsm.target = Target;
BSM.actors.Remove(this);
}
}
Call the AddToActionQueue() function when ready to Add the actor to the actors list and then check with this loop :
case BattleStates.PROCESS_ACTION:
{
foreach(Actor actor in actors)
{
if(actor.ChosenAction.ActionTime <= Time.time)
{
actor.ExecuteAndRemoveFromQueue();
break;
}
}
break;
}
If someone is ready to take their turn. There's no need to sort the list in this case. This also frees the Coroutine in the HeroStateMachine to wait some time for an animation to finish when I add it in the future.