40 lines
1.2 KiB
GDScript
40 lines
1.2 KiB
GDScript
class_name Slime extends Area2D
|
|
|
|
@onready var left_cast: RayCast2D = $LeftCast
|
|
@onready var left_cliff_cast: RayCast2D = $LeftCliffCast
|
|
@onready var right_cast: RayCast2D = $RightCast
|
|
@onready var right_cliff_cast: RayCast2D = $RightCliffCast
|
|
@onready var slime_sprite: AnimatedSprite2D = $SlimeSprite
|
|
|
|
@export var direction: int = -1
|
|
@export var speed: float = 100
|
|
|
|
signal slimeAttacked(body, slime)
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
pass # Replace with function body.
|
|
|
|
func cast_is_colliding(cast: RayCast2D) -> bool:
|
|
return cast.is_colliding() && cast.get_collider() is not Player
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
position.x += direction * speed * delta
|
|
if cast_is_colliding(left_cast) && direction == -1:
|
|
direction = 1
|
|
elif cast_is_colliding(right_cast) && direction == 1:
|
|
direction = -1
|
|
elif direction == -1 && !cast_is_colliding(left_cliff_cast):
|
|
direction = 1
|
|
elif direction == 1 && !cast_is_colliding(right_cliff_cast):
|
|
direction = -1
|
|
|
|
slime_sprite.flip_h = direction == -1
|
|
|
|
|
|
func _on_body_entered(body: Node2D) -> void:
|
|
if body is Player:
|
|
print("slime attack!")
|
|
slimeAttacked.emit(body, self)
|