freettrpg/scripts/player.gd
2024-06-18 18:58:47 +02:00

58 lines
2 KiB
GDScript

extends CharacterBody2D
# Exported variable for movement speed
@export var speed = 200
# Distance traveled accumulator
var distanceTo = 0
# Flag to indicate if the character is active
var active = false
# Function to handle user input
func get_input():
# 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
# Physics process function called every frame
func _physics_process(delta):
# 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)
# 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
# Function to start the character's activity
func start():
$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
# Function to stop the character's activity
func stop():
$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
# Handler for when the stats button is pressed
func _on_stats_pressed():
get_parent().stats() # Call stats function in the parent node