Hey guys! ?
Finally, we have finished semester exams at the university and now we devote more time to the project. During the session, we did not do much, but we also did not sit idly by.
So let me share with you our progress
New FX
Let me introduce a new spawning effect for units
We also made a new effect for a frost mage
FSM
I recently did refactoring for AI because, as the code base grew, managing it was getting harder and harder. Initially, I wrote a standard state machine, but I used “if” constructions and, you guessed it, it was extremely ineffective cause my code become more and more something like this
void Update()
{
if(IsInAttackState)
{
if(IsEnemyCloseEnough)
{
//Attack code
}
else
{
//Switch to chase state
//Reset a lot of variables
...
}
}
else if(IsInChaseState)
{
if(IsEnemyCloseEnough)
{
//Switch to attack state
//Reset a lot of variables
...
}
else
{
//Chase code
}
}
else
{
if(target == null)
{
//Search enemy
}
else
{
//Patrol code
}
}
}
You got the idea of whats going on. Such code become unexpandable and very hard to read not only for others but even for me
So I decide to do a refactoring and went in search of material on this theme.
After some work and fixing a huge number of bugs I've got this
private Dictionary<STATE_TYPE, FState> availableStates;
public FState currentState { get; set; }
private void Update()
{
if(!GameManager.Instance.IsGameEnd)
{
if (availableStates != null)
{
if (currentState == null)
{
currentState = availableStates.Values.First();
currentState.Init();
}
var nextState = currentState.Execute();
if (nextState.GetType() != currentState.GetType())
{
SwitchToNewState(nextState);
}
}
}
}
Code like this is easier to read and understand, all states are separated now and Its easy for me to modify them
Can be useful:
Here you can download “ready” FSM for your project - https://github.com/dubit/unity-fsm
In video below you can find how to make step-by-step your own FSM