-
Notifications
You must be signed in to change notification settings - Fork 6
/
player.gd
125 lines (94 loc) · 2.82 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
extends Area2D
signal hit
# Player's speed (pixels/sec)
@export var speed: int = 400
# Player's boost factor
@export var boost_factor: int = 4
# Player's boost duration (sec)
@export_range(0, 1, 0.05, "suffix:s", "or_greater")
var boost_time: float = 0.2
# Boost cooldown (sec)
@export_range(0, 5, 0.1, "suffix:s", "or_greater")
var boost_cooldown_time: float = 2
# Boost color
@export_color_no_alpha
var boost_color: Color = Color(1, 0.5, 1)
# Player's state (accelerated or not)
var is_boost: bool = false
# Player's state (cooling down after boost or not)
var is_cooldown: bool = false
# Boost timer
var timer_boost := Timer.new()
# Boost cooldown timer
var timer_cooldown := Timer.new()
# Size of the game window
var screen_size: Vector2i
# Called when the node enters
# the scene tree for the first time.
func _ready() -> void:
hide()
screen_size = get_viewport_rect().size
# Initialize boost timer
add_child(timer_boost)
timer_boost.wait_time = boost_time
timer_boost.one_shot = true
timer_boost.connect("timeout", boost_timer_timeout)
# Initialize cooldown timer
add_child(timer_cooldown)
timer_cooldown.wait_time = boost_cooldown_time
timer_cooldown.one_shot = true
timer_cooldown.connect("timeout", cooldown_timer_timeout)
# Called every frame.
# 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
# The player's velocity vector
var velocity: Vector2 = Vector2.ZERO
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 Input.is_action_pressed("accelerate"):
if not is_boost and not is_cooldown:
timer_boost.start()
timer_cooldown.start()
is_boost = true
is_cooldown = true
$Sprite2D.modulate = boost_color
$BoostSound.play()
if velocity.length() > 0:
velocity = velocity.normalized() * speed
if is_boost:
velocity *= boost_factor
position += velocity * delta
# Periodic boundaries
if position.x < 0:
position.x = screen_size.x
if position.x > screen_size.x:
position.x = 0
if position.y < 0:
position.y = screen_size.y
if position.y > screen_size.y:
position.y = 0
# Called when the boost timer triggered
func boost_timer_timeout() -> void:
is_boost = false
print("Boost time out")
# Called when boost cooldown timer triggered
func cooldown_timer_timeout() -> void:
is_cooldown = false
$Sprite2D.modulate = Color(1, 1, 1)
print("Cooldown time out")
# Called when an enemy hits the player
func _on_body_entered(body: Node2D) -> void:
# Emits the 'hit' signal and passes
# the 'body' that collided with the player
hit.emit(body)
# Called to reset the player when starting a new game
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false