Skip to content

Commit 212f95d

Browse files
committed
Refactoring
General reorganization and code cleanup
1 parent b8e7d9a commit 212f95d

10 files changed

Lines changed: 48 additions & 75 deletions

File tree

project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ config_version=5
1111
[application]
1212

1313
config/name="Funk Engine"
14-
run/main_scene="res://scenes/ChartViewport/test_scene.tscn"
14+
run/main_scene="res://scenes/BattleDirector/test_battle_scene.tscn"
1515
config/features=PackedStringArray("4.3", "C#", "Forward Plus")
1616
config/icon="res://icon.svg"
1717

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public struct SongData
3535
}
3636

3737
private SongData _curSong;
38+
//Assume queue structure for notes in each lane.
3839
private readonly Note[][] _laneNotes = new Note[][]
3940
{
4041
Array.Empty<Note>(),
@@ -52,44 +53,35 @@ public override void _Ready()
5253
Player = GetNode<HealthBar>("PlayerHP");
5354
Enemy = GetNode<HealthBar>("EnemyHP");
5455

55-
//TODO: Hook up signals
5656
CM.Connect(nameof(NoteManager.NotePressed), new Callable(this, nameof(OnNotePressed)));
5757
CM.Connect(nameof(NoteManager.NoteReleased), new Callable(this, nameof(OnNoteReleased)));
5858
}
5959

6060
public override void _Process(double delta)
6161
{
6262
TimeKeeper.CurrentTime += delta;
63-
//Check beats downwards
63+
//Check beats for each lane for passive misses
6464
double curBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm);
6565
for (int i = 0; i < _laneNotes.Length; i++)
6666
{
67-
if (_laneNotes[i].Length > 0)
67+
if (_laneNotes[i].Length <= 0) continue;
68+
double beatDif = (curBeat - _laneNotes[i].First().Beat);
69+
if (beatDif > 1)
6870
{
69-
if (curBeat > _laneNotes[i][0].Beat + 1)
70-
{
71-
GD.Print($"Beat: {_laneNotes[i][0].Beat} Miss");
72-
GD.Print(TimeKeeper.CurrentTime);
73-
//Cycle note queue
74-
_laneNotes[i].First().Beat += CM._beatsPerLoop;
75-
_laneNotes[i] = _laneNotes[i].Skip(1).Concat(_laneNotes[i].Take(1)).ToArray(); //TODO: No stackoverflow code
76-
Player.TakeDamage(10);
77-
CM.HandleNote((NoteArrow.ArrowType)i);
78-
}
71+
handleTiming((NoteArrow.ArrowType)i, Math.Abs(beatDif));
7972
}
8073
}
8174
}
8275

76+
//Creeate dummy song data and notes
8377
private void AddExampleNote()
8478
{
85-
//Create Dummy Song Data
8679
_curSong = new SongData
8780
{
8881
Bpm = 120,
8982
SongLength = 100,
9083
NumLoops = 5,
9184
};
92-
//Add note
9385
for (int i = 0; i < 4; i++)
9486
{
9587
Note exampleNote = new Note(NoteArrow.ArrowType.Up, i + 3);
@@ -110,29 +102,17 @@ private void AddNoteToLane(Note note)
110102

111103
private void OnNotePressed(NoteArrow.ArrowType type)
112104
{
113-
//GD.Print("Note pressed: " + type + " at " + _currentTime + " seconds.");
114105
CheckNoteTiming(type);
115106
}
116107

117108
private void OnNoteReleased(NoteArrow.ArrowType arrowType)
118109
{
119-
//GD.Print("Note released: " + arrowType + " at " + _currentTime + " seconds.");
120110
}
121111

122-
private void CheckNoteTiming(NoteArrow.ArrowType type)
112+
private void handleTiming(NoteArrow.ArrowType type, double beatDif)
123113
{
124-
//Assume queue structure for notes
125-
//Know current time, calculate beat timing
126-
double curBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm);
127-
if (_laneNotes[(int)type].Length == 0)
128-
return;
129-
double beatDif = Math.Abs(curBeat - _laneNotes[(int)type].First().Beat);
130-
if (beatDif > 1)
131-
return;
132-
GD.Print("Note Hit. Dif: " + beatDif);
133-
CM.HandleNote(type);
134114
//Cycle note queue
135-
_laneNotes[(int)type].First().Beat += CM._beatsPerLoop;
115+
_laneNotes[(int)type].First().Beat += CM.BeatsPerLoop;
136116
_laneNotes[(int)type] = _laneNotes[(int)type] //Credit: Stackoverflow https://stackoverflow.com/questions/49494535/moving-the-first-array-element-to-end-in-c-sharp
137117
.Skip(1)
138118
.Concat(_laneNotes[(int)type].Take(1))
@@ -159,4 +139,17 @@ private void CheckNoteTiming(NoteArrow.ArrowType type)
159139
Player.TakeDamage(10);
160140
}
161141
}
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+
}
162155
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ public partial class HealthBar : Control
88

99
[Export]
1010
public ProgressBar PlayerHealthBar;
11-
1211
//we can change this to a Texture Progress bar once we have art assets for it
1312

14-
15-
13+
1614
public override void _Ready()
1715
{
1816
if (PlayerHealthBar != null)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[gd_scene load_steps=2 format=3 uid="uid://bgomxovxs7sr8"]
22

3-
[ext_resource type="Script" path="res://scripts/HealthBar.cs" id="1_b1t4i"]
3+
[ext_resource type="Script" path="res://scenes/BattleDirector/HealthBar.cs" id="1_b1t4i"]
44

55
[node name="Control" type="Control" node_paths=PackedStringArray("PlayerHealthBar")]
66
layout_mode = 3

scenes/ChartViewport/test_scene.tscn renamed to scenes/BattleDirector/test_battle_scene.tscn

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[gd_scene load_steps=4 format=3 uid="uid://b0mrgr7h0ty1y"]
22

3-
[ext_resource type="PackedScene" uid="uid://dfevfib11kou1" path="res://scenes/ChartViewport/ChartViewport.tscn" id="1_1vh1u"]
4-
[ext_resource type="Script" path="res://scenes/ChartViewport/BattleDirector.cs" id="1_jbxt1"]
5-
[ext_resource type="PackedScene" uid="uid://bgomxovxs7sr8" path="res://scenes/HealthBar.tscn" id="2_1t4a4"]
3+
[ext_resource type="Script" path="res://scenes/BattleDirector/BattleDirector.cs" id="1_cwqqr"]
4+
[ext_resource type="PackedScene" uid="uid://dfevfib11kou1" path="res://scenes/ChartViewport/ChartViewport.tscn" id="2_cupb3"]
5+
[ext_resource type="PackedScene" uid="uid://bgomxovxs7sr8" path="res://scenes/BattleDirector/HealthBar.tscn" id="3_pp0u0"]
66

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

12-
[node name="SubViewport" parent="." instance=ExtResource("1_1vh1u")]
12+
[node name="SubViewport" parent="." instance=ExtResource("2_cupb3")]
1313
offset_left = 80.0
1414
offset_top = 140.0
1515
offset_right = 560.0
@@ -23,9 +23,9 @@ offset_right = 673.0
2323
offset_bottom = 393.0
2424
color = Color(0.147672, 0.147672, 0.147672, 1)
2525

26-
[node name="PlayerHP" parent="." instance=ExtResource("2_1t4a4")]
26+
[node name="PlayerHP" parent="." instance=ExtResource("3_pp0u0")]
2727

28-
[node name="EnemyHP" parent="." instance=ExtResource("2_1t4a4")]
28+
[node name="EnemyHP" parent="." instance=ExtResource("3_pp0u0")]
2929
offset_left = 403.0
3030
offset_top = -4.0
3131
offset_right = 443.0

scenes/ChartViewport/ChartManager.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ public partial class ChartManager : SubViewportContainer
2525
//Arbitrary vars, play with these
2626
private double ChartLength = 2400; //Might move this to be song specific?
2727

28-
//Speed that chart objs should move at, to be synced to song, in theory
29-
private double _rateOfChart; //Speed objects should move at px/s
3028
private double _loopLen; //secs
31-
public int _beatsPerLoop;
29+
public int BeatsPerLoop;
3230

3331
private NoteArrow[][] _currentArrows = new NoteArrow[][]
3432
{
@@ -44,15 +42,12 @@ private void InitBackgrounds()
4442
int i = 0;
4543
foreach (Node child in ChartLoopables.GetChildren())
4644
{
47-
if (child is Loopable)
48-
{
49-
Loopable loopable = (Loopable)child;
50-
//TODO: Consolidate
51-
loopable.SetSize(new Vector2((float)ChartLength / 2 + 1, Size.Y));
52-
loopable.Bounds = (float)ChartLength / 2 * i;
53-
54-
i++;
55-
}
45+
if (child is not Loopable)
46+
continue;
47+
Loopable loopable = (Loopable)child;
48+
loopable.SetSize(new Vector2((float)ChartLength / 2 + 1, Size.Y));
49+
loopable.Bounds = (float)ChartLength / 2 * i;
50+
i++;
5651
}
5752
}
5853

@@ -66,43 +61,40 @@ private void InitNotes(Note[] notes)
6661
foreach (Note noteData in notes) //Temporary solution
6762
{
6863
if (noteData != null)
69-
CreateNote(noteData.Type, noteData.Beat + _beatsPerLoop);
64+
CreateNote(noteData.Type, noteData.Beat + BeatsPerLoop);
7065
}
7166
}
7267

7368
public void PrepChart(BattleDirector.SongData songData, Note[] notes)
7469
{
7570
_loopLen = songData.SongLength / songData.NumLoops;
7671
TimeKeeper.LoopLength = (float)_loopLen;
77-
_beatsPerLoop = (int)(_loopLen / (60f / songData.Bpm));
72+
BeatsPerLoop = (int)(_loopLen / (60f / songData.Bpm));
7873
ChartLength = (float)_loopLen * (float)Math.Floor(ChartLength / _loopLen);
7974
TimeKeeper.ChartLength = (float)ChartLength;
8075

81-
_rateOfChart = ChartLength / 2 / _loopLen; //px/s
82-
8376
InitBackgrounds();
8477
InitNotes(notes);
8578

8679
NM.Connect(nameof(NoteManager.NotePressed), new Callable(this, nameof(OnNotePressed)));
8780
NM.Connect(nameof(NoteManager.NoteReleased), new Callable(this, nameof(OnNoteReleased)));
8881
}
8982

90-
//TODO: Rework these
83+
//TODO: Rework these?
9184
public NoteArrow CreateNote(ArrowType arrow, int beat = 0)
9285
{
9386
var newNote = CreateNote(NM.Arrows[(int)arrow], beat);
94-
newNote.Bounds = (float)((double)beat / _beatsPerLoop * (ChartLength / 2)); //eww
95-
newNote.Position += Vector2.Right * newNote.Bounds;
87+
newNote.Bounds = (float)((double)beat / BeatsPerLoop * (ChartLength / 2));
9688
return newNote;
9789
}
9890

9991
private NoteArrow CreateNote(NoteManager.ArrowData arrowData, int beat)
10092
{
10193
var noteScene = ResourceLoader.Load<PackedScene>("res://scenes/NoteManager/note.tscn");
10294
NoteArrow note = noteScene.Instantiate<NoteArrow>();
103-
10495
note.Init(arrowData);
105-
if (!(beat > _beatsPerLoop))
96+
97+
if (!(beat > BeatsPerLoop)) //All visual notes need a second to loop effectively. The second set should not be put in the queue.
10698
{
10799
_currentArrows[(int)arrowData.Type] = _currentArrows[(int)arrowData.Type]
108100
.Append(note)

scenes/ChartViewport/Loopable.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@ public partial class Loopable : TextureRect
99
{
1010
[Export]
1111
public float Bounds = 700f; //px Pos to loop or do something at.
12-
public float Speed = 5; //px/s
1312

1413
// Called every frame. 'delta' is the elapsed time since the previous frame.
1514
public override void _Process(double delta)
1615
{
17-
if (Position.X <= -Bounds)
18-
{
19-
//Loop();
20-
}
2116
Vector2 newPos = Position;
17+
//Loop position over the course of time across a loop
2218
newPos.X =
2319
(float)(
2420
(-TimeKeeper.CurrentTime / TimeKeeper.LoopLength * TimeKeeper.ChartLength)
@@ -27,9 +23,4 @@ public override void _Process(double delta)
2723
) + Bounds;
2824
Position = newPos;
2925
}
30-
31-
public void Loop()
32-
{
33-
Position = new Vector2(Bounds, Position.Y);
34-
}
3526
}

scenes/NoteManager/scripts/NoteArrow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @brief This class represents a visual note that scrolls across the screen to be played by the player. WIP
66
*/
77
public partial class NoteArrow : Sprite2D
8-
{ //TextRect makes it easier to resize to something specific, but could cause issues later. :)
8+
{ //TextRect caused issues later :)
99
public enum ArrowType
1010
{
1111
Up = 0,

scenes/NoteManager/scripts/NoteManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public partial class NoteManager : Node2D
1515
[Signal]
1616
public delegate void NoteReleasedEventHandler(ArrowType arrowType);
1717

18-
//TODO: Put in a Global/Somewhere it makes sense
1918
public struct ArrowData
2019
{
2120
public Color Color;

scripts/TimeKeeper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
public partial class TimeKeeper : Node
55
{
6-
public static double CurrentTime;
6+
public static double CurrentTime = 0;
77
public static float ChartLength;
88
public static float LoopLength;
99
}

0 commit comments

Comments
 (0)