-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
58 lines (48 loc) · 1.88 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
extends Area2D
# "export" makes this valuable available in the inspector, so we can play about with it quickly
export var speed = 400 # How fast the player will move (pixels/sec).
# To stop sprite going over edges
export var sprite_size = Vector2(94, 45)
export var min_height = 450
var screen_size # Size of the game window.
# Called when the node enters the scene tree
# Feels like a constructor which can access state
func _ready():
screen_size = get_viewport_rect().size # Get the current size of the screen
hide() # Hides player by default when scene starts
# Called every frame, used for updating game elements
func _process(delta):
var velocity = Vector2() # The player's movement vector, initialises at (0,0)
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
# $ returns a child node at a relative path
# $AnimatedSprite is a child node of Player, so we are accessing that
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
# clamp stops the player going out of the bounds of the screen
# syntax is clamp(value, min, max)
position += velocity * delta
position.x = clamp(position.x, (0 + sprite_size.x), (screen_size.x - sprite_size.x))
position.y = clamp(position.y, (0 + min_height + sprite_size.y), (screen_size.y - sprite_size.y))
# Flips the sprite based on which direction it is going
if velocity.x != 0:
$AnimatedSprite.animation = "right"
$AnimatedSprite.flip_v = false
# See the note below about boolean assignment
$AnimatedSprite.flip_h = velocity.x < 0
# To be called when starting a new game
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false
func stop():
hide()