Skip to content

Commit 1f507de

Browse files
committed
Consolidated into single lane structure
Renamed node manager for clarity Streamlined and consolidated adding notes into _laneData, to guarantee synced queues. NoteArrows hold noteIdx for eventual roguelike elements
1 parent 993b9c1 commit 1f507de

9 files changed

Lines changed: 109 additions & 114 deletions

File tree

Funk Engine.sln.DotSettings.user

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003Fgamef_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F6354a7b35d7821629924d3676acd7e67a6f7f94343e0e66ec439aa2bd6ed5_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
2+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003Fgamef_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F6354a7b35d7821629924d3676acd7e67a6f7f94343e0e66ec439aa2bd6ed5_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
3+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F6354a7b35d7821629924d3676acd7e67a6f7f94343e0e66ec439aa2bd6ed5_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>

scenes/BattleDirector/BattleDirector.cs

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
*/
1111
public 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
}

scenes/BattleDirector/test_battle_scene.tscn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
[ext_resource type="PackedScene" uid="uid://bgomxovxs7sr8" path="res://scenes/BattleDirector/HealthBar.tscn" id="3_pp0u0"]
66
[ext_resource type="PackedScene" uid="uid://duhiilcv4tat3" path="res://scenes/BattleDirector/NotePlacementBar.tscn" id="4_y2yh3"]
77

8-
[node name="ProtoBattleDirector" type="Node2D" node_paths=PackedStringArray("CM")]
8+
[node name="ProtoBattleDirector" type="Node2D" node_paths=PackedStringArray("CM", "IH")]
99
script = ExtResource("1_cwqqr")
1010
CM = NodePath("SubViewport")
11+
IH = NodePath("SubViewport/SubViewport/noteManager")
1112

1213
[node name="SubViewport" parent="." instance=ExtResource("2_cupb3")]
1314
offset_left = 80.0

scenes/ChartViewport/ChartManager.cs

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public partial class ChartManager : SubViewportContainer
1111
{
1212
//Nodes from scene
1313
[Export]
14-
public NoteManager NM;
14+
public InputHandler IH;
1515

1616
[Export]
1717
public CanvasGroup ChartLoopables;
@@ -24,21 +24,11 @@ public partial class ChartManager : SubViewportContainer
2424

2525
//Arbitrary vars, play with these
2626
private double ChartLength = 2400; //Might move this to be song specific?
27-
2827
private double _loopLen; //secs
2928
public int BeatsPerLoop;
3029

31-
private NoteArrow[][] _currentArrows = new NoteArrow[][]
32-
{
33-
Array.Empty<NoteArrow>(),
34-
Array.Empty<NoteArrow>(),
35-
Array.Empty<NoteArrow>(),
36-
Array.Empty<NoteArrow>(),
37-
};
38-
3930
private void InitBackgrounds()
4031
{
41-
//TODO: Get better visual for BG's
4232
int i = 0;
4333
foreach (Node child in ChartLoopables.GetChildren())
4434
{
@@ -51,21 +41,7 @@ private void InitBackgrounds()
5141
}
5242
}
5343

54-
private void InitNotes(Note[] notes)
55-
{
56-
foreach (Note noteData in notes)
57-
{
58-
if (noteData != null)
59-
CreateNote(noteData.Type, noteData.Beat);
60-
}
61-
foreach (Note noteData in notes) //Temporary solution
62-
{
63-
if (noteData != null)
64-
CreateNote(noteData.Type, noteData.Beat + BeatsPerLoop);
65-
}
66-
}
67-
68-
public void PrepChart(BattleDirector.SongData songData, Note[] notes)
44+
public void PrepChart(BattleDirector.SongData songData)
6945
{
7046
_loopLen = songData.SongLength / songData.NumLoops;
7147
TimeKeeper.LoopLength = (float)_loopLen;
@@ -74,56 +50,38 @@ public void PrepChart(BattleDirector.SongData songData, Note[] notes)
7450
TimeKeeper.ChartLength = (float)ChartLength;
7551

7652
InitBackgrounds();
77-
InitNotes(notes);
7853

79-
NM.Connect(nameof(NoteManager.NotePressed), new Callable(this, nameof(OnNotePressed)));
80-
NM.Connect(nameof(NoteManager.NoteReleased), new Callable(this, nameof(OnNoteReleased)));
54+
IH.Connect(nameof(InputHandler.NotePressed), new Callable(this, nameof(OnNotePressed)));
55+
IH.Connect(nameof(InputHandler.NoteReleased), new Callable(this, nameof(OnNoteReleased)));
8156
}
8257

8358
//TODO: Rework these?
84-
public NoteArrow CreateNote(ArrowType arrow, int beat = 0)
59+
public NoteArrow AddArrowToLane(Note note, int noteIdx)
8560
{
86-
var newNote = CreateNote(NM.Arrows[(int)arrow], beat);
61+
var newNote = CreateNote(note.Type, note.Beat);
62+
CreateNote(note.Type, note.Beat + BeatsPerLoop); //Create a dummy arrow for looping visuals
63+
newNote.NoteIdx = noteIdx;
64+
return newNote;
65+
}
66+
67+
private NoteArrow CreateNote(ArrowType arrow, int beat = 0)
68+
{
69+
var newNote = CreateNote(IH.Arrows[(int)arrow]);
8770
newNote.Bounds = (float)((double)beat / BeatsPerLoop * (ChartLength / 2));
8871
return newNote;
8972
}
9073

91-
private NoteArrow CreateNote(NoteManager.ArrowData arrowData, int beat)
74+
private NoteArrow CreateNote(InputHandler.ArrowData arrowData)
9275
{
9376
var noteScene = ResourceLoader.Load<PackedScene>("res://scenes/NoteManager/note.tscn");
9477
NoteArrow note = noteScene.Instantiate<NoteArrow>();
9578
note.Init(arrowData);
96-
97-
if (!(beat > BeatsPerLoop)) //All visual notes need a second to loop effectively. The second set should not be put in the queue.
98-
{
99-
_currentArrows[(int)arrowData.Type] = _currentArrows[(int)arrowData.Type]
100-
.Append(note)
101-
.ToArray();
102-
}
10379
ChartLoopables.AddChild(note);
104-
GD.Print(
105-
$"Adding note: {arrowData.Type}, Current count: {_currentArrows[(int)arrowData.Type].Length}"
106-
);
10780
return note;
10881
}
10982

110-
public void HandleNote(ArrowType type)
111-
{
112-
_currentArrows[(int)type].First().NoteHit();
113-
_currentArrows[(int)type] = _currentArrows[(int)type]
114-
.Skip(1)
115-
.Concat(_currentArrows[(int)type].Take(1))
116-
.ToArray();
117-
}
118-
11983
public void OnNotePressed(ArrowType type)
12084
{
121-
// removed this bit of code, since placement only worked on lines that already
122-
// had arrows before the player added any. if needed we can add this back
123-
// once we use charts that have arrows in all 4 rows from the beginning
124-
//if (_currentArrows[(int)type].Length == 0)
125-
//return;
126-
12785
EmitSignal(nameof(NotePressed), (int)type);
12886
}
12987

scenes/ChartViewport/ChartViewport.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
[ext_resource type="Script" path="res://scenes/ChartViewport/Loopable.cs" id="3_5u57h"]
66
[ext_resource type="PackedScene" uid="uid://bn8txx53xlguw" path="res://scenes/NoteManager/note_manager.tscn" id="4_fd5fw"]
77

8-
[node name="VPContainer" type="SubViewportContainer" node_paths=PackedStringArray("NM", "ChartLoopables")]
8+
[node name="VPContainer" type="SubViewportContainer" node_paths=PackedStringArray("IH", "ChartLoopables")]
99
offset_right = 480.0
1010
offset_bottom = 220.0
1111
script = ExtResource("1_ruh2l")
12-
NM = NodePath("SubViewport/noteManager")
12+
IH = NodePath("SubViewport/noteManager")
1313
ChartLoopables = NodePath("SubViewport/ChartLoopables")
1414

1515
[node name="SubViewport" type="SubViewport" parent="."]

scenes/NoteManager/note_manager.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[gd_scene load_steps=5 format=3 uid="uid://bn8txx53xlguw"]
22

3-
[ext_resource type="Script" path="res://scenes/NoteManager/scripts/NoteManager.cs" id="1_2oeuf"]
3+
[ext_resource type="Script" path="res://scenes/NoteManager/scripts/InputHandler.cs" id="1_2oeuf"]
44
[ext_resource type="Texture2D" uid="uid://bucvj4fquqpkr" path="res://scenes/NoteManager/assets/right-arrow.png" id="2_ldmuy"]
55
[ext_resource type="Script" path="res://scenes/NoteManager/scripts/NoteChecker.cs" id="3_0cioe"]
66
[ext_resource type="Texture2D" uid="uid://b0tvsewgnf2x7" path="res://icon.svg" id="4_foklt"]

scenes/NoteManager/scripts/NoteManager.cs renamed to scenes/NoteManager/scripts/InputHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
using ArrowType = NoteArrow.ArrowType;
55

66
/**
7-
* @class NoteManager
8-
* @brief Note Manager to handle input, timing and lower level data processing for notes. Should signal timing info to Battle Director for HP management. WIP
7+
* @class InputHandler
8+
* @brief InputHandler to handle input, and manage note checkers. WIP
99
*/
10-
public partial class NoteManager : Node2D
10+
public partial class InputHandler : Node2D
1111
{
1212
[Signal]
1313
public delegate void NotePressedEventHandler(ArrowType arrowType);

0 commit comments

Comments
 (0)