Contenido del Curso
Fighting Game in Unity
Fighting Game in Unity
Create State Manager
State:
Abstract Methods:
public abstract void StartState();
public abstract void EndState();
are abstract methods.
An abstract method doesn't have a body (implementation) here and must be
implemented by any non-abstract class that inherits from this State
class.
Virtual Method:
public virtual void UpdateState() {}
is a virtual method.
A virtual method has an implementation (in this case, an empty body {}
), but it can be overridden by derived classes if they need different behavior.
How It Works for Our Enemy
State Inheritance:
The State
class will be inherited by other specific states. For example, we might have classes like IdleState
, AttackState
, and PatrolState
that inherit from State
.
Each of these states will implement their own versions of StartState()
, UpdateState()
, and EndState()
.
Behavior of the Enemy:
Enemy behavior is managed through states, with StartState()
initializing the state, UpdateState()
updating behavior during the state, and EndState()
cleaning up after the state ends.
Abstract Class Usage:
Making State
an abstract class ensures all specific states implement necessary methods, organizing enemy behavior and simplifying state management without changing the enemy script.
State Manager:
Explanation
The constructor public StateManager(State startingState)
initializes the StateManager
with a starting state. It sets currentState
to startingState
and calls currentState.StartState()
to initialize it.
The ChangeState
method, public void ChangeState(State newState)
, changes the enemy's state. It calls currentState.EndState()
to clean up the current state, sets currentState
to the new state, and then calls currentState.StartState()
to initialize the new state.
The UpdateStates
method, public void UpdateStates()
, updates the current state by calling currentState.UpdateState()
to perform the actions associated with that state.
How It Works for Our Enemy
When we create a StateManager
for our enemy, the constructor initializes the starting state. When the enemy needs to change its behavior, the ChangeState
method is called, ending the current state with EndState()
, setting the new state, and initializing it with StartState()
. During the game, the UpdateStates
method is called repeatedly to execute the current state's actions via UpdateState()
.
Example Workflow:
The enemy starts in an IdleState
, with the StateManager
created and IdleState.StartState()
called to set up the idle behavior. When the enemy detects the player, ChangeState(new AttackState())
is called, ending the idle behavior with IdleState.EndState()
. The current state is then set to AttackState
, and AttackState.StartState()
initiates the attack behavior.
Every frame, UpdateStates()
is called, and if the current state is AttackState
, AttackState.UpdateState()
handles the attacking actions.
By using the StateManager
class, we can easily manage and switch between different behaviors of our enemy in an organized manner.
Idle State
Explanation
The constructor public IdleState(Animator animator)
takes an Animator
object as a parameter and assigns it to the class's animator
variable. The StartState
method, public override void StartState()
, sets the "idle" boolean parameter in the animator to true, triggering the idle animation. The EndState
method, public override void EndState()
, sets the "idle" parameter to false, stopping the idle animation.
How It Works for Our Enemy
When the enemy enters the IdleState
, the StartState()
method sets the "idle" parameter in the animator to true, starting the idle animation, making the enemy appear idle. When the enemy leaves the IdleState
, the EndState()
method sets the "idle" parameter to false, stopping the idle animation and preparing the enemy to transition to another state, like moving or attacking.
Example Workflow
The StateManager
sets the enemy's state to IdleState
, calling IdleState.StartState()
to set the animator's "idle" parameter to true, starting the idle animation. When changing the enemy's behavior, the StateManager
calls IdleState.EndState()
, setting the "idle" parameter to false and stopping the idle animation. The StateManager
then transitions the enemy to a new state, such as AttackState
or PatrolState
.
By using the IdleState
class, we can ensure that our enemy can enter and exit the idle behavior smoothly, making it more dynamic and realistic. This structure also makes it easy to add or modify the enemy's idle behavior without affecting other parts of the enemy's code.
¡Gracias por tus comentarios!