-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.gd
More file actions
74 lines (63 loc) · 2.54 KB
/
player.gd
File metadata and controls
74 lines (63 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
extends CharacterBody2D
@export var move_speed: float = 300.0
@onready var animation_state = $AnimationTree.get("parameters/playback")
@onready var initial_postion: Vector2 = position
@onready var nearest_interactable: Interactable = null
# Prepare interaciton indicator
func _ready():
$InteractIndicator.play("default")
DialogueManager.dialogue_finished.connect($InteractIndicator.show)
func _physics_process(delta):
move_and_slide()
# Transitions within animation state machine
func update_animation_blend(input_direction: Vector2) -> void:
if input_direction != Vector2.ZERO:
$InteractArea.rotation = -input_direction.angle_to(Vector2(0, 1))
$AnimationTree.set("parameters/idle/blend_position", input_direction)
$AnimationTree.set("parameters/walk/blend_position", input_direction)
# Transition animation state machine
func update_animation_state(input_direction: Vector2) -> void:
if input_direction == Vector2.ZERO:
animation_state.travel("idle")
else:
animation_state.travel("walk")
# Apply velocity and animation updates
func move_player(direction: Vector2) -> void:
update_animation_blend(direction)
update_animation_state(direction)
velocity = direction * move_speed
# Do main player inputs here
func _unhandled_input(event):
var input_direction = Vector2(
Input.get_axis("ui_left", "ui_right"),
Input.get_axis("ui_up", "ui_down")
)
move_player(input_direction)
if event.is_action_pressed("ui_accept"):
get_viewport().set_input_as_handled()
if is_instance_valid(nearest_interactable):
move_player(Vector2.ZERO)
$InteractIndicator.hide()
nearest_interactable.emit_signal("interacted")
# Function adapted from an example here: https://www.youtube.com/watch?v=-rytm4o1ndE
# It updates the nearest interactable object every time a new area enters or
# exits the view of the player
func check_nearest_interactable() -> void:
var areas: Array[Area2D] = $InteractArea.get_overlapping_areas()
if not len(areas) > 0:
$InteractIndicator.hide()
nearest_interactable = null
return
$InteractIndicator.show()
var shortest_distance: float = INF
var next_nearest_interactable: Interactable = null
for area in areas:
var distance: float = area.global_position.distance_to(global_position)
if distance < shortest_distance:
shortest_distance = distance
next_nearest_interactable = area
if next_nearest_interactable != nearest_interactable or not is_instance_valid(next_nearest_interactable):
nearest_interactable = next_nearest_interactable
# TODO: Remove wrapper
func _on_interact_area_entered_exited(area):
check_nearest_interactable()