Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat: control running time sound effect (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago authored Mar 11, 2024
1 parent d92bb70 commit fca3461
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
30 changes: 30 additions & 0 deletions packages/trashy_road/lib/src/audio/bloc/audio_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ class AudioCubit extends Cubit<AudioState> {
}
}

Future<void> pauseEffect(GameAudioData audioData) async {
final hasPlayer = _players.containsKey(audioData);
if (hasPlayer) {
final player = _players[audioData]!;
if (player.state == PlayerState.playing) {
await player.pause();
}
}
}

Future<void> resumeEffect(GameAudioData audioData) async {
final hasPlayer = _players.containsKey(audioData);
if (hasPlayer) {
final player = _players[audioData]!;
if (player.state == PlayerState.paused) {
await player.resume();
}
}
}

Future<void> stopEffect(GameAudioData audioData) async {
final hasPlayer = _players.containsKey(audioData);
if (hasPlayer) {
final player = _players[audioData]!;
if (player.state == PlayerState.playing) {
await player.stop();
}
}
}

Future<void> playBackgroundMusic(GameAudioData audioData) async {
// TODO(alestiago): Temporarily disabled the background music.
}
Expand Down
41 changes: 22 additions & 19 deletions packages/trashy_road/lib/src/game/widgets/game_stopwatch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class _GameStopwatchState extends State<GameStopwatch>
with SingleTickerProviderStateMixin {
final _stopwatch = Stopwatch();

bool _playingRunningOutSoundEffect = false;
bool _countingDown = false;
bool _timeIsUp = false;

late final _animation = AnimationController(
vsync: this,
Expand All @@ -31,48 +32,50 @@ class _GameStopwatchState extends State<GameStopwatch>
void _stop() {
_animation.stop();
_stopwatch.stop();

if (mounted && !_timeIsUp) {
context.read<AudioCubit>().pauseEffect(GameSoundEffects.runningTime);
}
}

void _start() {
_animation.repeat(reverse: true);
_stopwatch.start();

if (mounted && _countingDown) {
context.read<AudioCubit>().resumeEffect(GameSoundEffects.runningTime);
}
}

void _reset() {
_animation.reset();
_stopwatch.reset();
_playingRunningOutSoundEffect = false;
_countingDown = false;

if (mounted) {
context.read<AudioCubit>().stopEffect(GameSoundEffects.runningTime);
}
}

void _onTick() {
if (!mounted) return;
_playRunningOutSoundEffect();

final gameBloc = context.read<GameBloc>();
final mapsBloc = context.read<GameMapsBloc>();
final map = mapsBloc.state.maps[gameBloc.state.identifier]!;

final completionSeconds = map.completionSeconds;
final timeIsUp = _stopwatch.elapsed.inSeconds >= completionSeconds;

if (timeIsUp && gameBloc.state.status == GameStatus.playing) {
gameBloc.add(const GameLostEvent(reason: GameLostReason.timeIsUp));
}
}

void _playRunningOutSoundEffect() {
if (_playingRunningOutSoundEffect) return;

final gameBloc = context.read<GameBloc>();
final mapsBloc = context.read<GameMapsBloc>();
final map = mapsBloc.state.maps[gameBloc.state.identifier]!;
final completionSeconds = map.completionSeconds;

final timeLeft = (completionSeconds * Duration.millisecondsPerSecond) -
_stopwatch.elapsed.inMilliseconds;
if (timeLeft < 10100) {
if (timeLeft < 10100 && !_countingDown) {
_countingDown = true;
context.read<AudioCubit>().playEffect(GameSoundEffects.runningTime);
_playingRunningOutSoundEffect = true;
}

_timeIsUp = _stopwatch.elapsed.inSeconds >= completionSeconds;
if (_timeIsUp && gameBloc.state.status == GameStatus.playing) {
gameBloc.add(const GameLostEvent(reason: GameLostReason.timeIsUp));
}
}

Expand Down

0 comments on commit fca3461

Please sign in to comment.