style: refactored time offset generation of beat inside of Simfile class
This commit is contained in:
parent
5a763a5de2
commit
f577670d75
|
@ -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);
|
||||
|
||||
|
|
118
lib/simfile.dart
118
lib/simfile.dart
|
@ -32,6 +32,8 @@ class Chart {
|
|||
String? radarValues;
|
||||
|
||||
List<List<String>>? measures;
|
||||
|
||||
Map<double, String> beats = {};
|
||||
}
|
||||
|
||||
class Simfile {
|
||||
|
@ -48,6 +50,73 @@ class Simfile {
|
|||
|
||||
Simfile(this.path);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
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();
|
||||
|
||||
|
@ -56,54 +125,7 @@ class Simfile {
|
|||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
_parseTag(fieldData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue