Implemented group highlight on hover

It's not currently stable, but it's ok for now.
This commit is contained in:
Jack Case
2025-08-23 13:35:41 -04:00
parent 9bcafdff5c
commit 99e2dce2b6
3 changed files with 63 additions and 27 deletions

68
grid.gd
View File

@@ -1,4 +1,5 @@
extends Node2D extends Node2D
class_name Grid
@export var rows: int = 5 @export var rows: int = 5
@export var cols: int = 5 @export var cols: int = 5
@@ -8,6 +9,7 @@ extends Node2D
var grid: Array[Array] var grid: Array[Array]
var groups: Array var groups: Array
var hovered_group: Array
var debug_label: Label var debug_label: Label
@@ -15,7 +17,7 @@ var token = preload("res://token.tscn")
func _ready(): func _ready():
debug_label = $"Debug Label" debug_label = $"Debug Label"
for row in rows: for row in rows:
grid.append([]) grid.append([])
for column in cols: for column in cols:
@@ -25,13 +27,14 @@ func _ready():
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]))
grid[row].append(token_node) grid[row].append(token_node)
calculate_token_groups() calculate_token_groups()
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
pass pass
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)
if group.size() >= min_group_size: if group.size() >= min_group_size:
@@ -39,24 +42,41 @@ func _on_token_clicked(token_coord):
var current_token = grid[coord[0]][coord[1]] as Token var current_token = grid[coord[0]][coord[1]] as Token
current_token.queue_free() current_token.queue_free()
grid[coord[0]][coord[1]] = null # do I actually want a null value or should there be some other placeholder? grid[coord[0]][coord[1]] = null # do I actually want a null value or should there be some other placeholder?
update_grid() update_grid()
func highlight_group(group: Array): func _on_token_hovered(enter: bool, coord: Array):
pass var group = get_group_of_token(coord)
if enter:
highlight_group(group, true)
else:
highlight_group(group, false)
func highlight_group(group: Array, enable: bool):
if group != hovered_group:
# Un-highlight current, update current
for coord in hovered_group:
var current_token = grid[coord[0]][coord[1]]
current_token.set_highlighted(false)
for coord in group:
var current_token = grid[coord[0]][coord[1]] as Token
current_token.set_highlighted(enable)
hovered_group = group
func update_grid(): func update_grid():
pass pass
func populate_grid(): func populate_grid():
pass pass
func to_grid_coord(idx): func to_grid_coord(idx):
pass pass
func to_index(idx): func to_index(idx):
pass pass
func calculate_token_groups(): func calculate_token_groups():
groups = [] groups = []
var visited_nodes = [] var visited_nodes = []
@@ -65,45 +85,45 @@ func calculate_token_groups():
for col in cols: for col in cols:
if [row, col] in visited_nodes: if [row, col] in visited_nodes:
continue continue
group_queue.append([row,col]) group_queue.append([row,col])
var new_group = [] var new_group = []
while not group_queue.is_empty(): while not group_queue.is_empty():
var current_coord = group_queue.pop_back() var current_coord = group_queue.pop_back()
var current_token = grid[current_coord[0]][current_coord[1]] var current_token = grid[current_coord[0]][current_coord[1]]
new_group.append(current_coord) new_group.append(current_coord)
visited_nodes.append(current_coord) visited_nodes.append(current_coord)
var adjacent_token_coords = [ var adjacent_token_coords = [
[current_coord[0] - 1, current_coord[1]], [current_coord[0] - 1, current_coord[1]],
[current_coord[0], current_coord[1] + 1], [current_coord[0], current_coord[1] + 1],
[current_coord[0] + 1, current_coord[1]], [current_coord[0] + 1, current_coord[1]],
[current_coord[0], current_coord[1] - 1] [current_coord[0], current_coord[1] - 1]
] ]
var valid_coords = adjacent_token_coords.filter( var valid_coords = adjacent_token_coords.filter(
func(coord_pair): func(coord_pair):
return ( return (
coord_pair[0] >= 0 and coord_pair[0] >= 0 and
coord_pair[0] < rows and coord_pair[0] < rows and
coord_pair[1] >= 0 and coord_pair[1] >= 0 and
coord_pair[1] < cols coord_pair[1] < cols
) )
) )
for coord in valid_coords: for coord in valid_coords:
if (coord not in visited_nodes and if (coord not in visited_nodes and
grid[coord[0]][coord[1]].type == current_token.type and grid[coord[0]][coord[1]].type == current_token.type and
coord not in group_queue): coord not in group_queue):
group_queue.append(coord) group_queue.append(coord)
groups.append(new_group) groups.append(new_group)
debug_label.text = str(groups) debug_label.text = str(groups)
func get_group_of_token(token_coord) -> Array: func get_group_of_token(token_coord) -> Array:
for group in groups: for group in groups:
if token_coord in group: if token_coord in group:
return group return group
return [] return []

View File

@@ -2,6 +2,7 @@ extends Node2D
class_name Token class_name Token
signal token_clicked signal token_clicked
signal token_hovered(entered: bool)
enum token_type {TYPE_1, TYPE_2, TYPE_3, TYPE_4} enum token_type {TYPE_1, TYPE_2, TYPE_3, TYPE_4}
enum token_state {NONE, HIGHLIGHT} enum token_state {NONE, HIGHLIGHT}
@@ -17,7 +18,7 @@ func _ready():
color_polygon = $Color color_polygon = $Color
highlight_polygon = $highlight_indicator highlight_polygon = $highlight_indicator
debug_label = $"Debug Label" debug_label = $"Debug Label"
func set_type(type: token_type): func set_type(type: token_type):
self.type = type self.type = type
match self.type: match self.type:
@@ -29,7 +30,7 @@ func set_type(type: token_type):
color_polygon.color = Color.BLUE color_polygon.color = Color.BLUE
token_type.TYPE_4: token_type.TYPE_4:
color_polygon.color = Color.GOLD color_polygon.color = Color.GOLD
func set_debug_label(text: String): func set_debug_label(text: String):
debug_label.text = text debug_label.text = text
@@ -38,6 +39,18 @@ func _on_area_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int)
# emit event up to parent on click # emit event up to parent on click
if Input.is_action_just_pressed("select"): if Input.is_action_just_pressed("select"):
token_clicked.emit() token_clicked.emit()
func set_highlighted(highlight: bool): func set_highlighted(highlight: bool):
state = token_state.HIGHLIGHT if highlight else token_state.NONE state = token_state.HIGHLIGHT if highlight else token_state.NONE
match state:
token_state.HIGHLIGHT:
highlight_polygon.visible = true
token_state.NONE:
highlight_polygon.visible = false
func _on_area_2d_mouse_entered() -> void:
token_hovered.emit(true)
func _on_area_2d_mouse_exited() -> void:
token_hovered.emit(false)

View File

@@ -43,9 +43,12 @@ color = Color(0.84101, 0.000705212, 0.866408, 1)
polygon = PackedVector2Array(0, 0, 40, 0, 40, 40, 0, 40) polygon = PackedVector2Array(0, 0, 40, 0, 40, 40, 0, 40)
[node name="highlight_indicator" type="Polygon2D" parent="."] [node name="highlight_indicator" type="Polygon2D" parent="."]
visible = false
position = Vector2(52.8333, 76.6667) position = Vector2(52.8333, 76.6667)
scale = Vector2(0.427083, 0.319444) scale = Vector2(0.427083, 0.319444)
polygon = PackedVector2Array(-112, -96, -16, -96, -64, -168) polygon = PackedVector2Array(-112, -96, -16, -96, -64, -168)
uv = PackedVector2Array(-112, -96, -16, -96, -72, -160) uv = PackedVector2Array(-112, -96, -16, -96, -72, -160)
[connection signal="input_event" from="Area2D" to="." method="_on_area_2d_input_event"] [connection signal="input_event" from="Area2D" to="." method="_on_area_2d_input_event"]
[connection signal="mouse_entered" from="Area2D" to="." method="_on_area_2d_mouse_entered"]
[connection signal="mouse_exited" from="Area2D" to="." method="_on_area_2d_mouse_exited"]