load custom artwork

master
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

@ -8,8 +8,8 @@
[node name="DepthButton" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
margin_right = -734.0
margin_bottom = -464.0
margin_right = -732.0
margin_bottom = -452.0
rect_min_size = Vector2( 200, 80 )
theme = ExtResource( 1 )
script = ExtResource( 2 )

View File

@ -8,7 +8,8 @@ var _first_popup_call := true
func _ready():
taquin.rect_min_size = get_viewport().get_visible_rect().size * (2.0 / 3.0)
layout_reflow()
load_game()
if not load_game():
start_fresh()
print("Starting state: ", taquin.current_state_name())
func _notification(what):
@ -56,7 +57,7 @@ func save_game():
func load_game():
var save_game = File.new()
if not save_game.file_exists("user://savegame.save"):
return # Error! We don't have a save to load.
return false # Error! We don't have a save to load.
# Load the file line by line and process that dictionary to restore
# the object it represents.
@ -68,11 +69,16 @@ func load_game():
# Call the node's save function
var node = get_node(node_data["path"])
if node != null:
node.call("load", node_data)
if node.call("load", node_data) == false:
return false
else:
print("Cannot load node ", node_data["path"])
save_game.close()
return true
func start_fresh():
taquin.start_fresh()
#
# Signals

View File

@ -46,6 +46,7 @@ anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 540.0
margin_bottom = 560.0
autoload_fresh_game = false
[node name="VBoxContainer" type="VBoxContainer" parent="GridContainer"]
margin_left = 550.0
@ -74,7 +75,7 @@ rect_min_size = Vector2( 0, 70 )
text = "Hints"
[node name="NewGamePanel" parent="." instance=ExtResource( 3 )]
rect_pivot_offset = Vector2( 4, 4 )
rect_pivot_offset = Vector2( 203, 134 )
[connection signal="state_changed" from="GridContainer/Taquin" to="." method="_on_Taquin_state_changed"]
[connection signal="pressed" from="GridContainer/VBoxContainer/New Game" to="." method="_on_New_game_pressed"]
[connection signal="button_down" from="GridContainer/VBoxContainer/Hints" to="GridContainer/Taquin" method="_on_Hints_button_down"]

View File

@ -15,6 +15,9 @@ const normal_iterations := 10
const hard_columns := 5
const hard_rows := 5
const hard_iterations := 30
const default_artwork_path := "res://assets/hokusai.jpg"
const Utils = preload("res://src/Utils.gd")
export var window_scale_factor = 0.9 # how big the popup will be compared to screen
@ -23,6 +26,8 @@ var fade_duration = 0.2
var fade_scale_factor = 0.9
var flip_duration = 0.4
var _artwork_path: String = default_artwork_path
onready var popup = $"."
onready var panel = $Panel
onready var edit_panel = $EditPanel
@ -34,6 +39,8 @@ onready var normal_button = $Panel/VBoxContainer/Difficulty/Normal
onready var hard_button = $Panel/VBoxContainer/Difficulty/Hard
onready var custom_button = $Panel/VBoxContainer/HBoxContainer/Custom
onready var preview = $Panel/VBoxContainer/ArtworkSource/Preview
onready var columns_spinbox = $EditPanel/VBoxContainer/Columns/SpinBox
onready var rows_spinbox = $EditPanel/VBoxContainer/Rows/SpinBox
onready var iterations_spinbox = $EditPanel/VBoxContainer/Iterations/SpinBox
@ -173,6 +180,8 @@ func _on_Start_pressed():
preferences.set_value("game", "columns", columns_spinbox.value)
preferences.set_value("game", "rows", rows_spinbox.value)
preferences.set_value("game", "shuffle_iterations", iterations_spinbox.value)
preferences.set_value("game", "artwork_path", _artwork_path)
preferences.save(pref_path)
@ -191,6 +200,10 @@ func _on_NewGamePanel_about_to_show():
iterations_spinbox.value = preferences.get_value("game", "custom_shuffle_iterations", normal_iterations)
_update_description()
_artwork_path = preferences.get_value("game", "artwork_path", default_artwork_path)
print_debug("artwork path: ", _artwork_path)
preview.texture = Utils.load_texture_from_path(_artwork_path)
# $Panel/Start.grab_focus()
fade_in()
@ -223,3 +236,23 @@ func _on_Hard_pressed():
func _on_Custom_pressed():
_update_description()
flip_over()
func _on_LoadImage_pressed():
$FileDialog.popup_centered(rect_size)
func _on_FileDialog_file_selected(path: String):
var texture := load(path)
if texture == null:
print_debug("Cannot load image from path: ", path)
return
preview.texture = texture
var directory = Directory.new()
# TODO: remove previous artwork.png, artwork.jpg files
var cached_artwork_path := "user://artwork.%s" % [path.get_extension()]
var error = directory.copy(path, cached_artwork_path)
if error != OK:
print_debug("Cannot cache image")
_artwork_path = path
else:
_artwork_path = cached_artwork_path

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,7 @@ class_name Piece
extends Node2D
export var size: int = 160
export var texture: Texture
var order: int = 0
var taquin_original_index := Vector2.ZERO
@ -22,6 +23,7 @@ func _ready() -> void:
# We need a dedicated material to have separate uniform,
# otherwise uniforms will be shared.
var mat = $ColorRect.material.duplicate() as ShaderMaterial
mat.set_shader_param("artwork", texture) # TODO: make it common to all pieces
mat.set_shader_param("scale", Vector3(piece_scale.x, piece_scale.y, 1.0))
mat.set_shader_param("offset", Vector3(taquin_original_normalized_position.x, taquin_original_normalized_position.y, 0.0))
mat.set_shader_param("reflection", false)

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=19 format=2]
[ext_resource path="res://src/Piece.gd" type="Script" id=1]
[ext_resource path="res://assets/hokusai.jpg" type="Texture" id=2]
[ext_resource path="res://assets/escher_lizards.jpg" type="Texture" id=2]
[sub_resource type="VisualShaderNodeExpression" id=1]
output_port_for_preview = 0
@ -47,9 +47,10 @@ input_name = "time"
[sub_resource type="VisualShaderNodeBooleanUniform" id=8]
uniform_name = "reflection"
[sub_resource type="VisualShaderNodeTexture" id=9]
[sub_resource type="VisualShaderNodeTextureUniform" id=9]
output_port_for_preview = 0
texture = ExtResource( 2 )
uniform_name = "artwork"
texture_type = 1
[sub_resource type="VisualShaderNodeInput" id=10]
output_port_for_preview = 0
@ -74,7 +75,7 @@ code = "shader_type canvas_item;
uniform bool reflection;
uniform vec3 scale;
uniform vec3 offset;
uniform sampler2D tex_frg_3;
uniform sampler2D artwork : hint_albedo;
@ -127,10 +128,14 @@ void fragment() {
// VectorOp:6
vec3 n_out6p0 = n_out5p0 + n_out8p0;
// Texture:3
vec4 tex_frg_3_read = texture(tex_frg_3, n_out6p0.xy);
vec3 n_out3p0 = tex_frg_3_read.rgb;
float n_out3p1 = tex_frg_3_read.a;
// TextureUniform:25
vec3 n_out25p0;
float n_out25p1;
{
vec4 n_tex_read = texture(artwork, n_out6p0.xy);
n_out25p0 = n_tex_read.rgb;
n_out25p1 = n_tex_read.a;
}
// Input:22
float n_out22p0 = TIME;
@ -149,7 +154,7 @@ void fragment() {
}
// ColorOp:21
vec3 n_out21p0 = vec3(1.0) - (vec3(1.0) - n_out3p0) * (vec3(1.0) - n_out19p0);
vec3 n_out21p0 = vec3(1.0) - (vec3(1.0) - n_out25p0) * (vec3(1.0) - n_out19p0);
// VectorSwitch:23
vec3 n_out23p0;
@ -159,7 +164,7 @@ void fragment() {
}
else
{
n_out23p0 = n_out3p0;
n_out23p0 = n_out25p0;
}
// VectorSwitch:17
@ -186,9 +191,7 @@ void light() {
"
mode = 1
flags/light_only = false
nodes/fragment/0/position = Vector2( 1480, 300 )
nodes/fragment/3/node = SubResource( 9 )
nodes/fragment/3/position = Vector2( 740, 20 )
nodes/fragment/0/position = Vector2( 1680, 260 )
nodes/fragment/4/node = SubResource( 10 )
nodes/fragment/4/position = Vector2( 0, 260 )
nodes/fragment/5/node = SubResource( 11 )
@ -218,9 +221,9 @@ vec2 d_thick = abs(rel_p) - box_thick;
float dist_thick = length(max(d_thick, 0.0)) + min(max(d_thick.x, d_thick.y), 0.0);
is_thickness = rel_p.y > 0.0 && dist_thick > (0.5 - box_size);"
nodes/fragment/15/node = SubResource( 2 )
nodes/fragment/15/position = Vector2( 1280, 0 )
nodes/fragment/15/position = Vector2( 1480, -40 )
nodes/fragment/17/node = SubResource( 3 )
nodes/fragment/17/position = Vector2( 1260, 120 )
nodes/fragment/17/position = Vector2( 1460, 80 )
nodes/fragment/19/node = SubResource( 4 )
nodes/fragment/19/position = Vector2( 420, -500 )
nodes/fragment/19/size = Vector2( 545, 407 )
@ -234,14 +237,16 @@ if (abs(input0.x - ((1.0 - input0.y + delta) / slope)) < thickness) {
output0 = vec3(0.5);
}"
nodes/fragment/21/node = SubResource( 5 )
nodes/fragment/21/position = Vector2( 1020, -160 )
nodes/fragment/21/position = Vector2( 1220, -200 )
nodes/fragment/22/node = SubResource( 6 )
nodes/fragment/22/position = Vector2( 160, -300 )
nodes/fragment/23/node = SubResource( 7 )
nodes/fragment/23/position = Vector2( 1000, 60 )
nodes/fragment/23/position = Vector2( 1200, 20 )
nodes/fragment/24/node = SubResource( 8 )
nodes/fragment/24/position = Vector2( 1000, -20 )
nodes/fragment/connections = PoolIntArray( 5, 0, 6, 0, 6, 0, 3, 0, 13, 0, 0, 1, 4, 0, 5, 0, 4, 0, 13, 0, 13, 1, 17, 0, 15, 0, 17, 1, 17, 0, 0, 0, 8, 0, 6, 1, 7, 0, 5, 1, 3, 0, 21, 0, 19, 0, 21, 1, 22, 0, 19, 1, 6, 0, 19, 0, 24, 0, 23, 0, 3, 0, 23, 2, 21, 0, 23, 1, 23, 0, 17, 2 )
nodes/fragment/24/position = Vector2( 1200, -60 )
nodes/fragment/25/node = SubResource( 9 )
nodes/fragment/25/position = Vector2( 840, 20 )
nodes/fragment/connections = PoolIntArray( 5, 0, 6, 0, 13, 0, 0, 1, 4, 0, 5, 0, 4, 0, 13, 0, 13, 1, 17, 0, 15, 0, 17, 1, 17, 0, 0, 0, 8, 0, 6, 1, 7, 0, 5, 1, 19, 0, 21, 1, 22, 0, 19, 1, 6, 0, 19, 0, 24, 0, 23, 0, 21, 0, 23, 1, 23, 0, 17, 2, 6, 0, 25, 0, 25, 0, 23, 2, 25, 0, 21, 0 )
[sub_resource type="ShaderMaterial" id=16]
shader = SubResource( 15 )
@ -251,6 +256,7 @@ shader_param/offset = Vector3( 0, 0, 0 )
[node name="Piece" type="Node2D"]
script = ExtResource( 1 )
texture = ExtResource( 2 )
[node name="ColorRect" type="ColorRect" parent="."]
material = SubResource( 16 )

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():

View File

@ -1,9 +1,10 @@
[gd_scene load_steps=9 format=2]
[gd_scene load_steps=10 format=2]
[ext_resource path="res://src/Taquin.gd" type="Script" id=1]
[ext_resource path="res://src/Piece.tscn" type="PackedScene" id=2]
[ext_resource path="res://assets/sounds/PM_BBI_Bullet_Impact_Hit_Body_Flesh_2.wav" type="AudioStream" id=3]
[ext_resource path="res://assets/taqin_theme.tres" type="Theme" id=4]
[ext_resource path="res://assets/escher_convex_concave.jpg" type="Texture" id=5]
[sub_resource type="Animation" id=1]
resource_name = "MovingPiece"
@ -54,6 +55,7 @@ script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
artwork_texture = ExtResource( 5 )
[node name="Background" type="ColorRect" parent="."]
anchor_left = 0.5
@ -100,6 +102,7 @@ one_shot = true
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 3 )
volume_db = -16.672
[node name="HintTween" type="Tween" parent="."]
[connection signal="animation_finished" from="AnimationPlayer" to="." method="_on_AnimationPlayer_animation_finished"]

9
src/Utils.gd Normal file
View File

@ -0,0 +1,9 @@
static func load_texture_from_path(path: String) -> Texture:
var img = Image.new()
var texture = ImageTexture.new()
if img.load(path) != OK:
print_debug("Cannot load image at path: ", path)
return null
texture.create_from_image(img)
return texture