-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.gd
92 lines (60 loc) · 2.23 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
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
90
extends Area2D
signal hit # a custom signal to indicate when player is hit
signal moon_land # custom signal to indicate when we touch moon
export var speed = 400 # how fast player will move in pixel/sec. export allows us to adjust the variable in the inspector menu
var screen_size # size of the game window
# Called when the node enters the scene tree for the first time.
func _ready():
hide() # hide player when game begins
screen_size = get_viewport_rect().size # get size of game window
pass
func _on_Player_body_entered(body):
print("Player: Hit by Node: %s" %body.get_name())
$CollisionShape2D.set_deferred("disabled", true) # disable collision so we don't trigger hit more than once
# if player collides with Moon - the sheep is safe
if body.get_name() == "Moon":
hide()
emit_signal("moon_land")
# if player collides with anything else, assume its the Mob - game over
else:
hide() # player dissapears after being hit
emit_signal("hit")
pass
# reset player when starting a new game
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var velocity = Vector2() # player movement vector
# handle the keyboard direction presses
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
# allows animation to move
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
# update player position
position += velocity * delta
# clamp prevents from leaving the screen based on given boundaries
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
# # choose sprite icons to use when moving
# when we are moving on x axis
if velocity.x != 0:
$AnimatedSprite.animation = "walk"
$AnimatedSprite.flip_v = false
$AnimatedSprite.flip_h = velocity.x < 0 # flip h when we are going left
# elif velocity.y != 0:
# $AnimatedSprite.animation = "up"
# $AnimatedSprite.flip_v = velocity.y > 0