refactor: removed restart logic changes for another pr

pull/359/head
RuiAlonso 3 years ago
parent a017ec4d41
commit 45500885bb

@ -37,7 +37,6 @@ class CameraFocusingBehavior extends Component
case GameStatus.waiting: case GameStatus.waiting:
break; break;
case GameStatus.playing: case GameStatus.playing:
case GameStatus.replaying:
_zoom(_foci['game']!); _zoom(_foci['game']!);
break; break;
case GameStatus.gameOver: case GameStatus.gameOver:

@ -16,17 +16,12 @@ class GameBloc extends Bloc<GameEvent, GameState> {
on<SparkyTurboChargeActivated>(_onSparkyTurboChargeActivated); on<SparkyTurboChargeActivated>(_onSparkyTurboChargeActivated);
on<GameOver>(_onGameOver); on<GameOver>(_onGameOver);
on<GameStarted>(_onGameStarted); on<GameStarted>(_onGameStarted);
on<GameRestarted>(_onGameRestarted);
} }
void _onGameStarted(GameStarted _, Emitter emit) { void _onGameStarted(GameStarted _, Emitter emit) {
emit(state.copyWith(status: GameStatus.playing)); emit(state.copyWith(status: GameStatus.playing));
} }
void _onGameRestarted(GameRestarted _, Emitter emit) {
emit(const GameState.initial().copyWith(status: GameStatus.replaying));
}
void _onGameOver(GameOver _, Emitter emit) { void _onGameOver(GameOver _, Emitter emit) {
emit(state.copyWith(status: GameStatus.gameOver)); emit(state.copyWith(status: GameStatus.gameOver));
} }

@ -67,13 +67,6 @@ class GameStarted extends GameEvent {
List<Object?> get props => []; List<Object?> get props => [];
} }
class GameRestarted extends GameEvent {
const GameRestarted();
@override
List<Object?> get props => [];
}
class GameOver extends GameEvent { class GameOver extends GameEvent {
const GameOver(); const GameOver();

@ -70,7 +70,7 @@ class Backbox extends PositionComponent with ZIndex {
); );
} else if (state is InitialsSuccessState) { } else if (state is InitialsSuccessState) {
_display.add( _display.add(
InfoDisplay( GameOverInfoDisplay(
onShare: () { onShare: () {
_bloc.add( _bloc.add(
ShareScoreRequested( ShareScoreRequested(

@ -1,4 +1,4 @@
export 'info_display.dart'; export 'game_over_info_display.dart';
export 'initials_input_display.dart'; export 'initials_input_display.dart';
export 'initials_submission_failure_display.dart'; export 'initials_submission_failure_display.dart';
export 'initials_submission_success_display.dart'; export 'initials_submission_success_display.dart';

@ -10,11 +10,11 @@ import 'package:pinball_flame/pinball_flame.dart';
import 'package:pinball_ui/pinball_ui.dart'; import 'package:pinball_ui/pinball_ui.dart';
/// Signature for the callback called when the user tries to share their score /// Signature for the callback called when the user tries to share their score
/// from the [InfoDisplay]. /// from the [GameOverInfoDisplay].
typedef OnShareTap = void Function(); typedef OnShareTap = void Function();
/// Signature for the callback called when the user tries to navigate to the /// Signature for the callback called when the user tries to navigate to the
/// Google IO site from the [InfoDisplay]. /// Google IO site from the [GameOverInfoDisplay].
typedef OnNavigateTap = void Function(); typedef OnNavigateTap = void Function();
final _titleTextPaint = TextPaint( final _titleTextPaint = TextPaint(
@ -52,12 +52,12 @@ final _descriptionTextPaint = TextPaint(
), ),
); );
/// {@template info_display} /// {@template game_over_info_display}
/// Display that handles shows to the user share or goto IO website. /// Display that handles shows to the user share or goto IO website.
/// {@endtemplate} /// {@endtemplate}
class InfoDisplay extends Component with HasGameRef { class GameOverInfoDisplay extends Component with HasGameRef {
/// {@macro info_display} /// {@macro game_over_info_display}
InfoDisplay({ GameOverInfoDisplay({
OnShareTap? onShare, OnShareTap? onShare,
OnNavigateTap? onNavigate, OnNavigateTap? onNavigate,
}) : super( }) : super(
@ -72,7 +72,7 @@ class InfoDisplay extends Component with HasGameRef {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
gameRef.overlays.add(PinballGame.replayButtonOverlay); //gameRef.overlays.add(PinballGame.replayButtonOverlay);
} }
} }
@ -164,7 +164,7 @@ class _LinksComponent extends PositionComponent with HasGameRef {
position: Vector2(0, 9.2), position: Vector2(0, 9.2),
children: [ children: [
ShareLinkComponent(onTap: onShare), ShareLinkComponent(onTap: onShare),
GotoIOLinkComponent(onTap: onNavigate), GoogleIOLinkComponent(onTap: onNavigate),
], ],
); );
} }
@ -198,12 +198,12 @@ class ShareLinkComponent extends TextComponent with HasGameRef, Tappable {
} }
} }
/// {@template goto_io_link_component} /// {@template google_io_link_component}
/// Link button to navigate to Google I/O site. /// Link button to navigate to Google I/O site.
/// {@endtemplate} /// {@endtemplate}
class GotoIOLinkComponent extends TextComponent with HasGameRef, Tappable { class GoogleIOLinkComponent extends TextComponent with HasGameRef, Tappable {
/// {@macro goto_io_link_component} /// {@macro google_io_link_component}
GotoIOLinkComponent({ GoogleIOLinkComponent({
OnNavigateTap? onTap, OnNavigateTap? onTap,
}) : _onTap = onTap, }) : _onTap = onTap,
super( super(

@ -34,7 +34,6 @@ class StartGameBloc extends Bloc<StartGameEvent, StartGameState> {
emit( emit(
state.copyWith( state.copyWith(
status: StartGameStatus.selectCharacter, status: StartGameStatus.selectCharacter,
restarted: true,
), ),
); );
} }

@ -22,33 +22,23 @@ class StartGameState extends Equatable {
/// {@macro start_game_state} /// {@macro start_game_state}
const StartGameState({ const StartGameState({
required this.status, required this.status,
this.restarted = false,
}); });
/// Initial [StartGameState]. /// Initial [StartGameState].
const StartGameState.initial() const StartGameState.initial() : this(status: StartGameStatus.initial);
: this(
status: StartGameStatus.initial,
restarted: false,
);
/// Status of [StartGameState]. /// Status of [StartGameState].
final StartGameStatus status; final StartGameStatus status;
/// Game has been restarted from game over screen.
final bool restarted;
/// Creates a copy of [StartGameState]. /// Creates a copy of [StartGameState].
StartGameState copyWith({ StartGameState copyWith({
StartGameStatus? status, StartGameStatus? status,
bool? restarted,
}) { }) {
return StartGameState( return StartGameState(
status: status ?? this.status, status: status ?? this.status,
restarted: restarted ?? this.restarted,
); );
} }
@override @override
List<Object> get props => [status, restarted]; List<Object> get props => [status];
} }

@ -31,11 +31,7 @@ class StartGameListener extends StatelessWidget {
break; break;
case StartGameStatus.selectCharacter: case StartGameStatus.selectCharacter:
_onSelectCharacter(context); _onSelectCharacter(context);
if (state.restarted) {
context.read<GameBloc>().add(const GameRestarted());
} else {
context.read<GameBloc>().add(const GameStarted()); context.read<GameBloc>().add(const GameStarted());
}
break; break;
case StartGameStatus.howToPlay: case StartGameStatus.howToPlay:
_onHowToPlay(context); _onHowToPlay(context);

@ -38,20 +38,6 @@ void main() {
], ],
); );
blocTest<GameBloc, GameState>(
'GameRestarted restarts the game',
build: GameBloc.new,
act: (bloc) => bloc.add(const GameRestarted()),
expect: () => [
isA<GameState>()
..having(
(state) => state.status,
'status',
GameStatus.replaying,
),
],
);
group('RoundLost', () { group('RoundLost', () {
blocTest<GameBloc, GameState>( blocTest<GameBloc, GameState>(
'decreases number of rounds ' 'decreases number of rounds '

@ -98,19 +98,6 @@ void main() {
}); });
}); });
group('GameRestarted', () {
test('can be instantiated', () {
expect(const GameRestarted(), isNotNull);
});
test('supports value equality', () {
expect(
GameRestarted(),
equals(const GameRestarted()),
);
});
});
group('SparkyTurboChargeActivated', () { group('SparkyTurboChargeActivated', () {
test('can be instantiated', () { test('can be instantiated', () {
expect(const SparkyTurboChargeActivated(), isNotNull); expect(const SparkyTurboChargeActivated(), isNotNull);

@ -232,7 +232,7 @@ void main() {
await game.pump(backbox); await game.pump(backbox);
expect( expect(
game.descendants().whereType<InfoDisplay>().length, game.descendants().whereType<GameOverInfoDisplay>().length,
equals(1), equals(1),
); );
}, },

@ -8,7 +8,7 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart'; import 'package:mocktail/mocktail.dart';
import 'package:pinball/game/bloc/game_bloc.dart'; import 'package:pinball/game/bloc/game_bloc.dart';
import 'package:pinball/game/components/backbox/displays/info_display.dart'; import 'package:pinball/game/components/backbox/displays/game_over_info_display.dart';
import 'package:pinball/l10n/l10n.dart'; import 'package:pinball/l10n/l10n.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_flame/pinball_flame.dart';
@ -25,7 +25,7 @@ class _TestGame extends Forge2DGame with HasTappables {
); );
} }
Future<void> pump(InfoDisplay component) { Future<void> pump(GameOverInfoDisplay component) {
return ensureAdd( return ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: GameBloc(), value: GameBloc(),
@ -71,7 +71,7 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final component = InfoDisplay(); final component = GameOverInfoDisplay();
await game.pump(component); await game.pump(component);
expect(game.descendants(), contains(component)); expect(game.descendants(), contains(component));
}, },
@ -83,7 +83,7 @@ void main() {
var tapped = false; var tapped = false;
final tapDownInfo = _MockTapDownInfo(); final tapDownInfo = _MockTapDownInfo();
final component = InfoDisplay( final component = GameOverInfoDisplay(
onShare: () => tapped = true, onShare: () => tapped = true,
); );
await game.pump(component); await game.pump(component);
@ -103,7 +103,7 @@ void main() {
var tapped = false; var tapped = false;
final tapDownInfo = _MockTapDownInfo(); final tapDownInfo = _MockTapDownInfo();
final component = InfoDisplay( final component = GameOverInfoDisplay(
onNavigate: () => tapped = true, onNavigate: () => tapped = true,
); );
await game.pump(component); await game.pump(component);

@ -167,7 +167,7 @@ void main() {
await game.pump([component], pinballPlayer: player); await game.pump([component], pinballPlayer: player);
component.onNewState( component.onNewState(
const GameState.initial().copyWith(status: GameStatus.replaying), const GameState.initial().copyWith(status: GameStatus.playing),
); );
expect( expect(

@ -16,13 +16,12 @@ void main() {
); );
blocTest<StartGameBloc, StartGameState>( blocTest<StartGameBloc, StartGameState>(
'on ReplayTapped changes status to selectCharacter and restarted enabled', 'on ReplayTapped changes status to selectCharacter',
build: StartGameBloc.new, build: StartGameBloc.new,
act: (bloc) => bloc.add(const ReplayTapped()), act: (bloc) => bloc.add(const ReplayTapped()),
expect: () => [ expect: () => [
const StartGameState( const StartGameState(
status: StartGameStatus.selectCharacter, status: StartGameStatus.selectCharacter,
restarted: true,
) )
], ],
); );

@ -49,14 +49,12 @@ void main() {
); );
final otherState = StartGameState( final otherState = StartGameState(
status: StartGameStatus.play, status: StartGameStatus.play,
restarted: true,
); );
expect(state, isNot(equals(otherState))); expect(state, isNot(equals(otherState)));
expect( expect(
state.copyWith( state.copyWith(
status: otherState.status, status: otherState.status,
restarted: otherState.restarted,
), ),
equals(otherState), equals(otherState),
); );
@ -69,7 +67,6 @@ void main() {
testState.props, testState.props,
equals([ equals([
StartGameStatus.selectCharacter, StartGameStatus.selectCharacter,
false,
]), ]),
); );
}); });

@ -46,7 +46,7 @@ void main() {
}); });
testWidgets( testWidgets(
'calls GameStarted event only on play', 'calls GameStarted event',
(tester) async { (tester) async {
whenListen( whenListen(
startGameBloc, startGameBloc,
@ -66,38 +66,10 @@ void main() {
startGameBloc: startGameBloc, startGameBloc: startGameBloc,
); );
verifyNever(() => gameBloc.add(const GameRestarted()));
verify(() => gameBloc.add(const GameStarted())).called(1); verify(() => gameBloc.add(const GameStarted())).called(1);
}, },
); );
testWidgets(
'calls GameRestarted event only on replay',
(tester) async {
whenListen(
startGameBloc,
Stream.value(
const StartGameState(
status: StartGameStatus.selectCharacter,
restarted: true,
),
),
initialState: const StartGameState.initial(),
);
await tester.pumpApp(
const StartGameListener(
child: SizedBox.shrink(),
),
gameBloc: gameBloc,
startGameBloc: startGameBloc,
);
verifyNever(() => gameBloc.add(const GameStarted()));
verify(() => gameBloc.add(const GameRestarted())).called(1);
},
);
testWidgets( testWidgets(
'shows SelectCharacter dialog', 'shows SelectCharacter dialog',
(tester) async { (tester) async {

Loading…
Cancel
Save