|
1 | 1 | extends Node |
2 | 2 | class_name Common |
3 | | - |
| 3 | +# |
4 | 4 | const GEM_COLOR_NAMES = [Enums.GemColor.RED, Enums.GemColor.ORG, Enums.GemColor.YLW, Enums.GemColor.GRN, Enums.GemColor.BLU, Enums.GemColor.PRP] |
5 | 5 | const GEM_POINTS:int = 25 |
| 6 | +# |
| 7 | +var highlight_gem1: CommonGemCell = null |
| 8 | +var highlight_gem2: CommonGemCell = null |
6 | 9 |
|
7 | 10 | # ========================================================= |
8 | 11 |
|
@@ -37,6 +40,112 @@ func fill_hbox(hbox:HBoxContainer, gem_dict:Enums.GemDict, on_cell_click): |
37 | 40 | #control_node.connect("drag_start", self._on_cell_click) # TODO: |
38 | 41 | #control_node.connect("drag_ended", self._on_cell_click) # TODO: |
39 | 42 |
|
| 43 | +# ========================================================= |
| 44 | + |
| 45 | +# NEW |
| 46 | + |
| 47 | +func find_first_possible_swap(hbox:HBoxContainer) -> Array: |
| 48 | + var num_columns: int = hbox.get_child_count() |
| 49 | + var num_rows: int = hbox.get_child(0).get_child_count() |
| 50 | + # Create a 2D array to represent the board |
| 51 | + var board = [] |
| 52 | + for x in range(num_columns): |
| 53 | + var column = [] |
| 54 | + for y in range(num_rows): |
| 55 | + column.append(hbox.get_child(x).get_child(y)) |
| 56 | + board.append(column) |
| 57 | + # Try swapping each gem with its neighbors and check for matches |
| 58 | + var swaps = [[0, 1], [1, 0]] # Only right and down to avoid duplicate checks |
| 59 | + for x in range(num_columns): |
| 60 | + for y in range(num_rows): |
| 61 | + for swap in swaps: |
| 62 | + var dx = swap[0] |
| 63 | + var dy = swap[1] |
| 64 | + var nx = x + dx |
| 65 | + var ny = y + dy |
| 66 | + if nx < num_columns and ny < num_rows: |
| 67 | + # Swap using a temporary variable |
| 68 | + var temp = board[x][y] |
| 69 | + board[x][y] = board[nx][ny] |
| 70 | + board[nx][ny] = temp |
| 71 | + # Check for a match |
| 72 | + if has_match_at(x, y, board) or has_match_at(nx, ny, board): |
| 73 | + # Swap back |
| 74 | + temp = board[x][y] |
| 75 | + board[x][y] = board[nx][ny] |
| 76 | + board[nx][ny] = temp |
| 77 | + # Return the coordinates of the first valid swap |
| 78 | + return [board[x][y], board[nx][ny]] |
| 79 | + # Swap back |
| 80 | + temp = board[x][y] |
| 81 | + board[x][y] = board[nx][ny] |
| 82 | + board[nx][ny] = temp |
| 83 | + return [] # No moves possible |
| 84 | + |
| 85 | +func highlight_first_swap(hbox:HBoxContainer) -> void: |
| 86 | + var swap = find_first_possible_swap(hbox) |
| 87 | + if swap.size() == 2: |
| 88 | + highlight_gem1 = swap[0] |
| 89 | + highlight_gem2 = swap[1] |
| 90 | + highlight_gem1.highlight() |
| 91 | + highlight_gem2.highlight() |
| 92 | + # Optionally set a timer to remove highlight after a few seconds |
| 93 | + var timer = Timer.new() |
| 94 | + timer.wait_time = 3.0 # 3 seconds |
| 95 | + timer.one_shot = true |
| 96 | + timer.autostart = true |
| 97 | + timer.connect("timeout", self._on_HighlightTimer_timeout) |
| 98 | + add_child(timer) |
| 99 | + timer.start() |
| 100 | + |
| 101 | +func _on_HighlightTimer_timeout(): |
| 102 | + if highlight_gem1 and highlight_gem2: |
| 103 | + highlight_gem1.unhighlight() |
| 104 | + highlight_gem2.unhighlight() |
| 105 | + # Reset highlight gems to null |
| 106 | + highlight_gem1 = null |
| 107 | + highlight_gem2 = null |
| 108 | + |
| 109 | +func has_match_at(x, y, board): |
| 110 | + var color = board[x][y].gem_color |
| 111 | + |
| 112 | + # Check horizontal matches |
| 113 | + var count = 1 |
| 114 | + # Check left |
| 115 | + var i = x - 1 |
| 116 | + while i >= 0 and board[i][y].gem_color == color: |
| 117 | + count += 1 |
| 118 | + i -= 1 |
| 119 | + # Check right |
| 120 | + i = x + 1 |
| 121 | + while i < board.size() and board[i][y].gem_color == color: |
| 122 | + count += 1 |
| 123 | + i += 1 |
| 124 | + if count >= 3: |
| 125 | + return true |
| 126 | + |
| 127 | + # Check vertical matches |
| 128 | + count = 1 |
| 129 | + # Check up |
| 130 | + var j = y - 1 |
| 131 | + while j >= 0 and board[x][j].gem_color == color: |
| 132 | + count += 1 |
| 133 | + j -= 1 |
| 134 | + # Check down |
| 135 | + j = y + 1 |
| 136 | + while j < board[x].size() and board[x][j].gem_color == color: |
| 137 | + count += 1 |
| 138 | + j += 1 |
| 139 | + if count >= 3: |
| 140 | + return true |
| 141 | + |
| 142 | + return false |
| 143 | + |
| 144 | +func check_for_possible_moves(hbox:HBoxContainer) -> bool: |
| 145 | + if find_first_possible_swap(hbox).size() > 0: |
| 146 | + return true |
| 147 | + return false |
| 148 | + |
40 | 149 | # UTILS |
41 | 150 |
|
42 | 151 | func get_all_matches(hbox:HBoxContainer) -> Array: |
@@ -113,24 +222,6 @@ func extract_gem_cells_from_matches(matches: Array) -> Array: |
113 | 222 | all_gem_cells += match["cells"] # Append all cells in this match to the master list |
114 | 223 | return all_gem_cells |
115 | 224 |
|
116 | | -func calculate_score_for_matches(matches: Array) -> int: |
117 | | - var score = 0 |
118 | | - for match in matches: |
119 | | - var match_length = match["count"] |
120 | | - # Define scoring logic, e.g., exponential growth for larger matches |
121 | | - var match_score = match_length * match_length # Example: score grows quadratically with match length |
122 | | - score += match_score * GEM_POINTS |
123 | | - return score |
124 | | - |
125 | | -func calculate_scores_for_each_match(matches: Array) -> Dictionary: |
126 | | - var scores = {} |
127 | | - for match in matches: |
128 | | - var count = match["count"] |
129 | | - var score = count * GEM_POINTS |
130 | | - for cell in match["cells"]: |
131 | | - scores[cell] = score |
132 | | - return scores |
133 | | - |
134 | 225 | func find_gem_indices(gem_cell:CommonGemCell) -> Dictionary: |
135 | 226 | var parent_vbox = gem_cell.get_parent() # Assuming direct parent is a VBoxContainer |
136 | 227 | var hbox = parent_vbox.get_parent() # Assuming direct parent of VBox is the HBoxContainer |
@@ -173,6 +264,26 @@ func are_cells_adjacent(gemcell1:CommonGemCell, gemcell2:CommonGemCell) -> bool: |
173 | 264 |
|
174 | 265 | # ========================================================= |
175 | 266 |
|
| 267 | +func calculate_score_for_matches(matches: Array) -> int: |
| 268 | + var score = 0 |
| 269 | + for match in matches: |
| 270 | + var match_length = match["count"] |
| 271 | + # Define scoring logic, e.g., exponential growth for larger matches |
| 272 | + var match_score = match_length * match_length # Example: score grows quadratically with match length |
| 273 | + score += match_score * GEM_POINTS |
| 274 | + return score |
| 275 | + |
| 276 | +func calculate_scores_for_each_match(matches: Array) -> Dictionary: |
| 277 | + var scores = {} |
| 278 | + for match in matches: |
| 279 | + var count = match["count"] |
| 280 | + var score = count * GEM_POINTS |
| 281 | + for cell in match["cells"]: |
| 282 | + scores[cell] = score |
| 283 | + return scores |
| 284 | + |
| 285 | +# ========================================================= |
| 286 | + |
176 | 287 | func new_game_explode_replace(hbox:HBoxContainer, colors:Array, delay:float): |
177 | 288 | # A: |
178 | 289 | for vbox in hbox.get_children(): |
|
0 commit comments