Skip to content

Commit d620bec

Browse files
committed
DONE scoring works now, each gem shows correct points
1 parent b54141a commit d620bec

File tree

3 files changed

+66
-51
lines changed

3 files changed

+66
-51
lines changed

game_board/GameBoard.gd

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ signal props_updated_gemsdict(gems_dict:Dictionary)
1111
@onready var hbox_container:HBoxContainer = $HBoxContainer
1212
#VARS
1313
const GEM_COLOR_NAMES = [Enums.GemColor.WHITE, Enums.GemColor.RED, Enums.GemColor.YELLOW, Enums.GemColor.GREEN, Enums.GemColor.PURPLE, Enums.GemColor.BROWN]
14+
const GEM_POINTS:int = 25
1415
var selected_cell_1:GemCell = null
1516
var selected_cell_2:GemCell = null
1617
var undo_cell_1:GemCell = null
@@ -186,9 +187,18 @@ func calculate_score_for_matches(matches: Array) -> int:
186187
var match_length = match["count"]
187188
# Define scoring logic, e.g., exponential growth for larger matches
188189
var match_score = match_length * match_length # Example: score grows quadratically with match length
189-
score += match_score * 100
190+
score += match_score * GEM_POINTS
190191
return score
191192

193+
func calculate_scores_for_each_match(matches: Array) -> Dictionary:
194+
var scores = {}
195+
for match in matches:
196+
var count = match["count"]
197+
var score = count * GEM_POINTS
198+
for cell in match["cells"]:
199+
scores[cell] = score
200+
return scores
201+
192202
# STEP 1: Handle input (=capture first & second selection), swap gems
193203
# @desc: calls `swap_gem_cells()` when 2 cells selected and are adjacent
194204

@@ -340,18 +350,21 @@ func process_game_round():
340350
undo_cell_1 = null
341351
undo_cell_2 = null
342352
# B: explode matched gems
343-
explode_refill_gems(gem_cells)
353+
var match_scores = calculate_scores_for_each_match(matches)
354+
explode_refill_gems(matches, match_scores)
344355

345-
func explode_refill_gems(gem_cells: Array):
346-
Enums.debug_print("[explode_refill_gems]: !!!!!!!!!!!=======================================", Enums.DEBUG_LEVEL.INFO)
347-
Enums.debug_print("[explode_refill_gems]: *EXPLODING* gem_cell count: "+str(gem_cells.size()), Enums.DEBUG_LEVEL.INFO)
348-
Enums.debug_print("[explode_refill_gems]: !!!!!!!!!!!=======================================", Enums.DEBUG_LEVEL.INFO)
356+
func explode_refill_gems(matches: Array, match_scores: Dictionary):
357+
Enums.debug_print("[explode_refill_gems]: !!!!!!!!!!!=====================================", Enums.DEBUG_LEVEL.INFO)
358+
Enums.debug_print("[explode_refill_gems]: *EXPLODING* gem_cell count: "+str(matches.size()), Enums.DEBUG_LEVEL.INFO)
359+
Enums.debug_print("[explode_refill_gems]: !!!!!!!!!!!=====================================", Enums.DEBUG_LEVEL.INFO)
349360
if Enums.current_debug_level == Enums.DEBUG_LEVEL.DEBUG:
350-
debug_print_ascii_table(gem_cells)
361+
debug_print_ascii_table(extract_gem_cells_from_matches(matches))
351362

352363
# A: explode selected
353-
for gem_cell in gem_cells:
354-
gem_cell.explode_gem(gem_cell.gem_color)
364+
for match in matches:
365+
for gem_cell in match["cells"]:
366+
var score = match_scores[gem_cell]
367+
gem_cell.explode_gem(gem_cell.gem_color, score)
355368

356369
# TODO: FIXME: gem counts need to update faster (they currently update after the animation completes)!!
357370
# seemingly, this would work fine located here but its not - the UI update requires a frame update i guess?
@@ -362,14 +375,15 @@ func explode_refill_gems(gem_cells: Array):
362375

363376
# C: Dictionary to track columns and the number of gems to add in each column
364377
var columns_to_refill = {}
365-
for gem_cell in gem_cells:
366-
var column_index = gem_cell.get_parent().get_index()
367-
var row_index = gem_cell.get_index()
368-
if column_index in columns_to_refill:
369-
columns_to_refill[column_index]["count"] += 1
370-
columns_to_refill[column_index]["highest"] = max(columns_to_refill[column_index]["highest"], row_index)
371-
else:
372-
columns_to_refill[column_index] = {"highest": row_index, "count": 1}
378+
for match in matches:
379+
for gem_cell in match["cells"]:
380+
var column_index = gem_cell.get_parent().get_index()
381+
var row_index = gem_cell.get_index()
382+
if column_index in columns_to_refill:
383+
columns_to_refill[column_index]["count"] += 1
384+
columns_to_refill[column_index]["highest"] = max(columns_to_refill[column_index]["highest"], row_index)
385+
else:
386+
columns_to_refill[column_index] = {"highest": row_index, "count": 1}
373387

374388
# C: Process each column that needs refilling
375389
for column_index in columns_to_refill.keys():

game_board/GemCell.gd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ func initialize(colorIn: Enums.GemColor):
3131
# C:
3232
#panel_hover.visible = false
3333

34-
func explode_gem(colorIn: Enums.GemColor):
34+
func explode_gem(colorIn: Enums.GemColor, pointsIn:int):
3535
# A: set color immediately so code in `GameBoard.gd` canstart checking this cell's color
3636
gem_color = colorIn
3737
# B:
3838
play_selected_anim(false)
39-
play_anim_explode()
39+
play_anim_explode(pointsIn)
4040

4141
func replace_gem(colorIn: Enums.GemColor, rows_to_drop: int = 1):
4242
#print("[replace_gem]: colorIn=", colorIn, " rows_to_drop=", rows_to_drop)
@@ -87,7 +87,7 @@ func play_selected_anim(selected:bool):
8787
label_points.visible = false
8888

8989
# @desc: both AnimPlayer & AnimExplode are 1-sec
90-
func play_anim_explode():
90+
func play_anim_explode(points:int):
9191
# A: sound effect
9292
audio_gem_explode.play()
9393

@@ -97,6 +97,7 @@ func play_anim_explode():
9797
sprite.visible = false
9898

9999
# C: show points
100+
label_points.text = "+"+str(points)
100101
anim_player_fx.play("new_points")
101102

102103
# D: explode animation (exploding sprite)

game_board/gem_cell.tscn

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,83 +28,83 @@ tracks/0/keys = {
2828
"values": [Vector2(0.5, 0.5), Vector2(0, 0)]
2929
}
3030

31-
[sub_resource type="Animation" id="Animation_xkhqk"]
32-
resource_name = "selected"
33-
loop_mode = 2
31+
[sub_resource type="Animation" id="Animation_cqy0g"]
32+
resource_name = "new_points"
3433
tracks/0/type = "value"
3534
tracks/0/imported = false
3635
tracks/0/enabled = true
37-
tracks/0/path = NodePath("Sprite2D:scale")
38-
tracks/0/interp = 2
36+
tracks/0/path = NodePath("LabelPoints:position")
37+
tracks/0/interp = 1
3938
tracks/0/loop_wrap = true
4039
tracks/0/keys = {
41-
"times": PackedFloat32Array(0, 0.5, 1),
42-
"transitions": PackedFloat32Array(1, 1, 1),
40+
"times": PackedFloat32Array(0, 1),
41+
"transitions": PackedFloat32Array(1, 1),
4342
"update": 0,
44-
"values": [Vector2(0.5, 0.5), Vector2(0.4, 0.4), Vector2(0.55, 0.55)]
43+
"values": [Vector2(0, 0), Vector2(0, -99)]
4544
}
4645
tracks/1/type = "value"
4746
tracks/1/imported = false
48-
tracks/1/enabled = false
49-
tracks/1/path = NodePath("Sprite2D:flip_h")
47+
tracks/1/enabled = true
48+
tracks/1/path = NodePath("LabelPoints:visible")
5049
tracks/1/interp = 1
5150
tracks/1/loop_wrap = true
5251
tracks/1/keys = {
53-
"times": PackedFloat32Array(0, 0.5, 1),
54-
"transitions": PackedFloat32Array(1, 1, 1),
52+
"times": PackedFloat32Array(0, 1),
53+
"transitions": PackedFloat32Array(1, 1),
5554
"update": 1,
56-
"values": [false, true, false]
55+
"values": [true, false]
5756
}
5857
tracks/2/type = "value"
5958
tracks/2/imported = false
60-
tracks/2/enabled = false
61-
tracks/2/path = NodePath("Sprite2D:rotation")
59+
tracks/2/enabled = true
60+
tracks/2/path = NodePath("LabelPoints:modulate")
6261
tracks/2/interp = 1
6362
tracks/2/loop_wrap = true
6463
tracks/2/keys = {
6564
"times": PackedFloat32Array(0, 1),
6665
"transitions": PackedFloat32Array(1, 1),
6766
"update": 0,
68-
"values": [-0.261799, 0.261799]
67+
"values": [Color(1, 1, 1, 1), Color(1, 1, 1, 0)]
6968
}
7069

71-
[sub_resource type="Animation" id="Animation_cqy0g"]
72-
resource_name = "new_points"
70+
[sub_resource type="Animation" id="Animation_xkhqk"]
71+
resource_name = "selected"
72+
loop_mode = 2
7373
tracks/0/type = "value"
7474
tracks/0/imported = false
7575
tracks/0/enabled = true
76-
tracks/0/path = NodePath("LabelPoints:position")
77-
tracks/0/interp = 1
76+
tracks/0/path = NodePath("Sprite2D:scale")
77+
tracks/0/interp = 2
7878
tracks/0/loop_wrap = true
7979
tracks/0/keys = {
80-
"times": PackedFloat32Array(0, 1),
81-
"transitions": PackedFloat32Array(1, 1),
80+
"times": PackedFloat32Array(0, 0.5, 1),
81+
"transitions": PackedFloat32Array(1, 1, 1),
8282
"update": 0,
83-
"values": [Vector2(0, 0), Vector2(0, -99)]
83+
"values": [Vector2(0.5, 0.5), Vector2(0.4, 0.4), Vector2(0.55, 0.55)]
8484
}
8585
tracks/1/type = "value"
8686
tracks/1/imported = false
87-
tracks/1/enabled = true
88-
tracks/1/path = NodePath("LabelPoints:visible")
87+
tracks/1/enabled = false
88+
tracks/1/path = NodePath("Sprite2D:flip_h")
8989
tracks/1/interp = 1
9090
tracks/1/loop_wrap = true
9191
tracks/1/keys = {
92-
"times": PackedFloat32Array(0, 1),
93-
"transitions": PackedFloat32Array(1, 1),
92+
"times": PackedFloat32Array(0, 0.5, 1),
93+
"transitions": PackedFloat32Array(1, 1, 1),
9494
"update": 1,
95-
"values": [true, false]
95+
"values": [false, true, false]
9696
}
9797
tracks/2/type = "value"
9898
tracks/2/imported = false
99-
tracks/2/enabled = true
100-
tracks/2/path = NodePath("LabelPoints:modulate")
99+
tracks/2/enabled = false
100+
tracks/2/path = NodePath("Sprite2D:rotation")
101101
tracks/2/interp = 1
102102
tracks/2/loop_wrap = true
103103
tracks/2/keys = {
104104
"times": PackedFloat32Array(0, 1),
105105
"transitions": PackedFloat32Array(1, 1),
106106
"update": 0,
107-
"values": [Color(1, 1, 1, 1), Color(1, 1, 1, 0)]
107+
"values": [-0.261799, 0.261799]
108108
}
109109

110110
[sub_resource type="AnimationLibrary" id="AnimationLibrary_u2ci8"]

0 commit comments

Comments
 (0)