seeding-planets/entities/player/scripts/player.gd
Zacharie Guet 4c089dddd6 #10 gestion des interaction du joueur
- ajout d'une classe abstraite d'éléments interactifs : Interactables
- ajout d'une classe abstraite d'actions d'éléments d'interactifs : InteractablesActions
- ajout de la première classe d'action : WaterPlant
- ajout d'une plante rudimentaire
2025-08-01 15:45:17 +02:00

37 lines
894 B
GDScript

extends CharacterBody2D
class_name Player
@export var speed = 400
func get_input():
calculate_direction()
if Input.is_action_just_pressed("interact"):
try_interact()
func calculate_direction():
var input_direction : Vector2 = Input.get_vector("move_left", "move_right", "move_up", "move_down")
velocity = input_direction * speed
if input_direction.x:
$Sprite.flip_h = (input_direction.x < 0)
func try_interact():
var interactables : Array[Interactable]
for area2D in $InteractArea2D.get_overlapping_areas():
if area2D is Interactable:
interactables.push_front(area2D)
if len(interactables):
if len(interactables) > 1:
# Sort them to the closer
interactables.sort_custom(
func (el : Interactable): return el.global_position.distance_to(global_position)
)
interactables[0].interact(self)
func _physics_process(_delta):
get_input()
move_and_slide()