|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using Godot; |
| 6 | + |
| 7 | +/** |
| 8 | + * @class BattleDirector |
| 9 | + * @brief Higher priority director to manage battle effects. Can directly access managers, which should signal up to Director WIP |
| 10 | + */ |
| 11 | +public partial class BattleDirector : Node2D |
| 12 | +{ |
| 13 | + [Export] |
| 14 | + public ChartManager CM; |
| 15 | + |
| 16 | + [Export] |
| 17 | + public NoteManager NM; |
| 18 | + |
| 19 | + private HealthBar Player; |
| 20 | + private HealthBar Enemy; |
| 21 | + |
| 22 | + private double _timingInterval = .1; //secs |
| 23 | + |
| 24 | + [Signal] |
| 25 | + public delegate void PlayerDamageEventHandler(int damage); |
| 26 | + |
| 27 | + [Signal] |
| 28 | + public delegate void EnemyDamageEventHandler(int damage); |
| 29 | + |
| 30 | + public struct SongData |
| 31 | + { |
| 32 | + public int Bpm; |
| 33 | + public double SongLength; |
| 34 | + public int NumLoops; |
| 35 | + } |
| 36 | + |
| 37 | + private SongData _curSong; |
| 38 | + //Assume queue structure for notes in each lane. |
| 39 | + private readonly Note[][] _laneNotes = new Note[][] |
| 40 | + { |
| 41 | + Array.Empty<Note>(), |
| 42 | + Array.Empty<Note>(), |
| 43 | + Array.Empty<Note>(), |
| 44 | + Array.Empty<Note>(), |
| 45 | + }; |
| 46 | + private Note[] _notes = Array.Empty<Note>(); |
| 47 | + |
| 48 | + public override void _Ready() |
| 49 | + { |
| 50 | + AddExampleNote(); |
| 51 | + CM.PrepChart(_curSong, _notes); |
| 52 | + |
| 53 | + Player = GetNode<HealthBar>("PlayerHP"); |
| 54 | + Enemy = GetNode<HealthBar>("EnemyHP"); |
| 55 | + |
| 56 | + CM.Connect(nameof(NoteManager.NotePressed), new Callable(this, nameof(OnNotePressed))); |
| 57 | + CM.Connect(nameof(NoteManager.NoteReleased), new Callable(this, nameof(OnNoteReleased))); |
| 58 | + } |
| 59 | + |
| 60 | + public override void _Process(double delta) |
| 61 | + { |
| 62 | + TimeKeeper.CurrentTime += delta; |
| 63 | + //Check beats for each lane for passive misses |
| 64 | + double curBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm); |
| 65 | + for (int i = 0; i < _laneNotes.Length; i++) |
| 66 | + { |
| 67 | + if (_laneNotes[i].Length <= 0) continue; |
| 68 | + double beatDif = (curBeat - _laneNotes[i].First().Beat); |
| 69 | + if (beatDif > 1) |
| 70 | + { |
| 71 | + handleTiming((NoteArrow.ArrowType)i, Math.Abs(beatDif)); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + //Creeate dummy song data and notes |
| 77 | + private void AddExampleNote() |
| 78 | + { |
| 79 | + _curSong = new SongData |
| 80 | + { |
| 81 | + Bpm = 120, |
| 82 | + SongLength = 100, |
| 83 | + NumLoops = 5, |
| 84 | + }; |
| 85 | + for (int i = 0; i < 4; i++) |
| 86 | + { |
| 87 | + Note exampleNote = new Note(NoteArrow.ArrowType.Up, i + 3); |
| 88 | + AddNoteToLane(exampleNote); |
| 89 | + } |
| 90 | + for (int i = 0; i < 1; i++) |
| 91 | + { |
| 92 | + Note exampleNote = new Note(NoteArrow.ArrowType.Left, i + 4); |
| 93 | + AddNoteToLane(exampleNote); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private void AddNoteToLane(Note note) |
| 98 | + { |
| 99 | + _notes = _notes.Append(note).ToArray(); |
| 100 | + _laneNotes[(int)note.Type] = _laneNotes[(int)note.Type].Append(note).ToArray(); |
| 101 | + } |
| 102 | + |
| 103 | + private void OnNotePressed(NoteArrow.ArrowType type) |
| 104 | + { |
| 105 | + CheckNoteTiming(type); |
| 106 | + } |
| 107 | + |
| 108 | + private void OnNoteReleased(NoteArrow.ArrowType arrowType) |
| 109 | + { |
| 110 | + } |
| 111 | + |
| 112 | + private void handleTiming(NoteArrow.ArrowType type, double beatDif) |
| 113 | + { |
| 114 | + //Cycle note queue |
| 115 | + _laneNotes[(int)type].First().Beat += CM.BeatsPerLoop; |
| 116 | + _laneNotes[(int)type] = _laneNotes[(int)type] //Credit: Stackoverflow https://stackoverflow.com/questions/49494535/moving-the-first-array-element-to-end-in-c-sharp |
| 117 | + .Skip(1) |
| 118 | + .Concat(_laneNotes[(int)type].Take(1)) |
| 119 | + .ToArray(); //TODO: No stackoverflow code |
| 120 | + //Do timing stuff |
| 121 | + if (beatDif < _timingInterval * 2) |
| 122 | + { |
| 123 | + GD.Print("Perfect"); |
| 124 | + Enemy.TakeDamage(10); |
| 125 | + } |
| 126 | + else if (beatDif < _timingInterval * 4) |
| 127 | + { |
| 128 | + GD.Print("Good"); |
| 129 | + Enemy.TakeDamage(5); |
| 130 | + } |
| 131 | + else if (beatDif < _timingInterval * 6) |
| 132 | + { |
| 133 | + GD.Print("Okay"); |
| 134 | + Enemy.TakeDamage(1); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + GD.Print("Miss"); |
| 139 | + Player.TakeDamage(10); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void CheckNoteTiming(NoteArrow.ArrowType type) |
| 144 | + { |
| 145 | + double curBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm); |
| 146 | + if (_laneNotes[(int)type].Length == 0) |
| 147 | + return; |
| 148 | + double beatDif = Math.Abs(curBeat - _laneNotes[(int)type].First().Beat); |
| 149 | + if (beatDif > 1) |
| 150 | + return; |
| 151 | + GD.Print("Note Hit. Dif: " + beatDif); |
| 152 | + CM.HandleNote(type); |
| 153 | + handleTiming(type, beatDif); |
| 154 | + } |
| 155 | +} |
0 commit comments