save slots! (-;

This commit is contained in:
Harmony Honey 2024-01-11 00:01:31 -05:00
parent 86a18eed39
commit 7e1a59fc8f
4 changed files with 134 additions and 98 deletions

View file

@ -23,10 +23,13 @@ var splash_path := "res://src/menu/splash.tscn"
var creator_path := "res://src/menu/Creator.tscn"
var scene_path := level_select_path
var save_data := {}
var save_data := {0: {}, 1: {}, 2: {}}
var save_slot := 0
var save_maps := {}
var replays := {}
var save_path := "user://save/"
var save_filename := "box.save"
var scene_dict := {}
var replays := {}
var window_scale := 1
var view_size := Vector2(228, 128)
@ -55,10 +58,6 @@ export (Array, Color) var palette := []
var player_colors = [8, 0, 11, 13]
var preset_palettes = [[7, 13, 6, 3], [8, 0, 11, 13], [11, 7, 9, 0], [12, 1, 7, 5], [9, 8, 12, 3]]
var scene_dict := {}
var save_slot := 0
var save_path := "user://save/0/"
func _ready():
print("Shared._ready(): ")
randomize()
@ -89,18 +88,16 @@ func _ready():
maps.append(i.split(".")[0])
print("maps: ", maps, " ", maps.size(), " ", scene_dict)
# make save folders
var dir = Directory.new()
if !dir.open("user://save") == OK:
dir.make_dir("user://save")
if !dir.open(save_path) == OK:
dir.make_dir(save_path)
for i in 3:
var s = "user://save/" + str(i)
var s = save_path + str(i)
if !dir.open(s) == OK:
dir.make_dir(s)
# load save data
load_save()
load_replays()
count_score()
load_slots()
Wipe.connect("finish", self, "wipe_finish")
@ -230,47 +227,64 @@ func load_file(fname = ""):
return content
func save():
save_file(save_path + save_filename, JSON.print(save_data, "\t"))
var data = {}
data["username"] = username
data["player_colors"] = player_colors
data["maps"] = save_maps
save_file(save_path + str(save_slot) + "/" + save_filename, JSON.print(data, "\t"))
func save_replays(arg := replay_map):
save_file(save_path + arg + ".save", JSON.print(replays[arg], "\t"))
save_file(save_path + str(save_slot) + "/" + arg + ".save", JSON.print(replays[arg], "\t"))
func load_save():
var l = load_file(save_path + save_filename)
if l:
var p = JSON.parse(l).result
if p is Dictionary:
save_data = p
func load_slot(_slot):
save_slot = clamp(_slot, 0, 2)
load_save(_slot, save_data[save_slot])
func load_slots():
for i in 3:
load_save(i)
func load_save(_slot = save_slot, _dict := {}):
save_slot = clamp(_slot, 0, 2)
save_data[save_slot] = {}
var s = save_data[save_slot]
save_maps = {}
if _dict.empty():
var l = load_file(save_path + str(save_slot) + "/" + save_filename)
if l: _dict = JSON.parse(l).result
if !_dict.empty():
if _dict.has("username"):
username = _dict["username"]
s["username"] = username
# remove old keys
for i in ["replays", "map", "notes", "times", "deaths"]:
if save_data.has(i):
save_data.erase(i)
if _dict.has("player_colors"):
player_colors = _dict["player_colors"].duplicate()
s["player_colors"] = player_colors
if _dict.has("maps"):
save_maps = _dict["maps"].duplicate()
s["maps"] = save_maps
if save_data.has("username"):
username = save_data["username"]
if save_data.has("player_colors"):
player_colors = save_data["player_colors"]
count_score()
s["gems"] = count_gems
s["notes"] = count_notes
if save_data.has("maps"):
save_maps = save_data["maps"]
else:
create_save()
else:
print(save_path + save_filename + " not found")
create_save()
print(save_path + save_filename + " not found")
func load_replays():
for i in dir_list(save_path):
var l = load_file(save_path + i)
replays = {}
var s = save_path + str(save_slot) + "/"
for i in dir_list(s):
var l = load_file(s + i)
if l:
var p = JSON.parse(l).result
if p is Array and p[0] is Dictionary and p[0].has("frames"):
replays[i.split(".")[0]] = p
else:
print(save_path + i + " not found")
print(s + i + " not found")
func generate_username():
var u = ""
@ -290,20 +304,9 @@ func generate_username():
func delete_save():
print("delete save")
create_save()
func create_save():
save_data = {}
save_data["map"] = 0
save_data["notes"] = {}
save_data["times"] = {}
save_data["username"] = username
save_data["player_colors"] = player_colors
save()
func unlock():
# nothing
save()
print("unlock")
func win():
is_win = true
@ -321,9 +324,6 @@ func win():
if !ht or (ht and map_frame < s["time"]):
s["time"] = map_frame
save_data["maps"] = save_maps
save_data["username"] = username
# replays
var m = map_name + ("-note" if is_note else "")
replay_map = m

View file

@ -102,8 +102,6 @@ func _input(event):
Shared.wipe_scene(Shared.main_menu_path)
Shared.username = name_label.text.to_lower()
Shared.player_colors = colors.duplicate()
Shared.save_data["username"] = Shared.username
Shared.save_data["player_colors"] = Shared.player_colors.duplicate()
Audio.play("menu_bell", 0.8, 1.2)
elif is_action:
Audio.play("menu_scroll2", 0.8, 1.2)

View file

@ -1,13 +1,15 @@
extends Node2D
onready var main_menu := $Control/Menu/List
onready var menu_stuff := main_menu.get_children()
onready var main_menu := $Control/Main
onready var quit_menu := $Control/Quit
onready var slot_menu := $Control/Slot
onready var menu_stuff := main_menu.get_children()
var cursor := 0 setget set_cursor
var menu_items := []
var main_items := ["play", "creator", "options", "credits"]
var quit_items := ["yes", "no"]
var slot_items := ["slot", "slot", "slot"]
var is_input = true
export var blink_on := 0.3
@ -16,15 +18,17 @@ var blink_clock := 0.0
func _ready():
switch_menu("main", true)
Shared.load_slots()
var smc = slot_menu.get_children()
for i in smc.size():
smc[i].text = str(Shared.save_data[i]["gems"]) + " win"
func _input(event):
if !is_input:
return
if event.is_action_pressed("action"):
if menu_items == quit_items:
switch_menu("main")
else:
switch_menu("quit")
switch_menu("quit" if menu_items == main_items else "main")
elif event.is_action_pressed("jump"):
menu_select()
else:
@ -35,7 +39,7 @@ func _input(event):
Audio.play("menu_scroll", 0.8, 1.2)
func _physics_process(delta):
# blink
# blink
blink_clock -= delta
if blink_clock < -blink_off:
blink_clock = blink_on
@ -48,9 +52,7 @@ func write_menu():
func menu_select(tag : String = menu_items[cursor].to_lower()):
match tag:
"play":
Shared.wipe_scene(Shared.level_select_path)
is_input = false
Audio.play("menu_play", 0.9, 1.1)
switch_menu("slot")
"creator":
Shared.wipe_scene(Shared.creator_path)
is_input = false
@ -73,18 +75,26 @@ func menu_select(tag : String = menu_items[cursor].to_lower()):
Shared.wipe_quit()
"no":
switch_menu("main")
"slot":
Shared.load_slot(cursor)
Shared.wipe_scene(Shared.level_select_path)
is_input = false
Audio.play("menu_play", 0.9, 1.1)
func switch_menu(arg, silent := false):
var is_main : bool = arg == "main"
quit_menu.visible = !is_main
main_menu.visible = is_main
menu_items = main_items if is_main else quit_items
menu_stuff = (main_menu if is_main else quit_menu).get_children()
var s = ["quit", "main", "slot"]
var items = [quit_items, main_items, slot_items]
var node = [quit_menu, main_menu, slot_menu]
var audio = ["pick", "exit", "pick"]
for i in 3:
node[i].visible = arg == s[i]
if arg == s[i]:
menu_items = items[i]
menu_stuff = node[i].get_children()
if !silent:
Audio.play("menu_" + audio[i], 0.9, 1.1)
if !silent:
Audio.play("menu_" + ("exit" if is_main else "pick"), 0.9, 1.1)
self.cursor = 0 if is_main else 1
self.cursor = 1 if arg == "quit" else 0
func find_cursor(arg := ""):
if is_input and menu_items.has(arg):

View file

@ -143,11 +143,7 @@ margin_top = 12.0
margin_right = 204.0
margin_bottom = 140.0
[node name="Menu" type="Control" parent="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
[node name="List" type="VBoxContainer" parent="Control/Menu"]
[node name="Main" type="VBoxContainer" parent="Control"]
margin_left = 4.0
margin_top = 32.0
margin_right = 68.0
@ -155,26 +151,26 @@ margin_bottom = 104.0
custom_constants/separation = 5
alignment = 1
[node name="Item" type="HBoxContainer" parent="Control/Menu/List"]
[node name="Item" type="HBoxContainer" parent="Control/Main"]
margin_top = 4.0
margin_right = 64.0
margin_bottom = 16.0
rect_min_size = Vector2( 0, 12 )
custom_constants/separation = 2
[node name="Image" type="Control" parent="Control/Menu/List/Item"]
[node name="Image" type="Control" parent="Control/Main/Item"]
margin_right = 8.0
margin_bottom = 12.0
rect_min_size = Vector2( 8, 8 )
[node name="Sprite" type="Sprite" parent="Control/Menu/List/Item/Image"]
[node name="Sprite" type="Sprite" parent="Control/Main/Item/Image"]
position = Vector2( 0, 2 )
texture = ExtResource( 16 )
centered = false
hframes = 4
vframes = 4
[node name="Label" type="Label" parent="Control/Menu/List/Item"]
[node name="Label" type="Label" parent="Control/Main/Item"]
margin_left = 10.0
margin_top = 2.0
margin_right = 43.0
@ -183,26 +179,26 @@ custom_fonts/font = SubResource( 3 )
text = "play"
uppercase = true
[node name="Item4" type="HBoxContainer" parent="Control/Menu/List"]
[node name="Item4" type="HBoxContainer" parent="Control/Main"]
margin_top = 21.0
margin_right = 64.0
margin_bottom = 33.0
rect_min_size = Vector2( 0, 12 )
custom_constants/separation = 2
[node name="Image" type="Control" parent="Control/Menu/List/Item4"]
[node name="Image" type="Control" parent="Control/Main/Item4"]
margin_right = 8.0
margin_bottom = 12.0
rect_min_size = Vector2( 8, 8 )
[node name="Sprite" type="Sprite" parent="Control/Menu/List/Item4/Image"]
[node name="Sprite" type="Sprite" parent="Control/Main/Item4/Image"]
position = Vector2( 0, 2 )
texture = ExtResource( 1 )
centered = false
hframes = 4
frame = 2
[node name="Label" type="Label" parent="Control/Menu/List/Item4"]
[node name="Label" type="Label" parent="Control/Main/Item4"]
margin_left = 10.0
margin_top = 2.0
margin_right = 42.0
@ -211,19 +207,19 @@ custom_fonts/font = SubResource( 3 )
text = "char"
uppercase = true
[node name="Item2" type="HBoxContainer" parent="Control/Menu/List"]
[node name="Item2" type="HBoxContainer" parent="Control/Main"]
margin_top = 38.0
margin_right = 64.0
margin_bottom = 50.0
rect_min_size = Vector2( 0, 12 )
custom_constants/separation = 2
[node name="Image" type="Control" parent="Control/Menu/List/Item2"]
[node name="Image" type="Control" parent="Control/Main/Item2"]
margin_right = 8.0
margin_bottom = 12.0
rect_min_size = Vector2( 8, 8 )
[node name="Sprite" type="Sprite" parent="Control/Menu/List/Item2/Image"]
[node name="Sprite" type="Sprite" parent="Control/Main/Item2/Image"]
position = Vector2( 0, 2 )
texture = ExtResource( 16 )
centered = false
@ -231,7 +227,7 @@ hframes = 4
vframes = 4
frame = 1
[node name="Label" type="Label" parent="Control/Menu/List/Item2"]
[node name="Label" type="Label" parent="Control/Main/Item2"]
margin_left = 10.0
margin_top = 2.0
margin_right = 58.0
@ -240,19 +236,19 @@ custom_fonts/font = SubResource( 3 )
text = "option"
uppercase = true
[node name="Item3" type="HBoxContainer" parent="Control/Menu/List"]
[node name="Item3" type="HBoxContainer" parent="Control/Main"]
margin_top = 55.0
margin_right = 64.0
margin_bottom = 67.0
rect_min_size = Vector2( 0, 12 )
custom_constants/separation = 2
[node name="Image" type="Control" parent="Control/Menu/List/Item3"]
[node name="Image" type="Control" parent="Control/Main/Item3"]
margin_right = 8.0
margin_bottom = 12.0
rect_min_size = Vector2( 8, 8 )
[node name="Sprite" type="Sprite" parent="Control/Menu/List/Item3/Image"]
[node name="Sprite" type="Sprite" parent="Control/Main/Item3/Image"]
position = Vector2( 0, 2 )
texture = ExtResource( 16 )
centered = false
@ -260,7 +256,7 @@ hframes = 4
vframes = 4
frame = 2
[node name="Label" type="Label" parent="Control/Menu/List/Item3"]
[node name="Label" type="Label" parent="Control/Main/Item3"]
margin_left = 10.0
margin_top = 2.0
margin_right = 58.0
@ -294,6 +290,38 @@ custom_fonts/font = SubResource( 3 )
text = "no"
uppercase = true
[node name="Slot" type="VBoxContainer" parent="Control"]
margin_left = 12.0
margin_top = 32.0
margin_right = 81.0
margin_bottom = 104.0
custom_constants/separation = 9
alignment = 1
[node name="1" type="Label" parent="Control/Slot"]
margin_top = 15.0
margin_right = 69.0
margin_bottom = 23.0
custom_fonts/font = SubResource( 3 )
text = "27 win"
uppercase = true
[node name="2" type="Label" parent="Control/Slot"]
margin_top = 32.0
margin_right = 69.0
margin_bottom = 40.0
custom_fonts/font = SubResource( 3 )
text = "3 win"
uppercase = true
[node name="3" type="Label" parent="Control/Slot"]
margin_top = 49.0
margin_right = 69.0
margin_bottom = 57.0
custom_fonts/font = SubResource( 3 )
text = "new game"
uppercase = true
[node name="Header" type="Label" parent="Control"]
margin_top = 5.0
margin_right = 228.0