-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
59 lines (49 loc) · 1.58 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
extends Area2D
# class member variables go here, for example:
# var a = 2
# var b = "textvar"
signal hit
export (int) var speed # How fast the player will move (pixels/sec).
var screensize # Size of the game window.
func _ready():
# Called when the node is added to the scene for the first time.
# Initialization here
screensize = get_viewport_rect().size
hide()
#func _process(delta):
# # Called every frame. Delta is time since last frame.
# # Update game logic here.
# pass
func _process(delta):
var velocity = Vector2() # The player's movement vector.
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)
if velocity.x != 0:
$AnimatedSprite.animation = "right"
$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_area_entered(area):
hide() # Player disappears after being hit.
emit_signal("hit")
$CollisionShape2D.disabled = true
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false