Skip to content

Commit e42324c

Browse files
committed
highlight of avail moves working
1 parent 8b88aa7 commit e42324c

File tree

4 files changed

+184
-20
lines changed

4 files changed

+184
-20
lines changed

game_boards/all_common/cmn_gem_cell.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class_name CommonGemCell
1111
@onready var audio_gem_explode:AudioStreamPlayer = $AudioGemExplode
1212
@onready var audio_gem_move:AudioStreamPlayer = $AudioGemMove
1313
@onready var label_points:Label = $LabelPoints
14+
@onready var highlight_rect:ColorRect = $Highlight
1415
# PROPS
1516
const SPRITE_SCALE:Vector2 = Vector2(0.5, 0.5)
1617
const DROP_OFFSET:int = 128 # (the sprite is centered in the 128x128 container, and uses a 64,64 position)
@@ -106,6 +107,13 @@ func update_texture():
106107

107108
# =========================================================
108109

110+
func highlight():
111+
anim_player_fx.play("highlight")
112+
113+
func unhighlight():
114+
anim_player_fx.stop()
115+
highlight_rect.color = Color('ffffff00')
116+
109117
func play_audio_gem_move():
110118
audio_gem_move.play()
111119

game_boards/all_common/cmn_gem_cell.tscn

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=90 format=3 uid="uid://8rbpehubrrps"]
1+
[gd_scene load_steps=91 format=3 uid="uid://8rbpehubrrps"]
22

33
[ext_resource type="Script" path="res://game_boards/all_common/cmn_gem_cell.gd" id="1_taq7m"]
44
[ext_resource type="Texture2D" uid="uid://duau66qew6757" path="res://assets/gems/food/characters_0001.png" id="2_nap56"]
@@ -33,6 +33,36 @@ tracks/0/keys = {
3333
"values": [Vector2(0.5, 0.5), Vector2(0, 0)]
3434
}
3535

36+
[sub_resource type="Animation" id="Animation_xv0ay"]
37+
resource_name = "highlight"
38+
length = 3.6
39+
loop_mode = 1
40+
step = 0.2
41+
tracks/0/type = "value"
42+
tracks/0/imported = false
43+
tracks/0/enabled = false
44+
tracks/0/path = NodePath("Highlight:modulate")
45+
tracks/0/interp = 1
46+
tracks/0/loop_wrap = true
47+
tracks/0/keys = {
48+
"times": PackedFloat32Array(0, 0.4, 0.6, 3.6),
49+
"transitions": PackedFloat32Array(1, 1, 1, 1),
50+
"update": 0,
51+
"values": [Color(1, 1, 1, 0), Color(1, 1, 1, 1), Color(1, 1, 1, 0), Color(1, 1, 1, 0)]
52+
}
53+
tracks/1/type = "value"
54+
tracks/1/imported = false
55+
tracks/1/enabled = true
56+
tracks/1/path = NodePath("Highlight:color")
57+
tracks/1/interp = 1
58+
tracks/1/loop_wrap = true
59+
tracks/1/keys = {
60+
"times": PackedFloat32Array(0, 0.2, 0.4, 0.6, 0.8, 3.6),
61+
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1),
62+
"update": 0,
63+
"values": [Color(1, 1, 1, 0), Color(1, 1, 1, 0.533333), Color(1, 1, 1, 0), Color(1, 1, 1, 0.533333), Color(1, 1, 1, 0), Color(1, 1, 1, 0)]
64+
}
65+
3666
[sub_resource type="Animation" id="Animation_cqy0g"]
3767
resource_name = "new_points"
3868
tracks/0/type = "value"
@@ -115,6 +145,7 @@ tracks/2/keys = {
115145
[sub_resource type="AnimationLibrary" id="AnimationLibrary_u2ci8"]
116146
_data = {
117147
"explode": SubResource("Animation_3mn62"),
148+
"highlight": SubResource("Animation_xv0ay"),
118149
"new_points": SubResource("Animation_cqy0g"),
119150
"selected": SubResource("Animation_xkhqk")
120151
}
@@ -637,6 +668,15 @@ grow_horizontal = 2
637668
grow_vertical = 2
638669
theme_override_styles/panel = SubResource("StyleBoxFlat_lpbyf")
639670

671+
[node name="Highlight" type="ColorRect" parent="."]
672+
layout_mode = 1
673+
anchors_preset = 15
674+
anchor_right = 1.0
675+
anchor_bottom = 1.0
676+
grow_horizontal = 2
677+
grow_vertical = 2
678+
color = Color(1, 1, 1, 0)
679+
640680
[node name="Sprite2D" type="Sprite2D" parent="."]
641681
position = Vector2(64, 64)
642682
scale = Vector2(0.5, 0.5)
@@ -728,6 +768,7 @@ anchor_right = 1.0
728768
anchor_bottom = 1.0
729769
grow_horizontal = 2
730770
grow_vertical = 2
771+
mouse_filter = 2
731772
theme_override_styles/panel = SubResource("StyleBoxFlat_lpbyf")
732773

733774
[node name="GridContainer" type="GridContainer" parent="DebugUIPanel"]

game_boards/all_common/common.gd

Lines changed: 130 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
extends Node
22
class_name Common
3-
3+
#
44
const GEM_COLOR_NAMES = [Enums.GemColor.RED, Enums.GemColor.ORG, Enums.GemColor.YLW, Enums.GemColor.GRN, Enums.GemColor.BLU, Enums.GemColor.PRP]
55
const GEM_POINTS:int = 25
6+
#
7+
var highlight_gem1: CommonGemCell = null
8+
var highlight_gem2: CommonGemCell = null
69

710
# =========================================================
811

@@ -37,6 +40,112 @@ func fill_hbox(hbox:HBoxContainer, gem_dict:Enums.GemDict, on_cell_click):
3740
#control_node.connect("drag_start", self._on_cell_click) # TODO:
3841
#control_node.connect("drag_ended", self._on_cell_click) # TODO:
3942

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+
40149
# UTILS
41150

42151
func get_all_matches(hbox:HBoxContainer) -> Array:
@@ -113,24 +222,6 @@ func extract_gem_cells_from_matches(matches: Array) -> Array:
113222
all_gem_cells += match["cells"] # Append all cells in this match to the master list
114223
return all_gem_cells
115224

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-
134225
func find_gem_indices(gem_cell:CommonGemCell) -> Dictionary:
135226
var parent_vbox = gem_cell.get_parent() # Assuming direct parent is a VBoxContainer
136227
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:
173264

174265
# =========================================================
175266

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+
176287
func new_game_explode_replace(hbox:HBoxContainer, colors:Array, delay:float):
177288
# A:
178289
for vbox in hbox.get_children():

game_boards/board_space/board_space.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ func process_game_round():
212212
Enums.debug_print("[check_board_explode_matches]: No more matches. Board stable.", Enums.DEBUG_LEVEL.INFO)
213213
# A:
214214
# B: TODO: check for "NO MORE MOVES"
215+
var brent = CmnFunc.check_for_possible_moves(hbox_container)
216+
print("CHECK FOR MOVES = ", str(brent))
217+
# C: TODO: highlight available moves after short delay
218+
CmnFunc.highlight_first_swap(hbox_container)
215219
# C: Reset undo cells or perform other cleanup here.
216220
if undo_cell_1 and undo_cell_2:
217221
swap_gem_cells(undo_cell_2, undo_cell_1)

0 commit comments

Comments
 (0)