diff --git a/assets/audio/Action_-_Midnight_Cruisers.wav b/assets/audio/Action_-_Midnight_Cruisers.wav new file mode 100644 index 0000000..bbf0d9f Binary files /dev/null and b/assets/audio/Action_-_Midnight_Cruisers.wav differ diff --git a/assets/audio/LightRunners.wav b/assets/audio/LightRunners.wav new file mode 100644 index 0000000..6ab1d22 Binary files /dev/null and b/assets/audio/LightRunners.wav differ diff --git a/assets/audio/Menu_-_Floating_Point.wav b/assets/audio/Menu_-_Floating_Point.wav new file mode 100644 index 0000000..e69e63b Binary files /dev/null and b/assets/audio/Menu_-_Floating_Point.wav differ diff --git a/lib/audio/audio.dart b/lib/audio/audio.dart new file mode 100644 index 0000000..f195127 --- /dev/null +++ b/lib/audio/audio.dart @@ -0,0 +1,35 @@ +import 'package:flame_audio/flame_audio.dart'; + +class AudioManager { + factory AudioManager() => _instance; + + AudioManager._(); + + static final AudioManager _instance = AudioManager._(); + + String? _currentBgm; + + static const _titlePath = 'Menu_-_Floating_Point.wav'; + static const _gamePath = 'Action_-_Midnight_Cruisers.wav'; + + void stopBgm() { + FlameAudio.bgm.stop(); + _currentBgm = null; + } + + void playTitle() => _play(_titlePath); + + void playGame() => _play(_gamePath); + + void introSfx() { + FlameAudio.play('LightRunners.wav'); + } + + void _play(String path) { + if (_currentBgm == path) { + return; + } + FlameAudio.bgm.play(path); + _currentBgm = path; + } +} diff --git a/lib/game/view/game_page.dart b/lib/game/view/game_page.dart index 51603e6..fb8605e 100644 --- a/lib/game/view/game_page.dart +++ b/lib/game/view/game_page.dart @@ -1,5 +1,6 @@ import 'package:flame/game.dart' hide Route; import 'package:flutter/material.dart'; +import 'package:lightrunners/audio/audio.dart'; import 'package:lightrunners/end_game/end_game.dart'; import 'package:lightrunners/game/game.dart'; @@ -25,6 +26,9 @@ class _GamePageState extends State { @override void initState() { super.initState(); + + AudioManager().playGame(); + _game = LightRunnersGame( players: widget.players, onEndGame: (scores) { diff --git a/lib/title/view/title_page.dart b/lib/title/view/title_page.dart index ad39e06..3f9722f 100644 --- a/lib/title/view/title_page.dart +++ b/lib/title/view/title_page.dart @@ -1,11 +1,12 @@ import 'package:flutter/material.dart'; +import 'package:lightrunners/audio/audio.dart'; import 'package:lightrunners/leaderboard/view/view.dart'; import 'package:lightrunners/lobby/view/lobby_page.dart'; import 'package:lightrunners/title/title.dart'; import 'package:lightrunners/widgets/controller_menu.dart'; import 'package:lightrunners/widgets/screen_scaffold.dart'; -class TitlePage extends StatelessWidget { +class TitlePage extends StatefulWidget { static Route route() { return MaterialPageRoute( maintainState: false, @@ -15,6 +16,24 @@ class TitlePage extends StatelessWidget { const TitlePage({super.key}); + @override + State createState() => _TitlePageState(); +} + +class _TitlePageState extends State { + @override + void initState() { + super.initState(); + + final audioManager = AudioManager(); + + audioManager.stopBgm(); + + audioManager.introSfx(); + + Future.delayed(const Duration(milliseconds: 1500), audioManager.playTitle); + } + @override Widget build(BuildContext context) { return ScreenScaffold( diff --git a/pubspec.yaml b/pubspec.yaml index c4d3014..951340e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,3 +35,4 @@ flutter: - google_fonts/ - assets/images/ - assets/images/ships/ + - assets/audio/