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) signal start_triggered(config)
const pref_path = "user://preferences.cfg"
var preferences = ConfigFile.new()
func _init(): func _init():
# TODO: load config with ConfigFile var err = preferences.load(pref_path)
# https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html
pass
func _ready(): func _ready():
# TODO: load previous preferences
$Panel/HBoxContainer/Normal.toggle_mode = true
pass pass
func _on_Cancel_pressed(): func _on_Cancel_pressed():
self.hide() self.hide()
func _on_Start_pressed(): func _on_Start_pressed():
var config = {} if $Panel/HBoxContainer/Easy.pressed:
# TODO: save config preferences.set_value("game", "difficulty", "easy")
emit_signal("start_triggered", config) 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() 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_right = 200.0
margin_bottom = 70.0 margin_bottom = 70.0
rect_min_size = Vector2( 200, 70 ) rect_min_size = Vector2( 200, 70 )
toggle_mode = true
group = ExtResource( 2 ) group = ExtResource( 2 )
text = "Easy" text = "Easy"
@ -84,5 +85,6 @@ margin_bottom = -20.0
rect_min_size = Vector2( 200, 70 ) rect_min_size = Vector2( 200, 70 )
text = "Cancel" text = "Cancel"
flat = true 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/Start" to="." method="_on_Start_pressed"]
[connection signal="pressed" from="Panel/Cancel" to="." method="_on_Cancel_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 $Background.rect_size.y = height
rng.randomize() rng.randomize()
var piece_size: int = compute_piece_size() # var piece_size: int = compute_piece_size()
padding = compute_padding(piece_size) # padding = compute_padding(piece_size)
print("piece size: ", piece_size) # print("piece size: ", piece_size)
print("padding: ", padding) # print("padding: ", padding)
var pieces_order: Array = [] var pieces_order: Array = []
for order in range(1, rows * columns + 1): for order in range(1, rows * columns + 1):
pieces_order.append(order) pieces_order.append(order)
@ -338,8 +338,8 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
print("padding: ", padding) print("padding: ", padding)
if pieces.size() > 0: if pieces.size() > 0:
for c in range(columns): for c in range(pieces.size()):
for r in range(rows): for r in range(pieces[0].size()):
var piece: Piece = pieces[c][r] var piece: Piece = pieces[c][r]
$Background.remove_child(piece) $Background.remove_child(piece)
piece.queue_free() piece.queue_free()
@ -358,7 +358,7 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
# 4 5 6 # 4 5 6
# 7 8 9 # 7 8 9
piece.order = pieces_order[r + c * rows] 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) # place inside the taquin as indices (i, j)
piece.taquin_index = Vector2(c, r) piece.taquin_index = Vector2(c, r)
@ -395,5 +395,29 @@ func _on_AnimationPlayer_animation_finished(anim_name):
update() update()
check_solved() check_solved()
func _on_NewGamePanel_start_triggered(config): func _on_NewGamePanel_start_triggered(preferences):
pass # Replace with function body. 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)