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