Files
Defensaria/Game/Mobs/enemy.gd
2025-03-09 20:04:04 +01:00

36 lines
1.1 KiB
GDScript

extends Node2D
@export var speed = 100
var last_position: Vector2 = Vector2.ZERO # To store the previous position
func _ready() -> void:
last_position = get_parent().position # Set the initial position when the mob starts
func _process(delta: float) -> void:
var path_follow = get_parent()
# Update the progress along the path
path_follow.set_progress(path_follow.get_progress() + speed * delta)
# Prevent rotation (lock rotation to 0)
path_follow.rotation = 0
# Get the current position on the path (mob's position)
var current_position = path_follow.position
# Flip the character if moving left (when moving to the left, the x position decreases)
if current_position.x < last_position.x:
# Flip the character horizontally by inverting its scale on the x-axis
path_follow.scale.x = -abs(path_follow.scale.x)
else:
# Ensure the character is not flipped when moving right
path_follow.scale.x = abs(path_follow.scale.x)
# Update the last_position for the next frame
last_position = current_position
# If the mob reaches the end of the path, queue it for deletion
if path_follow.get_progress_ratio() >= 0.999:
get_parent().queue_free()