From 0080f9c8d3035918556316c1a39c0e9b552bf848 Mon Sep 17 00:00:00 2001 From: Iman Khoshabi Date: Wed, 20 Nov 2024 00:03:49 +0100 Subject: [PATCH] Fix the auto jump loop issue --- .../bloc/multiplayer/multiplayer_cubit.dart | 14 +++++++++++++- .../component/dash/auto_jump_dash.dart | 6 +++++- lib/presentation/component/dash/dash.dart | 5 ++++- .../multiplayer/game/multiplayer_game_page.dart | 1 + 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/presentation/bloc/multiplayer/multiplayer_cubit.dart b/lib/presentation/bloc/multiplayer/multiplayer_cubit.dart index a99804e..c76c56e 100644 --- a/lib/presentation/bloc/multiplayer/multiplayer_cubit.dart +++ b/lib/presentation/bloc/multiplayer/multiplayer_cubit.dart @@ -372,7 +372,9 @@ class MultiplayerCubit extends Cubit { )); if (state.isCurrentPlayerAutoJump) { const Duration(milliseconds: 300); - startPlaying(); + if (!isClosed) { + startPlaying(); + } } } } @@ -460,4 +462,14 @@ class MultiplayerCubit extends Cubit { return super.close(); } + + void onGamePageOpened(String matchId) async { + assert(matchId == state.matchId); + if (state.isCurrentPlayerAutoJump) { + await Future.delayed(const Duration(milliseconds: 300)); + if (!isClosed) { + startPlaying(); + } + } + } } diff --git a/lib/presentation/component/dash/auto_jump_dash.dart b/lib/presentation/component/dash/auto_jump_dash.dart index b3a3ff9..471b140 100644 --- a/lib/presentation/component/dash/auto_jump_dash.dart +++ b/lib/presentation/component/dash/auto_jump_dash.dart @@ -84,7 +84,6 @@ class AutoJumpDash extends Component with ParentIsA { } else { return; } - randomFail = false; } if (position.y > getBottomLineEdge(nextPipe) && velocityY > 10) { @@ -92,6 +91,11 @@ class AutoJumpDash extends Component with ParentIsA { } } + void onDashDied() { + randomFail = false; + _nextPipe = null; + } + @override void render(Canvas canvas) { super.render(canvas); diff --git a/lib/presentation/component/dash/dash.dart b/lib/presentation/component/dash/dash.dart index 2b4c17b..eeae21e 100644 --- a/lib/presentation/component/dash/dash.dart +++ b/lib/presentation/component/dash/dash.dart @@ -61,6 +61,8 @@ class Dash extends PositionComponent final bool autoJump; + AutoJumpDash? _autoJumpDash; + set isNameVisible(bool value) { _isNameVisible = value; if (value) { @@ -101,7 +103,7 @@ class Dash extends PositionComponent _resetCorrectPositionAfter(); if (autoJump) { - add(AutoJumpDash()); + add(_autoJumpDash = AutoJumpDash()); } } @@ -260,6 +262,7 @@ class Dash extends PositionComponent other.removeFromParent(); } else if (other is Pipe) { game.gameOver(x, y, _velocityY); + _autoJumpDash?.onDashDied(); } } } diff --git a/lib/presentation/pages/multiplayer/game/multiplayer_game_page.dart b/lib/presentation/pages/multiplayer/game/multiplayer_game_page.dart index 5796bf5..c31e2e6 100644 --- a/lib/presentation/pages/multiplayer/game/multiplayer_game_page.dart +++ b/lib/presentation/pages/multiplayer/game/multiplayer_game_page.dart @@ -53,6 +53,7 @@ class _MultiPlayerGamePageState extends State { multiplayerCubit, leaderboardCubit, ); + multiplayerCubit.onGamePageOpened(widget.matchId); super.initState(); }