2024-06-13 13:29:05 +02:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
2024-06-18 18:58:47 +02:00
|
|
|
# Exported variable for movement speed
|
2024-06-13 13:29:05 +02:00
|
|
|
@export var speed = 200
|
2024-06-18 18:58:47 +02:00
|
|
|
|
|
|
|
# Distance traveled accumulator
|
2024-06-13 14:25:37 +02:00
|
|
|
var distanceTo = 0
|
2024-06-18 18:58:47 +02:00
|
|
|
|
|
|
|
# Flag to indicate if the character is active
|
2024-06-13 14:25:37 +02:00
|
|
|
var active = false
|
2024-06-13 13:29:05 +02:00
|
|
|
|
2024-06-18 18:58:47 +02:00
|
|
|
# Function to handle user input
|
2024-06-13 13:29:05 +02:00
|
|
|
func get_input():
|
2024-06-18 18:58:47 +02:00
|
|
|
# Change scene if Escape key is pressed
|
|
|
|
if Input.is_action_pressed("escape"):
|
|
|
|
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
|
|
|
|
|
|
|
# Get directional input and set velocity accordingly
|
|
|
|
var input_direction = Input.get_vector("left", "right", "up", "down")
|
|
|
|
velocity = input_direction * speed
|
2024-06-13 13:29:05 +02:00
|
|
|
|
2024-06-18 18:58:47 +02:00
|
|
|
# Physics process function called every frame
|
2024-06-13 13:29:05 +02:00
|
|
|
func _physics_process(delta):
|
2024-06-18 18:58:47 +02:00
|
|
|
# Store current position for distance calculation
|
|
|
|
var toCalculate = position
|
|
|
|
|
|
|
|
# Process input if the character is active
|
|
|
|
if active:
|
|
|
|
get_input()
|
|
|
|
move_and_slide(velocity) # Move the character based on current velocity
|
|
|
|
|
|
|
|
# Accumulate distance traveled
|
|
|
|
distanceTo += position.distance_to(toCalculate)
|
|
|
|
print(distanceTo) # Print distance traveled (for debugging)
|
2024-06-13 14:25:37 +02:00
|
|
|
|
2024-06-18 18:58:47 +02:00
|
|
|
# Check if distance threshold is exceeded and the character is active
|
|
|
|
if distanceTo > 500 and active:
|
|
|
|
stop() # Stop character movement and reset distance traveled
|
|
|
|
distanceTo = 0
|
|
|
|
get_parent().next() # Trigger next action in parent node
|
2024-06-13 14:25:37 +02:00
|
|
|
|
2024-06-18 18:58:47 +02:00
|
|
|
# Function to start the character's activity
|
2024-06-13 14:25:37 +02:00
|
|
|
func start():
|
2024-06-18 18:58:47 +02:00
|
|
|
$camera.enabled = true # Enable camera
|
|
|
|
active = true # Set character to active state
|
|
|
|
$stats.disabled = false # Enable stats UI node
|
|
|
|
$stats.visible = true # Make stats UI node visible
|
2024-06-14 09:42:54 +02:00
|
|
|
|
2024-06-18 18:58:47 +02:00
|
|
|
# Function to stop the character's activity
|
2024-06-16 19:32:19 +02:00
|
|
|
func stop():
|
2024-06-18 18:58:47 +02:00
|
|
|
$camera.enabled = false # Disable camera
|
|
|
|
active = false # Set character to inactive state
|
|
|
|
$stats.disabled = true # Disable stats UI node
|
|
|
|
$stats.visible = false # Hide stats UI node
|
2024-06-16 19:32:19 +02:00
|
|
|
|
2024-06-18 18:58:47 +02:00
|
|
|
# Handler for when the stats button is pressed
|
2024-06-16 19:32:19 +02:00
|
|
|
func _on_stats_pressed():
|
2024-06-18 18:58:47 +02:00
|
|
|
get_parent().stats() # Call stats function in the parent node
|