#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
This commit is contained in:
2025-08-01 15:45:17 +02:00
parent b60c445592
commit 4c089dddd6
18 changed files with 221 additions and 4 deletions

View File

@@ -1,10 +1,13 @@
[gd_scene load_steps=4 format=3 uid="uid://bgvbgeq46wee2"]
[gd_scene load_steps=5 format=3 uid="uid://bgvbgeq46wee2"]
[ext_resource type="Script" uid="uid://das7twcy5153p" path="res://entities/player/scripts/player.gd" id="1_abrql"]
[ext_resource type="Texture2D" uid="uid://c7ff87jniga5m" path="res://entities/player/assets/sprites/robot.png" id="1_symyc"]
[sub_resource type="CircleShape2D" id="CircleShape2D_sglur"]
radius = 38.0526
radius = 27.0
[sub_resource type="CircleShape2D" id="CircleShape2D_abrql"]
radius = 40.0
[node name="Player" type="CharacterBody2D"]
script = ExtResource("1_abrql")
@@ -15,4 +18,11 @@ scale = Vector2(0.084375, 0.084375)
texture = ExtResource("1_symyc")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(-2, 15)
shape = SubResource("CircleShape2D_sglur")
[node name="InteractArea2D" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="InteractArea2D"]
position = Vector2(0, 37)
shape = SubResource("CircleShape2D_abrql")

View File

@@ -4,11 +4,33 @@ 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()
move_and_slide()