grid collapse downwards implemented

This commit is contained in:
Jack Case
2025-08-23 15:46:47 -04:00
parent c493d881c4
commit 05677ec35a

47
grid.gd
View File

@@ -23,14 +23,13 @@ func _ready():
for column in cols: for column in cols:
var token_node: Token = token.instantiate() var token_node: Token = token.instantiate()
add_child(token_node) add_child(token_node)
token_node.position = Vector2(offset*column, offset*row)
token_node.set_type(randi_range(0,3) as Token.token_type) token_node.set_type(randi_range(0,3) as Token.token_type)
token_node.set_debug_label(str(row) + "," + str(column)) token_node.set_debug_label(str(row) + "," + str(column))
token_node.token_clicked.connect(_on_token_clicked.bind([row,column])) token_node.token_clicked.connect(_on_token_clicked.bind([row,column]))
token_node.token_hovered.connect(_on_token_hovered.bind([row,column])) #token_node.token_hovered.connect(_on_token_hovered.bind([row,column]))
grid[row].append(token_node) grid[row].append(token_node)
calculate_token_groups() redraw_grid()
func _on_token_clicked(token_coord): func _on_token_clicked(token_coord):
var group = get_group_of_token(token_coord) var group = get_group_of_token(token_coord)
@@ -58,24 +57,42 @@ func highlight_group(group: Array, enable: bool):
for coord in group: for coord in group:
var current_token = grid[coord[0]][coord[1]] as Token var current_token = grid[coord[0]][coord[1]] as Token
if current_token == null: continue
current_token.set_highlighted(enable) current_token.set_highlighted(enable)
hovered_group = group hovered_group = group
func update_grid(): func update_grid():
# search for empty cells and drop tokens above them down # search for empty cells and drop tokens above them down
for column in range(cols):
var col_array = []
for row in range(rows-1, -1, -1):
# create a temporary array from the column
col_array.append(grid[row][column])
# filter the nulls out of the array
col_array = col_array.filter(func(token): return token != null)
# put the filtered column back into the grid
for row in range(rows-1, -1, -1):
var idx = rows - row - 1
grid[row][column] = col_array[idx] if idx < col_array.size() else null
# search for empty coluns and shift tokens horizontally to fill them # search for empty coluns and shift tokens horizontally to fill them
pass
func populate_grid(): redraw_grid()
pass
func to_grid_coord(idx): func redraw_grid():
pass for row in range(rows):
for column in range(cols):
var current_token = grid[row][column]
if current_token == null:
continue
func to_index(idx): current_token.position = Vector2(offset*column, offset*row)
pass
calculate_token_groups()
func calculate_token_groups(): func calculate_token_groups():
groups = [] groups = []
@@ -83,7 +100,7 @@ func calculate_token_groups():
var group_queue = [] var group_queue = []
for row in rows: for row in rows:
for col in cols: for col in cols:
if [row, col] in visited_nodes: if [row, col] in visited_nodes or grid[row][col] == null:
continue continue
group_queue.append([row,col]) group_queue.append([row,col])
@@ -113,9 +130,11 @@ func calculate_token_groups():
) )
for coord in valid_coords: for coord in valid_coords:
if (coord not in visited_nodes and var token = grid[coord[0]][coord[1]]
grid[coord[0]][coord[1]].type == current_token.type and if (token != null and
coord not in group_queue): coord not in visited_nodes and
grid[coord[0]][coord[1]].type == current_token.type and
coord not in group_queue):
group_queue.append(coord) group_queue.append(coord)
groups.append(new_group) groups.append(new_group)