freettrpg/scripts/player.gd
Patrick_Pluto 6b2159b840 Code Refactoring: Part 2
Hopefully final refactoring, offloaded ui scripts to its own global .gd to prevent repetition.
2024-06-19 18:27:40 +02:00

55 lines
1.4 KiB
GDScript

extends CharacterBody2D
@export var speed = 200
# Distance traveled accumulator
var distanceTo = 0
# Flag to indicate if the character is currently the active player.
var active = false
# Function to handle user input
func get_input():
# Main Menu "shortcut"
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
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()
# Accumulate distance traveled
distanceTo += position.distance_to(toCalculate)
# Check if distance threshold is exceeded and the character is active
if distanceTo > 500 and active:
stop()
distanceTo = 0
get_parent().next() # Go to the next player
# Set main character as active
func start():
$camera.enabled = true
active = true
$stats.disabled = false
$stats.visible = true
# Set main character as inactive
func stop():
$camera.enabled = false
active = false
$stats.disabled = true
$stats.visible = false
# Handler for when the stats button is pressed
func _on_stats_pressed():
get_parent().stats() # Open up the stats menu for the current player