1010 */
1111public partial class BattleDirector : Node2D
1212{
13+ private HealthBar Player ;
14+ private HealthBar Enemy ;
15+
1316 [ Export ]
1417 public ChartManager CM ;
1518
1619 [ Export ]
17- public NoteManager NM ;
18-
19- private HealthBar Player ;
20- private HealthBar Enemy ;
20+ public InputHandler IH ;
2121
2222 private NotePlacementBar NotePlacementBar ;
2323
@@ -29,64 +29,77 @@ public partial class BattleDirector : Node2D
2929 [ Signal ]
3030 public delegate void EnemyDamageEventHandler ( int damage ) ;
3131
32+ private SongData _curSong ;
33+
3234 public struct SongData
3335 {
3436 public int Bpm ;
3537 public double SongLength ;
3638 public int NumLoops ;
3739 }
3840
39- private SongData _curSong ;
40-
4141 //Assume queue structure for notes in each lane.
42- private readonly Note [ ] [ ] _laneNotes = new Note [ ] [ ]
42+ //Can eventually make this its own structure
43+ private readonly NoteArrow [ ] [ ] _laneData = new NoteArrow [ ] [ ]
4344 {
44- Array . Empty < Note > ( ) ,
45- Array . Empty < Note > ( ) ,
46- Array . Empty < Note > ( ) ,
47- Array . Empty < Note > ( ) ,
45+ Array . Empty < NoteArrow > ( ) ,
46+ Array . Empty < NoteArrow > ( ) ,
47+ Array . Empty < NoteArrow > ( ) ,
48+ Array . Empty < NoteArrow > ( ) ,
4849 } ;
4950 private Note [ ] _notes = Array . Empty < Note > ( ) ;
5051
52+ //Cycles lane of dir and returns the, initially, first note
53+ private Note CycleNote ( NoteArrow . ArrowType dir )
54+ {
55+ var note = GetFirstNote ( dir ) ;
56+ _laneData [ ( int ) dir ] = _laneData [ ( int ) dir ] //Credit: Stackoverflow https://stackoverflow.com/questions/49494535/moving-the-first-array-element-to-end-in-c-sharp
57+ . Skip ( 1 )
58+ . Concat ( _laneData [ ( int ) dir ] . Take ( 1 ) )
59+ . ToArray ( ) ;
60+ return note ;
61+ }
62+
63+ //Returns first note of lane without modifying lane data
64+ private Note GetFirstNote ( NoteArrow . ArrowType dir )
65+ {
66+ return GetNote ( _laneData [ ( int ) dir ] . First ( ) ) ;
67+ }
68+
69+ //Get note of a note arrow
70+ private Note GetNote ( NoteArrow arrow )
71+ {
72+ return _notes [ arrow . NoteIdx ] ;
73+ }
74+
5175 public override void _Ready ( )
5276 {
77+ _curSong = new SongData
78+ {
79+ Bpm = 120 ,
80+ SongLength = 100 ,
81+ NumLoops = 5 ,
82+ } ;
83+ CM . PrepChart ( _curSong ) ;
5384 AddExampleNote ( ) ;
54- CM . PrepChart ( _curSong , _notes ) ;
5585
5686 Player = GetNode < HealthBar > ( "PlayerHP" ) ;
5787 Enemy = GetNode < HealthBar > ( "EnemyHP" ) ;
5888 NotePlacementBar = GetNode < NotePlacementBar > ( "NotePlacementBar" ) ;
5989
60- CM . Connect ( nameof ( NoteManager . NotePressed ) , new Callable ( this , nameof ( OnNotePressed ) ) ) ;
61- CM . Connect ( nameof ( NoteManager . NoteReleased ) , new Callable ( this , nameof ( OnNoteReleased ) ) ) ;
90+ CM . Connect ( nameof ( InputHandler . NotePressed ) , new Callable ( this , nameof ( OnNotePressed ) ) ) ;
91+ CM . Connect ( nameof ( InputHandler . NoteReleased ) , new Callable ( this , nameof ( OnNoteReleased ) ) ) ;
6292 }
6393
6494 public override void _Process ( double delta )
6595 {
6696 TimeKeeper . CurrentTime += delta ;
67- //Check beats for each lane for passive misses
68- double curBeat = TimeKeeper . CurrentTime / ( 60 / ( double ) _curSong . Bpm ) ;
69- for ( int i = 0 ; i < _laneNotes . Length ; i ++ )
70- {
71- if ( _laneNotes [ i ] . Length <= 0 )
72- continue ;
73- double beatDif = ( curBeat - _laneNotes [ i ] . First ( ) . Beat ) ;
74- if ( beatDif > 1 )
75- {
76- handleTiming ( ( NoteArrow . ArrowType ) i , Math . Abs ( beatDif ) ) ;
77- }
78- }
97+ CheckMiss ( ) ;
7998 }
8099
81- //Creeate dummy song data and notes
100+ //Creeate dummy notes
82101 private void AddExampleNote ( )
83102 {
84- _curSong = new SongData
85- {
86- Bpm = 120 ,
87- SongLength = 100 ,
88- NumLoops = 5 ,
89- } ;
90103 for ( int i = 0 ; i < 4 ; i ++ )
91104 {
92105 Note exampleNote = new Note ( NoteArrow . ArrowType . Up , i + 3 ) ;
@@ -99,10 +112,18 @@ private void AddExampleNote()
99112 }
100113 }
101114
102- private void AddNoteToLane ( Note note )
115+ private bool AddNoteToLane ( Note note )
103116 {
117+ //Don't add dupe notes
118+ if ( _notes . Any ( nt => nt . Type == note . Type && nt . Beat == note . Beat ) )
119+ {
120+ return false ;
121+ }
104122 _notes = _notes . Append ( note ) . ToArray ( ) ;
105- _laneNotes [ ( int ) note . Type ] = _laneNotes [ ( int ) note . Type ] . Append ( note ) . ToArray ( ) ;
123+ //Get noteArrow from CM
124+ var arrow = CM . AddArrowToLane ( note , _notes . Length - 1 ) ;
125+ _laneData [ ( int ) note . Type ] = _laneData [ ( int ) note . Type ] . Append ( arrow ) . ToArray ( ) ;
126+ return true ;
106127 }
107128
108129 private void OnNotePressed ( NoteArrow . ArrowType type )
@@ -112,14 +133,26 @@ private void OnNotePressed(NoteArrow.ArrowType type)
112133
113134 private void OnNoteReleased ( NoteArrow . ArrowType arrowType ) { }
114135
115- private void handleTiming ( NoteArrow . ArrowType type , double beatDif )
136+ //Check all lanes for misses from missed inputs
137+ private void CheckMiss ( )
138+ {
139+ double curBeat = TimeKeeper . CurrentTime / ( 60 / ( double ) _curSong . Bpm ) ;
140+ for ( int i = 0 ; i < _laneData . Length ; i ++ )
141+ {
142+ if ( _laneData [ i ] . Length <= 0 )
143+ continue ;
144+ double beatDif = ( curBeat - GetFirstNote ( ( NoteArrow . ArrowType ) i ) . Beat ) ;
145+ if ( beatDif > 1 )
146+ {
147+ HandleTiming ( ( NoteArrow . ArrowType ) i , Math . Abs ( beatDif ) ) ;
148+ }
149+ }
150+ }
151+
152+ private void HandleTiming ( NoteArrow . ArrowType type , double beatDif )
116153 {
117154 //Cycle note queue
118- _laneNotes [ ( int ) type ] . First ( ) . Beat += CM . BeatsPerLoop ;
119- _laneNotes [ ( int ) type ] = _laneNotes [ ( int ) type ] //Credit: Stackoverflow https://stackoverflow.com/questions/49494535/moving-the-first-array-element-to-end-in-c-sharp
120- . Skip ( 1 )
121- . Concat ( _laneNotes [ ( int ) type ] . Take ( 1 ) )
122- . ToArray ( ) ; //TODO: No stackoverflow code
155+ CycleNote ( type ) . Beat += CM . BeatsPerLoop ;
123156 //Do timing stuff
124157 if ( beatDif < _timingInterval * 2 )
125158 {
@@ -150,20 +183,20 @@ private void handleTiming(NoteArrow.ArrowType type, double beatDif)
150183 private void CheckNoteTiming ( NoteArrow . ArrowType type )
151184 {
152185 double curBeat = TimeKeeper . CurrentTime / ( 60 / ( double ) _curSong . Bpm ) ;
153- if ( _laneNotes [ ( int ) type ] . Length == 0 )
186+ if ( _laneData [ ( int ) type ] . Length == 0 )
154187 {
155188 PlayerAddNote ( type , ( int ) curBeat ) ;
156189 return ;
157190 }
158- double beatDif = Math . Abs ( curBeat - _laneNotes [ ( int ) type ] . First ( ) . Beat ) ;
191+ double beatDif = Math . Abs ( curBeat - GetFirstNote ( type ) . Beat ) ;
159192 if ( beatDif > 1 )
160193 {
161194 PlayerAddNote ( type , ( int ) curBeat ) ;
162195 return ;
163196 }
164197 GD . Print ( "Note Hit. Dif: " + beatDif ) ;
165- CM . HandleNote ( type ) ;
166- handleTiming ( type , beatDif ) ;
198+ _laneData [ ( int ) type ] . First ( ) . NoteHit ( ) ;
199+ HandleTiming ( type , beatDif ) ;
167200 }
168201
169202 private void PlayerAddNote ( NoteArrow . ArrowType type , int beat )
@@ -181,8 +214,9 @@ private void PlayerAddNote(NoteArrow.ArrowType type, int beat)
181214 ) ;
182215 if ( NotePlacementBar . CanPlaceNote ( ) )
183216 {
184- CM . CreateNote ( type , beat ) ;
185- NotePlacementBar . PlacedNote ( ) ;
217+ Note exampleNote = new Note ( type , beat % CM . BeatsPerLoop ) ;
218+ if ( AddNoteToLane ( exampleNote ) )
219+ NotePlacementBar . PlacedNote ( ) ;
186220 }
187221 }
188222}
0 commit comments