2025-09-09 00:54:50 +00:00
|
|
|
class_name Slime extends Area2D
|
|
|
|
@onready var slime_graphic: AnimatedSprite2D = $SlimeGraphic
|
|
|
|
@onready var right_cast: RayCast2D = $RightCast
|
|
|
|
@onready var left_cast: RayCast2D = $LeftCast
|
|
|
|
@onready var left_down_cast: RayCast2D = $LeftDownCast
|
|
|
|
@onready var right_down_cast: RayCast2D = $RightDownCast
|
|
|
|
|
|
|
|
var speed:int = 100
|
|
|
|
var direction = 1
|
|
|
|
|
|
|
|
signal playerDamageSignal(body, slime)
|
|
|
|
|
2025-09-23 01:07:24 +00:00
|
|
|
enum State{IDLE, HURT, DEATH}
|
|
|
|
var currentState = State.IDLE
|
|
|
|
|
|
|
|
|
2025-09-09 00:54:50 +00:00
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready() -> void:
|
|
|
|
pass # Replace with function body.
|
2025-09-23 01:07:24 +00:00
|
|
|
func updateAnimation()->void:
|
|
|
|
match currentState:
|
|
|
|
State.IDLE:
|
|
|
|
slime_graphic.play("idle")
|
|
|
|
State.HURT:
|
|
|
|
slime_graphic.play("hurt")
|
|
|
|
|
|
|
|
func handle_damage()->void:
|
|
|
|
currentState = State.HURT
|
|
|
|
|
2025-09-09 00:54:50 +00:00
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
|
|
func _process(delta: float) -> void:
|
|
|
|
if not right_down_cast.is_colliding():
|
|
|
|
direction = -1
|
|
|
|
slime_graphic.flip_h = true
|
|
|
|
|
|
|
|
if not left_down_cast.is_colliding():
|
|
|
|
direction = 1
|
|
|
|
slime_graphic.flip_h = false
|
|
|
|
|
|
|
|
position.x += direction * speed * delta
|
2025-09-23 01:07:24 +00:00
|
|
|
updateAnimation()
|
2025-09-09 00:54:50 +00:00
|
|
|
|
|
|
|
func _on_body_entered(body: Node2D) -> void:
|
|
|
|
if body is Player:
|
|
|
|
print("Slime attack!")
|
|
|
|
playerDamageSignal.emit(body, self)
|
2025-09-23 01:07:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _on_slime_graphic_animation_finished() -> void:
|
|
|
|
match currentState:
|
|
|
|
State.HURT:
|
|
|
|
currentState = State.IDLE
|