|
| 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 | + #region Declarations |
| 14 | + private HealthBar Player; |
| 15 | + private HealthBar Enemy; |
| 16 | + |
| 17 | + [Export] |
| 18 | + private ChartManager CM; |
| 19 | + |
| 20 | + [Export] |
| 21 | + private InputHandler IH; |
| 22 | + |
| 23 | + [Export] |
| 24 | + private NotePlacementBar NotePlacementBar; |
| 25 | + |
| 26 | + [Export] |
| 27 | + private AudioStreamPlayer Audio; |
| 28 | + |
| 29 | + private double _timingInterval = .1; //secs |
| 30 | + |
| 31 | + [Signal] |
| 32 | + public delegate void PlayerDamageEventHandler(int damage); |
| 33 | + |
| 34 | + [Signal] |
| 35 | + public delegate void EnemyDamageEventHandler(int damage); |
| 36 | + |
| 37 | + private SongData _curSong; |
| 38 | + |
| 39 | + public struct SongData |
| 40 | + { |
| 41 | + public int Bpm; |
| 42 | + public double SongLength; |
| 43 | + public int NumLoops; |
| 44 | + } |
| 45 | + #endregion |
| 46 | + |
| 47 | + #region Note Handling |
| 48 | + //Assume queue structure for notes in each lane. |
| 49 | + //Can eventually make this its own structure |
| 50 | + private NoteArrow[][] _laneData = Array.Empty<NoteArrow[]>(); |
| 51 | + private int[] _laneLastBeat = new int[] |
| 52 | + { //Temporary (hopefully) measure to bridge from note queue structure to ordered array |
| 53 | + 0, |
| 54 | + 0, |
| 55 | + 0, |
| 56 | + 0, |
| 57 | + }; |
| 58 | + private Note[] _notes = Array.Empty<Note>(); |
| 59 | + |
| 60 | + //Returns first note of lane without modifying lane data |
| 61 | + private Note GetNoteAt(NoteArrow.ArrowType dir, int beat) |
| 62 | + { |
| 63 | + return GetNote(_laneData[(int)dir][beat]); |
| 64 | + } |
| 65 | + |
| 66 | + //Get note of a note arrow |
| 67 | + private Note GetNote(NoteArrow arrow) |
| 68 | + { |
| 69 | + return _notes[arrow.NoteIdx]; |
| 70 | + } |
| 71 | + |
| 72 | + private bool AddNoteToLane(Note note, bool isActive = true) |
| 73 | + { |
| 74 | + note.Beat %= CM.BeatsPerLoop; |
| 75 | + //Don't add dupe notes |
| 76 | + if (note.Beat == 0 || _notes.Any(nt => nt.Type == note.Type && nt.Beat == note.Beat)) |
| 77 | + { |
| 78 | + return false; //Beat at 0 is too messy. |
| 79 | + } |
| 80 | + _notes = _notes.Append(note).ToArray(); |
| 81 | + //Get noteArrow from CM |
| 82 | + var arrow = CM.AddArrowToLane(note, _notes.Length - 1); |
| 83 | + arrow.IsActive = isActive; |
| 84 | + _laneData[(int)note.Type][note.Beat] = arrow; |
| 85 | + return true; |
| 86 | + } |
| 87 | + #endregion |
| 88 | + |
| 89 | + //Creeate dummy notes |
| 90 | + private void AddExampleNotes() |
| 91 | + { |
| 92 | + GD.Print(CM.BeatsPerLoop); |
| 93 | + for (int i = 1; i < 15; i++) |
| 94 | + { |
| 95 | + Note exampleNote = new Note(NoteArrow.ArrowType.Up, i * 4); |
| 96 | + AddNoteToLane(exampleNote); |
| 97 | + } |
| 98 | + for (int i = 1; i < 15; i++) |
| 99 | + { |
| 100 | + Note exampleNote = new Note(NoteArrow.ArrowType.Left, 4 * i + 1); |
| 101 | + AddNoteToLane(exampleNote); |
| 102 | + } |
| 103 | + for (int i = 0; i < 10; i++) |
| 104 | + { |
| 105 | + Note exampleNote = new Note(NoteArrow.ArrowType.Right, 3 * i + 32); |
| 106 | + AddNoteToLane(exampleNote); |
| 107 | + } |
| 108 | + for (int i = 0; i < 3; i++) |
| 109 | + { |
| 110 | + Note exampleNote = new Note(NoteArrow.ArrowType.Down, 8 * i + 16); |
| 111 | + AddNoteToLane(exampleNote); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public override void _Ready() |
| 116 | + { |
| 117 | + _curSong = new SongData |
| 118 | + { |
| 119 | + Bpm = 120, |
| 120 | + SongLength = Audio.Stream.GetLength(), |
| 121 | + NumLoops = 5, |
| 122 | + }; |
| 123 | + |
| 124 | + var timer = GetTree().CreateTimer(AudioServer.GetTimeToNextMix()); |
| 125 | + timer.Timeout += Begin; |
| 126 | + } |
| 127 | + |
| 128 | + private void Begin() |
| 129 | + { |
| 130 | + CM.PrepChart(_curSong); |
| 131 | + _laneData = new NoteArrow[][] |
| 132 | + { |
| 133 | + new NoteArrow[CM.BeatsPerLoop], |
| 134 | + new NoteArrow[CM.BeatsPerLoop], |
| 135 | + new NoteArrow[CM.BeatsPerLoop], |
| 136 | + new NoteArrow[CM.BeatsPerLoop], |
| 137 | + }; |
| 138 | + AddExampleNotes(); |
| 139 | + |
| 140 | + Player = GetNode<HealthBar>("PlayerHP"); |
| 141 | + Player.GetNode<Sprite2D>("Sprite2D").Scale *= .5f; //TEMP |
| 142 | + Player.GetNode<Sprite2D>("Sprite2D").Position += Vector2.Down * 30; //TEMP |
| 143 | + Enemy = GetNode<HealthBar>("EnemyHP"); |
| 144 | + |
| 145 | + //TEMP |
| 146 | + var enemTween = CreateTween(); |
| 147 | + enemTween |
| 148 | + .TweenProperty(Enemy.GetNode<Sprite2D>("Sprite2D"), "position", Vector2.Down * 5, 1f) |
| 149 | + .AsRelative(); |
| 150 | + enemTween |
| 151 | + .TweenProperty(Enemy.GetNode<Sprite2D>("Sprite2D"), "position", Vector2.Up * 5, 1f) |
| 152 | + .AsRelative(); |
| 153 | + enemTween.SetTrans(Tween.TransitionType.Spring); |
| 154 | + enemTween.SetEase(Tween.EaseType.In); |
| 155 | + enemTween.SetLoops(); |
| 156 | + enemTween.Play(); |
| 157 | + |
| 158 | + CM.Connect(nameof(InputHandler.NotePressed), new Callable(this, nameof(OnNotePressed))); |
| 159 | + CM.Connect(nameof(InputHandler.NoteReleased), new Callable(this, nameof(OnNoteReleased))); |
| 160 | + |
| 161 | + Audio.Play(); |
| 162 | + } |
| 163 | + |
| 164 | + public override void _Process(double delta) |
| 165 | + { |
| 166 | + TimeKeeper.CurrentTime = Audio.GetPlaybackPosition(); |
| 167 | + CheckMiss(); |
| 168 | + } |
| 169 | + |
| 170 | + #region Input&Timing |
| 171 | + private void OnNotePressed(NoteArrow.ArrowType type) |
| 172 | + { |
| 173 | + CheckNoteTiming(type); |
| 174 | + } |
| 175 | + |
| 176 | + private void OnNoteReleased(NoteArrow.ArrowType arrowType) { } |
| 177 | + |
| 178 | + //Check all lanes for misses from missed inputs |
| 179 | + private void CheckMiss() |
| 180 | + { |
| 181 | + //On current beat, if prev beat is active and not inputted |
| 182 | + double realBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm) % CM.BeatsPerLoop; |
| 183 | + for (int i = 0; i < _laneData.Length; i++) |
| 184 | + { |
| 185 | + if ( |
| 186 | + _laneLastBeat[i] < Math.Floor(realBeat) |
| 187 | + || (_laneLastBeat[i] == CM.BeatsPerLoop - 1 && Math.Floor(realBeat) == 0) |
| 188 | + ) |
| 189 | + { //If above, a note has been missed |
| 190 | + //GD.Print("Last beat " + _laneLastBeat[i]); |
| 191 | + if ( |
| 192 | + _laneData[i][_laneLastBeat[i]] == null |
| 193 | + || !_laneData[i][_laneLastBeat[i]].IsActive |
| 194 | + ) |
| 195 | + { |
| 196 | + _laneLastBeat[i] = (_laneLastBeat[i] + 1) % CM.BeatsPerLoop; |
| 197 | + continue; |
| 198 | + } |
| 199 | + //Note exists and has been missed |
| 200 | + _laneData[i][_laneLastBeat[i]].NoteHit(); |
| 201 | + HandleTiming((NoteArrow.ArrowType)i, 1); |
| 202 | + _laneLastBeat[i] = (_laneLastBeat[i] + 1) % CM.BeatsPerLoop; |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private void CheckNoteTiming(NoteArrow.ArrowType type) |
| 208 | + { |
| 209 | + double realBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm) % CM.BeatsPerLoop; |
| 210 | + int curBeat = (int)Math.Round(realBeat); |
| 211 | + GD.Print("Cur beat " + curBeat + "Real: " + realBeat.ToString("#.###")); |
| 212 | + if ( |
| 213 | + _laneData[(int)type][curBeat % CM.BeatsPerLoop] == null |
| 214 | + || !_laneData[(int)type][curBeat % CM.BeatsPerLoop].IsActive |
| 215 | + ) |
| 216 | + { |
| 217 | + _laneLastBeat[(int)type] = (curBeat) % CM.BeatsPerLoop; |
| 218 | + PlayerAddNote(type, curBeat); |
| 219 | + return; |
| 220 | + } |
| 221 | + double beatDif = Math.Abs(realBeat - curBeat); |
| 222 | + _laneData[(int)type][curBeat % CM.BeatsPerLoop].NoteHit(); |
| 223 | + _laneLastBeat[(int)type] = (curBeat) % CM.BeatsPerLoop; |
| 224 | + HandleTiming(type, beatDif); |
| 225 | + } |
| 226 | + |
| 227 | + private void HandleTiming(NoteArrow.ArrowType type, double beatDif) |
| 228 | + { |
| 229 | + if (beatDif < _timingInterval * 1) |
| 230 | + { |
| 231 | + GD.Print("Perfect"); |
| 232 | + Enemy.TakeDamage(3); |
| 233 | + NotePlacementBar.HitNote(); |
| 234 | + NotePlacementBar.ComboText("Perfect!"); |
| 235 | + } |
| 236 | + else if (beatDif < _timingInterval * 2) |
| 237 | + { |
| 238 | + GD.Print("Good"); |
| 239 | + Enemy.TakeDamage(1); |
| 240 | + NotePlacementBar.HitNote(); |
| 241 | + NotePlacementBar.ComboText("Good"); |
| 242 | + } |
| 243 | + else if (beatDif < _timingInterval * 3) |
| 244 | + { |
| 245 | + GD.Print("Ok"); |
| 246 | + Player.TakeDamage(1); |
| 247 | + NotePlacementBar.HitNote(); |
| 248 | + NotePlacementBar.ComboText("Okay"); |
| 249 | + } |
| 250 | + else |
| 251 | + { |
| 252 | + GD.Print("Miss"); |
| 253 | + Player.TakeDamage(2); |
| 254 | + NotePlacementBar.MissNote(); |
| 255 | + NotePlacementBar.ComboText("Miss"); |
| 256 | + } |
| 257 | + } |
| 258 | + #endregion |
| 259 | + |
| 260 | + private void PlayerAddNote(NoteArrow.ArrowType type, int beat) |
| 261 | + { |
| 262 | + // can also add some sort of keybind here to also have pressed |
| 263 | + // in case the user just presses the note too early and spawns a note |
| 264 | + GD.Print( |
| 265 | + $"Player trying to place {type} typed note at beat: " |
| 266 | + + beat |
| 267 | + + " Verdict: " |
| 268 | + + NotePlacementBar.CanPlaceNote() |
| 269 | + ); |
| 270 | + if (NotePlacementBar.CanPlaceNote()) |
| 271 | + { |
| 272 | + Note exampleNote = new Note(type, beat % CM.BeatsPerLoop); |
| 273 | + if (AddNoteToLane(exampleNote, false)) |
| 274 | + NotePlacementBar.PlacedNote(); |
| 275 | + } |
| 276 | + } |
| 277 | +} |
0 commit comments