39 lines
1.1 KiB
GDScript
39 lines
1.1 KiB
GDScript
class_name Enemy extends Area2D
|
|
|
|
signal playerDamage
|
|
@onready var cast_right: RayCast2D = $CastRight
|
|
@onready var cast_left: RayCast2D = $CastLeft
|
|
@onready var cast_floor_right: RayCast2D = $CastFloorRight
|
|
@onready var cast_floor_left: RayCast2D = $CastFloorLeft
|
|
const speed = 60
|
|
var direction = 1
|
|
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
|
|
# 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 cast_right.is_colliding():
|
|
if not cast_right.get_collider().is_in_group("player"):
|
|
direction = -1
|
|
sprite.flip_h = true
|
|
if cast_left.is_colliding():
|
|
if not cast_left.get_collider().is_in_group("player"):
|
|
direction = 1
|
|
sprite.flip_h = false
|
|
if not cast_floor_right.is_colliding():
|
|
direction = -1
|
|
sprite.flip_h = true
|
|
if not cast_floor_left.is_colliding():
|
|
direction = 1
|
|
sprite.flip_h = false
|
|
position.x += direction * speed * delta
|
|
|
|
|
|
func _on_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("player"):
|
|
playerDamage.emit()
|