diff options
Diffstat (limited to 'lib/screens')
-rw-r--r-- | lib/screens/game_over.dart | 63 | ||||
-rw-r--r-- | lib/screens/level.dart | 285 | ||||
-rw-r--r-- | lib/screens/level_selection.dart | 161 |
3 files changed, 509 insertions, 0 deletions
diff --git a/lib/screens/game_over.dart b/lib/screens/game_over.dart new file mode 100644 index 0000000..76ea2fb --- /dev/null +++ b/lib/screens/game_over.dart @@ -0,0 +1,63 @@ +import 'package:flutter/material.dart'; +import 'package:sense_the_rhythm/models/note.dart'; +import 'package:sense_the_rhythm/utils/simfile.dart'; +import 'package:sense_the_rhythm/screens/level.dart'; + +class GameOverStats extends StatelessWidget { + const GameOverStats({super.key, required this.simfile, required this.notes}); + + final Simfile simfile; + final List<Note> notes; + + @override + Widget build(BuildContext context) { + int hits = notes.where((note) => note.wasHit == true).length; + int misses = notes.where((note) => note.wasHit == false).length; + int total = notes.length; + int percent = (hits.toDouble() / total.toDouble() * 100).toInt(); + + return Scaffold( + appBar: AppBar( + leading: IconButton( + onPressed: () => Navigator.pop(context), + icon: Icon(Icons.arrow_back)), + title: Text('Game Stats'), + ), + body: Center( + child: Column( + children: [ + Text(' $percent%', + style: TextStyle( + fontSize: 60, + fontWeight: FontWeight.bold, + color: Colors.orange)), + DataTable(columns: [ + DataColumn(label: Container()), + DataColumn(label: Container()), + ], rows: [ + DataRow(cells: [ + DataCell(Text('Hits')), + DataCell(Text(hits.toString())), + ]), + DataRow(cells: [ + DataCell(Text('Misses')), + DataCell(Text(misses.toString())), + ]), + DataRow(cells: [ + DataCell(Text('Total')), + DataCell(Text(total.toString())), + ]), + ]), + TextButton( + onPressed: () { + Route route = + MaterialPageRoute(builder: (context) => Level(simfile)); + Navigator.pushReplacement(context, route); + }, + child: Text('Retry')) + ], + ), + ), + ); + } +} diff --git a/lib/screens/level.dart b/lib/screens/level.dart new file mode 100644 index 0000000..000c43c --- /dev/null +++ b/lib/screens/level.dart @@ -0,0 +1,285 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:audioplayers/audioplayers.dart'; +import 'package:flutter/services.dart'; +import 'package:sense_the_rhythm/models/arrow_direction.dart'; +import 'package:sense_the_rhythm/models/input_direction.dart'; +import 'package:sense_the_rhythm/models/note.dart'; +import 'package:sense_the_rhythm/utils/esense_input.dart'; +import 'package:sense_the_rhythm/utils/simfile.dart'; +import 'package:sense_the_rhythm/widgets/arrows.dart'; +import 'package:sense_the_rhythm/screens/game_over.dart'; + +class Level extends StatefulWidget { + const Level(this.simfile, {super.key}); + final Simfile simfile; + + @override + State<Level> createState() => _LevelState(); +} + +class _LevelState extends State<Level> with SingleTickerProviderStateMixin { + final _player = AudioPlayer(); + bool _isPlaying = true; + Duration? _duration; + Duration? _position; + + StreamSubscription? _durationSubscription; + StreamSubscription? _positionSubscription; + StreamSubscription? _buttonSubscription; + + final FocusNode _focusNode = FocusNode(); + final InputDirection _inputDirection = InputDirection(); + + String _hitOrMissMessage = 'Play!'; + + final List<Note> _notes = []; + + late AnimationController _animationController; + late Animation<double> _animation; + + @override + void setState(VoidCallback fn) { + // Subscriptions only can be closed asynchronously, + // therefore events can occur after widget has been disposed. + if (mounted) { + super.setState(fn); + } + } + + @override + void initState() { + super.initState(); + ESenseInput.instance.resetAngles(); + + _animationController = AnimationController( + vsync: this, + duration: Duration(seconds: 2), + ); + _animation = + Tween<double>(begin: 1.0, end: 0.0).animate(_animationController); + _animationController.forward(); + + // Use initial values from player + _player.getDuration().then( + (value) => setState(() { + _duration = value; + }), + ); + _player.getCurrentPosition().then( + (value) => setState(() { + _position = value; + }), + ); + + // 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); + } + }); + + // go to GameOverStats when level finishes + _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(); + } + }); + } + + // convert beats to notes + 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(); + } + + /// toggle between pause and resume + void _pauseResume() { + if (_isPlaying) { + _player.pause(); + setState(() { + _isPlaying = false; + }); + } else { + _player.resume(); + setState(() { + _isPlaying = true; + }); + } + } + + /// checks if the [note] is hit on [time] with the correct InputDirection + void _noteHitCheck(Note note, Duration time) { + note.position = note.time - time.inMilliseconds / 60000.0; + if (note.wasHit != null) { + return; + } + + // you have +- half a second to hit + if (note.position.abs() < 0.5 * 1.0 / 60.0) { + // combine keyboard and esense input + InputDirection esenseDirection = + ESenseInput.instance.getInputDirection(note.direction); + _inputDirection.up |= esenseDirection.up; + _inputDirection.down |= esenseDirection.down; + _inputDirection.left |= esenseDirection.left; + _inputDirection.right |= esenseDirection.right; + + // check if input matches arrow direction + bool keypressCorrect = false; + switch (note.direction) { + case ArrowDirection.up: + keypressCorrect = _inputDirection.up; + break; + case ArrowDirection.down: + keypressCorrect = _inputDirection.down; + break; + case ArrowDirection.right: + keypressCorrect = _inputDirection.right; + break; + case ArrowDirection.left: + keypressCorrect = _inputDirection.left; + break; + } + if (keypressCorrect) { + print("you hit!"); + note.wasHit = true; + _animationController.reset(); + _animationController.forward(); + _inputDirection.reset(); + setState(() { + _hitOrMissMessage = 'Great!'; + }); + } + } else if (note.position < -0.5 * 1.0 / 60.0) { + print("Missed"); + note.wasHit = false; + _animationController.reset(); + _animationController.forward(); + _inputDirection.reset(); + setState(() { + _hitOrMissMessage = 'Missed'; + }); + } + } + + /// sets the InputDirection based on the arrow keys + void _keyboardHandler(event) { + bool isDown = false; + if (event is KeyDownEvent) { + isDown = true; + } else if (event is KeyUpEvent) { + isDown = false; + } else { + return; + } + switch (event.logicalKey) { + case LogicalKeyboardKey.arrowUp: + _inputDirection.up = isDown; + break; + case LogicalKeyboardKey.arrowDown: + _inputDirection.down = isDown; + break; + case LogicalKeyboardKey.arrowLeft: + _inputDirection.left = isDown; + break; + case LogicalKeyboardKey.arrowRight: + _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: _pauseResume, + ), + title: Text(widget.simfile.tags['TITLE']!), + actions: [ + IconButton( + icon: Icon(Icons.close), + onPressed: () => Navigator.pop(context)) + ], + bottom: PreferredSize( + preferredSize: Size(double.infinity, 1.0), + child: LinearProgressIndicator( + value: (_duration != null && + _position != null && + _position!.inMilliseconds > 0 && + _position!.inMilliseconds < _duration!.inMilliseconds) + ? _position!.inMilliseconds / _duration!.inMilliseconds + : 0.0, + )), + ), + body: Stack(children: [ + Arrows(notes: _notes), + Positioned( + top: 50, + width: MediaQuery.of(context).size.width, + left: 0, + child: FadeTransition( + opacity: _animation, + child: Text( + _hitOrMissMessage, + textScaler: TextScaler.linear(4), + textAlign: TextAlign.center, + ), + ), + ), + Positioned( + left: MediaQuery.of(context).size.width / 2 - 50, + bottom: 50, + child: Container( + width: 100, + height: 100, + decoration: BoxDecoration( + shape: BoxShape.circle, + border: Border.all(color: Colors.black, width: 10)), + ), + ), + ])), + ); + } +} diff --git a/lib/screens/level_selection.dart b/lib/screens/level_selection.dart new file mode 100644 index 0000000..0987063 --- /dev/null +++ b/lib/screens/level_selection.dart @@ -0,0 +1,161 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:permission_handler/permission_handler.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:file_picker/file_picker.dart'; +import 'package:sense_the_rhythm/utils/esense_input.dart'; +import 'package:sense_the_rhythm/utils/simfile.dart'; +import 'package:sense_the_rhythm/widgets/connection_status_button.dart'; +import 'package:sense_the_rhythm/widgets/level_list_entry.dart'; + +class LevelSelection extends StatefulWidget { + const LevelSelection({super.key}); + + @override + State<LevelSelection> createState() => _LevelSelectionState(); +} + +class _LevelSelectionState extends State<LevelSelection> { + String? _stepmaniaCoursesPath; + List<Simfile> _stepmaniaCoursesFolders = []; + List<Simfile> _stepmaniaCoursesFoldersFiltered = []; + + @override + void initState() { + super.initState(); + _loadFolderPath(); + } + + /// gets folder path from persistent storage and updates state with loaded simfiles + Future<void> _loadFolderPath() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + final String? stepmaniaCoursesPathSetting = + prefs.getString('stepmania_courses'); + + if (stepmaniaCoursesPathSetting == null) return; + List<Simfile> stepmaniaCoursesFoldersFuture = + await _listFilesAndFolders(stepmaniaCoursesPathSetting); + + setState(() { + _stepmaniaCoursesPath = stepmaniaCoursesPathSetting; + _stepmaniaCoursesFolders = stepmaniaCoursesFoldersFuture; + _stepmaniaCoursesFoldersFiltered = stepmaniaCoursesFoldersFuture; + }); + } + + /// open folder selection dialog and save selected folder in persistent storage + Future<void> _selectFolder() async { + await Permission.manageExternalStorage.request(); + String? selectedFolder = await FilePicker.platform.getDirectoryPath(); + + if (selectedFolder != null) { + // Save the selected folder path + SharedPreferences prefs = await SharedPreferences.getInstance(); + await prefs.setString('stepmania_courses', selectedFolder); + + _loadFolderPath(); + } + } + + /// load all simfiles from a [directoryPath] + Future<List<Simfile>> _listFilesAndFolders(String directoryPath) async { + final directory = Directory(directoryPath); + try { + // List all files and folders in the directory + List<Simfile> simfiles = directory + .listSync(recursive: true) + .where((entity) => entity.path.endsWith('.sm')) + .map((entity) => Simfile(entity.path)) + .toList(); + + List<bool> successfullLoads = + await Future.wait(simfiles.map((simfile) => simfile.load())); + List<Simfile> simfilesFiltered = []; + for (int i = 0; i < simfiles.length; i++) { + if (successfullLoads[i]) { + simfilesFiltered.add(simfiles[i]); + } + } + + simfilesFiltered + .sort((a, b) => a.tags['TITLE']!.compareTo(b.tags['TITLE']!)); + + return simfilesFiltered; + } catch (e) { + print("Error reading directory: $e"); + return []; + } + } + + /// filter stepmaniaCoursesFolders based on [input] + void _filterLevels(String input) { + setState(() { + _stepmaniaCoursesFoldersFiltered = _stepmaniaCoursesFolders + .where((simfile) => simfile.tags["TITLE"]! + .toLowerCase() + .contains(input.toLowerCase())) + .toList(); + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Sense the Rhythm'), + actions: [ + Padding( + padding: const EdgeInsets.symmetric(horizontal: 8.0), + child: ValueListenableBuilder( + valueListenable: ESenseInput.instance.deviceStatus, + builder: + (BuildContext context, String deviceStatus, Widget? child) { + return ConnectionStatusButton(deviceStatus); + }, + ), + ) + ], + ), + body: Builder(builder: (context) { + if (_stepmaniaCoursesPath == null) { + return Text('Add a Directory with Stepmania Songs on \'+\''); + } else if (_stepmaniaCoursesFolders.isEmpty) { + return Text( + 'Folder empty. Add Stepmania Songs to Folder or select a different folder on \'+\''); + } else { + return Column( + children: [ + Padding( + padding: + const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0.0), + child: TextField( + onChanged: _filterLevels, + decoration: InputDecoration( + // icon: Icon(Icons.search), + hintText: 'Search'), + ), + ), + Expanded( + child: ListView.separated( + itemCount: _stepmaniaCoursesFoldersFiltered.length, + separatorBuilder: (BuildContext context, int index) => + const Divider(), + itemBuilder: (context, index) { + Simfile simfile = _stepmaniaCoursesFoldersFiltered[index]; + return LevelListEntry(simfile: simfile); + }, + ), + ), + ], + ); + } + }), + floatingActionButton: FloatingActionButton( + onPressed: () { + _selectFolder(); + }, + child: Icon(Icons.add)), + ); + } +} |