StateMachine - SamToorchi/Godot-Zombi-Game GitHub Wiki

entity.gd
func _physics_process(delta):
	match state:
		"default":
			state_default()
		"swing":
			state_swing()

func state_default():
	controls_loop()
	movement_loop()
	spritedir_loop()
	damage_loop()
	
	if is_on_wall():
		if spritedir == "left" and test_move(transform, dir.left):
			anim_switch("push")
		if spritedir == "right" and test_move(transform, dir.right):
			anim_switch("push")
		if spritedir == "up" and test_move(transform, dir.up):
			anim_switch("push")
		if spritedir == "down" and test_move(transform, dir.down):
			anim_switch("push")
	
	elif movedir != dir.center:
		anim_switch("walk")
	else:
		anim_switch("idle")
	
	if Input.is_action_just_pressed("a"):
		use_item(preload("res://scene/sword.tscn"))

func state_swing():
	anim_switch("idle")
	damage_loop()
sword.gd
func destroy(animation):
	if get_parent().has_method("state_swing"):
		get_parent().state = "default"
	queue_free()