-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.gd
More file actions
373 lines (311 loc) · 10.8 KB
/
main.gd
File metadata and controls
373 lines (311 loc) · 10.8 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
extends Node2D
signal piece_unavailable(piece_name: String)
@export var board : Node
@export var enemy_texture : Texture2D
var active_piece_type : String = "none"
var player_pieces : Array[Dictionary] = []
var enemy_count : int = 15
var score : int = 0
const BOARD_SIZE : int = 8
const FLASH_DURATION : float = 0.2
const RESPAWN_DELAY : float = 0.3
var piece_types : Dictionary = {
"k" : "knight",
"r" : "rook",
"b" : "bishop",
"q" : "queen",
"p" : "pawn",
}
var available_pieces : Dictionary = {
"knight": 2,
"rook": 2,
"bishop": 2,
"queen": 1,
# "king": 1,
"pawn": 4,
}
var textures : Dictionary = {
"knight": preload("res://assets/WKnight.svg"),
"rook": preload("res://assets/WRook.svg"),
"bishop": preload("res://assets/WBishop.svg"),
"queen": preload("res://assets/WQueen.svg"),
"king": preload("res://assets/WKing.svg"),
"pawn": preload("res://assets/WPawn.svg"),
}
var flashing_squares = {}
var game_seed: int
var rng: RandomNumberGenerator
var elapsed_time: float = 0.0
var stopwatch_active: bool = false
func _ready() -> void:
rng = RandomNumberGenerator.new()
game_seed = GameState.current_seed
rng.seed = game_seed
$"seed".text = str(game_seed)
$"seed".text_submitted.connect(_on_seed_submitted)
$ResetButton.pressed.connect(_on_reset_pressed)
player_pieces.clear()
board.square_clicked.connect(_on_square_clicked)
piece_unavailable.connect(_on_piece_unavailable)
for i in enemy_count:
spawn_enemy(false)
update_score_and_time_history()
stopwatch_active = true
func set_seed(new_seed: int) -> void:
game_seed = new_seed
GameState.current_seed = new_seed
rng.seed = game_seed
func _on_seed_submitted(new_seed) -> void:
if new_seed.is_valid_int():
print("reached to int, seed on signal submit :"+ str(game_seed))
set_seed(new_seed.to_int())
get_tree().reload_current_scene()
print("just after reload:"+str(game_seed))
else:
$"seed".text = str(game_seed)
func spawn_enemy(should_flash: bool = true) -> void:
var empty_positions = _get_empty_positions()
if empty_positions.is_empty():
return
var random_pos = empty_positions[rng.randi() % empty_positions.size()]
var square = board.get_square(random_pos)
var color_rect = square.get_node("ColorRect")
if should_flash:
_cleanup_flashing_squares()
await _flash_square(color_rect)
var content = square.get_node("ColorRect/ContentTextureRect")
content.texture = enemy_texture
content.visible = true
board.occupied_squares[random_pos] = true
board.occupied_by_piece[random_pos] = "enemy"
func _flash_square(color_rect: ColorRect) -> void:
var original_color = color_rect.color
flashing_squares[color_rect] = original_color
color_rect.color = Color("#12f2c9b3")
await get_tree().create_timer(FLASH_DURATION).timeout
if color_rect in flashing_squares:
color_rect.color = original_color
flashing_squares.erase(color_rect)
func _cleanup_flashing_squares() -> void:
for color_rect in flashing_squares:
color_rect.color = flashing_squares[color_rect]
flashing_squares.clear()
func _get_empty_positions() -> Array:
var empty = []
for x in BOARD_SIZE:
for y in BOARD_SIZE:
var pos = Vector2(x, y)
if not board.occupied_squares.get(pos, false):
empty.append(pos)
return empty
func _process(delta: float) -> void:
if stopwatch_active:
elapsed_time += delta
update_stopwatch_display()
if Input.is_action_just_pressed("use_piece"):
get_piece()
func update_stopwatch_display() -> void:
var minutes = int(elapsed_time / 60)
var seconds = int(elapsed_time) % 60
$StopwatchLabel.text = "Time: %d:%02d" % [minutes, seconds]
func get_piece() -> void:
for key in piece_types:
if Input.is_action_just_pressed(key):
var piece_name = piece_types[key]
if piece_name in available_pieces and available_pieces[piece_name] > 0:
# Remove previous piece if it exists
for piece in player_pieces:
var square = board.get_square(piece.pos)
var content = square.get_node("ColorRect/ContentTextureRect")
content.texture = null
content.visible = false
board.occupied_squares[piece.pos] = false
board.occupied_by_piece[piece.pos] = "none"
player_pieces.clear()
active_piece_type = piece_name
$SelectedPiece.text = "Selected Piece: " + active_piece_type
else:
var is_game_over = await check_game_over()
if not is_game_over:
piece_unavailable.emit(piece_name)
func _on_piece_unavailable(piece_name: String) -> void:
var popup = Popup.new()
var label = Label.new()
label.text = piece_name + " not available"
label.add_theme_font_size_override("font_size", 70) # Increased font size
popup.add_child(label)
add_child(popup)
popup.popup_centered()
await get_tree().create_timer(FLASH_DURATION).timeout
popup.queue_free()
func _on_square_clicked(grid_pos: Vector2) -> void:
if active_piece_type != "none":
place_piece(grid_pos)
board.clear_highlights()
func place_piece(grid_pos: Vector2) -> void:
if board.occupied_squares.get(grid_pos, false):
return
var square = board.get_square(grid_pos)
var content = square.get_node("ColorRect/ContentTextureRect")
content.texture = textures[active_piece_type]
content.visible = true
player_pieces.append({"type": active_piece_type, "pos": grid_pos})
board.occupied_squares[grid_pos] = true
board.occupied_by_piece[grid_pos] = "player"
available_pieces[active_piece_type] -= 1
$Available.text = str(available_pieces)
active_piece_type = "none"
$SelectedPiece.text = "Selected Piece: none"
check_kills()
func check_kills() -> void:
var killed_positions = _get_killed_positions()
if killed_positions.is_empty():
return
# Handle all kills at once
for pos in killed_positions:
var square = board.get_square(pos)
var content = square.get_node("ColorRect/ContentTextureRect")
content.texture = null
content.visible = false
board.occupied_squares[pos] = false
score += 1
$ScoreLabel.text = "Score: " + str(score)
# Wait a moment before respawning
await get_tree().create_timer(RESPAWN_DELAY).timeout
# Respawn enemies until we're back to enemy_count
var current_enemies = count_enemies()
var enemies_to_spawn = enemy_count - current_enemies
for i in enemies_to_spawn:
spawn_enemy(true)
# Add a small delay between spawns
await get_tree().create_timer(0.1).timeout
func count_enemies() -> int:
var count = 0
for pos in board.occupied_squares:
if board.occupied_squares[pos]:
var square = board.get_square(pos)
var content = square.get_node("ColorRect/ContentTextureRect")
if content.texture == enemy_texture:
count += 1
return count
func _get_killed_positions() -> Array:
var killed = []
for piece in player_pieces:
var kills = get_moves(piece.type, piece.pos)
for pos in kills:
if board.occupied_squares.get(pos, false) and board.occupied_by_piece[pos] == "enemy":
killed.append(pos)
return killed
func get_moves(piece_type : String, grid_position : Vector2) -> Array[Vector2]:
match piece_type:
"knight":
return get_knight_moves(grid_position)
"rook":
return get_rook_moves(grid_position)
"bishop":
return get_bishop_moves(grid_position)
"queen":
return get_queen_moves(grid_position)
"king":
return get_king_moves(grid_position)
"pawn":
return get_pawn_moves(grid_position)
return []
func get_knight_moves(grid_position : Vector2) -> Array[Vector2]:
var moves : Array[Vector2] = []
var directions = [
Vector2(-2, -1), Vector2(-2, 1),
Vector2(-1, -2), Vector2(-1, 2),
Vector2(1, -2), Vector2(1, 2),
Vector2(2, -1), Vector2(2, 1)
]
for dir in directions:
var new_pos = grid_position + dir
if new_pos.x >= 0 and new_pos.x < 8 and new_pos.y >= 0 and new_pos.y < 8:
moves.append(new_pos)
return moves
func get_rook_moves(grid_position : Vector2) -> Array[Vector2]:
var moves : Array[Vector2] = []
var directions = [
Vector2(1,0), Vector2(-1,0),Vector2(0,1), Vector2(0,-1)
]
for dir in directions:
var current_pos = grid_position + dir
while current_pos.x >= 0 and current_pos.x < 8 and current_pos.y >= 0 and current_pos.y < 8:
moves.append(current_pos)
current_pos += dir
return moves
func get_bishop_moves(grid_position : Vector2) -> Array[Vector2]:
var moves : Array[Vector2] = []
var directions = [
Vector2(1,1), Vector2(-1,-1),Vector2(1,-1), Vector2(-1,1)
]
for dir in directions:
var current_pos = grid_position + dir
while current_pos.x >= 0 and current_pos.x < 8 and current_pos.y >= 0 and current_pos.y < 8:
moves.append(current_pos)
current_pos += dir
return moves
func get_queen_moves(grid_position : Vector2) -> Array[Vector2]:
var moves : Array[Vector2] = []
var directions = [
Vector2(1,0), Vector2(-1,0),Vector2(0,1), Vector2(0,-1),
Vector2(1,1), Vector2(-1,-1),Vector2(1,-1), Vector2(-1,1)
]
for dir in directions:
var current_pos = grid_position + dir
while current_pos.x >= 0 and current_pos.x < 8 and current_pos.y >= 0 and current_pos.y < 8:
moves.append(current_pos)
current_pos += dir
return moves
func get_king_moves(grid_position : Vector2) -> Array[Vector2]:
var moves : Array[Vector2] = []
var directions = [
Vector2(1,0), Vector2(-1,0),Vector2(0,1), Vector2(0,-1),
Vector2(1,1), Vector2(-1,-1),Vector2(1,-1), Vector2(-1,1)
]
for dir in directions:
var new_pos = grid_position + dir
if new_pos.x >= 0 and new_pos.x < 8 and new_pos.y >= 0 and new_pos.y < 8:
moves.append(new_pos)
return moves
func get_pawn_moves(grid_position : Vector2) -> Array[Vector2]:
var moves : Array[Vector2] = []
var move_direction = -1
var new_pos = grid_position + Vector2(0,move_direction)
if new_pos.x >= 0 and new_pos.x < 8 and new_pos.y >= 0 and new_pos.y < 8:
moves.append(new_pos)
new_pos = grid_position + Vector2(0,move_direction*2)
if new_pos.x >= 0 and new_pos.x < 8 and new_pos.y >= 0 and new_pos.y < 8:
moves.append(new_pos)
new_pos = grid_position + Vector2(1,move_direction)
if new_pos.x >= 0 and new_pos.x < 8 and new_pos.y >= 0 and new_pos.y < 8:
moves.append(new_pos)
new_pos = grid_position + Vector2(-1,move_direction)
if new_pos.x >= 0 and new_pos.x < 8 and new_pos.y >= 0 and new_pos.y < 8:
moves.append(new_pos)
return moves
func get_current_seed() -> int:
return game_seed
func _on_reset_pressed() -> void:
GameState.score_history.append(score)
GameState.time_history.append(elapsed_time)
get_tree().reload_current_scene()
func update_score_and_time_history() -> void:
var score_history = GameState.score_history
var time_history = GameState.time_history
var history_text = "History:\n"
for i in range(score_history.size()):
var minutes = int(time_history[i] / 60)
var seconds = int(time_history[i]) % 60
history_text += "Score: %d - Time: %d:%02d\n" % [score_history[i], minutes, seconds]
$ScoreHistoryLabel.text = history_text
func check_game_over() -> bool:
for piece_name in available_pieces:
if available_pieces[piece_name] > 0:
return false
stopwatch_active = false
$GameOverPopup.popup_centered()
await get_tree().create_timer(1.0).timeout
_on_reset_pressed()
return true