Skip to content

Commit 3b79813

Browse files
committed
added loading notes from JSON file
SaveData.json can store all our game's save data (unless we want to split the save data amongst different files). NoteQueue now pulls notes from the SaveData.
1 parent 2c5311a commit 3b79813

7 files changed

Lines changed: 127 additions & 10 deletions

File tree

Funk Engine.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<EnableDynamicLoading>true</EnableDynamicLoading>
77
<RootNamespace>FunkEngine</RootNamespace>
88
</PropertyGroup>
9+
<ItemGroup>
10+
<Content Include="SaveData\SaveData.json" />
11+
</ItemGroup>
912
<Target Name="Husky" BeforeTargets="Restore;CollectPackageReferences" Condition="'$(HUSKY)' != 0">
1013
<Exec Command="dotnet tool restore" StandardOutputImportance="Low" StandardErrorImportance="High" />
1114
<Exec Command="dotnet husky install" StandardOutputImportance="Low" StandardErrorImportance="High" WorkingDirectory="." />

Globals/Scribe.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ public partial class Scribe : Node
3030
new Note(
3131
"PlayerDouble",
3232
null,
33-
2,
33+
1,
3434
(director, note, timing) =>
3535
{
36-
director.Enemy.TakeDamage((int)timing);
36+
// can change later, but I want it like this instead of changing base
37+
// in case we have some relic that messes with timing
38+
director.Enemy.TakeDamage(2 * (int)timing);
3739
}
3840
),
3941
};

SaveData/SaveData.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"AccountName": null,
3+
"Notes": {
4+
"PlayerBase": 2,
5+
"PlayerDouble": 1
6+
},
7+
"Relics": {},
8+
"Settings": {}
9+
}

SaveData/SaveSystem.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text.Json;
4+
using Godot;
5+
6+
// TODO: implement saving
7+
8+
public static class SaveSystem
9+
{
10+
private static string SavePath => "res://SaveData/SaveData.json"; // Update if needed
11+
12+
// Loads only the notes section
13+
public static Dictionary<string, int> LoadNotes()
14+
{
15+
var saveData = LoadSaveData();
16+
if (saveData != null && saveData.Notes != null)
17+
{
18+
return saveData.Notes;
19+
}
20+
else
21+
{
22+
return new Dictionary<string, int>();
23+
}
24+
}
25+
26+
// This method loads the entire save data
27+
public static SaveData LoadSaveData()
28+
{
29+
string path = ProjectSettings.GlobalizePath(SavePath);
30+
if (!File.Exists(path))
31+
{
32+
GD.PrintErr("Can't load save game");
33+
return null;
34+
}
35+
36+
string json = File.ReadAllText(path);
37+
SaveData data = JsonSerializer.Deserialize<SaveData>(json);
38+
return data;
39+
}
40+
}
41+
42+
public class SaveData
43+
{
44+
public string AccountName { get; set; }
45+
public Dictionary<string, int> Notes { get; set; } = new Dictionary<string, int>();
46+
public Dictionary<string, object> Relics { get; set; } = new Dictionary<string, object>();
47+
public Dictionary<string, float> Settings { get; set; } = new Dictionary<string, float>();
48+
}

scenes/BattleDirector/test_battle_scene.tscn

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
[gd_scene load_steps=8 format=3 uid="uid://b0mrgr7h0ty1y"]
1+
[gd_scene load_steps=9 format=3 uid="uid://b0mrgr7h0ty1y"]
22

33
[ext_resource type="Script" path="res://scenes/BattleDirector/scripts/BattleDirector.cs" id="1_cwqqr"]
44
[ext_resource type="PackedScene" uid="uid://dfevfib11kou1" path="res://scenes/ChartViewport/ChartViewport.tscn" id="2_cupb3"]
55
[ext_resource type="Script" path="res://scenes/BattleDirector/scripts/Conductor.cs" id="2_pcp76"]
66
[ext_resource type="Texture2D" uid="uid://ci0g72j8q4ec2" path="res://scenes/BattleDirector/assets/CoolBG.jpg" id="4_13o87"]
77
[ext_resource type="PackedScene" uid="uid://duhiilcv4tat3" path="res://scenes/BattleDirector/NotePlacementBar.tscn" id="7_3ko4g"]
8+
[ext_resource type="PackedScene" uid="uid://bvhpon5liybd1" path="res://scenes/CustomNotes/NoteQueue.tscn" id="8_7wwxo"]
89
[ext_resource type="AudioStream" uid="uid://cv6lqjj6lu36h" path="res://Audio/335571__magntron__gamemusic_120bpm.mp3" id="8_caqms"]
910
[ext_resource type="Script" path="res://scenes/SceneTransitions/scripts/SceneChange.cs" id="9_bxa6e"]
1011

@@ -62,3 +63,14 @@ offset_right = 613.0
6263
offset_bottom = 188.0
6364
theme_override_font_sizes/font_size = 10
6465
text = "Relics:"
66+
67+
[node name="NoteQueue" parent="." instance=ExtResource("8_7wwxo")]
68+
anchors_preset = 0
69+
anchor_right = 0.0
70+
anchor_bottom = 0.0
71+
offset_left = 100.0
72+
offset_top = 140.0
73+
offset_right = 100.0
74+
offset_bottom = 140.0
75+
grow_horizontal = 1
76+
grow_vertical = 1

scenes/CustomNotes/NoteQueue.cs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using FunkEngine;
43
using Godot;
54

65
public partial class NoteQueue : Node
@@ -23,15 +22,56 @@ public override void _Ready()
2322
"res://scenes/CustomNotes/assets/double_note.png"
2423
);
2524

25+
LoadQueueFromSave();
26+
ScrambleQueue();
2627
UpdateQueue();
2728
}
2829

29-
public void LoadQueue(PlayerPuppet player)
30+
// Loads the notes from SaveData.json, and adds them to the queue
31+
public void LoadQueueFromSave()
3032
{
31-
for (int i = 0; i < player.Stats.CurNotes.Length; i++)
33+
Dictionary<string, int> savedNotes = SaveSystem.LoadNotes();
34+
foreach (var noteEntry in savedNotes)
3235
{
33-
AddNoteToQueue(player.Stats.CurNotes[i]);
36+
string noteName = noteEntry.Key;
37+
int numNotes = noteEntry.Value;
38+
39+
for (int i = 0; i < numNotes; i++)
40+
{
41+
AddNoteToQueue(CreateNoteFromName(noteName));
42+
}
43+
}
44+
}
45+
46+
// Creates a note from a string of the note's name.
47+
private Note CreateNoteFromName(string noteName)
48+
{
49+
if (noteName == "PlayerBase")
50+
{
51+
return new Note(
52+
"PlayerBase",
53+
null,
54+
1,
55+
(director, note, timing) =>
56+
{
57+
director.Enemy.TakeDamage((int)timing);
58+
}
59+
);
60+
}
61+
if (noteName == "PlayerDouble")
62+
{
63+
return new Note(
64+
"PlayerDouble",
65+
null,
66+
1,
67+
(director, note, timing) =>
68+
{
69+
director.Enemy.TakeDamage(2 * (int)timing);
70+
}
71+
);
3472
}
73+
74+
return null;
3575
}
3676

3777
public void AddNoteToQueue(Note noteType)
@@ -40,7 +80,7 @@ public void AddNoteToQueue(Note noteType)
4080
UpdateQueue();
4181
}
4282

43-
//returns current note, and removes it from the queue
83+
// Returns current note, and removes it from the queue
4484
public Note GetCurrentNote()
4585
{
4686
if (_noteQueue.Count > 0)
@@ -52,6 +92,7 @@ public Note GetCurrentNote()
5292
return null;
5393
}
5494

95+
// Updates the queue's graphics
5596
private void UpdateQueue()
5697
{
5798
if (_noteQueue.Count > 0 && _noteSprites.ContainsKey(_noteQueue.Peek().Name))
@@ -73,7 +114,7 @@ private void UpdateQueue()
73114
}
74115
}
75116

76-
//Fisher-Yates shuffle from: https://stackoverflow.com/a/1262619
117+
// Fisher-Yates shuffle from: https://stackoverflow.com/a/1262619
77118
public void ScrambleQueue()
78119
{
79120
List<Note> tempList = new List<Note>(_noteQueue);

scenes/CustomNotes/NoteQueue.tscn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
[ext_resource type="Texture2D" uid="uid://c3chrsxrulapd" path="res://scenes/CustomNotes/assets/single_note.png" id="3_ewo1s"]
66
[ext_resource type="Texture2D" uid="uid://caw70lr5e1yiq" path="res://scenes/CustomNotes/assets/double_note.png" id="4_7sgy6"]
77

8-
[node name="NoteQueue" type="Control"]
8+
[node name="NoteQueue" type="Control" node_paths=PackedStringArray("_currentNote", "_nextNote")]
99
layout_mode = 3
1010
anchors_preset = 15
1111
anchor_right = 1.0
1212
anchor_bottom = 1.0
1313
grow_horizontal = 2
1414
grow_vertical = 2
1515
script = ExtResource("1_jeqam")
16+
_currentNote = NodePath("CurrentNote")
17+
_nextNote = NodePath("NextNote")
1618

1719
[node name="NoteQueueSprite" type="Sprite2D" parent="."]
1820
texture = ExtResource("2_0p21a")

0 commit comments

Comments
 (0)