-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
70 lines (54 loc) · 1.57 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
extends KinematicBody2D
var velocity = Vector2()
var anim
const TILE_SIZE = 16
# Store the last input command's direction.
var direction: Vector2 = Vector2.ZERO
# Speed of movement
var _pixels_per_second: float = 2 * TILE_SIZE
var _step_size: float = 1 / _pixels_per_second
# Accumulator of deltas, aka fractions of seconds, to time movement.
var _step: float = 0
# Count movement progress in distinct integer steps
var _pixels_moved: int = 0
var motion = Vector2.ZERO
var moved = false
var stats = load("res://AishaStats.tres")
func _ready():
anim = $AnimatedSprite
func is_moving() -> bool:
return self.direction.x != 0 or self.direction.y != 0
func _physics_process(delta):
handle_movement(delta)
func handle_movement(delta):
moved = false
motion = Vector2.ZERO
if Input.is_action_pressed("MoveRight"):
self.direction.x = 1.1
anim.play('MoveRight')
if Input.is_action_pressed("MoveLeft"):
self.direction.x = -1.1
anim.play('MoveLeft')
if Input.is_action_pressed("MoveDown"):
self.direction.y = 1.1
anim.play('MoveDown')
if Input.is_action_pressed("MoveUp"):
self.direction.y = -1.1
anim.play('MoveUp')
if not is_moving(): return false
# 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
moved = true
return true