33 lines
989 B
GDScript3
33 lines
989 B
GDScript3
|
|
class_name Slime extends Area2D
|
||
|
|
@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
|
||
|
|
@onready var slime_graphic: AnimatedSprite2D = $SlimeGraphic
|
||
|
|
|
||
|
|
var speed:int = 100
|
||
|
|
var direction = 1
|
||
|
|
|
||
|
|
signal slimeDamageSignal(body, slime)
|
||
|
|
|
||
|
|
#Animation code
|
||
|
|
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
|
||
|
|
if right_cast.is_colliding() && not right_cast.get_collider() is Player:
|
||
|
|
direction = -1
|
||
|
|
slime_graphic.flip_h = true
|
||
|
|
if left_cast.is_colliding() && not left_cast.get_collider() is Player:
|
||
|
|
direction = 1
|
||
|
|
slime_graphic.flip_h = false
|
||
|
|
position.x += direction * speed * delta
|
||
|
|
|
||
|
|
|
||
|
|
func _on_body_entered(body: Node2D) -> void:
|
||
|
|
if body is Player:
|
||
|
|
slimeDamageSignal.emit(body, self)
|