style: refactored level into smaller methods

This commit is contained in:
Orangerot 2025-01-13 15:16:08 +01:00
parent 7bf1ead030
commit d52f60ef92

View file

@ -72,13 +72,61 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
_position = value;
}),
);
_durationSubscription = player.onDurationChanged.listen((duration) {
// listen for new values from player
_durationSubscription =
player.onDurationChanged.listen((Duration duration) {
setState(() => _duration = duration);
});
_positionSubscription =
player.onPositionChanged.listen((Duration position) {
setState(() => _position = position);
for (final note in notes) {
_noteHitCheck(note, position);
}
});
player.onPlayerComplete.listen((void _) {
Route route = MaterialPageRoute(
builder: (context) => GameOverStats(
simfile: widget.simfile,
notes: notes,
));
Navigator.pushReplacement(context, route);
});
// listen for esense button and pause/resume
if (ESenseInput.instance.connected) {
_buttonSubscription = ESenseInput.instance.buttonEvents().listen((event) {
if (!event.pressed) {
_pauseResume();
}
});
}
widget.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]));
});
player.play(DeviceFileSource(widget.simfile.audioPath!));
}
@override
void dispose() {
_animationController.dispose();
_durationSubscription?.cancel();
_positionSubscription?.cancel();
_buttonSubscription?.cancel();
player.dispose();
super.dispose();
}
void _pauseResume() {
if (_isPlaying) {
player.pause();
setState(() {
@ -91,34 +139,11 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
});
}
}
});
}
_positionSubscription = player.onPositionChanged.listen(
(p) => setState(() => _position = p),
);
player.onDurationChanged.listen((Duration d) {
// print('Max duration: $d');
setState(() => _duration = d);
});
player.onPlayerComplete.listen((void _) {
Route route = MaterialPageRoute(
builder: (context) => GameOverStats(
simfile: widget.simfile,
notes: notes,
));
Navigator.pushReplacement(context, route);
});
player.onPositionChanged.listen((Duration p) {
// print('Current position: $p');
setState(() => _position = p);
for (final note in notes) {
note.position = note.time - p.inMilliseconds / 60000.0;
void _noteHitCheck(Note note, Duration time) {
note.position = note.time - time.inMilliseconds / 60000.0;
if (note.wasHit != null) {
continue;
return;
}
if (note.position.abs() < 0.5 * 1.0 / 60.0) {
InputDirection esenseDirection =
@ -163,25 +188,8 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
});
}
}
});
widget.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]));
});
player.play(DeviceFileSource(widget.simfile.audioPath!));
}
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (event) {
void _keyboardHandler(event) {
bool isDown = false;
if (event is KeyDownEvent) {
isDown = true;
@ -204,24 +212,19 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
inputDirection.right = isDown;
break;
}
},
}
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: _keyboardHandler,
child: Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
onPressed: () {
if (_isPlaying) {
player.pause();
setState(() {
_isPlaying = false;
});
} else {
player.resume();
setState(() {
_isPlaying = true;
});
}
},
onPressed: _pauseResume,
),
title: Text(widget.simfile.tags['TITLE']!),
actions: [
@ -273,14 +276,4 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
])),
);
}
@override
void dispose() {
_animationController.dispose();
_durationSubscription?.cancel();
_positionSubscription?.cancel();
_buttonSubscription?.cancel();
player.dispose();
super.dispose();
}
}