133 lines
3.8 KiB
GDScript
133 lines
3.8 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"
|
|
@onready var feetSound = $FeetSound
|
|
|
|
var interactionClueFor : Human
|
|
var interactionPaused= false
|
|
var kill_path_finder = false
|
|
var pause_everything = true
|
|
|
|
signal start_intracting
|
|
signal loadFromGameState
|
|
|
|
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:
|
|
if pause_everything:
|
|
return
|
|
|
|
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);
|
|
|
|
|
|
if pause_everything:
|
|
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()
|
|
feetSound.stream_paused = false
|
|
else:
|
|
feetSound.stream_paused = true
|
|
|
|
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()
|
|
|
|
|
|
func _on_load_from_game_state() -> void:
|
|
emit_signal("loadFromGameState")
|
|
feetSound.play()
|
|
feetSound.stream_paused = true
|
|
|
|
|
|
func _on_pause_game() -> void:
|
|
pause_everything = true
|
|
|
|
|
|
func _on_resume_game() -> void:
|
|
pause_everything = false
|
|
_on_load_from_game_state()
|