-
Notifications
You must be signed in to change notification settings - Fork 2
/
Player.gd
97 lines (84 loc) · 2.81 KB
/
Player.gd
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
extends KinematicBody2D
signal on_new_position
# Player movement speed
export var speed: int = 150
export var interaction_range: float = 50.0
var keyboard_pressed: bool = false
var mouse_pressed: bool = false
var touch_pressed: bool = false
var touch_initial_direction: Vector2 = Vector2(0, 1)
func set_player_name(player_name):
$Label.text = player_name + " (You)"
func _physics_process(delta):
var direction: Vector2
if mouse_pressed:
direction = position.direction_to(get_global_mouse_position())
elif touch_pressed:
direction = touch_initial_direction
elif keyboard_pressed:
direction.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
direction.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
# avoid diagonal movement
if abs(direction.x) > abs(direction.y):
direction.y = 0
if direction.x > 0:
$AnimationPlayer.play("right")
else:
$AnimationPlayer.play("left")
else:
direction.x = 0
if direction.y > 0:
$AnimationPlayer.play("down")
else:
$AnimationPlayer.play("up")
if direction.x == 0 and direction.y == 0:
$AnimationPlayer.stop()
else:
# try a movement only if there is something to do
direction = direction.normalized()
var movement = speed * direction * delta
move_and_collide(movement)
emit_signal("on_new_position", position)
$RayCast2D.cast_to = direction.normalized() * 32
func _unhandled_input(event):
# see https://docs.godotengine.org/en/latest/tutorials/inputs/inputevent.html
var is_interaction = false
if event.is_action_pressed("interact"):
is_interaction = true
if (event.is_action_pressed("ui_down")
or event.is_action_pressed("ui_up")
or event.is_action_pressed("ui_left")
or event.is_action_pressed("ui_right")
):
keyboard_pressed = true
if (event.is_action_released("ui_down")
or event.is_action_released("ui_up")
or event.is_action_released("ui_left")
or event.is_action_released("ui_right")
):
keyboard_pressed = false
if event is InputEventScreenTouch:
touch_pressed = event.is_pressed()
var world_position = get_canvas_transform().xform_inv(event.position)
if position.distance_to(world_position) < interaction_range:
is_interaction = true
# the intent was not to move, pretend it's not touching to not
# trigger the movement
touch_pressed = false
else:
touch_initial_direction = position.direction_to(world_position)
return
if event.is_action_pressed("click"):
if position.distance_to(get_global_mouse_position()) < interaction_range:
is_interaction = true
else:
mouse_pressed = true
if event.is_action_released("click"):
mouse_pressed = false
if is_interaction:
var target = $RayCast2D.get_collider()
if target != null:
if target.has_method("on_interact"):
target.on_interact()
else:
print_debug("Cannot interact with this")