Course Content
Fighting Game in Unity
Fighting Game in Unity
State Transition
In these images, we see a Transition
class and a StatePourcentage
class. These classes work together to handle transitions between different states based on certain conditions and probabilities. Let's break down what this code does in simple terms:
Explanation
StatePourcentage Class
Class Definition:
The line public class StatePourcentage
defines a class named StatePourcentage
. This class holds information about a state and the percentage chance of transitioning to that state.
Member Variables:
public State state;
holds a reference to a state.
public float percentage;
holds the percentage chance of transitioning to this state.
Constructor:
public StatePourcentage(State state, float percentage)
is a constructor method. It takes a State
and a float
as parameters and assigns them to the member variables.
Transition Class
Class Definition:
The line public class Transition
defines a class named Transition
. This class handles the logic for transitioning between states based on a condition and percentages.
Member Variables:
Func<bool> condition;
is a delegate that holds a condition function. This function returns a boolean indicating whether the transition condition is met.
List<StatePourcentage> availableStates;
is a list that holds multiple StatePourcentage
objects, representing potential states to transition to and their respective probabilities.
Constructor:
public Transition(Func<bool> condition, params StatePourcentage[] statePourcentage)
is a constructor method. It takes a condition function and an array of StatePourcentage
objects as parameters. It initializes the condition
member variable with the provided condition function and creates a new list for availableStates
, adding each StatePourcentage
object from the array to this list.
GetState Method:
public State GetState()
is a method that determines which state to transition to based on random selection and the provided percentages. It generates a random number between 0 and 100 and iterates through the available states, accumulating their percentages until it finds the state that matches the random number.
IsConditionMet Method:
public bool IsConditionMet()
is a method that checks if the transition condition is met by calling the condition function. It returns the result of the condition function.
How It Works for Our Enemy
Transition Setup: We define transitions between states, specifying the conditions under which these transitions occur and the probabilities of transitioning to each state.
Determining the Next State: When a transition is triggered, the GetState()
method randomly selects the next state based on the defined probabilities. This adds an element of unpredictability to the enemy's behavior, making it more dynamic and challenging.
Checking Conditions: The IsConditionMet()
method is used to check if the conditions for a transition are met. If the condition is true, the transition can occur, and the GetState()
method determines the next state.
State Manager
Changes and Additions
State Transitions Dictionary: Dictionary<State, List<Transition>> stateTransitions;
is a dictionary that holds lists of transitions for each state. Each state is associated with a list of possible transitions that can be checked when updating the state.
Constructor: stateTransitions = new Dictionary<State, List<Transition>>();
initializes the stateTransitions
dictionary in the constructor, ensuring that it is ready to store transitions for different states.
AddStateTransition Method: public void AddStateTransition(State state, Transition transition)
is a method that adds a transition to the list of transitions for a given state. If the state does not already have an entry in the dictionary, a new entry is created with an empty list of transitions. The transition is then added to the list of transitions for the specified state.
UpdateStates Method: The UpdateStates
method has been updated to first check transitions by retrieving the list of transitions for the current state after updating it with currentState.UpdateState(deltaTime);
. It then evaluates each transition in the list to check if the transition condition is met using transition.IsConditionMet()
. If a transition condition is met, it changes the state using ChangeState(transition.GetState());
and breaks the loop to prevent multiple state changes in one update.
Why These Changes Were Made
The addition of the stateTransitions
dictionary and AddStateTransition
method enables the StateManager
to manage multiple potential transitions for each state, enhancing the flexibility and complexity of the state machine. By dynamically checking transitions within the UpdateStates
method, the StateManager
allows the enemy to respond to different events and conditions in real-time. The updated method ensures that the current state is updated first, followed by checking possible transitions, which ensures proper execution of state logic before making transition decisions.
By implementing these changes, the StateManager
becomes more capable of handling complex and varied behaviors, making the enemy's actions more dynamic and responsive to the game environment.
Thanks for your feedback!