-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.gd
More file actions
90 lines (72 loc) · 2.16 KB
/
Player.gd
File metadata and controls
90 lines (72 loc) · 2.16 KB
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
signal bonus_points
export var speed = 400 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.
var playerDisabled
func _ready():
screen_size = get_viewport_rect().size
playerDisabled = true
hide()
func _process(delta):
if not playerDisabled:
var velocity = Vector2.ZERO # The player's movement vector.
if Input.is_action_pressed("move_right"):
velocity.x += 1
if Input.is_action_pressed("move_left"):
velocity.x -= 1
if Input.is_action_pressed("move_down"):
velocity.y += 1
if Input.is_action_pressed("move_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
position += velocity * delta
# clamp() will lock the player to the screen size. We don't want dis
# position.x = clamp(position.x, 0, screen_size.x)
# position.y = clamp(position.y, 0, screen_size.y)
if velocity.x != 0:
$AnimatedSprite.animation = "move"
elif velocity.y != 0:
$AnimatedSprite.animation = "move"
func start(pos):
position = pos
show()
$CollisionPolygon2D.disabled = false
playerDisabled = false
func update_player_name(playerName):
$Label.text = playerName
func _handle_powerup():
print("powerup grabbed")
func _on_Player_body_entered(_body):
if _body.is_in_group("enemies"):
hide() # Player disappears after being hit.
emit_signal("hit")
# Must be deferred as we can't change physics properties on a physics callback.
$CollisionPolygon2D.set_deferred("disabled", true)
if _body.is_in_group("powerups"):
emit_signal("bonus_points")
_handle_powerup()
# hide the powerup once it is grabbed
_body.queue_free()
func scale_player(direction):
print("scale player")
var scaler
if direction == "up":
scaler = .3
if direction == "down":
scaler = -.3
$AnimatedSprite.scale.x += scaler
$AnimatedSprite.scale.y += scaler
$CollisionPolygon2D.scale.x += scaler
$CollisionPolygon2D.scale.y += scaler
speed = speed*.75
func reset_scale_player():
$AnimatedSprite.scale.x = 1
$AnimatedSprite.scale.y = 1
$CollisionPolygon2D.scale.x = 1
$CollisionPolygon2D.scale.y = 1
speed = 400