game1/scripts/slime.gd

38 lines
1.1 KiB
GDScript3
Raw Normal View History

2025-09-09 00:57:14 +00:00
class_name Slime extends Area2D
@onready var slime_graphic: AnimatedSprite2D = $SlimeGraphic
@onready var right_cast: RayCast2D = $"right cast"
@onready var left_cast: RayCast2D = $"left cast"
@onready var left_down_cast: RayCast2D = $"left down cast"
@onready var right_down_cast: RayCast2D = $"right down cast"
var speed:int = 100
var direction = 1
signal playerDamageSignal(body, slime)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
position.x += direction * speed * delta
if not right_down_cast.is_colliding():
direction = -1
if not left_down_cast.is_colliding():
direction = 1
if left_cast.is_colliding() && direction == -1:
direction = 1
if right_cast.is_colliding() && direction == 1:
direction = -1
slime_graphic.flip_h = direction == -1
func _on_body_entered(body: Node2D) -> void:
if body is Player:
print("slime attack")
playerDamageSignal.emit(body, self)