JuneGame/scripts/slime.gd

47 lines
1.4 KiB
GDScript

class_name Slime extends Area2D
@onready var slime_graphic: AnimatedSprite2D = $slimeGraphic
@onready var right_side_cast: RayCast2D = $slimeGraphic/RightSideCast
@onready var left_side_cast: RayCast2D = $slimeGraphic/LeftSideCast
@onready var left_down_cast: RayCast2D = $"slimeGraphic/Left DownCast"
@onready var right_down_cast: RayCast2D = $"slimeGraphic/Right DownCast"
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:
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_side_cast.is_colliding():
if not right_side_cast.get_collider() is Player && not right_side_cast.get_collider() is Slime:
direction = -1
slime_graphic.flip_h = true
if left_side_cast.is_colliding():
if not left_side_cast.get_collider() is Player && not left_side_cast.get_collider() is Slime:
direction = 1
slime_graphic.flip_h = false
position.x += direction * speed * delta
func _on_body_entered(body: Node2D) -> void:
if body is Player:
print("player attacked")
playerDamageSignal.emit(body, self)
func takeDamage(body):
if body == self:
slime_graphic.play("hurt")