-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.gd
52 lines (42 loc) · 1.19 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
extends Area2D
signal hit
export var speed = 400.0
var screen_size = Vector2.ZERO
func _ready():
screen_size = get_viewport_rect().size
print(screen_size)
hide()
func _process(delta):
var direction = Vector2.ZERO
if Input.is_action_pressed("move_left"):
direction.x -= 1
if Input.is_action_pressed("move_right"):
direction.x += 1
if Input.is_action_pressed("move_down"):
direction.y += 1
if Input.is_action_pressed("move_up"):
direction.y -= 1
if direction.length() > 0:
direction = direction.normalized()
$AnimatedSprite.play();
else:
$AnimatedSprite.stop()
position += direction * speed * delta
position.x = clamp(position.x, 0, screen_size.x);
position.y = clamp(position.y, 0, screen_size.y)
if direction.x != 0:
$AnimatedSprite.animation = "right"
$AnimatedSprite.flip_v = false
$AnimatedSprite.flip_h = direction.x < 0
elif direction.y != 0:
$AnimatedSprite.animation = "up"
$AnimatedSprite.flip_v = direction.y > 0
$AnimatedSprite.flip_h = false
func start(new_position):
position = new_position
show()
$CollisionShape2D.disabled = false
func _on_Player_body_entered(body):
hide()
$CollisionShape2D.set_deferred("disabled", true)
emit_signal("hit")