save difficulty in prefs and apply it

master
Fabien Freling 2020-05-22 15:34:52 +02:00
parent 0c905be6fc
commit 6d788465ab
3 changed files with 68 additions and 17 deletions

View File

@ -4,21 +4,46 @@ extends PopupPanel
signal start_triggered(config)
const pref_path = "user://preferences.cfg"
var preferences = ConfigFile.new()
func _init():
# TODO: load config with ConfigFile
# https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html
pass
var err = preferences.load(pref_path)
func _ready():
# TODO: load previous preferences
$Panel/HBoxContainer/Normal.toggle_mode = true
pass
func _on_Cancel_pressed():
self.hide()
func _on_Start_pressed():
var config = {}
# TODO: save config
emit_signal("start_triggered", config)
if $Panel/HBoxContainer/Easy.pressed:
preferences.set_value("game", "difficulty", "easy")
if $Panel/HBoxContainer/Normal.pressed:
preferences.set_value("game", "difficulty", "normal")
if $Panel/HBoxContainer/Hard.pressed:
preferences.set_value("game", "difficulty", "hard")
preferences.save(pref_path)
emit_signal("start_triggered", preferences)
self.hide()
func _on_NewGamePanel_about_to_show():
$Panel/HBoxContainer/Easy.pressed = false
$Panel/HBoxContainer/Normal.pressed = false
$Panel/HBoxContainer/Hard.pressed = false
var difficulty = preferences.get_value("game", "difficulty", "normal")
match difficulty:
"easy":
$Panel/HBoxContainer/Easy.pressed = true
"normal":
$Panel/HBoxContainer/Normal.pressed = true
"hard":
$Panel/HBoxContainer/Hard.pressed = true
_:
$Panel/HBoxContainer/Normal.pressed = true
$Panel/Start.grab_focus()

View File

@ -38,6 +38,7 @@ __meta__ = {
margin_right = 200.0
margin_bottom = 70.0
rect_min_size = Vector2( 200, 70 )
toggle_mode = true
group = ExtResource( 2 )
text = "Easy"
@ -84,5 +85,6 @@ margin_bottom = -20.0
rect_min_size = Vector2( 200, 70 )
text = "Cancel"
flat = true
[connection signal="about_to_show" from="." to="." method="_on_NewGamePanel_about_to_show"]
[connection signal="pressed" from="Panel/Start" to="." method="_on_Start_pressed"]
[connection signal="pressed" from="Panel/Cancel" to="." method="_on_Cancel_pressed"]

View File

@ -71,10 +71,10 @@ func _ready() -> void:
$Background.rect_size.y = height
rng.randomize()
var piece_size: int = compute_piece_size()
padding = compute_padding(piece_size)
print("piece size: ", piece_size)
print("padding: ", padding)
# var piece_size: int = compute_piece_size()
# padding = compute_padding(piece_size)
# print("piece size: ", piece_size)
# print("padding: ", padding)
var pieces_order: Array = []
for order in range(1, rows * columns + 1):
pieces_order.append(order)
@ -338,8 +338,8 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
print("padding: ", padding)
if pieces.size() > 0:
for c in range(columns):
for r in range(rows):
for c in range(pieces.size()):
for r in range(pieces[0].size()):
var piece: Piece = pieces[c][r]
$Background.remove_child(piece)
piece.queue_free()
@ -358,7 +358,7 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
# 4 5 6
# 7 8 9
piece.order = pieces_order[r + c * rows]
print("piece at ", c, ", ", r, " -> order: ", piece.order)
# print("piece at ", c, ", ", r, " -> order: ", piece.order)
# place inside the taquin as indices (i, j)
piece.taquin_index = Vector2(c, r)
@ -395,5 +395,29 @@ func _on_AnimationPlayer_animation_finished(anim_name):
update()
check_solved()
func _on_NewGamePanel_start_triggered(config):
pass # Replace with function body.
func _on_NewGamePanel_start_triggered(preferences):
var difficulty = preferences.get_value("game", "difficulty", "normal")
match difficulty:
"easy":
rows = 3
columns = 3
difficulty = 4
"normal":
rows = 4
columns = 4
difficulty = 10
"hard":
rows = 5
columns = 5
difficulty = 30
_:
rows = 4
columns = 4
difficulty = 10
var pieces_order: Array = []
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)
shuffle(difficulty)