From 6f35c0df0e112bb4c35e1bd1bcad997e674f49d6 Mon Sep 17 00:00:00 2001 From: Russell Matney Date: Sat, 22 Jun 2024 17:45:28 -0400 Subject: [PATCH] wip: towards splitting a whole column at once --- addons/log/log.gd | 8 ++++--- src/BloxBucket.tscn | 12 +++++++++- src/BloxGrid.gd | 53 +++++++++++++++++++++++++++++------------- src/BloxPiece.gd | 1 + test/blox_grid_test.gd | 50 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 103 insertions(+), 21 deletions(-) diff --git a/addons/log/log.gd b/addons/log/log.gd index e485b04..62ee1c4 100644 --- a/addons/log/log.gd +++ b/addons/log/log.gd @@ -108,10 +108,12 @@ static func enable_colors(): ## DEPRECATED static func set_color_scheme(theme): - set_color_theme(theme) + Log.set_color_theme(theme) static func set_color_theme(theme): + Log.pr("setting color theme", KEY_COLOR_THEME, theme) Log.config[KEY_COLOR_THEME] = theme + Log.pr("set!", Log.config[KEY_COLOR_THEME]) ## colors ########################################################################### @@ -250,14 +252,14 @@ static var COLORS_PRETTY_V1 = { ## Use the terminal safe color scheme, which should handle colors in most tty-like environments. static func set_colors_termsafe(): - set_color_theme(LOG_THEME_TERMSAFE) + Log.set_color_theme(LOG_THEME_TERMSAFE) ## Use prettier colors - looks nice in most dark godot themes. ## ## [br][br] ## Hopefully we'll support more themes (including light themes) soon! static func set_colors_pretty(): - set_color_theme(LOG_THEME_PRETTY_V1) + Log.set_color_theme(LOG_THEME_PRETTY_V1) static var theme_overwrites = {} diff --git a/src/BloxBucket.tscn b/src/BloxBucket.tscn index 835057c..5ba2e29 100644 --- a/src/BloxBucket.tscn +++ b/src/BloxBucket.tscn @@ -1,6 +1,16 @@ -[gd_scene load_steps=2 format=3 uid="uid://dwj7dbdpb053r"] +[gd_scene load_steps=4 format=3 uid="uid://dwj7dbdpb053r"] [ext_resource type="Script" path="res://src/BloxBucket.gd" id="1_sogpg"] +[ext_resource type="Script" path="res://src/GridRules.gd" id="2_hmt8t"] + +[sub_resource type="Resource" id="Resource_fyxcv"] +script = ExtResource("2_hmt8t") +puyo_split = true +puyo_group_clear = true +puyo_group_size = 4 +tetris_row_clear = true +step_direction = Vector2i(0, 1) [node name="BloxBucket" type="Node2D"] script = ExtResource("1_sogpg") +grid_rules = SubResource("Resource_fyxcv") diff --git a/src/BloxGrid.gd b/src/BloxGrid.gd index 9f48636..16bbea2 100644 --- a/src/BloxGrid.gd +++ b/src/BloxGrid.gd @@ -245,31 +245,53 @@ func split_piece_coord(piece: BloxPiece, grid_coord: Vector2i) -> void: var new_p = BloxPiece.new({grid_cells=[cell]}) add_piece(new_p) +func coords_to_edge(coord: Vector2i, dir: Vector2i) -> Array[Vector2i]: + var coords: Array[Vector2i] = [] + match dir: + Vector2i.DOWN: + for y in range(coord.y, height): + coords.append(Vector2i(coord.x, y)) + Vector2i.UP: + for y in range(coord.y, 0): + coords.append(Vector2i(coord.x, y)) + Vector2i.LEFT: + for x in range(coord.x, 0): + coords.append(Vector2i(x, coord.y)) + Vector2i.RIGHT: + for x in range(coord.x, width): + coords.append(Vector2i(x, coord.y)) + return coords + + # splits pieces apart based on room-to-fall beneath cells func apply_split_puyo(dir=Vector2i.DOWN) -> bool: var crd_to_piece = coords_to_piece_dict() var to_split = [] - var bottom_up = range(height) - bottom_up.reverse() - bottom_up.pop_front() # skip the whole bottom row! - for y in bottom_up: + + for y in range(height): for x in range(width): var crd = Vector2i(x, y) - var p = crd_to_piece.get(crd) - if not p: - continue # no piece at coord, nothing to do - var p_below = crd_to_piece.get(crd + dir) - if p_below: - continue # don't split if there's nowhere to fall - - # maybe don't split unless some other piece can't fall? or just split everything in puyo mode? - - if p.cell_count() > 1: # only split if more than one cell - to_split.append(crd) + var crds_to_split = [] + var should_split = false + for crd_below in coords_to_edge(crd, dir): + var p_below = crd_to_piece.get(crd_below) + if p_below and p_below.cell_count() > 1: # only split if more than one cell + crds_to_split.append(crd) + elif not p_below: + # no cell, mark split and stop collecting + should_split = true + break + + if should_split: + to_split.append_array(crds_to_split) for crd in to_split: var p = crd_to_piece.get(crd) + if not p: + Log.warn("Missing p in apply_split_puyo", crd) + continue + Log.pr("Splitting piece in apply_split_puyo", p) split_piece_coord(p, crd) var did_split = not to_split.is_empty() @@ -374,7 +396,6 @@ func step(opts={}) -> bool: return true # puyo piece split - # TODO split more cells here, not just those immediately above a gap if rules.puyo_split and apply_split_puyo(rules.step_direction): # TODO include pieces on_pieces_split.emit() diff --git a/src/BloxPiece.gd b/src/BloxPiece.gd index fed4558..c19ab8f 100644 --- a/src/BloxPiece.gd +++ b/src/BloxPiece.gd @@ -114,6 +114,7 @@ func get_grid_cells() -> Array[BloxCell]: func grid_coords() -> Array[Vector2i]: var ret: Array[Vector2i] = [] + Log.pr("getting cells for piece", grid_cells) for c in grid_cells: ret.append(c.coord) return ret diff --git a/test/blox_grid_test.gd b/test/blox_grid_test.gd index 631577b..8d32bde 100644 --- a/test/blox_grid_test.gd +++ b/test/blox_grid_test.gd @@ -1,5 +1,10 @@ extends GdUnitTestSuite +func before(): + Log.set_colors_termsafe() + Log.pr("ran before! to set colors") + Log.pr(Log.config) + ## grid coords ############################################ func test_all_coords(): @@ -225,7 +230,7 @@ func test_rotate_piece_maintains_objects(): ## puyo split/fall ################################################## -func test_puyo_piece_split(): +func test_puyo_split(): var grid = BloxGrid.new({width=2, height=2}) var p = BloxPiece.new({cells=[ Vector2i(), Vector2i(1, 0), @@ -261,6 +266,49 @@ func test_puyo_piece_split(): Vector2i(0, 1), Vector2i(1, 1), ]) +func test_puyo_split_splits_all_cells(): + var grid = BloxGrid.new({width=2, height=3}) + var p2 = BloxPiece.new({cells=[ + Vector2i(), Vector2i(1, 0), + ]}) + var p1 = BloxPiece.new({cells=[ + Vector2i(0, 1), Vector2i(1, 1), + Vector2i(0, 2), + ]}) + grid.add_piece(p1) + grid.add_piece(p2) + + var crds = grid.piece_coords() + assert_int(len(grid.pieces)).is_equal(2) + assert_int(len(crds)).is_equal(5) + assert_array(crds).contains([ + Vector2i(), Vector2i(1, 0), + Vector2i(0, 1), Vector2i(1, 1), + Vector2i(0, 2), + ]) + + grid.apply_split_puyo() + + crds = grid.piece_coords() + assert_int(len(grid.pieces)).is_equal(4) + assert_int(len(crds)).is_equal(5) + assert_array(crds).contains([ + Vector2i(), Vector2i(1, 0), + Vector2i(0, 1), Vector2i(1, 1), + Vector2i(0, 2), + ]) + + grid.apply_step_tetris() + + crds = grid.piece_coords() + assert_int(len(grid.pieces)).is_equal(4) + assert_int(len(crds)).is_equal(5) + assert_array(crds).contains([ + Vector2i(), + Vector2i(0, 1), Vector2i(1, 1), + Vector2i(0, 2), Vector2i(1, 2), + ]) + func test_puyo_split_maintains_ids(): var grid = BloxGrid.new({width=2, height=2}) var p = BloxPiece.new({cells=[