Files
chaussette.sale/caracters/human.gd
Thomas a82237ff28 L'arrivée de la boulangerie
Il n'y a rien dedans, mais elle est ouverte. Bonne visite
2025-03-30 14:38:20 +02:00

107 lines
3.3 KiB
GDScript

class_name Human
extends CharacterBody2D
@export var speed = 250 # How fast the player will move (pixels/sec).
# intensions of the player turner into a boolean
@export var wants_to_grab = false;
var wants_to_interact_with: Node2D
var humanInteractionTarget: Human = null
@onready var animation_tree := $AnimationTree
@onready var animation_player := $AnimationPlayer
@onready var state_machine := animation_tree.get("parameters/HumanState/playback") as AnimationNodeStateMachinePlayback
@onready var interactionZone : InteractionZone = $"interaction zone"
var interactionClueFor : Human
var interactionPaused= false
var kill_path_finder = false
signal start_intracting
var last_facing_direction = Vector2(0,1) # facing south
var velocityVector = Vector2(0, 0)
var targetGlobalPosition = null
func moveTo(p: Vector2) -> void:
targetGlobalPosition = p
func moveFeetTo(p: Vector2) -> void:
targetGlobalPosition = p - Vector2(0, 43)
func face(whereToFace: Vector2) -> void:
last_facing_direction = (whereToFace - global_position).normalized()
func decideAction() -> void:
pass
func _unhandled_input(event: InputEvent) -> void:
if interactionPaused and interactionClueFor:
interactionPaused = false
enable_interaction_clue(interactionClueFor)
func updateFacingDirectionInAnimationTree():
animation_tree.set("parameters/HumanState/grabing/blend_position", last_facing_direction)
animation_tree.set("parameters/HumanState/idling/blend_position", last_facing_direction)
animation_tree.set("parameters/HumanState/walking/blend_position", last_facing_direction)
func _physics_process(delta):
decideAction()
if (targetGlobalPosition != null):
velocity = (targetGlobalPosition - global_position).normalized() *speed
targetGlobalPosition = null
else:
velocity = velocityVector * speed
if state_machine.get_current_node() == "grabing" or humanInteractionTarget != null :
velocity = Vector2(0,0);
# move the caracter
move_and_slide()
# compute the direction the player wants to look at
if velocity:
last_facing_direction = velocity.normalized()
updateFacingDirectionInAnimationTree()
if wants_to_interact_with and wants_to_interact_with.has_method("start_interaction"):
if humanInteractionTarget == null:
humanInteractionTarget = wants_to_interact_with as Human
humanInteractionTarget.start_interaction(self)
func start_interaction(askingForInteraction: Human):
emit_signal("start_intracting", askingForInteraction)
interactionPaused = true
pause_or_stop_interaction_clue()
func stop_interaction():
humanInteractionTarget = null
func get_feet_global_position():
return global_position + Vector2(0, 43)
func set_feet_global_position(p : Vector2):
global_position = p - Vector2(0, 43)
func pause_or_stop_interaction_clue():
if interactionZone:
var mat = $Sprite2D.get("material") as ShaderMaterial
if mat:
mat.set_shader_parameter("line_thickness", 0)
interactionZone.disable_interaction_clue()
func enable_interaction_clue(from: Human):
interactionClueFor = from
if interactionZone:
var mat = $Sprite2D.get("material") as ShaderMaterial
if mat:
mat.set_shader_parameter("line_thickness", 1)
interactionZone.enable_interaction_clue(from)
func disable_interaction_clue():
interactionClueFor = null
interactionPaused = false
pause_or_stop_interaction_clue()