Skip to content

Commit

Permalink
typed variables on Match3Preloader and renamed PluginUtilities to Mat…
Browse files Browse the repository at this point in the history
…ch3BoardPluginUtilities
  • Loading branch information
ninetailsrabbit committed Oct 21, 2024
1 parent c1f221e commit 47a0d16
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 163 deletions.
2 changes: 1 addition & 1 deletion addons/ninetailsrabbit.match3_board/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="Match3-Board"
description="This lightweight library provides the core logic and functionality you need to build engaging match-3 games. Focus on game design and mechanics while leaving the complex logic to this library"
author="Ninetailsrabbit"
version="1.0.0"
version="1.0.1"
script="ninetailsrabbit.match3_board.gd"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class_name MyPluginSettings extends RefCounted
const PluginPrefixName: String = "ninetailsrabbit.match3_board" ## The folder name
const GitRepositoryName: String = "match3-board"

static var PluginName: String = "MyPlugin"
static var PluginName: String = "Match3Board"
static var PluginProjectName: String = ProjectSettings.get_setting("application/config/name")
static var PluginBasePath: String = "res://addons/%s" % PluginPrefixName
static var PluginLocalConfigFilePath = "%s/plugin.cfg" % PluginBasePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func available_neighbours(include_diagonals: bool = false) -> Array[GridCellUI]:
var neighbours: Array[GridCellUI] = []

if include_diagonals:
neighbours.assign(PluginUtilities.remove_falsy_values([
neighbours.assign(Match3BoardPluginUtilities.remove_falsy_values([
neighbour_up,
neighbour_bottom,
neighbour_right,
Expand All @@ -241,7 +241,7 @@ func available_neighbours(include_diagonals: bool = false) -> Array[GridCellUI]:
]))
else:

neighbours.assign(PluginUtilities.remove_falsy_values([
neighbours.assign(Match3BoardPluginUtilities.remove_falsy_values([
neighbour_up,
neighbour_bottom,
neighbour_right,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func contain_special_piece() -> bool:
func pieces() -> Array[PieceUI]:
var current_pieces: Array[PieceUI] = []

current_pieces.assign(PluginUtilities.remove_falsy_values(cells.map(func(grid_cell: GridCellUI): return grid_cell.current_piece)))
current_pieces.assign(Match3BoardPluginUtilities.remove_falsy_values(cells.map(func(grid_cell: GridCellUI): return grid_cell.current_piece)))

return current_pieces

Expand All @@ -65,7 +65,7 @@ func all_pieces_are_of_type(type: PieceDefinitionResource.PieceType) -> bool:

#region Cell position in sequence
func middle_cell() -> GridCellUI:
return PluginUtilities.middle_element(cells)
return Match3BoardPluginUtilities.middle_element(cells)


func top_edge_cell():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ enum BoardFillModes {
#endregion

#region Textures
const BlueGem = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_pieces/blue_gem.png")
const GreenGem = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_pieces/green_gem.png")
const PurpleGem = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_pieces/purple_gem.png")
const YellowGem = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_pieces/yellow_gem.png")
const BlueGem: CompressedTexture2D = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_pieces/blue_gem.png")
const GreenGem: CompressedTexture2D = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_pieces/green_gem.png")
const PurpleGem: CompressedTexture2D = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_pieces/purple_gem.png")
const YellowGem: CompressedTexture2D = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_pieces/yellow_gem.png")
#endregion

#region Cell textures
const EvenCellTexture = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_cells/even.png")
const HighlightedTexture = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_cells/highlighted.png")
const OddCellTexture = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_cells/odd.png")
const EvenCellTexture: CompressedTexture2D = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_cells/even.png")
const HighlightedTexture: CompressedTexture2D = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_cells/highlighted.png")
const OddCellTexture: CompressedTexture2D = preload("res://addons/ninetailsrabbit.match3_board/src/debug_ui/preview_cells/odd.png")
#endregion
#region Pieces
const CrossPieceScene = preload("res://addons/ninetailsrabbit.match3_board/src/components/pieces/swap_mode/cross_piece.tscn")
const LineConnectorPieceScene = preload("res://addons/ninetailsrabbit.match3_board/src/components/pieces/swap_mode/line_connector_piece.tscn")
const SwapPieceScene = preload("res://addons/ninetailsrabbit.match3_board/src/components/pieces/swap_mode/swap_piece.tscn")
const CrossPieceScene: PackedScene = preload("res://addons/ninetailsrabbit.match3_board/src/components/pieces/swap_mode/cross_piece.tscn")
const LineConnectorPieceScene: PackedScene = preload("res://addons/ninetailsrabbit.match3_board/src/components/pieces/swap_mode/line_connector_piece.tscn")
const SwapPieceScene: PackedScene = preload("res://addons/ninetailsrabbit.match3_board/src/components/pieces/swap_mode/swap_piece.tscn")
#endregion
34 changes: 17 additions & 17 deletions addons/ninetailsrabbit.match3_board/src/match3_board.gd
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func prepare_board():

grid_cells[column].append(grid_cell)

grid_cells_flattened.append_array(PluginUtilities.flatten(grid_cells))
grid_cells_flattened.append_array(Match3BoardPluginUtilities.flatten(grid_cells))

add_pieces(available_pieces)

Expand Down Expand Up @@ -267,7 +267,7 @@ func get_cell_or_null(column: int, row: int):

func cross_cells_from(origin_cell: GridCellUI) -> Array[GridCellUI]:
var cross_cells: Array[GridCellUI] = []
cross_cells.assign(PluginUtilities.remove_duplicates(
cross_cells.assign(Match3BoardPluginUtilities.remove_duplicates(
grid_cells_from_row(origin_cell.row) + grid_cells_from_column(origin_cell.column))
)

Expand All @@ -278,7 +278,7 @@ func cross_diagonal_cells_from(origin_cell: GridCellUI) -> Array[GridCellUI]:
var distance: int = grid_width + grid_height
var cross_diagonal_cells: Array[GridCellUI] = []

cross_diagonal_cells.assign(PluginUtilities.remove_falsy_values(PluginUtilities.remove_duplicates(
cross_diagonal_cells.assign(Match3BoardPluginUtilities.remove_falsy_values(Match3BoardPluginUtilities.remove_duplicates(
diagonal_top_left_cells_from(origin_cell, distance)\
+ diagonal_top_right_cells_from(origin_cell, distance)\
+ diagonal_bottom_left_cells_from(origin_cell, distance)\
Expand Down Expand Up @@ -360,7 +360,7 @@ func grid_cell_from_piece(piece: PieceUI):
func grid_cells_from_row(row: int) -> Array[GridCellUI]:
var cells: Array[GridCellUI] = []

if grid_cells.size() > 0 and PluginUtilities.value_is_between(row, 0, grid_height - 1):
if grid_cells.size() > 0 and Match3BoardPluginUtilities.value_is_between(row, 0, grid_height - 1):
for column in grid_width:
cells.append(grid_cells[column][row])

Expand All @@ -370,7 +370,7 @@ func grid_cells_from_row(row: int) -> Array[GridCellUI]:
func grid_cells_from_column(column: int) -> Array[GridCellUI]:
var cells: Array[GridCellUI] = []

if grid_cells.size() > 0 and PluginUtilities.value_is_between(column, 0, grid_width - 1):
if grid_cells.size() > 0 and Match3BoardPluginUtilities.value_is_between(column, 0, grid_width - 1):
for row in grid_height:
cells.append(grid_cells[column][row])

Expand Down Expand Up @@ -432,13 +432,13 @@ func find_horizontal_sequences(cells: Array[GridCellUI]) -> Array[Sequence]:
sequences.append(Sequence.new(current_matches, Sequence.Shapes.Horizontal))
current_matches.clear()
else:
if PluginUtilities.value_is_between(current_matches.size(), min_match, max_match):
if Match3BoardPluginUtilities.value_is_between(current_matches.size(), min_match, max_match):
sequences.append(Sequence.new(current_matches, Sequence.Shapes.Horizontal))

current_matches.clear()
current_matches.append(current_cell)

if current_cell == valid_cells.back() and PluginUtilities.value_is_between(current_matches.size(), min_match, max_match):
if current_cell == valid_cells.back() and Match3BoardPluginUtilities.value_is_between(current_matches.size(), min_match, max_match):
sequences.append(Sequence.new(current_matches, Sequence.Shapes.Horizontal))

previous_cell = current_cell
Expand Down Expand Up @@ -467,13 +467,13 @@ func find_vertical_sequences(cells: Array[GridCellUI]) -> Array[Sequence]:
sequences.append(Sequence.new(current_matches, Sequence.Shapes.Vertical))
current_matches.clear()
else:
if PluginUtilities.value_is_between(current_matches.size(), min_match, max_match):
if Match3BoardPluginUtilities.value_is_between(current_matches.size(), min_match, max_match):
sequences.append(Sequence.new(current_matches, Sequence.Shapes.Vertical))

current_matches.clear()
current_matches.append(current_cell)

if current_cell.in_same_grid_position_as(valid_cells.back().board_position()) and PluginUtilities.value_is_between(current_matches.size(), min_match, max_match):
if current_cell.in_same_grid_position_as(valid_cells.back().board_position()) and Match3BoardPluginUtilities.value_is_between(current_matches.size(), min_match, max_match):
sequences.append(Sequence.new(current_matches, Sequence.Shapes.Vertical))

previous_cell = current_cell
Expand Down Expand Up @@ -504,7 +504,7 @@ func find_tshape_sequence(sequence_a: Sequence, sequence_b: Sequence):
var cells: Array[GridCellUI] = []

## We need to iterate manually to be able append the item type on the array
for cell: GridCellUI in PluginUtilities.remove_duplicates(horizontal_sequence.cells + vertical_sequence.cells):
for cell: GridCellUI in Match3BoardPluginUtilities.remove_duplicates(horizontal_sequence.cells + vertical_sequence.cells):
cells.append(cell)

return Sequence.new(cells, Sequence.Shapes.TShape)
Expand All @@ -530,7 +530,7 @@ func find_lshape_sequence(sequence_a: Sequence, sequence_b: Sequence):
var cells: Array[GridCellUI] = []

## We need to iterate manually to be able append the item type on the array
for cell: GridCellUI in PluginUtilities.remove_duplicates(horizontal_sequence.cells + vertical_sequence.cells):
for cell: GridCellUI in Match3BoardPluginUtilities.remove_duplicates(horizontal_sequence.cells + vertical_sequence.cells):
cells.append(cell)

return Sequence.new(cells, Sequence.Shapes.LShape)
Expand Down Expand Up @@ -707,7 +707,7 @@ func swap_pieces(from_grid_cell: GridCellUI, to_grid_cell: GridCellUI) -> void:
if from_grid_cell.can_swap_piece_with(to_grid_cell):
var matches: Array[Sequence] = []

for sequence: Sequence in PluginUtilities.remove_falsy_values([
for sequence: Sequence in Match3BoardPluginUtilities.remove_falsy_values([
find_match_from_cell(from_grid_cell),
find_match_from_cell(to_grid_cell)
]):
Expand Down Expand Up @@ -761,12 +761,12 @@ func fill_pieces() -> void:


func lock_all_pieces() -> void:
for piece: PieceUI in PluginUtilities.find_nodes_of_custom_class(self, PieceUI):
for piece: PieceUI in Match3BoardPluginUtilities.find_nodes_of_custom_class(self, PieceUI):
piece.lock()


func unlock_all_pieces() -> void:
for piece: PieceUI in PluginUtilities.find_nodes_of_custom_class(self, PieceUI):
for piece: PieceUI in Match3BoardPluginUtilities.find_nodes_of_custom_class(self, PieceUI):
piece.unlock()


Expand All @@ -785,7 +785,7 @@ func draw_preview_grid() -> void:
debug_preview_node = Node2D.new()
debug_preview_node.name = "BoardEditorPreview"
add_child(debug_preview_node)
PluginUtilities.set_owner_to_edited_scene_root(debug_preview_node)
Match3BoardPluginUtilities.set_owner_to_edited_scene_root(debug_preview_node)

for column in grid_width:
for row in grid_height:
Expand All @@ -799,7 +799,7 @@ func draw_preview_grid() -> void:
current_cell_sprite.position = Vector2(cell_size.x * column + cell_offset.x, cell_size.y * row + cell_offset.y)

debug_preview_node.add_child(current_cell_sprite)
PluginUtilities.set_owner_to_edited_scene_root(current_cell_sprite)
Match3BoardPluginUtilities.set_owner_to_edited_scene_root(current_cell_sprite)

if current_cell_sprite.texture:
var cell_texture_size = current_cell_sprite.texture.get_size()
Expand All @@ -812,7 +812,7 @@ func draw_preview_grid() -> void:
current_piece_sprite.position = current_cell_sprite.position

debug_preview_node.add_child(current_piece_sprite)
PluginUtilities.set_owner_to_edited_scene_root(current_piece_sprite)
Match3BoardPluginUtilities.set_owner_to_edited_scene_root(current_piece_sprite)

if current_piece_sprite.texture:
var piece_texture_size = current_piece_sprite.texture.get_size()
Expand Down
87 changes: 82 additions & 5 deletions addons/ninetailsrabbit.match3_board/updater/update_progress_bar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func _ready() -> void:

@warning_ignore("return_value_discarded")
func run_update(progress_steps: int = 5, message: String = "Press 'Start Update' to start!") -> void:
if PluginUtilities.is_valid_url(download_url):
if _is_valid_url(download_url):
show()
set_process(true)
get_cancel_button().disabled = true
Expand Down Expand Up @@ -148,9 +148,9 @@ func on_http_request_request_completed(result: int, response_code: int, headers:

if MyPluginSettings.DebugMode:
if DirAccess.dir_exists_absolute(MyPluginSettings.PluginDebugDirectoryPath):
PluginUtilities.remove_files_recursive(MyPluginSettings.PluginDebugDirectoryPath)
_remove_files_recursive(MyPluginSettings.PluginDebugDirectoryPath)
else:
PluginUtilities.remove_files_recursive(MyPluginSettings.PluginBasePath)
_remove_files_recursive(MyPluginSettings.PluginBasePath)

EditorInterface.get_resource_filesystem().scan()

Expand All @@ -159,9 +159,9 @@ func on_http_request_request_completed(result: int, response_code: int, headers:
if MyPluginSettings.DebugMode:
if not DirAccess.dir_exists_absolute(MyPluginSettings.PluginDebugDirectoryPath):
DirAccess.make_dir_absolute(MyPluginSettings.PluginDebugDirectoryPath)
PluginUtilities.copy_directory_recursive(MyPluginSettings.PluginTemporaryReleaseUpdateDirectoryPath, MyPluginSettings.PluginDebugDirectoryPath)
_copy_directory_recursive(MyPluginSettings.PluginTemporaryReleaseUpdateDirectoryPath, MyPluginSettings.PluginDebugDirectoryPath)
else:
PluginUtilities.copy_directory_recursive(MyPluginSettings.PluginTemporaryReleaseUpdateDirectoryPath, "res://")
_copy_directory_recursive(MyPluginSettings.PluginTemporaryReleaseUpdateDirectoryPath, "res://")

EditorInterface.get_resource_filesystem().scan()

Expand All @@ -175,3 +175,80 @@ func on_http_request_request_completed(result: int, response_code: int, headers:
enable_plugin()
hide()
update_finished.emit()


#region Utils
func _is_valid_url(url: String) -> bool:
var regex = RegEx.new()
var url_pattern = "/(https:\\/\\/www\\.|http:\\/\\/www\\.|https:\\/\\/|http:\\/\\/)?[a-zA-Z]{2,}(\\.[a-zA-Z]{2,})(\\.[a-zA-Z]{2,})?\\/[a-zA-Z0-9]{2,}|((https:\\/\\/www\\.|http:\\/\\/www\\.|https:\\/\\/|http:\\/\\/)?[a-zA-Z]{2,}(\\.[a-zA-Z]{2,})(\\.[a-zA-Z]{2,})?)|(https:\\/\\/www\\.|http:\\/\\/www\\.|https:\\/\\/|http:\\/\\/)?[a-zA-Z0-9]{2,}\\.[a-zA-Z0-9]{2,}\\.[a-zA-Z0-9]{2,}(\\.[a-zA-Z0-9]{2,})?/g"
regex.compile(url_pattern)

return regex.search(url) != null


func _copy_directory_recursive(from_dir :String, to_dir :String) -> bool:
if not DirAccess.dir_exists_absolute(from_dir):
push_error("copy_directory_recursive: directory not found '%s'" % from_dir)
return false

if not DirAccess.dir_exists_absolute(to_dir):

var err := DirAccess.make_dir_recursive_absolute(to_dir)
if err != OK:
push_error("copy_directory_recursive: Can't create directory '%s'. Error: %s" % [to_dir, error_string(err)])
return false

var source_dir := DirAccess.open(from_dir)
var dest_dir := DirAccess.open(to_dir)

if source_dir != null:
source_dir.list_dir_begin()
var next := "."

while next != "":
next = source_dir.get_next()
if next == "" or next == "." or next == "..":
continue
var source := source_dir.get_current_dir() + "/" + next
var dest := dest_dir.get_current_dir() + "/" + next

if source_dir.current_is_dir():
_copy_directory_recursive(source + "/", dest)
continue

var err := source_dir.copy(source, dest)

if err != OK:
push_error("_copy_directory_recursive: Error checked copy file '%s' to '%s'" % [source, dest])
return false

return true
else:
push_error("copy_directory_recursive: Directory not found: " + from_dir)
return false


func _remove_files_recursive(path: String, regex: RegEx = null) -> void:
var directory = DirAccess.open(path)

if DirAccess.get_open_error() == OK:
directory.list_dir_begin()

var file_name = directory.get_next()

while file_name != "":
if directory.current_is_dir():
_remove_files_recursive(directory.get_current_dir().path_join(file_name), regex)
else:
if regex != null:
if regex.search(file_name):
directory.remove(file_name)
else:
directory.remove(file_name)

file_name = directory.get_next()

directory.remove(path)
else:
push_error("remove_recursive: An error %s happened open directory: %s " % [DirAccess.get_open_error(), path])
#endregion
Loading

0 comments on commit 47a0d16

Please sign in to comment.