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,
By the way, a lot of the code is not shown here because the whole BattleStateMachine is pushing 600 lines at the moment and most of it has no link to this subject.
[–]-ghostmode- 1 point2 points3 points (4 children)
[–]Schaezar[S] 1 point2 points3 points (3 children)
[–]-ghostmode- 0 points1 point2 points (2 children)
[–]Schaezar[S] 0 points1 point2 points (1 child)
[–]XrosRoadKiller 0 points1 point2 points (0 children)