sure , here is the state machine code and the walk ( chasing ) code attached to the FSM .
state machine :
extends Node
@export var initial_state : states
var current_state : states
var player_states : Dictionary = {}
func _ready():
for child in get_children():
if child is states:
player_states[child.name.to_lower()] = child
child.statetransitioned.connect(on_child_transition)
if initial_state:
initial_state.enter()
current_state = initial_state
func _process(delta):
# Ensure that current_state is not null before calling update
if current_state != null:
current_state.update(delta)
func on_child_transition(state, new_state_name : String):
# Only transition if the current state exists and is not freed (null check)
if state != current_state or current_state == null:
return
var new_state = player_states.get(new_state_name.to_lower())
# Ensure the new state is valid
if new_state == null:
return
if current_state != null:
current_state.exit()
new_state.enter()
current_state = new_state
func free_state(state: states):
# Clear references and safely free the state
if state == current_state:
current_state = null # Clear current_state reference
state.queue_free()
walk/chase state :
extends states
class_name dragaur_walk
@export var enemy_anim : AnimatedSprite2D
@onready var enemy_body = get_tree().get_first_node_in_group(“enemy”)
@onready var player : CharacterBody2D = get_tree().get_first_node_in_group(“player”)
@onready var colli_enemy = $“…/…/CollisionShape2D”
func enter():
print(“enemy enetered walking”)
enemy_anim.play(“WALK”)
func update(_delta : float):
var hunt_distance = player.global_position.distance_to(enemy_body.global_position)
if hunt_distance < 70:
var move_direction = player.global_position - enemy_body.global_position
enemy_body.velocity = move_direction.normalized() * 20
# Flip the sprite based on direction
if enemy_body.velocity.x > 0:
enemy_anim.scale.x = -1
colli_enemy.position = Vector2(12.594, -8.571)
else:
enemy_anim.scale.x = 1
colli_enemy.position = Vector2(-2.857, -8.571)
enemy_body.move_and_slide()
enemy_anim.play("WALK")
else:
statetransitioned.emit(self, "dragaur_idle")
print("enemy into idle state")
func _on_area_2d_area_entered(area):
print(“enemy hit”)
statetransitioned.emit(self, “dragaur_hurt”)