2025-02-27 23:50:12 +01:00
|
|
|
extends Node2D
|
|
|
|
|
|
|
|
@export var speed = 50
|
|
|
|
|
|
|
|
@onready var ray_cast_right: RayCast2D = $RayCastRight
|
|
|
|
@onready var ray_cast_left: RayCast2D = $RayCastLeft
|
2025-03-03 22:57:23 +01:00
|
|
|
@onready var ray_cast_top_left: RayCast2D = $RayCastTopLeft
|
|
|
|
@onready var ray_cast_top_right: RayCast2D = $RayCastTopRight
|
2025-02-27 23:50:12 +01:00
|
|
|
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
|
|
|
|
|
|
var direction = 1
|
|
|
|
|
|
|
|
func _process(delta: float) -> void:
|
2025-03-03 22:57:23 +01:00
|
|
|
if direction == 1 and animated_sprite.flip_h == false:
|
|
|
|
animated_sprite.flip_h = true
|
|
|
|
if direction == -1 and animated_sprite.flip_h == true:
|
|
|
|
animated_sprite.flip_h = false
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
move(delta)
|
|
|
|
|
|
|
|
if ray_cast_top_left.is_colliding() or ray_cast_top_right.is_colliding():
|
|
|
|
queue_free()
|
|
|
|
|
|
|
|
|
|
|
|
func move(delta: float) -> void:
|
|
|
|
"""Keep moving from left to right, changing direction when near something."""
|
2025-02-27 23:50:12 +01:00
|
|
|
if direction == 1 and ray_cast_right.is_colliding():
|
|
|
|
direction = -1
|
|
|
|
animated_sprite.flip_h = true
|
|
|
|
|
|
|
|
if direction == -1 and ray_cast_left.is_colliding():
|
|
|
|
direction = 1
|
|
|
|
animated_sprite.flip_h = false
|
|
|
|
|
|
|
|
position.x += direction * speed * delta
|