load custom artwork

This commit is contained in:
Fabien Freling 2020-06-10 17:15:33 +02:00
parent 8cc55513b7
commit 5e1e27b2cb
10 changed files with 225 additions and 47 deletions

View file

@ -17,11 +17,14 @@ const _state_transitions = {
State.GAME_OVER : [ State.MAIN ]
}
const Piece = preload("res://src/Piece.tscn")
const Utils = preload("res://src/Utils.gd")
export var rows: int = NewGamePanel.normal_rows
export var columns: int = NewGamePanel.normal_columns
export var shuffle_iterations: int = NewGamePanel.normal_iterations
export var artwork_texture: Texture
export(State) var current_state = State.MAIN
export var autoload_fresh_game := true
var board_size := Vector2.ZERO
var interpiece := 4
@ -49,6 +52,8 @@ var local_max_position := Vector2.ZERO
var hint_active := false setget set_hint_active, get_hint_active
var _artwork_path := NewGamePanel.default_artwork_path
onready var hint_tween = $HintTween
func position_for_index(index: Vector2, size: int) -> Vector2:
@ -79,7 +84,12 @@ func _ready() -> void:
rng.randomize()
new_game(NewGamePanel.normal_columns, NewGamePanel.normal_rows, NewGamePanel.normal_iterations)
if autoload_fresh_game:
start_fresh()
if artwork_texture == null:
print_debug("Load texture from: ", NewGamePanel.default_artwork_path)
artwork_texture = Utils.load_texture_from_path(NewGamePanel.default_artwork_path)
new_game(NewGamePanel.normal_columns, NewGamePanel.normal_rows, NewGamePanel.normal_iterations, artwork_texture)
func _unhandled_input(event):
# Forward keyboard event
@ -347,26 +357,43 @@ func transition_to(state):
emit_signal("state_changed", previous_state, current_state)
func save() -> Dictionary:
# order start from top-left (1) and iterate over every row
# eg. 1 2 3
# 4 5 6
# 7 8 9
var serialized_pieces = []
for c in range(columns):
for r in range(rows):
for r in range(rows):
for c in range(columns):
var piece: Piece = pieces[c][r]
serialized_pieces.append(piece.order)
return {
"rows": rows,
"columns": columns,
"pieces": serialized_pieces,
"hidden_piece": serialized_pieces.size(),
"artwork_path": _artwork_path,
}
func load(saved_state) -> void:
if not saved_state.has_all(["rows", "columns", "pieces", "hidden_piece"]):
return
func load(saved_state) -> bool:
print("load save state: ", saved_state)
if not saved_state.has_all(["rows", "columns", "pieces", "hidden_piece", "artwork_path"]):
assert(false, "Invalid save state")
return false
rows = saved_state["rows"]
columns = saved_state["columns"]
init(saved_state["pieces"], saved_state["hidden_piece"])
func init(pieces_order: Array, hidden_piece: int) -> void:
var texture := Utils.load_texture_from_path(saved_state["artwork_path"])
if texture == null:
return false
artwork_texture = texture
_artwork_path = saved_state["artwork_path"]
print_debug("Load artwork from: ", _artwork_path)
init(saved_state["pieces"], saved_state["hidden_piece"], artwork_texture)
return true
func init(pieces_order: Array, hidden_piece: int, artwork: Texture) -> void:
var piece_size: int = compute_piece_size()
padding = compute_padding(piece_size)
print("piece size: ", piece_size)
@ -386,6 +413,7 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
# Uniforms
piece.size = piece_size
piece.piece_scale = Vector2(piece_size, piece_size) / board_size
piece.texture = artwork_texture
# order start from top-left (1) and iterate over every row
# eg. 1 2 3
@ -410,7 +438,7 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
pieces.append(pieces_row)
assert(missing_piece != null)
func new_game(columns: int, rows: int, shuffle_iterations: int) -> void:
func new_game(columns: int, rows: int, shuffle_iterations: int, artwork_texture: Texture) -> void:
self.columns = columns
self.rows = rows
self.shuffle_iterations = shuffle_iterations
@ -419,9 +447,16 @@ func new_game(columns: int, rows: int, shuffle_iterations: int) -> void:
for order in range(1, rows * columns + 1):
pieces_order.append(order)
var hidden_piece = rows * columns # Last piece is hidden
init(pieces_order, hidden_piece)
init(pieces_order, hidden_piece, artwork_texture)
shuffle(shuffle_iterations, 0.0)
func start_fresh():
if artwork_texture == null:
print_debug("Load texture from: ", NewGamePanel.default_artwork_path)
artwork_texture = Utils.load_texture_from_path(NewGamePanel.default_artwork_path)
new_game(NewGamePanel.normal_columns, NewGamePanel.normal_rows, NewGamePanel.normal_iterations, artwork_texture)
#
# Hints
#
@ -469,7 +504,12 @@ func _on_NewGamePanel_start_triggered(preferences):
var columns = preferences.get_value("game", "columns", NewGamePanel.normal_columns)
var rows = preferences.get_value("game", "rows", NewGamePanel.normal_rows)
var shuffle_iterations = preferences.get_value("game", "shuffle_iterations", NewGamePanel.normal_iterations)
new_game(columns, rows, shuffle_iterations)
var artwork_path = preferences.get_value("game", "artwork_path", NewGamePanel.default_artwork_path)
print_debug("new game triggered with artwork path: ", artwork_path)
artwork_texture = Utils.load_texture_from_path(artwork_path)
if artwork_path != null:
_artwork_path = artwork_path
new_game(columns, rows, shuffle_iterations, artwork_texture)
func _on_Hints_button_down():