1
- using System ;
2
1
using System . Collections . Generic ;
3
2
using System . IO ;
4
- using System . Linq ;
5
3
using System . Threading . Tasks ;
6
4
using System . Web ;
7
5
using RhythmGameUtilities ;
@@ -51,67 +49,21 @@ public class RenderSong : MonoBehaviour
51
49
52
50
private readonly Vector3 _beatBarScaleQuarter = new ( 5 , 0.03f , 0.01f ) ;
53
51
54
- public int resolution { get ; set ; } = 192 ;
55
-
56
- public Tempo [ ] tempoChanges { get ; set ; }
57
-
58
- public TimeSignature [ ] timeSignatureChanges { get ; set ; }
59
-
60
- public Dictionary < Difficulty , Note [ ] > difficulties { get ; set ; }
61
-
62
- public Dictionary < int , List < Note > > notesGroupedByHandPosition { get ; set ; }
63
-
64
- public BeatBar [ ] beatBars { get ; set ; }
52
+ private Song _song ;
65
53
66
54
private async void Start ( )
67
55
{
68
56
var path = Path . Join ( Application . dataPath , _songPath , "notes.chart" ) ;
69
57
70
58
var contents = await LoadTextFileFromPath ( $ "file://{ HttpUtility . UrlPathEncode ( path ) } ") ;
71
59
72
- var sections = Parsers . ParseSectionsFromChart ( contents ) ;
73
-
74
- var metadata = Parsers . ParseMetaDataFromChartSection ( sections
75
- . First ( section => section . Key == NamedSection . Song )
76
- . Value ) ;
60
+ _song = new Song ( contents ) ;
77
61
78
62
_audioSource . clip =
79
63
await LoadAudioFileFromPath (
80
- $ "file://{ HttpUtility . UrlPathEncode ( Path . Join ( Path . GetDirectoryName ( path ) , metadata [ "MusicStream" ] ) ) } ") ;
81
-
82
- resolution = int . Parse ( metadata [ "Resolution" ] ) ;
83
-
84
- tempoChanges = Parsers . ParseTempoChangesFromChartSection ( sections . First ( section => section . Key == NamedSection . SyncTrack )
85
- . Value ) ;
86
-
87
- timeSignatureChanges = Parsers . ParseTimeSignatureChangesFromChartSection ( sections [ NamedSection . SyncTrack ] ) ;
88
-
89
- var lastTick =
90
- Utilities . ConvertSecondsToTicks ( _audioSource . clip . length , resolution , tempoChanges , timeSignatureChanges ) ;
91
-
92
- tempoChanges = tempoChanges . Concat ( new Tempo [ ]
93
- {
94
- new ( )
95
- {
96
- Position = Utilities . RoundUpToTheNearestMultiplier ( lastTick , resolution ) ,
97
- BPM = tempoChanges . Last ( ) . BPM
98
- }
99
- } )
100
- . ToArray ( ) ;
101
-
102
- difficulties = Enum . GetValues ( typeof ( Difficulty ) )
103
- . Cast < Difficulty > ( )
104
- . Where ( difficulty => sections . ToDictionary ( item => item . Key , x => x . Value )
105
- . ContainsKey ( $ "{ difficulty } Single") )
106
- . ToDictionary ( difficulty => difficulty ,
107
- difficulty => Parsers . ParseNotesFromChartSection ( sections [ $ "{ difficulty } Single"] ) ) ;
64
+ $ "file://{ HttpUtility . UrlPathEncode ( Path . Join ( Path . GetDirectoryName ( path ) , _song . metaData [ "MusicStream" ] ) ) } ") ;
108
65
109
- notesGroupedByHandPosition = difficulties [ Difficulty . Expert ]
110
- . Where ( note => note . HandPosition < 5 )
111
- . GroupBy ( note => note . HandPosition )
112
- . ToDictionary ( group => group . Key , group => group . ToList ( ) ) ;
113
-
114
- beatBars = Utilities . CalculateBeatBars ( tempoChanges , includeHalfNotes : true ) ;
66
+ _song . RecalculateBeatBarsWithSongLength ( _audioSource . clip . length ) ;
115
67
116
68
_audioSource . Play ( ) ;
117
69
}
@@ -161,18 +113,19 @@ private void Update()
161
113
{
162
114
RenderTrack ( ) ;
163
115
164
- if ( notesGroupedByHandPosition == null )
116
+ if ( _song ? . difficulties ? [ Difficulty . Easy ] == null )
165
117
{
166
118
return ;
167
119
}
168
120
169
121
var tickOffset =
170
- Utilities . ConvertSecondsToTicks ( _audioSource . time , resolution , tempoChanges , timeSignatureChanges ) ;
122
+ Utilities . ConvertSecondsToTicks ( _audioSource . time , _song . resolution , _song . tempoChanges ,
123
+ _song . timeSignatureChanges ) ;
171
124
172
- RenderHitNotes ( notesGroupedByHandPosition ) ;
125
+ RenderHitNotes ( ) ;
173
126
174
- RenderNotes ( notesGroupedByHandPosition , resolution , tickOffset ) ;
175
- RenderBeatBars ( beatBars , resolution , tickOffset ) ;
127
+ RenderNotes ( _song . difficulties [ Difficulty . Easy ] , _song . resolution , tickOffset ) ;
128
+ RenderBeatBars ( _song . beatBars , _song . resolution , tickOffset ) ;
176
129
}
177
130
178
131
private void RenderTrack ( )
@@ -183,51 +136,43 @@ private void RenderTrack()
183
136
_trackMaterial , 0 ) ;
184
137
}
185
138
186
- private void RenderHitNotes ( Dictionary < int , List < Note > > notesGroupedByHandPosition )
139
+ private void RenderHitNotes ( )
187
140
{
188
- for ( var x = 0 ; x < notesGroupedByHandPosition . Count ; x += 1 )
141
+ for ( var x = 0 ; x < 5 ; x += 1 )
189
142
{
190
143
Graphics . DrawMesh ( _mesh ,
191
144
Matrix4x4 . TRS ( new Vector3 ( x + 0.5f , 0 , 0 ) , Quaternion . identity , _noteScaleFlat ) ,
192
145
_materials [ x ] , 0 ) ;
193
146
}
194
147
}
195
148
196
- private void RenderNotes ( Dictionary < int , List < Note > > notesGroupedByHandPosition , int resolution , int tickOffset )
149
+ private void RenderNotes ( Note [ ] notes , int resolution , int tickOffset )
197
150
{
198
- for ( var x = 0 ; x < notesGroupedByHandPosition . Count ; x += 1 )
199
- {
200
- if ( ! notesGroupedByHandPosition . ContainsKey ( x ) )
201
- {
202
- continue ;
203
- }
204
-
205
- var noteMatrix = new List < Matrix4x4 >
206
- {
207
- Matrix4x4 . TRS ( new Vector3 ( x + 0.5f , 0 , 0 ) , Quaternion . identity , _noteScaleFlat )
208
- } ;
151
+ var laneArray = new Dictionary < int , List < Matrix4x4 > > ( ) ;
209
152
210
- for ( var y = 0 ; y < notesGroupedByHandPosition [ x ] . Count ; y += 1 )
211
- {
212
- var position = Utilities . ConvertTickToPosition ( notesGroupedByHandPosition [ x ] [ y ] . Position - tickOffset ,
213
- resolution ) * _scale ;
153
+ for ( var x = 0 ; x < 5 ; x += 1 )
154
+ {
155
+ laneArray . Add ( x , new List < Matrix4x4 > ( ) ) ;
156
+ }
214
157
215
- if ( position > _distance )
216
- {
217
- break ;
218
- }
158
+ for ( var i = 0 ; i < notes . Length ; i += 1 )
159
+ {
160
+ if ( notes [ i ] . HandPosition > 5 ) continue ;
219
161
220
- if ( position < 0 )
221
- {
222
- continue ;
223
- }
162
+ var position = Utilities . ConvertTickToPosition ( notes [ i ] . Position - tickOffset ,
163
+ resolution ) * _scale ;
224
164
225
- noteMatrix . Add ( Matrix4x4 . TRS (
226
- new Vector3 ( notesGroupedByHandPosition [ x ] [ y ] . HandPosition + 0.5f , 0 , position ) ,
165
+ if ( position > 0 && position < _distance )
166
+ {
167
+ laneArray [ notes [ i ] . HandPosition ] . Add ( Matrix4x4 . TRS (
168
+ new Vector3 ( notes [ i ] . HandPosition + 0.5f , 0 , position ) ,
227
169
Quaternion . identity , _noteScale ) ) ;
228
170
}
171
+ }
229
172
230
- Graphics . DrawMeshInstanced ( _mesh , 0 , _materials [ x ] , noteMatrix ) ;
173
+ for ( var x = 0 ; x < 5 ; x += 1 )
174
+ {
175
+ Graphics . DrawMeshInstanced ( _mesh , 0 , _materials [ x ] , laneArray [ x ] ) ;
231
176
}
232
177
}
233
178
@@ -242,30 +187,23 @@ private void RenderBeatBars(BeatBar[] beatBars, int resolution, int tickOffset)
242
187
var position = Utilities . ConvertTickToPosition ( beatBars [ x ] . Position - tickOffset , resolution ) *
243
188
_scale ;
244
189
245
- if ( position > _distance )
246
- {
247
- break ;
248
- }
249
-
250
- if ( position < 0 )
251
- {
252
- continue ;
253
- }
254
-
255
- if ( x % 8 == 0 )
256
- {
257
- beatBarMatrix . Add ( Matrix4x4 . TRS ( new Vector3 ( 2.5f , 0 , position ) , Quaternion . identity ,
258
- _beatBarScaleFull ) ) ;
259
- }
260
- else if ( x % 2 == 0 )
261
- {
262
- beatBarHalfMatrix . Add ( Matrix4x4 . TRS ( new Vector3 ( 2.5f , 0 , position ) , Quaternion . identity ,
263
- _beatBarScaleHalf ) ) ;
264
- }
265
- else
190
+ if ( position > 0 && position < _distance )
266
191
{
267
- beatBarQuarterMatrix . Add ( Matrix4x4 . TRS ( new Vector3 ( 2.5f , 0 , position ) , Quaternion . identity ,
268
- _beatBarScaleQuarter ) ) ;
192
+ if ( x % 8 == 0 )
193
+ {
194
+ beatBarMatrix . Add ( Matrix4x4 . TRS ( new Vector3 ( 2.5f , 0 , position ) , Quaternion . identity ,
195
+ _beatBarScaleFull ) ) ;
196
+ }
197
+ else if ( x % 2 == 0 )
198
+ {
199
+ beatBarHalfMatrix . Add ( Matrix4x4 . TRS ( new Vector3 ( 2.5f , 0 , position ) , Quaternion . identity ,
200
+ _beatBarScaleHalf ) ) ;
201
+ }
202
+ else
203
+ {
204
+ beatBarQuarterMatrix . Add ( Matrix4x4 . TRS ( new Vector3 ( 2.5f , 0 , position ) , Quaternion . identity ,
205
+ _beatBarScaleQuarter ) ) ;
206
+ }
269
207
}
270
208
}
271
209
0 commit comments