diff options
author | Orangerot <purple@orangerot.dev> | 2024-12-29 16:49:42 +0100 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-12-29 16:49:42 +0100 |
commit | f577670d7564b24dfe8cc247459670a2d7ef5bab (patch) | |
tree | fbf29aa99ecd3f9cde927cea07f1edbb250c6b9f | |
parent | 5a763a5de26981c7d015a9e6276017b0f6c12f67 (diff) |
style: refactored time offset generation of beat inside of Simfile class
-rw-r--r-- | lib/level.dart | 23 | ||||
-rw-r--r-- | lib/simfile.dart | 116 |
2 files changed, 75 insertions, 64 deletions
diff --git a/lib/level.dart b/lib/level.dart index 548a752..a6d4967 100644 --- a/lib/level.dart +++ b/lib/level.dart @@ -125,24 +125,13 @@ class _LevelState extends State<Level> { simfile = Simfile(simfilePath); simfile!.load(); - double bpm = simfile!.bpms.entries.first.value; - - for (final (measureIndex, measure) - in simfile!.chartSimplest!.measures!.indexed) { - for (final (noteIndex, noteData) in measure.indexed) { - int arrowIndex = noteData.indexOf('1'); - if (arrowIndex < 0 || arrowIndex > 3) { - continue; - } - double beat = - measureIndex * 4.0 + (noteIndex.toDouble() / measure.length) * 4.0; - double minutesPerBeat = 1.0 / bpm; - double offsetMinutes = simfile!.offset / 60.0; - notes.add(Note( - time: beat * minutesPerBeat + offsetMinutes, - direction: ArrowDirection.values[arrowIndex])); + simfile!.chartSimplest!.beats.forEach((time, noteData) { + int arrowIndex = noteData.indexOf('1'); + if (arrowIndex < 0 || arrowIndex > 3) { + return; } - } + notes.add(Note(time: time, direction: ArrowDirection.values[arrowIndex])); + }); print(audioPath); diff --git a/lib/simfile.dart b/lib/simfile.dart index 2174de9..102c989 100644 --- a/lib/simfile.dart +++ b/lib/simfile.dart @@ -32,6 +32,8 @@ class Chart { String? radarValues; List<List<String>>? measures; + + Map<double, String> beats = {}; } class Simfile { @@ -48,62 +50,82 @@ class Simfile { Simfile(this.path); - void load() { - lines = File(path).readAsStringSync(); - - RegExp commentsRegExp = RegExp(r'//.*$'); - lines = lines?.replaceAll(commentsRegExp, ''); - RegExp fieldDataRegExp = RegExp(r'#([^;]+):([^;]*);'); - - for (final fieldData in fieldDataRegExp.allMatches(lines!)) { - List<String> keys = - fieldData[1]!.split(':').map((key) => key.trim()).toList(); - String value = fieldData[2]!; - if (keys[0] == "BPMS") { - for (final pairRaw in value.split(',')) { - List<String> pair = pairRaw.split('='); - if (pair.length != 2) { - continue; + void _parseChart({required List<String> keys, required String value}) { + Chart chart = Chart(); + chart.chartType = keys[1]; + chart.author = keys[2]; + chart.difficulty = Difficulty.values.byName(keys[3]); + chart.numericalMeter = int.parse(keys[4]); + chart.radarValues = keys[5]; + + if (chartSimplest == null || + (chart.difficulty!.index <= chartSimplest!.difficulty!.index && + chart.numericalMeter! <= chartSimplest!.numericalMeter!)) { + List<List<String>> measures = []; + for (final measureRaw in value.split(',')) { + List<String> measure = []; + for (final noteRaw in measureRaw.split('\n')) { + String note = noteRaw.trim(); + if (noteTypes.hasMatch(note)) { + measure.add(note); } - double time = double.parse(pair[0]); - double bpm = double.parse(pair[1]); - bpms[time] = bpm; } + measures.add(measure); } - if (keys[0] == "OFFSET") { - offset = double.parse(value); - } + double bpm = bpms.entries.first.value; - if (keys[0] != "NOTES") { - tags[keys[0]] = value; - continue; + for (final (measureIndex, measure) in measures.indexed) { + for (final (noteIndex, noteData) in measure.indexed) { + double beat = measureIndex * 4.0 + + (noteIndex.toDouble() / measure.length) * 4.0; + double minutesPerBeat = 1.0 / bpm; + double offsetMinutes = offset / 60.0; + chart.beats[beat * minutesPerBeat + offsetMinutes] = noteData; + } } - Chart chart = Chart(); - chart.chartType = keys[1]; - chart.author = keys[2]; - chart.difficulty = Difficulty.values.byName(keys[3]); - chart.numericalMeter = int.parse(keys[4]); - chart.radarValues = keys[5]; - - if (chartSimplest == null || - (chart.difficulty!.index <= chartSimplest!.difficulty!.index && - chart.numericalMeter! <= chartSimplest!.numericalMeter!)) { - List<List<String>> measures = []; - for (final measureRaw in value.split(',')) { - List<String> measure = []; - for (final noteRaw in measureRaw.split('\n')) { - String note = noteRaw.trim(); - if (noteTypes.hasMatch(note)) { - measure.add(note); - } - } - measures.add(measure); + chart.measures = measures; + chartSimplest = chart; + } + } + + void _parseTag(RegExpMatch fieldData) { + List<String> keys = + fieldData[1]!.split(':').map((key) => key.trim()).toList(); + String value = fieldData[2]!; + if (keys[0] == "BPMS") { + for (final pairRaw in value.split(',')) { + List<String> pair = pairRaw.split('='); + if (pair.length != 2) { + continue; } - chart.measures = measures; - chartSimplest = chart; + double time = double.parse(pair[0]); + double bpm = double.parse(pair[1]); + bpms[time] = bpm; } } + + if (keys[0] == "OFFSET") { + offset = double.parse(value); + } + + if (keys[0] != "NOTES") { + tags[keys[0]] = value; + return; + } + _parseChart(keys: keys, value: value); + } + + void load() { + lines = File(path).readAsStringSync(); + + RegExp commentsRegExp = RegExp(r'//.*$'); + lines = lines?.replaceAll(commentsRegExp, ''); + RegExp fieldDataRegExp = RegExp(r'#([^;]+):([^;]*);'); + + for (final fieldData in fieldDataRegExp.allMatches(lines!)) { + _parseTag(fieldData); + } } } |