highlight on hover and click to remove groups
grid is updated after a click
This commit is contained in:
@@ -1,17 +1,8 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://bai2mcthexgm5"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c1k5pgejmn3bb" path="res://grid.gd" id="1_mn5vm"]
|
||||
[ext_resource type="PackedScene" uid="uid://bnfois00347c2" path="res://grid.tscn" id="1_mn5vm"]
|
||||
|
||||
[node name="GameWindow" type="Node2D"]
|
||||
|
||||
[node name="Grid" type="Node2D" parent="."]
|
||||
[node name="Grid" parent="." instance=ExtResource("1_mn5vm")]
|
||||
position = Vector2(393, 224)
|
||||
script = ExtResource("1_mn5vm")
|
||||
offset = 60.0
|
||||
|
||||
[node name="Debug Label" type="Label" parent="Grid"]
|
||||
offset_left = -372.0
|
||||
offset_top = -206.0
|
||||
offset_right = 729.0
|
||||
offset_bottom = -123.0
|
||||
autowrap_mode = 1
|
||||
|
||||
76
grid.gd
76
grid.gd
@@ -7,6 +7,13 @@ class_name Grid
|
||||
|
||||
@export var offset: float = 55.0
|
||||
|
||||
@onready
|
||||
var grid_area: Area2D = $BoundaryArea
|
||||
|
||||
@onready
|
||||
var grid_shape: CollisionShape2D = $BoundaryArea/BoundaryShape
|
||||
|
||||
|
||||
var grid: Array[Array]
|
||||
var groups: Array
|
||||
var hovered_group: Array
|
||||
@@ -18,6 +25,9 @@ var token = preload("res://token.tscn")
|
||||
func _ready():
|
||||
debug_label = $"Debug Label"
|
||||
|
||||
grid_area.position = Vector2(cols * offset / 2, rows * offset / 2)
|
||||
grid_shape.shape.size = Vector2(cols * offset, rows * offset)
|
||||
|
||||
for row in rows:
|
||||
grid.append([])
|
||||
for column in cols:
|
||||
@@ -25,35 +35,38 @@ func _ready():
|
||||
add_child(token_node)
|
||||
token_node.set_type(randi_range(0,3) as Token.token_type)
|
||||
token_node.set_debug_label(str(row) + "," + str(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)
|
||||
|
||||
redraw_grid()
|
||||
calculate_token_groups()
|
||||
|
||||
func _on_token_clicked(token_coord):
|
||||
var group = get_group_of_token(token_coord)
|
||||
func _process(_delta: float) -> void:
|
||||
clear_highlights()
|
||||
|
||||
# get mouse position for hover
|
||||
var mouse_position = self.get_local_mouse_position()
|
||||
if not pixel_within_grid(mouse_position):
|
||||
return
|
||||
|
||||
var coord_under_mouse = pixel_to_grid_coord(mouse_position)
|
||||
var token_under_mouse: Token = grid[coord_under_mouse[0]][coord_under_mouse[1]]
|
||||
|
||||
if token_under_mouse != null:
|
||||
var group = get_group_of_token(coord_under_mouse)
|
||||
if group.size() >= min_group_size:
|
||||
for coord in group:
|
||||
var current_token = grid[coord[0]][coord[1]] as Token
|
||||
current_token.queue_free()
|
||||
grid[coord[0]][coord[1]] = null # do I actually want a null value or should there be some other placeholder?
|
||||
|
||||
update_grid()
|
||||
|
||||
func _on_token_hovered(enter: bool, coord: Array):
|
||||
var group = get_group_of_token(coord)
|
||||
if enter:
|
||||
highlight_group(group, true)
|
||||
else:
|
||||
highlight_group(group, false)
|
||||
|
||||
# if click, determine token clicked
|
||||
func pixel_within_grid(coord: Vector2) -> bool:
|
||||
return (coord.x >= 0 and coord.x < cols * offset and
|
||||
coord.y >= 0 and coord.y < rows * offset)
|
||||
|
||||
func pixel_to_grid_coord(coord: Vector2) -> Array[int]:
|
||||
return [coord.y / offset, coord.x / offset]
|
||||
|
||||
|
||||
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
|
||||
@@ -62,6 +75,12 @@ func highlight_group(group: Array, enable: bool):
|
||||
|
||||
hovered_group = group
|
||||
|
||||
func clear_highlights():
|
||||
for row in grid:
|
||||
for token: Token in row:
|
||||
if token != null:
|
||||
token.set_highlighted(false)
|
||||
|
||||
func update_grid():
|
||||
# search for empty cells and drop tokens above them down
|
||||
for column in range(cols):
|
||||
@@ -82,6 +101,7 @@ func update_grid():
|
||||
# search for empty coluns and shift tokens horizontally to fill them
|
||||
|
||||
redraw_grid()
|
||||
calculate_token_groups()
|
||||
|
||||
func redraw_grid():
|
||||
for row in range(rows):
|
||||
@@ -92,7 +112,7 @@ func redraw_grid():
|
||||
|
||||
current_token.position = Vector2(offset*column, offset*row)
|
||||
|
||||
calculate_token_groups()
|
||||
|
||||
|
||||
func calculate_token_groups():
|
||||
groups = []
|
||||
@@ -146,3 +166,17 @@ func get_group_of_token(token_coord) -> Array:
|
||||
return group
|
||||
|
||||
return []
|
||||
|
||||
|
||||
func _on_boundary_area_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
|
||||
if event.is_action_pressed("select"):
|
||||
var position = self.get_local_mouse_position()
|
||||
var token_coord = pixel_to_grid_coord(position)
|
||||
var group = get_group_of_token(token_coord)
|
||||
if group.size() >= min_group_size:
|
||||
for coord in group:
|
||||
var current_token = grid[coord[0]][coord[1]] as Token
|
||||
current_token.queue_free()
|
||||
grid[coord[0]][coord[1]] = null # do I actually want a null value or should there be some other placeholder?
|
||||
|
||||
update_grid()
|
||||
|
||||
24
grid.tscn
Normal file
24
grid.tscn
Normal file
@@ -0,0 +1,24 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://bnfois00347c2"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c1k5pgejmn3bb" path="res://grid.gd" id="1_ebq2e"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ebq2e"]
|
||||
resource_local_to_scene = true
|
||||
|
||||
[node name="Grid" type="Node2D"]
|
||||
script = ExtResource("1_ebq2e")
|
||||
offset = 60.0
|
||||
|
||||
[node name="BoundaryArea" type="Area2D" parent="."]
|
||||
|
||||
[node name="BoundaryShape" type="CollisionShape2D" parent="BoundaryArea"]
|
||||
shape = SubResource("RectangleShape2D_ebq2e")
|
||||
|
||||
[node name="Debug Label" type="Label" parent="."]
|
||||
offset_left = -372.0
|
||||
offset_top = -206.0
|
||||
offset_right = 729.0
|
||||
offset_bottom = -123.0
|
||||
autowrap_mode = 1
|
||||
|
||||
[connection signal="input_event" from="BoundaryArea" to="." method="_on_boundary_area_input_event"]
|
||||
15
token.gd
15
token.gd
@@ -1,9 +1,6 @@
|
||||
extends Node2D
|
||||
class_name Token
|
||||
|
||||
signal token_clicked
|
||||
signal token_hovered(entered: bool)
|
||||
|
||||
enum token_type {TYPE_1, TYPE_2, TYPE_3, TYPE_4}
|
||||
enum token_state {NONE, HIGHLIGHT}
|
||||
|
||||
@@ -35,11 +32,6 @@ func set_debug_label(text: String):
|
||||
debug_label.text = text
|
||||
|
||||
|
||||
func _on_area_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
|
||||
# emit event up to parent on click
|
||||
if Input.is_action_just_pressed("select"):
|
||||
token_clicked.emit()
|
||||
|
||||
func set_highlighted(highlight: bool):
|
||||
state = token_state.HIGHLIGHT if highlight else token_state.NONE
|
||||
match state:
|
||||
@@ -47,10 +39,3 @@ func set_highlighted(highlight: bool):
|
||||
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)
|
||||
|
||||
18
token.tscn
18
token.tscn
@@ -7,12 +7,12 @@
|
||||
outline_size = 2
|
||||
outline_color = Color(0, 0, 0, 1)
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_0ucay"]
|
||||
size = Vector2(51, 51)
|
||||
|
||||
[sub_resource type="CanvasItemMaterial" id="CanvasItemMaterial_bd72f"]
|
||||
blend_mode = 1
|
||||
|
||||
[sub_resource type="CanvasItemMaterial" id="CanvasItemMaterial_0ucay"]
|
||||
blend_mode = 2
|
||||
|
||||
[node name="Token" type="Node2D"]
|
||||
script = ExtResource("1_0ucay")
|
||||
|
||||
@@ -30,12 +30,6 @@ scale = Vector2(0.4, 0.4)
|
||||
texture = ExtResource("1_bd72f")
|
||||
centered = false
|
||||
|
||||
[node name="Area2D" type="Area2D" parent="."]
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||
position = Vector2(26, 25.5)
|
||||
shape = SubResource("RectangleShape2D_0ucay")
|
||||
|
||||
[node name="Color" type="Polygon2D" parent="."]
|
||||
material = SubResource("CanvasItemMaterial_bd72f")
|
||||
position = Vector2(6, 6)
|
||||
@@ -43,12 +37,8 @@ color = Color(0.84101, 0.000705212, 0.866408, 1)
|
||||
polygon = PackedVector2Array(0, 0, 40, 0, 40, 40, 0, 40)
|
||||
|
||||
[node name="highlight_indicator" type="Polygon2D" parent="."]
|
||||
visible = false
|
||||
material = SubResource("CanvasItemMaterial_0ucay")
|
||||
position = Vector2(52.8333, 76.6667)
|
||||
scale = Vector2(0.427083, 0.319444)
|
||||
polygon = PackedVector2Array(-112, -96, -16, -96, -64, -168)
|
||||
uv = PackedVector2Array(-112, -96, -16, -96, -72, -160)
|
||||
|
||||
[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"]
|
||||
|
||||
Reference in New Issue
Block a user