style: refactored time offset generation of beat inside of Simfile class

This commit is contained in:
Orangerot 2024-12-29 16:49:42 +01:00
parent 5a763a5de2
commit f577670d75
2 changed files with 76 additions and 65 deletions

View file

@ -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) {
simfile!.chartSimplest!.beats.forEach((time, noteData) {
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]));
}
return;
}
notes.add(Note(time: time, direction: ArrowDirection.values[arrowIndex]));
});
print(audioPath);

View file

@ -32,6 +32,8 @@ class Chart {
String? radarValues;
List<List<String>>? measures;
Map<double, String> beats = {};
}
class Simfile {
@ -48,38 +50,7 @@ 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;
}
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;
continue;
}
void _parseChart({required List<String> keys, required String value}) {
Chart chart = Chart();
chart.chartType = keys[1];
chart.author = keys[2];
@ -101,9 +72,60 @@ class Simfile {
}
measures.add(measure);
}
double bpm = bpms.entries.first.value;
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.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;
}
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);
}
}
}