-
Notifications
You must be signed in to change notification settings - Fork 3
/
player.gd
38 lines (30 loc) · 1006 Bytes
/
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
extends CharacterBody2D
#0 = front, 1 = right, 2 = back, 3 = left
var direction = 0
var idle = ["front-idle","side-idle","back-idle","side-idle"]
var walk = ["front-walk","side-walk","back-walk","side-walk"]
const SPEED = 75.0
func _physics_process(delta):
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var directionX = Input.get_axis("ui_left", "ui_right")
var directionY = Input.get_axis("ui_up", "ui_down")
if directionX:
velocity.x = directionX * SPEED
direction = 2 + directionX
if (directionX < 0) :
$Sprite2D.flip_h = true
else:
$Sprite2D.flip_h = false
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if directionY:
velocity.y = directionY * SPEED
direction = 1 - directionY
else:
velocity.y = move_toward(velocity.y, 0, SPEED)
if directionX || directionY:
$Sprite2D.play(walk[direction])
else:
$Sprite2D.play(idle[direction])
move_and_slide()