-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
59 lines (47 loc) · 1.73 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
extends KinematicBody2D
const TILE_SIZE = 16
var direction: Vector2 = Vector2.ZERO
var pixels_per_second: float setget _pixels_per_second_changed
var _step_size: float
func _pixels_per_second_changed(value: float) -> void:
pixels_per_second = value
_step_size = (1 / pixels_per_second)
var _step: float = 0 # Accumulates delta, aka fractions of seconds, to time movement
var _pixels_moved: int = 0 # Count movement in distinct integer steps
func show_coordinates() -> void:
$Label.text = "x: %d (%dpx)\ny: %d (%dpx)" % [
self.position.x / TILE_SIZE, self.position.x,
self.position.y / TILE_SIZE, self.position.y
]
func is_moving() -> bool:
return self.direction.x != 0 or self.direction.y != 0
func _ready() -> void:
self.pixels_per_second = 1 * TILE_SIZE
func _process(delta: float) -> void:
show_coordinates()
func _physics_process(delta: float) -> void:
if not is_moving(): return
# delta is measured in fractions of seconds, so for a speed of
# 4 pixels_per_second, we need to accumulate deltas until we
# reach 1 / 4 = 0.25
_step += delta
if _step < _step_size: return
# Move a pixel
_step -= _step_size
_pixels_moved += 1
move_and_collide(direction)
# Complete movement
if _pixels_moved >= TILE_SIZE:
direction = Vector2.ZERO
_pixels_moved = 0
_step = 0
func _input(event: InputEvent) -> void:
if is_moving(): return
if Input.is_action_pressed("ui_right"):
direction = Vector2(1, 0)
elif Input.is_action_pressed("ui_left"):
direction = Vector2(-1, 0)
elif Input.is_action_pressed("ui_down"):
direction = Vector2(0, 1)
elif Input.is_action_pressed("ui_up"):
direction = Vector2(0, -1)