-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteQueue.cs
More file actions
122 lines (103 loc) · 3.19 KB
/
NoteQueue.cs
File metadata and controls
122 lines (103 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using Godot;
public partial class NoteQueue : Node
{
[Export]
private Sprite2D _currentNote;
[Export]
private Sprite2D _nextNote;
private Queue<Note> _noteQueue = new Queue<Note>();
private Dictionary<string, Texture2D> _noteSprites = new Dictionary<string, Texture2D>();
public override void _Ready()
{
_noteSprites["PlayerBase"] = GD.Load<Texture2D>(
"res://scenes/CustomNotes/assets/single_note.png"
);
_noteSprites["PlayerDouble"] = GD.Load<Texture2D>(
"res://scenes/CustomNotes/assets/double_note.png"
);
LoadQueueFromSave();
ScrambleQueue();
UpdateQueue();
}
// Loads the notes from SaveData.json, and adds them to the queue
public void LoadQueueFromSave()
{
Dictionary<string, int> savedNotes = SaveSystem.LoadNotes();
foreach (var noteEntry in savedNotes)
{
string noteName = noteEntry.Key;
int numNotes = noteEntry.Value;
for (int i = 0; i < numNotes; i++)
{
GD.Print($"Creating note from noteName: {noteName}");
AddNoteToQueue(CreateNoteFromName(noteName));
}
}
}
// Creates a note from a string of the note's name.
private Note CreateNoteFromName(string noteName)
{
if (noteName == "PlayerBase")
return Scribe.NoteDictionary[1];
if (noteName == "PlayerDouble")
return Scribe.NoteDictionary[2];
GD.Print($"Failed to create not from noteName: {noteName}");
return null;
}
public void AddNoteToQueue(Note noteType)
{
_noteQueue.Enqueue(noteType);
UpdateQueue();
}
// Returns current note, and removes it from the queue
public Note GetCurrentNote()
{
if (_noteQueue.Count > 0)
{
return _noteQueue.Peek();
}
return null;
}
public void DequeueNote()
{
_noteQueue.Dequeue();
UpdateQueue();
}
// Updates the queue's graphics
private void UpdateQueue()
{
if (_noteQueue.Count > 0 && _noteSprites.ContainsKey(_noteQueue.Peek().Name))
_currentNote.Texture = _noteSprites[_noteQueue.Peek().Name];
else
_currentNote.Texture = null;
if (_noteQueue.Count > 1)
{
Note[] notes = _noteQueue.ToArray();
if (_noteSprites.ContainsKey(notes[1].Name))
_nextNote.Texture = _noteSprites[notes[1].Name];
else
_nextNote.Texture = null;
}
else
{
_nextNote.Texture = null;
}
}
// Fisher-Yates shuffle from: https://stackoverflow.com/a/1262619
public void ScrambleQueue()
{
List<Note> tempList = new List<Note>(_noteQueue);
Random rng = new Random();
int n = tempList.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
(tempList[k], tempList[n]) = (tempList[n], tempList[k]);
}
_noteQueue = new Queue<Note>(tempList);
}
//TODO: MAYBE? implement saving the notequeue to savedata
}