#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:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user