-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
53 lines (40 loc) · 1.22 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
extends Area2D
signal hit
export var speed = 200
onready var screen_size = get_viewport_rect().size
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false
func _ready():
hide()
func _process(delta):
var velocity = Vector2()
# Support WASD as well as arrow keys
if Input.is_key_pressed(KEY_W) || Input.is_key_pressed(KEY_UP):
velocity.y -= 1
if Input.is_key_pressed(KEY_S) || Input.is_key_pressed(KEY_DOWN):
velocity.y += 1
if Input.is_key_pressed(KEY_A) || Input.is_key_pressed(KEY_LEFT):
velocity.x -= 1
if Input.is_key_pressed(KEY_D) || Input.is_key_pressed(KEY_RIGHT):
velocity.x += 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
position += velocity * delta
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
if velocity.x != 0:
$AnimatedSprite.animation = "walk"
$AnimatedSprite.flip_v = false
$AnimatedSprite.flip_h = velocity.x < 0
elif velocity.y != 0:
$AnimatedSprite.animation = "up"
$AnimatedSprite.flip_v = velocity.y > 0
func _on_Player_body_entered(_body):
hide()
emit_signal("hit")
$CollisionShape2D.set_deferred("disabled", true)