-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvaders.gd
216 lines (169 loc) · 5.53 KB
/
invaders.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
extends Node2D
var ufo_scene: PackedScene = preload("res://ufo.tscn")
var ufos = []
var ufos_by_word = Dictionary()
var writing_to_ufo
var playing = false
var current_level
const ROW_SIZE = 235
const UFO_WIDTH = 250
const UFO_HEIGTH = 242
const LEFT = 0
const RIGHT = 2560 - UFO_WIDTH
var ufo_speed = 500
var time = 0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
$LevelSelection.init(exit_to_menu, start)
func _unhandled_input(event):
if event is InputEventKey:
if event.pressed:
if event.keycode == KEY_ESCAPE:
exit_to_menu()
#get_tree().quit()
elif playing:
var character = char(event.unicode)
if writing_to_ufo != null and writing_to_ufo.alive:
writing_to_ufo.add_letter(character)
else:
for ufo in ufos:
if ufo.alive and ufo.word[0] == character:
ufo.add_letter(character)
writing_to_ufo = ufo
break
if writing_to_ufo == null:
_on_letter_failed()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if playing:
update_time(delta)
#for ufo in ufos:
# move_ufo(ufo, delta)
move_ufo(ufos[0], delta)
func update_time(delta):
time += delta
$Background/Time/Label.text = str(floor(time))
func move_ufo(moving_ufo, delta):
var moving_ufo_row = moving_ufo.data["moving_ufo_row"]
var target = Vector2(moving_ufo.position.x, moving_ufo.position.y)
if playing and moving_ufo != null and moving_ufo.active:
if moving_ufo_row % 2 == 0: #move left
if moving_ufo.position.x <= LEFT: #move down
target.y = (moving_ufo_row + 1) * ROW_SIZE
if moving_ufo.position.y >= target.y: #advance_row
moving_ufo_row += 1
moving_ufo.data["moving_ufo_row"] = moving_ufo_row
else:
target.x = 0
else: #move right
if moving_ufo.position.x >= RIGHT: #move down
target.y = (moving_ufo_row + 1) * ROW_SIZE
if moving_ufo.position.y >= target.y: #advance_row
moving_ufo_row += 1
moving_ufo.data["moving_ufo_row"] = moving_ufo_row
else:
target.x = RIGHT
Globals.move(moving_ufo, target, ufo_speed, delta)
check_end()
func check_end():
if ufos[0].data["moving_ufo_row"] == 4 and ufos[0].position.x + 30 < $City.position.x + $City.size.x / 2:
playing = false
$Explosion.visible = true
$CityDestroyed.play()
await get_tree().create_timer(1).timeout
$Explosion.visible = false
$City.visible = false
while len(ufos) > 0:
delete_ufo(ufos[0])
ufos_by_word.clear()
$WinPopup.init("Has perdido :(", "¿Quieres jugar otra vez?", "", 0, current_level+1, exit_to_menu, start)
$WinPopup.visible = true
$Background/Time.visible = false
func create_ufos():
var x = 10
var y = 10
var num_ufos = current_level % 5 + 4
var word_size = min((floor(current_level / 5)) + 2, 7)
ufo_speed = min(225 + (current_level * 25), 750)
for i in range(num_ufos):
create_ufo(Vector2(x + i * 325, y), word_size)
func create_ufo(position, word_size):
var letters = ufos_by_word.keys().map(func(w): return w[0])
var word = Globals.next_word(word_size, letters)
var ufo: Node = ufo_scene.instantiate()
ufo.set_word(word)
ufo.position = position
ufos.append(ufo)
ufos_by_word[word] = ufo
ufo.connect("destroyed", _on_ufo_destroyed)
ufo.connect("letter_failed", _on_letter_failed)
ufo.data["moving_ufo_row"] = 0
add_child(ufo)
func _on_ufo_destroyed(word):
$ExplosionSound.play()
var ufo = ufos_by_word[word]
if ufo == writing_to_ufo:
writing_to_ufo = null
$LaserBeam2D.target_position.x = ufo.position.x + UFO_WIDTH / 2 - $LaserBeam2D.position.x
$LaserBeam2D.target_position.y = ufo.position.y + UFO_HEIGTH / 2 - $LaserBeam2D.position.y
$LaserBeam2D.set_is_casting(true)
await get_tree().create_timer(1.0 - (ufo_speed / 1500.0)).timeout
$LaserBeam2D.set_is_casting(false)
delete_ufo(ufo)
if len(ufos) == 0:
win()
func delete_ufo(ufo):
if ufo != null:
ufo.disconnect("destroyed", _on_ufo_destroyed)
ufo.disconnect("letter_failed", _on_letter_failed)
ufos.erase(ufo)
ufos_by_word.erase(ufo)
ufo.queue_free()
remove_child(ufo)
func win():
playing = false
var your_time = floor(time)
var best_times = []
best_times.resize(40)
best_times.fill(0)
best_times = Globals.config.get_value("invaders", "best_times", best_times)
if best_times[current_level]== 0 or (your_time < best_times[current_level]):
best_times[current_level] = floor(time)
Globals.config.set_value("invaders", "best_times", best_times)
var stars = 1
var num_ufos = current_level % 5 + 4
if time < 2 * num_ufos:
stars = 3
elif time < 4 * num_ufos:
stars = 2
$WinPopup.init("¡Has ganado!", "Tu tiempo: " + str(your_time), "Mejor tiempo: " + str(best_times[current_level]), stars, current_level+1, exit_to_menu, next_level)
$WinPopup.visible = true
$Background/Time.visible = false
var max_level = Globals.config.get_value("invaders", "level", -1)
if current_level+1 > max_level:
Globals.config.set_value("invaders", "level", current_level + 1)
Globals.save_config()
func exit_to_menu():
get_tree().change_scene_to_file("res://main_menu.tscn")
func next_level():
current_level = min(current_level+1, 39)
start()
func start():
$LevelSelection.visible = false
$WinPopup.visible = false
$Explosion.visible = false
$Background/Time.visible = true
$City.visible = true
while len(ufos) > 0:
delete_ufo(ufos[0])
ufos_by_word.clear()
create_ufos()
time = 0
playing = true
func _on_letter_failed():
writing_to_ufo = null
time += 5
$Error.play()
func _on_level_selection_level_select(level: Variant) -> void:
current_level = level
print("====>"+str(level))