freettrpg/scripts/player.gd

56 lines
1.4 KiB
GDScript3
Raw Permalink Normal View History

2024-06-13 13:29:05 +02:00
extends CharacterBody2D
@export var speed = 200
2024-06-18 18:58:47 +02:00
# Distance traveled accumulator
var distanceTo = 0
2024-06-18 18:58:47 +02:00
# Flag to indicate if the character is currently the active player.
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():
# 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
2024-06-13 13:29:05 +02:00
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
2024-06-14 09:42:54 +02:00
# Set main character as inactive
func stop():
$camera.enabled = false
active = false
$stats.disabled = true
$stats.visible = false
2024-06-18 18:58:47 +02:00
# Handler for when the stats button is pressed
func _on_stats_pressed():
get_parent().stats() # Open up the stats menu for the current player