Skip to content

Commit

Permalink
wip: towards splitting a whole column at once
Browse files Browse the repository at this point in the history
  • Loading branch information
russmatney committed Jun 22, 2024
1 parent a7d7046 commit 6f35c0d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
8 changes: 5 additions & 3 deletions addons/log/log.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###########################################################################

Expand Down Expand Up @@ -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 = {}

Expand Down
12 changes: 11 additions & 1 deletion src/BloxBucket.tscn
Original file line number Diff line number Diff line change
@@ -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")
53 changes: 37 additions & 16 deletions src/BloxGrid.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/BloxPiece.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 49 additions & 1 deletion test/blox_grid_test.gd
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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=[
Expand Down

0 comments on commit 6f35c0d

Please sign in to comment.