22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . Linq ;
5+ using FunkEngine ;
56using Godot ;
67
78/**
@@ -26,13 +27,7 @@ public partial class BattleDirector : Node2D
2627 [ Export ]
2728 private AudioStreamPlayer Audio ;
2829
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 ) ;
30+ private double _timingInterval = .1 ; //secs, maybe make somewhat note dependent
3631
3732 private SongData _curSong ;
3833
@@ -58,7 +53,7 @@ public struct SongData
5853 private Note [ ] _notes = Array . Empty < Note > ( ) ;
5954
6055 //Returns first note of lane without modifying lane data
61- private Note GetNoteAt ( NoteArrow . ArrowType dir , int beat )
56+ private Note GetNoteAt ( ArrowType dir , int beat )
6257 {
6358 return GetNote ( _laneData [ ( int ) dir ] [ beat ] ) ;
6459 }
@@ -69,19 +64,23 @@ private Note GetNote(NoteArrow arrow)
6964 return _notes [ arrow . NoteIdx ] ;
7065 }
7166
72- private bool AddNoteToLane ( Note note , bool isActive = true )
67+ private bool IsNoteActive ( ArrowType type , int beat )
68+ {
69+ return _laneData [ ( int ) type ] [ beat ] != null && _laneData [ ( int ) type ] [ beat ] . IsActive ;
70+ }
71+
72+ private bool AddNoteToLane ( ArrowType type , int beat , bool isActive = true )
7373 {
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 ) )
74+ beat %= CM . BeatsPerLoop ;
75+ //Don't add dupe notes //Beat at 0 is too messy.
76+ if ( beat == 0 || _laneData [ ( int ) type ] [ beat ] != null )
7777 {
78- return false ; //Beat at 0 is too messy.
78+ return false ;
7979 }
80- _notes = _notes . Append ( note ) . ToArray ( ) ;
8180 //Get noteArrow from CM
82- var arrow = CM . AddArrowToLane ( note , _notes . Length - 1 ) ;
81+ var arrow = CM . AddArrowToLane ( type , beat , _notes . Length - 1 ) ;
8382 arrow . IsActive = isActive ;
84- _laneData [ ( int ) note . Type ] [ note . Beat ] = arrow ;
83+ _laneData [ ( int ) type ] [ beat ] = arrow ;
8584 return true ;
8685 }
8786 #endregion
@@ -92,23 +91,19 @@ private void AddExampleNotes()
9291 GD . Print ( CM . BeatsPerLoop ) ;
9392 for ( int i = 1 ; i < 15 ; i ++ )
9493 {
95- Note exampleNote = new Note ( NoteArrow . ArrowType . Up , i * 4 ) ;
96- AddNoteToLane ( exampleNote ) ;
94+ AddNoteToLane ( ArrowType . Up , i * 4 ) ;
9795 }
9896 for ( int i = 1 ; i < 15 ; i ++ )
9997 {
100- Note exampleNote = new Note ( NoteArrow . ArrowType . Left , 4 * i + 1 ) ;
101- AddNoteToLane ( exampleNote ) ;
98+ AddNoteToLane ( ArrowType . Left , 4 * i + 1 ) ;
10299 }
103100 for ( int i = 0 ; i < 10 ; i ++ )
104101 {
105- Note exampleNote = new Note ( NoteArrow . ArrowType . Right , 3 * i + 32 ) ;
106- AddNoteToLane ( exampleNote ) ;
102+ AddNoteToLane ( ArrowType . Right , 3 * i + 32 ) ;
107103 }
108104 for ( int i = 0 ; i < 3 ; i ++ )
109105 {
110- Note exampleNote = new Note ( NoteArrow . ArrowType . Down , 8 * i + 16 ) ;
111- AddNoteToLane ( exampleNote ) ;
106+ AddNoteToLane ( ArrowType . Down , 8 * i + 16 ) ;
112107 }
113108 }
114109
@@ -120,13 +115,16 @@ public override void _Ready()
120115 SongLength = Audio . Stream . GetLength ( ) ,
121116 NumLoops = 5 ,
122117 } ;
123-
118+
124119 Player = new Puppet_Template ( ) ;
125120 AddChild ( Player ) ;
126- Player . Init ( GD . Load < Texture2D > ( "res://scenes/BattleDirector/assets/Character1.png" ) , "Player" ) ;
121+ Player . Init (
122+ GD . Load < Texture2D > ( "res://scenes/BattleDirector/assets/Character1.png" ) ,
123+ "Player"
124+ ) ;
127125 Player . SetPosition ( new Vector2 ( 80 , 0 ) ) ;
128126 Player . Sprite . Position += Vector2 . Down * 30 ; //TEMP
129-
127+
130128 Enemy = new Puppet_Template ( ) ;
131129 Enemy . SetPosition ( new Vector2 ( 400 , 0 ) ) ;
132130 AddChild ( Enemy ) ;
@@ -149,15 +147,11 @@ private void Begin()
149147 new NoteArrow [ CM . BeatsPerLoop ] ,
150148 } ;
151149 AddExampleNotes ( ) ;
152-
150+
153151 //TEMP
154152 var enemTween = CreateTween ( ) ;
155- enemTween
156- . TweenProperty ( Enemy . Sprite , "position" , Vector2 . Down * 5 , 1f )
157- . AsRelative ( ) ;
158- enemTween
159- . TweenProperty ( Enemy . Sprite , "position" , Vector2 . Up * 5 , 1f )
160- . AsRelative ( ) ;
153+ enemTween . TweenProperty ( Enemy . Sprite , "position" , Vector2 . Down * 5 , 1f ) . AsRelative ( ) ;
154+ enemTween . TweenProperty ( Enemy . Sprite , "position" , Vector2 . Up * 5 , 1f ) . AsRelative ( ) ;
161155 enemTween . SetTrans ( Tween . TransitionType . Spring ) ;
162156 enemTween . SetEase ( Tween . EaseType . In ) ;
163157 enemTween . SetLoops ( ) ;
@@ -176,12 +170,12 @@ public override void _Process(double delta)
176170 }
177171
178172 #region Input&Timing
179- private void OnNotePressed ( NoteArrow . ArrowType type )
173+ private void OnNotePressed ( ArrowType type )
180174 {
181175 CheckNoteTiming ( type ) ;
182176 }
183177
184- private void OnNoteReleased ( NoteArrow . ArrowType arrowType ) { }
178+ private void OnNoteReleased ( ArrowType arrowType ) { }
185179
186180 //Check all lanes for misses from missed inputs
187181 private void CheckMiss ( )
@@ -190,49 +184,39 @@ private void CheckMiss()
190184 double realBeat = TimeKeeper . CurrentTime / ( 60 / ( double ) _curSong . Bpm ) % CM . BeatsPerLoop ;
191185 for ( int i = 0 ; i < _laneData . Length ; i ++ )
192186 {
193- if (
194- _laneLastBeat [ i ] < Math . Floor ( realBeat )
195- || ( _laneLastBeat [ i ] == CM . BeatsPerLoop - 1 && Math . Floor ( realBeat ) == 0 )
196- )
197- { //If above, a note has been missed
198- //GD.Print("Last beat " + _laneLastBeat[i]);
199- if (
200- _laneData [ i ] [ _laneLastBeat [ i ] ] == null
201- || ! _laneData [ i ] [ _laneLastBeat [ i ] ] . IsActive
202- )
203- {
204- _laneLastBeat [ i ] = ( _laneLastBeat [ i ] + 1 ) % CM . BeatsPerLoop ;
205- continue ;
206- }
207- //Note exists and has been missed
208- _laneData [ i ] [ _laneLastBeat [ i ] ] . NoteHit ( ) ;
209- HandleTiming ( ( NoteArrow . ArrowType ) i , 1 ) ;
187+ if ( ! ( _laneLastBeat [ i ] < Math . Floor ( realBeat ) ) )
188+ continue ;
189+ if ( ! IsNoteActive ( ( ArrowType ) i , _laneLastBeat [ i ] ) )
190+ {
210191 _laneLastBeat [ i ] = ( _laneLastBeat [ i ] + 1 ) % CM . BeatsPerLoop ;
192+ continue ;
211193 }
194+ //Note exists and has been missed
195+ _laneData [ i ] [ _laneLastBeat [ i ] ] . NoteHit ( ) ;
196+ HandleTiming ( 1 ) ;
197+ _laneLastBeat [ i ] = ( _laneLastBeat [ i ] + 1 ) % CM . BeatsPerLoop ;
212198 }
213199 }
214200
215- private void CheckNoteTiming ( NoteArrow . ArrowType type )
201+ private void CheckNoteTiming ( ArrowType type )
216202 {
217203 double realBeat = TimeKeeper . CurrentTime / ( 60 / ( double ) _curSong . Bpm ) % CM . BeatsPerLoop ;
218204 int curBeat = ( int ) Math . Round ( realBeat ) ;
219205 GD . Print ( "Cur beat " + curBeat + "Real: " + realBeat . ToString ( "#.###" ) ) ;
220- if (
221- _laneData [ ( int ) type ] [ curBeat % CM . BeatsPerLoop ] == null
222- || ! _laneData [ ( int ) type ] [ curBeat % CM . BeatsPerLoop ] . IsActive
223- )
206+ if ( _laneData [ ( int ) type ] [ curBeat % CM . BeatsPerLoop ] == null )
224207 {
225- _laneLastBeat [ ( int ) type ] = ( curBeat ) % CM . BeatsPerLoop ;
226208 PlayerAddNote ( type , curBeat ) ;
227209 return ;
228210 }
211+ if ( ! _laneData [ ( int ) type ] [ curBeat % CM . BeatsPerLoop ] . IsActive )
212+ return ;
229213 double beatDif = Math . Abs ( realBeat - curBeat ) ;
230214 _laneData [ ( int ) type ] [ curBeat % CM . BeatsPerLoop ] . NoteHit ( ) ;
231215 _laneLastBeat [ ( int ) type ] = ( curBeat ) % CM . BeatsPerLoop ;
232- HandleTiming ( type , beatDif ) ;
216+ HandleTiming ( beatDif ) ;
233217 }
234218
235- private void HandleTiming ( NoteArrow . ArrowType type , double beatDif )
219+ private void HandleTiming ( double beatDif )
236220 {
237221 if ( beatDif < _timingInterval * 1 )
238222 {
@@ -265,21 +249,16 @@ private void HandleTiming(NoteArrow.ArrowType type, double beatDif)
265249 }
266250 #endregion
267251
268- private void PlayerAddNote ( NoteArrow . ArrowType type , int beat )
252+ private void PlayerAddNote ( ArrowType type , int beat )
269253 {
270254 // can also add some sort of keybind here to also have pressed
271255 // in case the user just presses the note too early and spawns a note
272- GD . Print (
273- $ "Player trying to place { type } typed note at beat: "
274- + beat
275- + " Verdict: "
276- + NotePlacementBar . CanPlaceNote ( )
277- ) ;
256+ GD . Print ( $ "Player trying to place { type } typed note at beat: " + beat ) ;
278257 if ( NotePlacementBar . CanPlaceNote ( ) )
279258 {
280- Note exampleNote = new Note ( type , beat % CM . BeatsPerLoop ) ;
281- if ( AddNoteToLane ( exampleNote , false ) )
259+ if ( AddNoteToLane ( type , beat % CM . BeatsPerLoop , false ) )
282260 NotePlacementBar . PlacedNote ( ) ;
261+ GD . Print ( "Note Placed." ) ;
283262 }
284263 }
285264}
0 commit comments