I'm actually not sure if this is possible, I have the following implementations on a turn based combat system
public abstract class State{
protected FightSystem FightSystem;
public State(FightSystem fightSystem)
{
FightSystem = fightSystem;
}
public virtual IEnumerator Start()
{
yield break;
}
}
public abstract class StateMachine : MonoBehaviour{
protected State State;
public void SetState (State state)
{
State = state;
StartCoroutine(State.Start());
}
}
Currently, the FightSystem
class holds all objects and references, when it is the player's turn and the button for example the basic attack is pressed, it calls
FightSystem.SetState(new PlayerBasicAttack(FightSystem));
the behaviour is executed and it goes through a transport class to check all turns, reset positions etc. When an enemy unit has a turn, it is also called through
FightSystem.SetState(new EnemyGhostPhaseAttack(FightSystem));
I currently use a enum
on the enemy unit class to keep track of which States
it has in the FightSystem
and a switch case
to select which one to execute
My issue here is, as my project is scaling I will eventually have a switch case with 50+ states being called every turn which is not my preference. Is it it possible to store 3-4 of such states in a variable on unit prefabs and have them called? (randomly but that'd be just a random range)
Another solution I had in mind, but is not very extensible or clean, is to have a region of all the set this state methods and have the unit classes hold multiple delegates to call these.
I know this is a complex question, I usually respond quick please let me know if there's any way I can clarify things further.
This project is 2D, Thank you for your time.
Further Details: The current state selection method
public List<StateMachine.EnemyStates> statesOnThisUnit; // on the unit class
public enum EnemyStates // the states enum on the state machine class
{
GhostAttack,
GhostDefend,
...,
}
public void SelectState(EnemyStates state, FightSystem fightSystem) // on the fight system
{
switch (state)
{
case EnemyStates.GhostAttack:
SetState(new GhostAttack(fightSystem));
break;
case EnemyStates.GhostDefend:
SetState(new GhostDefend(fightSystem));
break;
// ...
default:
break;
}
}