@@ -11,6 +11,7 @@ signal props_updated_gemsdict(gems_dict:Dictionary)
1111@onready var hbox_container :HBoxContainer = $ HBoxContainer
1212# VARS
1313const GEM_COLOR_NAMES = [Enums .GemColor .WHITE , Enums .GemColor .RED , Enums .GemColor .YELLOW , Enums .GemColor .GREEN , Enums .GemColor .PURPLE , Enums .GemColor .BROWN ]
14+ const GEM_POINTS :int = 25
1415var selected_cell_1 :GemCell = null
1516var selected_cell_2 :GemCell = null
1617var undo_cell_1 :GemCell = null
@@ -27,14 +28,7 @@ func _ready():
2728 fill_grid ()
2829 fill_hbox ()
2930 # B: check board after init
30- check_board_explode_matches ()
31-
32- # TODO: feed props to parent controller for display
33- func get_gem_props ():
34- var brent :Dictionary = {
35- "GREEN" : 99
36- }
37- return brent
31+ process_game_round ()
3832
3933# =========================================================
4034
@@ -113,10 +107,10 @@ func are_cells_adjacent(gemcell1:GemCell, gemcell2:GemCell) -> bool:
113107 # Cells are not adjacent
114108 return false
115109
116- func get_first_match_gems () -> Array :
110+ func get_all_matches () -> Array :
117111 var num_columns : int = hbox_container .get_child_count ()
118112 var num_rows : int = hbox_container .get_child (0 ).get_child_count ()
119- var match_cells : Array = []
113+ var matches = []
120114
121115 # Horizontal Check (check each row)
122116 for row in range (num_rows ):
@@ -129,19 +123,24 @@ func get_first_match_gems() -> Array:
129123 streak += 1
130124 else :
131125 if streak >= 3 :
132- # Collect matching GemCells
133- match_cells = []
126+ var match_cells = []
134127 for i in range (match_start , column ):
135128 match_cells .append (hbox_container .get_child (i ).get_child (row ))
136- return match_cells
129+ matches .append ({
130+ "cells" : match_cells ,
131+ "count" : streak
132+ })
137133 streak = 1
138134 match_start = column
139135 last_color = gem_cell .gem_color
140136 if streak >= 3 :
141- match_cells = []
137+ var match_cells = []
142138 for i in range (match_start , num_columns ):
143139 match_cells .append (hbox_container .get_child (i ).get_child (row ))
144- return match_cells
140+ matches .append ({
141+ "cells" : match_cells ,
142+ "count" : streak
143+ })
145144
146145 # Vertical Check (check each column)
147146 for column in range (num_columns ):
@@ -154,21 +153,51 @@ func get_first_match_gems() -> Array:
154153 streak += 1
155154 else :
156155 if streak >= 3 :
157- # Collect matching GemCells
158- match_cells = []
156+ var match_cells = []
159157 for i in range (match_start , row ):
160158 match_cells .append (hbox_container .get_child (column ).get_child (i ))
161- return match_cells
159+ matches .append ({
160+ "cells" : match_cells ,
161+ "count" : streak
162+ })
162163 streak = 1
163164 match_start = row
164165 last_color = gem_cell .gem_color
165166 if streak >= 3 :
166- match_cells = []
167+ var match_cells = []
167168 for i in range (match_start , num_rows ):
168169 match_cells .append (hbox_container .get_child (column ).get_child (i ))
169- return match_cells
170-
171- return [] # No matches found
170+ matches .append ({
171+ "cells" : match_cells ,
172+ "count" : streak
173+ })
174+
175+ return matches
176+
177+ func extract_gem_cells_from_matches (matches : Array ) -> Array :
178+ var all_gem_cells = []
179+ for match in matches :
180+ # `match["cells"]` is the array of gem cells for this particular match
181+ all_gem_cells += match ["cells" ] # Append all cells in this match to the master list
182+ return all_gem_cells
183+
184+ func calculate_score_for_matches (matches : Array ) -> int :
185+ var score = 0
186+ for match in matches :
187+ var match_length = match ["count" ]
188+ # Define scoring logic, e.g., exponential growth for larger matches
189+ var match_score = match_length * match_length # Example: score grows quadratically with match length
190+ score += match_score * GEM_POINTS
191+ return score
192+
193+ func calculate_scores_for_each_match (matches : Array ) -> Dictionary :
194+ var scores = {}
195+ for match in matches :
196+ var count = match ["count" ]
197+ var score = count * GEM_POINTS
198+ for cell in match ["cells" ]:
199+ scores [cell ] = score
200+ return scores
172201
173202# STEP 1: Handle input (=capture first & second selection), swap gems
174203# @desc: calls `swap_gem_cells()` when 2 cells selected and are adjacent
@@ -271,27 +300,43 @@ func tween_completed():
271300
272301 # C: once all tweens complete, check board
273302 if tweens_running_cnt == 0 :
274- check_board_explode_matches ()
303+ process_game_round ()
275304
276305# STEP 4: Check board, then explode first match found... (repeat until exhausted)
277306
278- func check_board_explode_matches ():
279- Enums .debug_print ("[check_board_explode_matches ]: =====================================" , Enums .DEBUG_LEVEL .INFO )
280- Enums .debug_print ("[check_board_explode_matches ]: CHECKING BOARD... " , Enums .DEBUG_LEVEL .INFO )
281- Enums .debug_print ("[check_board_explode_matches ]: =====================================" , Enums .DEBUG_LEVEL .INFO )
307+ func process_game_round ():
308+ Enums .debug_print ("[process_game_round ]: =====================================" , Enums .DEBUG_LEVEL .INFO )
309+ Enums .debug_print ("[process_game_round ]: CHECKING BOARD... " , Enums .DEBUG_LEVEL .INFO )
310+ Enums .debug_print ("[process_game_round ]: =====================================" , Enums .DEBUG_LEVEL .INFO )
282311
283- # TDDO: WIP: this will change once we start exploding all at once
284- board_props_score += 10
312+ # A:
313+ # EX:
314+ # matches[
315+ # {
316+ # "cells":["@Control@112:<Control#89338678177>","@Control@113:<Control#90143984570>","@Control@114:<Control#90949290963>"],
317+ # "count":3},
318+ # {
319+ # "cells":["@Control@119:<Control#95781129321>","@Control@120:<Control#96586435714>","@Control@121:<Control#97391742107>"],
320+ # "count":3
321+ # }
322+ # ]
323+ var matches = get_all_matches ()
324+ var gem_cells = extract_gem_cells_from_matches (matches )
325+ Enums .debug_print ("[process_game_round]: matches.. = " + JSON .stringify (matches ), Enums .DEBUG_LEVEL .DEBUG )
326+ Enums .debug_print ("[process_game_round]: gem_cells = " + str (gem_cells ), Enums .DEBUG_LEVEL .DEBUG )
327+ if matches .size () > 0 and Enums .current_debug_level == Enums .DEBUG_LEVEL .DEBUG :
328+ debug_print_ascii_table (gem_cells )
329+
330+ # B:
331+ var score = calculate_score_for_matches (matches )
332+ board_props_score += score
285333 emit_signal ("props_updated_score" , board_props_score )
286334
287- # A:
335+ # C: Update UI or game state as necessary
288336 signal_game_props_count_gems ()
289337
290- # B:
291- var gem_matches = get_first_match_gems ()
292- if gem_matches .size () > 0 :
293- debug_print_ascii_table (gem_matches )
294- if gem_matches .size () == 0 :
338+ # D: Handle resuolts: explode matches, or halt
339+ if matches .size () == 0 :
295340 Enums .debug_print ("[check_board_explode_matches]: No more matches. Board stable." , Enums .DEBUG_LEVEL .INFO )
296341 # A:
297342 # B: TODO: check for "NO MORE MOVES"
@@ -305,38 +350,49 @@ func check_board_explode_matches():
305350 undo_cell_1 = null
306351 undo_cell_2 = null
307352 # B: explode matched gems
308- explode_refill_gems (gem_matches )
353+ var match_scores = calculate_scores_for_each_match (matches )
354+ explode_refill_gems (matches , match_scores )
309355
310- func explode_refill_gems (gem_cells : Array ):
311- Enums .debug_print ("[explode_refill_gems........ ]: =====================================" , Enums .DEBUG_LEVEL .INFO )
312- Enums .debug_print ("[explode_refill_gems........ ]: *EXPLODING* gem_cell count: " + str (gem_cells .size ()), Enums .DEBUG_LEVEL .INFO )
313- Enums .debug_print ("[explode_refill_gems........ ]: =====================================" , Enums .DEBUG_LEVEL .INFO )
356+ func explode_refill_gems (matches : Array , match_scores : Dictionary ):
357+ Enums .debug_print ("[explode_refill_gems]: !!!!!!!!!!! =====================================" , Enums .DEBUG_LEVEL .INFO )
358+ Enums .debug_print ("[explode_refill_gems]: *EXPLODING* gem_cell count: " + str (matches .size ()), Enums .DEBUG_LEVEL .INFO )
359+ Enums .debug_print ("[explode_refill_gems]: !!!!!!!!!!! =====================================" , Enums .DEBUG_LEVEL .INFO )
314360 if Enums .current_debug_level == Enums .DEBUG_LEVEL .DEBUG :
315- debug_print_ascii_table (gem_cells )
361+ debug_print_ascii_table (extract_gem_cells_from_matches ( matches ) )
316362
317363 # A: explode selected
318- for gem_cell in gem_cells :
319- gem_cell .explode_gem (gem_cell .gem_color )
320- await get_tree ().create_timer (Enums .EXPLODE_DELAY ).timeout
364+ for match in matches :
365+ for gem_cell in match ["cells" ]:
366+ var score = match_scores [gem_cell ]
367+ gem_cell .explode_gem (gem_cell .gem_color , score )
368+
369+ # TODO: FIXME: gem counts need to update faster (they currently update after the animation completes)!!
370+ # seemingly, this would work fine located here but its not - the UI update requires a frame update i guess?
371+ signal_game_props_count_gems ()
321372
322- # B: Dictionary to track columns and the number of gems to add in each column
373+ # B: let explode animation run
374+ await get_tree ().create_timer (Enums .TWEEN_TIME ).timeout
375+
376+ # C: Dictionary to track columns and the number of gems to add in each column
323377 var columns_to_refill = {}
324- for gem_cell in gem_cells :
325- var column_index = gem_cell .get_parent ().get_index ()
326- var row_index = gem_cell .get_index ()
327- if column_index in columns_to_refill :
328- columns_to_refill [column_index ]["count" ] += 1
329- columns_to_refill [column_index ]["highest" ] = max (columns_to_refill [column_index ]["highest" ], row_index )
330- else :
331- columns_to_refill [column_index ] = {"highest" : row_index , "count" : 1 }
378+ for match in matches :
379+ for gem_cell in match ["cells" ]:
380+ var column_index = gem_cell .get_parent ().get_index ()
381+ var row_index = gem_cell .get_index ()
382+ if column_index in columns_to_refill :
383+ columns_to_refill [column_index ]["count" ] += 1
384+ columns_to_refill [column_index ]["highest" ] = max (columns_to_refill [column_index ]["highest" ], row_index )
385+ else :
386+ columns_to_refill [column_index ] = {"highest" : row_index , "count" : 1 }
332387
333388 # C: Process each column that needs refilling
334389 for column_index in columns_to_refill .keys ():
335390 var details = columns_to_refill [column_index ]
336391 refill_column (column_index , details ["highest" ], details ["count" ])
337392
338393 # D:
339- check_board_explode_matches ()
394+ await get_tree ().create_timer (Enums .EXPLODE_DELAY ).timeout # let refill animations above complete (otherwise new, matching gems would start exploding before they're even in place!)
395+ process_game_round ()
340396
341397func refill_column (column_index : int , highest_exploded_row : int , count_exploded : int ):
342398 var column = hbox_container .get_child (column_index )
@@ -444,7 +500,7 @@ func new_game():
444500 gem_cell .get_child (1 ).visible = true
445501 gem_cell .get_child (1 ).position = Enums .SRPITE_POS
446502 # B:
447- check_board_explode_matches ()
503+ process_game_round ()
448504
449505func debug_clear_debug_labels ():
450506 for vbox in hbox_container .get_children ():
@@ -477,4 +533,5 @@ func debug_make_gem_grid():
477533 gem .initialize (Enums .GemColor .GREEN )
478534 if (i + j ) % 2 == 0 :
479535 gem .initialize (Enums .GemColor .WHITE )
536+ gem .get_child (1 ).position = Enums .SRPITE_POS
480537 signal_game_props_count_gems ()
0 commit comments