Skip to content

Commit 6b2eee8

Browse files
committed
Created input and timing
Very hacky, but it works now.
1 parent 38c5ff1 commit 6b2eee8

5 files changed

Lines changed: 117 additions & 9 deletions

File tree

Funk Engine.sln.DotSettings.user

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<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>

scenes/ChartViewport/BattleDirector.cs

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
25
using Godot;
36

47
/**
@@ -14,6 +17,10 @@ public partial class BattleDirector : Node2D
1417
public NoteManager NM;
1518

1619
//TODO: Slowly add data based on what it needs.
20+
private double _startTime;
21+
private double _currentTime;
22+
private double _timingInterval = .1; //secs
23+
1724
public struct SongData
1825
{
1926
public int Bpm;
@@ -22,14 +29,32 @@ public struct SongData
2229
}
2330

2431
private SongData _curSong;
25-
private Note[] _notes;
32+
private Dictionary<string, Note[]> LaneNotes;
33+
private Note[] _notes = Array.Empty<Note>();
2634

2735
public override void _Ready()
2836
{
37+
LaneNotes = new()
38+
{
39+
{ "arrowUp", Array.Empty<Note>() },
40+
{ "arrowLeft", Array.Empty<Note>() },
41+
{ "arrowDown", Array.Empty<Note>() },
42+
{ "arrowRight", Array.Empty<Note>() },
43+
};
2944
AddExampleNote();
3045

3146
CM.PrepChart(_curSong, _notes);
3247
//TODO: Hook up signals
48+
NM.Connect(nameof(NoteManager.NotePressed), new Callable(this, nameof(OnNotePressed)));
49+
NM.Connect(nameof(NoteManager.NoteReleased), new Callable(this, nameof(OnNoteReleased)));
50+
51+
_startTime = (double)Time.GetTicksMsec() / 1000;
52+
}
53+
54+
public override void _Process(double delta)
55+
{
56+
_currentTime += delta;
57+
//GD.Print($"Current Time: {_currentTime}");
3358
}
3459

3560
private void AddExampleNote()
@@ -42,7 +67,67 @@ private void AddExampleNote()
4267
NumLoops = 5,
4368
};
4469
//Add note
45-
_notes = new Note[1];
46-
_notes[0] = new Note(NoteArrow.ArrowType.Left, 32);
70+
for (int i = 0; i < 5; i++)
71+
{
72+
Note exampleNote = new Note(NoteArrow.ArrowType.Left, i + 9);
73+
AddNoteToLane(exampleNote);
74+
}
75+
}
76+
77+
private void AddNoteToLane(Note note)
78+
{
79+
_notes = _notes.Append(note).ToArray();
80+
LaneNotes[NM.Arrows[note.Type].Key] = LaneNotes[NM.Arrows[note.Type].Key]
81+
.Append(note)
82+
.ToArray();
83+
}
84+
85+
private void OnNotePressed(string key)
86+
{
87+
GD.Print("Note pressed: " + key + " at " + _currentTime + " seconds.");
88+
CheckNoteTiming(key);
89+
}
90+
91+
private void OnNoteReleased(NoteArrow.ArrowType arrowType)
92+
{
93+
GD.Print("Note released: " + arrowType + " at " + _currentTime + " seconds.");
94+
}
95+
96+
private void CheckNoteTiming(string arrowString)
97+
{
98+
//Assume queue structure for notes
99+
//Know current time, calculate beat timing
100+
var curBeat = _currentTime / (60 / (double)_curSong.Bpm);
101+
if (LaneNotes[arrowString].Length == 0)
102+
return;
103+
double beatDif = Math.Abs(curBeat - LaneNotes[arrowString].First().Beat);
104+
GD.Print(beatDif);
105+
if (beatDif > 1)
106+
return;
107+
GD.Print("Note Hit.");
108+
//Cycle note queue
109+
LaneNotes[arrowString] = LaneNotes[arrowString]
110+
.Skip(1)
111+
.Concat(LaneNotes[arrowString].Take(1))
112+
.ToArray(); //TODO: No stackoverflow code
113+
//Change note visual
114+
CM.TriggerArrow();
115+
//Do timing stuff
116+
if (beatDif < _timingInterval * 2)
117+
{
118+
GD.Print("Perfect");
119+
}
120+
else if (beatDif < _timingInterval * 4)
121+
{
122+
GD.Print("Good");
123+
}
124+
else if (beatDif < _timingInterval * 6)
125+
{
126+
GD.Print("Okay");
127+
}
128+
else
129+
{
130+
GD.Print("Miss");
131+
}
47132
}
48133
}

scenes/ChartViewport/ChartManager.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using Godot;
34
using ArrowType = NoteArrow.ArrowType;
45

@@ -44,13 +45,16 @@ public partial class ChartManager : SubViewportContainer
4445
public BattleDirector.SongData SongData; //TODO: Maybe. Make settable from outside, but readonly
4546

4647
//Arbitrary vars, play with these
47-
private const double ChartLength = 1400; //Might move this to be song specific?
48+
private const double ChartLength = 2800; //Might move this to be song specific?
4849

4950
//Speed that chart objs should move at, to be synced to song, in theory
5051
private double _rateOfChart;
5152
private double _loopLen; //secs
5253
private int _beatsPerLoop;
5354

55+
//TODO: Lanes
56+
private NoteArrow[] _currentArrows = Array.Empty<NoteArrow>();
57+
5458
private void InitBackgrounds()
5559
{
5660
//TODO: Get better visual for BG's, and/or create BG's on demand. Though we should only ever need 2.
@@ -75,7 +79,8 @@ private void InitNotes(Note[] notes)
7579
{
7680
foreach (Note noteData in notes)
7781
{
78-
CreateNote(noteData.Type, noteData.Beat);
82+
if (noteData != null)
83+
CreateNote(noteData.Type, noteData.Beat);
7984
}
8085
}
8186

@@ -86,7 +91,7 @@ public void PrepChart(BattleDirector.SongData songData, Note[] notes)
8691
_loopLen = SongData.SongLength / SongData.NumLoops;
8792
_beatsPerLoop = (int)(_loopLen / (60f / SongData.Bpm));
8893

89-
_rateOfChart = 700 / _loopLen; //px/s
94+
_rateOfChart = ChartLength / 2 / _loopLen; //px/s
9095

9196
InitBackgrounds();
9297
InitNotes(notes);
@@ -105,14 +110,20 @@ public NoteArrow CreateNote(ArrowType arrow, int beat = 0)
105110
private NoteArrow CreateNote(NoteManager.ArrowData arrowData)
106111
{
107112
var noteScene = ResourceLoader.Load<PackedScene>("res://scenes/NoteManager/note.tscn");
108-
var note = noteScene.Instantiate<NoteArrow>();
113+
NoteArrow note = noteScene.Instantiate<NoteArrow>();
109114

110115
note.Init(arrowData, (float)_rateOfChart, -1);
111-
116+
_currentArrows = _currentArrows.Append(note).ToArray();
112117
ChartLoopables.AddChild(note);
113118
return note;
114119
}
115120

121+
public void TriggerArrow()
122+
{
123+
_currentArrows.First().NoteHit();
124+
_currentArrows = _currentArrows.Skip(1).ToArray();
125+
}
126+
116127
//TODO: Queue next notes. Needs Timing System
117128
/*The logic:
118129
*Spawn in pos is a proportion (intended beat/beats per loop) = (intended pos/track length in px) ->

scenes/NoteManager/scripts/NoteArrow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override void _Process(double delta)
3838

3939
public void NoteHit()
4040
{
41-
Visible = false;
41+
Modulate = Colors.Transparent;
4242
}
4343

4444
public override void Loop()

scenes/NoteManager/scripts/NoteManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using Godot;
34
using ArrowType = NoteArrow.ArrowType;
@@ -9,6 +10,12 @@
910
public partial class NoteManager : Node2D
1011
{
1112
//TODO: Put in a Global/Somewhere it makes sense
13+
[Signal]
14+
public delegate void NotePressedEventHandler(ArrowType arrowType);
15+
16+
[Signal]
17+
public delegate void NoteReleasedEventHandler(ArrowType arrowType);
18+
1219
public struct ArrowData
1320
{
1421
public Color Color;
@@ -79,10 +86,13 @@ public override void _Process(double delta)
7986
if (Input.IsActionJustPressed(arrow.Value.Key))
8087
{
8188
arrow.Value.Node.SetPressed(true);
89+
GD.Print(arrow.Value.Key);
90+
EmitSignal(nameof(NotePressed), arrow.Value.Key);
8291
}
8392
else if (Input.IsActionJustReleased(arrow.Value.Key))
8493
{
8594
arrow.Value.Node.SetPressed(false);
95+
EmitSignal(nameof(NoteReleased), arrow.Value.Key);
8696
}
8797
}
8898
}

0 commit comments

Comments
 (0)