-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopDownPlayer.gd
89 lines (70 loc) · 1.86 KB
/
TopDownPlayer.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
extends KinematicBody2D
onready var animation = $AnimatedSprite
######################################################## Movement Variablen
var speed : float = 120.0
var runspeed : float = 180.0
var targetSpeed : float = 0.0
var curspeed : Vector2 = Vector2.ZERO
var direction : Vector2 = Vector2.ZERO
func _ready():
pass
func _physics_process(_delta):
movement(_delta)
Animation()
func _process(_delta):
curspeed = direction.normalized()*targetSpeed
curspeed = move_and_slide(curspeed,Vector2.UP)
#####################################################
##
## Movement
##
## Move the player
##
#####################################################
func movement(_delta):
if Input.is_action_pressed("left"):
direction.x = -1
elif Input.is_action_pressed("right"):
direction.x = 1
else:
direction.x = 0
if Input.is_action_pressed("up"):
direction.y = -1
elif Input.is_action_pressed("down"):
direction.y = 1
else:
direction.y = 0
if Input.is_action_pressed("sprint"):
targetSpeed = runspeed
else:
targetSpeed = speed
#####################################################
#####################################################
##
## Animation
##
## Animate the Player
##
#####################################################
func Animation() :
var anim = "idle"
if direction.x < 0 :
anim="walk_left"
elif direction.x > 0 :
anim="walk_right"
if direction.y < 0 :
anim="walk_up"
elif direction.y > 0 :
anim="walk_down"
# prevent animation stalling when 2 directions simultanously
if direction.x < 0 and direction.y < 0 :
anim="walk_left"
if direction.x > 0 and direction.y < 0 :
anim="walk_right"
if direction.x < 0 and direction.y > 0 :
anim="walk_down"
if direction.x > 0 and direction.y > 0 :
anim="walk_down"
if animation.animation!=anim:
animation.play(anim)
#####################################################