Merge branch 'main' into release

release
Tom Arra 3 years ago
commit a88b57759c

@ -13,7 +13,8 @@ class BallSpawningBehavior extends Component
bool listenWhen(GameState? previousState, GameState newState) {
if (!newState.status.isPlaying) return false;
final startedGame = previousState?.status.isWaiting ?? true;
final startedGame = (previousState?.status.isWaiting ?? true) ||
(previousState?.status.isGameOver ?? true);
final lostRound =
(previousState?.rounds ?? newState.rounds + 1) > newState.rounds;
return startedGame || lostRound;

@ -19,7 +19,7 @@ class GameBloc extends Bloc<GameEvent, GameState> {
static const _maxScore = 9999999999;
void _onGameStarted(GameStarted _, Emitter emit) {
emit(state.copyWith(status: GameStatus.playing));
emit(const GameState.initial().copyWith(status: GameStatus.playing));
}
void _onGameOver(GameOver _, Emitter emit) {

@ -66,7 +66,7 @@ class GameOverInfoDisplay extends Component with HasGameRef {
@override
Future<void> onLoad() async {
await super.onLoad();
gameRef.overlays.add(PinballGame.playButtonOverlay);
gameRef.overlays.add(PinballGame.replayButtonOverlay);
}
}
@ -290,7 +290,7 @@ class OpenSourceTextComponent extends TextComponent with HasGameRef, Tappable {
);
@override
bool onTapDown(TapDownInfo info) {
bool onTapUp(TapUpInfo info) {
openLink(ShareRepository.openSourceCode);
return true;
}

@ -22,6 +22,7 @@ class GameBlocStatusListener extends Component
break;
case GameStatus.playing:
readProvider<PinballAudioPlayer>().play(PinballAudio.backgroundMusic);
_resetBonuses();
gameRef
.descendants()
.whereType<Flipper>()
@ -32,6 +33,7 @@ class GameBlocStatusListener extends Component
.forEach(_addPlungerBehaviors);
gameRef.overlays.remove(PinballGame.playButtonOverlay);
gameRef.overlays.remove(PinballGame.replayButtonOverlay);
break;
case GameStatus.gameOver:
readProvider<PinballAudioPlayer>().play(PinballAudio.gameOverVoiceOver);
@ -54,6 +56,15 @@ class GameBlocStatusListener extends Component
}
}
void _resetBonuses() {
gameRef
.descendants()
.whereType<FlameBlocProvider<GoogleWordCubit, GoogleWordState>>()
.single
.bloc
.onReset();
}
void _addPlungerBehaviors(Plunger plunger) {
final platformHelper = readProvider<PlatformHelper>();
const pullingStrength = 7.0;

@ -17,7 +17,7 @@ class GoogleWordBonusBehavior extends Component {
onNewState: (state) {
readBloc<GameBloc, GameState>()
.add(const BonusActivated(GameBonus.googleWord));
readBloc<GoogleWordCubit, GoogleWordState>().onBonusAwarded();
readBloc<GoogleWordCubit, GoogleWordState>().onReset();
add(BonusBallSpawningBehavior());
add(GoogleWordAnimatingBehavior());
},

@ -38,10 +38,13 @@ class PinballGame extends PinballForge2DGame
images.prefix = '';
}
/// Identifier of the play button overlay
/// Identifier of the play button overlay.
static const playButtonOverlay = 'play_button';
/// Identifier of the mobile controls overlay
/// Identifier of the replay button overlay.
static const replayButtonOverlay = 'replay_button';
/// Identifier of the mobile controls overlay.
static const mobileControlsOverlay = 'mobile_controls';
@override

@ -100,22 +100,25 @@ class PinballGameLoadedView extends StatelessWidget {
focusNode: game.focusNode,
initialActiveOverlays: const [PinballGame.playButtonOverlay],
overlayBuilderMap: {
PinballGame.playButtonOverlay: (context, game) {
return const Positioned(
PinballGame.playButtonOverlay: (_, game) => const Positioned(
bottom: 20,
right: 0,
left: 0,
child: PlayButtonOverlay(),
);
},
PinballGame.mobileControlsOverlay: (context, game) {
return Positioned(
),
PinballGame.mobileControlsOverlay: (_, game) => Positioned(
bottom: 0,
left: 0,
right: 0,
child: MobileControls(game: game),
);
},
),
PinballGame.replayButtonOverlay: (context, game) =>
const Positioned(
bottom: 20,
right: 0,
left: 0,
child: ReplayButtonOverlay(),
)
},
),
),

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart';
import 'package:pinball/start_game/start_game.dart';
import 'package:pinball_ui/pinball_ui.dart';
@ -18,6 +19,7 @@ class ReplayButtonOverlay extends StatelessWidget {
return PinballButton(
text: l10n.replay,
onTap: () {
context.read<GameBloc>().add(const GameStarted());
context.read<StartGameBloc>().add(const ReplayTapped());
},
);

@ -213,7 +213,7 @@
"@socialMediaAccount": {
"description": "Text displayed on share screen for description"
},
"iGotScoreAtPinball": "I got {score} at the #IOPinball machine, can you beat my score? See you at #GoogleIO!",
"iGotScoreAtPinball": "I got {score} points in #IOPinball, can you beat my score? \nSee you at #GoogleIO!",
"@iGotScoreAtPinball": {
"description": "Text to share score on Social Network",
"placeholders": {

@ -1,6 +1,8 @@
import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/android_animatronic/behaviors/behaviors.dart';
import 'package:pinball_flame/pinball_flame.dart';
/// {@template android_animatronic}
@ -13,6 +15,7 @@ class AndroidAnimatronic extends BodyComponent
: super(
children: [
_AndroidAnimatronicSpriteAnimationComponent(),
AndroidAnimatronicBallContactBehavior(),
...?children,
],
renderBody: false,
@ -21,6 +24,13 @@ class AndroidAnimatronic extends BodyComponent
zIndex = ZIndexes.androidHead;
}
/// Creates an [AndroidAnimatronic] without any children.
///
/// This can be used for testing [AndroidAnimatronic]'s behaviors in
/// isolation.
@visibleForTesting
AndroidAnimatronic.test();
@override
Body createBody() {
final shape = EllipseShape(

@ -1,18 +1,15 @@
// ignore_for_file: public_member_api_docs
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
class AndroidSpaceshipEntranceBallContactBehavior
extends ContactBehavior<AndroidSpaceshipEntrance>
with FlameBlocReader<AndroidSpaceshipCubit, AndroidSpaceshipState> {
class AndroidAnimatronicBallContactBehavior
extends ContactBehavior<AndroidAnimatronic> {
@override
void beginContact(Object other, Contact contact) {
super.beginContact(other, contact);
if (other is! Ball) return;
bloc.onBallEntered();
readBloc<AndroidSpaceshipCubit, AndroidSpaceshipState>().onBallContacted();
}
}

@ -0,0 +1 @@
export 'android_animatronic_ball_contact_behavior.dart.dart';

@ -5,7 +5,6 @@ import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/android_spaceship/behaviors/behaviors.dart';
import 'package:pinball_flame/pinball_flame.dart';
export 'cubit/android_spaceship_cubit.dart';
@ -17,9 +16,6 @@ class AndroidSpaceship extends Component {
_SpaceshipSaucer()..initialPosition = position,
_SpaceshipSaucerSpriteAnimationComponent()..position = position,
_LightBeamSpriteComponent()..position = position + Vector2(2.5, 5),
AndroidSpaceshipEntrance(
children: [AndroidSpaceshipEntranceBallContactBehavior()],
),
_SpaceshipHole(
outsideLayer: Layer.spaceshipExitRail,
outsidePriority: ZIndexes.ballOnSpaceshipRail,
@ -134,35 +130,6 @@ class _LightBeamSpriteComponent extends SpriteComponent
}
}
class AndroidSpaceshipEntrance extends BodyComponent
with ParentIsA<AndroidSpaceship>, Layered {
AndroidSpaceshipEntrance({Iterable<Component>? children})
: super(
children: children,
renderBody: false,
) {
layer = Layer.spaceship;
}
@override
Body createBody() {
final shape = PolygonShape()
..setAsBox(
2,
0.1,
Vector2(-27.4, -37.2),
-0.12,
);
final fixtureDef = FixtureDef(
shape,
isSensor: true,
);
final bodyDef = BodyDef();
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}
class _SpaceshipHole extends LayerSensor {
_SpaceshipHole({required Layer outsideLayer, required int outsidePriority})
: super(

@ -1 +0,0 @@
export 'android_spaceship_entrance_ball_contact_behavior.dart.dart';

@ -5,7 +5,7 @@ part 'android_spaceship_state.dart';
class AndroidSpaceshipCubit extends Cubit<AndroidSpaceshipState> {
AndroidSpaceshipCubit() : super(AndroidSpaceshipState.withoutBonus);
void onBallEntered() => emit(AndroidSpaceshipState.withBonus);
void onBallContacted() => emit(AndroidSpaceshipState.withBonus);
void onBonusAwarded() => emit(AndroidSpaceshipState.withoutBonus);
}

@ -1,4 +1,4 @@
export 'android_animatronic.dart';
export 'android_animatronic/android_animatronic.dart';
export 'android_bumper/android_bumper.dart';
export 'android_spaceship/android_spaceship.dart';
export 'arcade_background/arcade_background.dart';

@ -17,7 +17,7 @@ class GoogleWordAnimatingBehavior extends TimerComponent
_blinks++;
} else {
timer.stop();
bloc.onAnimationFinished();
bloc.onReset();
shouldRemove = true;
}
}

@ -68,7 +68,7 @@ class GoogleWordCubit extends Cubit<GoogleWordState> {
);
}
void onAnimationFinished() {
void onReset() {
emit(GoogleWordState.initial());
_lastLitLetter = 0;
}

@ -7,7 +7,7 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/android_spaceship/behaviors/behaviors.dart';
import 'package:pinball_components/src/components/android_animatronic/behaviors/behaviors.dart';
import '../../../../helpers/helpers.dart';
@ -23,19 +23,19 @@ void main() {
final flameTester = FlameTester(TestGame.new);
group(
'AndroidSpaceshipEntranceBallContactBehavior',
'AndroidAnimatronicBallContactBehavior',
() {
test('can be instantiated', () {
expect(
AndroidSpaceshipEntranceBallContactBehavior(),
isA<AndroidSpaceshipEntranceBallContactBehavior>(),
AndroidAnimatronicBallContactBehavior(),
isA<AndroidAnimatronicBallContactBehavior>(),
);
});
flameTester.test(
'beginContact calls onBallEntered when entrance contacts with a ball',
'beginContact calls onBallContacted when in contact with a ball',
(game) async {
final behavior = AndroidSpaceshipEntranceBallContactBehavior();
final behavior = AndroidAnimatronicBallContactBehavior();
final bloc = _MockAndroidSpaceshipCubit();
whenListen(
bloc,
@ -43,20 +43,20 @@ void main() {
initialState: AndroidSpaceshipState.withoutBonus,
);
final entrance = AndroidSpaceshipEntrance();
final animatronic = AndroidAnimatronic.test();
final androidSpaceship = FlameBlocProvider<AndroidSpaceshipCubit,
AndroidSpaceshipState>.value(
value: bloc,
children: [
AndroidSpaceship.test(children: [entrance])
AndroidSpaceship.test(children: [animatronic])
],
);
await entrance.add(behavior);
await animatronic.add(behavior);
await game.ensureAdd(androidSpaceship);
behavior.beginContact(_MockBall(), _MockContact());
verify(bloc.onBallEntered).called(1);
verify(bloc.onBallContacted).called(1);
},
);
},

@ -4,6 +4,7 @@ import 'package:flame/components.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/android_animatronic/behaviors/behaviors.dart';
import '../../helpers/helpers.dart';
@ -58,7 +59,8 @@ void main() {
},
);
flameTester.test('adds new children', (game) async {
group('adds', () {
flameTester.test('new children', (game) async {
final component = Component();
final androidAnimatronic = AndroidAnimatronic(
children: [component],
@ -66,5 +68,17 @@ void main() {
await game.ensureAdd(androidAnimatronic);
expect(androidAnimatronic.children, contains(component));
});
flameTester.test('a AndroidAnimatronicBallContactBehavior', (game) async {
final androidAnimatronic = AndroidAnimatronic();
await game.ensureAdd(androidAnimatronic);
expect(
androidAnimatronic.children
.whereType<AndroidAnimatronicBallContactBehavior>()
.single,
isNotNull,
);
});
});
});
}

@ -6,7 +6,6 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/android_spaceship/behaviors/behaviors.dart';
import 'package:pinball_flame/pinball_flame.dart';
import '../../../helpers/helpers.dart';
@ -84,26 +83,5 @@ void main() {
);
},
);
flameTester.test(
'AndroidSpaceshipEntrance has an '
'AndroidSpaceshipEntranceBallContactBehavior', (game) async {
final androidSpaceship = AndroidSpaceship(position: Vector2.zero());
final provider =
FlameBlocProvider<AndroidSpaceshipCubit, AndroidSpaceshipState>.value(
value: bloc,
children: [androidSpaceship],
);
await game.ensureAdd(provider);
final androidSpaceshipEntrance =
androidSpaceship.firstChild<AndroidSpaceshipEntrance>();
expect(
androidSpaceshipEntrance!.children
.whereType<AndroidSpaceshipEntranceBallContactBehavior>()
.single,
isNotNull,
);
});
});
}

@ -9,7 +9,7 @@ void main() {
blocTest<AndroidSpaceshipCubit, AndroidSpaceshipState>(
'onBallEntered emits withBonus',
build: AndroidSpaceshipCubit.new,
act: (bloc) => bloc.onBallEntered(),
act: (bloc) => bloc.onBallContacted(),
expect: () => [AndroidSpaceshipState.withBonus],
);

@ -41,7 +41,7 @@ void main() {
);
flameTester.testGameWidget(
'calls onAnimationFinished and removes itself '
'calls onReset and removes itself '
'after all blinks complete',
setUp: (game, tester) async {
final behavior = GoogleWordAnimatingBehavior();
@ -53,7 +53,7 @@ void main() {
}
await game.ready();
verify(bloc.onAnimationFinished).called(1);
verify(bloc.onReset).called(1);
expect(
game.descendants().whereType<GoogleWordAnimatingBehavior>().isEmpty,
isTrue,

@ -62,9 +62,9 @@ void main() {
);
blocTest<GoogleWordCubit, GoogleWordState>(
'onAnimationFinished emits initial state',
'onReset emits initial state',
build: GoogleWordCubit.new,
act: (bloc) => bloc.onAnimationFinished(),
act: (bloc) => bloc.onReset(),
expect: () => [GoogleWordState.initial()],
);
},

@ -176,7 +176,7 @@ void main() {
final openSourceLink =
component.descendants().whereType<OpenSourceTextComponent>().first;
openSourceLink.onTapDown(_MockTapDownInfo());
openSourceLink.onTapUp(_MockTapUpInfo());
await game.ready();

@ -37,6 +37,7 @@ class _TestGame extends Forge2DGame with HasTappables {
Iterable<Component> children, {
PinballAudioPlayer? pinballAudioPlayer,
PlatformHelper? platformHelper,
GoogleWordCubit? googleWordBloc,
}) async {
return ensureAdd(
FlameMultiBlocProvider(
@ -47,6 +48,9 @@ class _TestGame extends Forge2DGame with HasTappables {
FlameBlocProvider<CharacterThemeCubit, CharacterThemeState>.value(
value: CharacterThemeCubit(),
),
FlameBlocProvider<GoogleWordCubit, GoogleWordState>.value(
value: googleWordBloc ?? GoogleWordCubit(),
),
],
children: [
MultiFlameProvider(
@ -80,6 +84,8 @@ class _MockPlatformHelper extends Mock implements PlatformHelper {}
class _MockPlungerCubit extends Mock implements PlungerCubit {}
class _MockGoogleWordCubit extends Mock implements GoogleWordCubit {}
class _MockAppLocalizations extends Mock implements AppLocalizations {
@override
String get score => '';
@ -332,6 +338,20 @@ void main() {
},
);
flameTester.test(
'resets the GoogleWordCubit',
(game) async {
final googleWordBloc = _MockGoogleWordCubit();
final component = GameBlocStatusListener();
await game.pump([component], googleWordBloc: googleWordBloc);
expect(state.status, equals(GameStatus.playing));
component.onNewState(state);
verify(googleWordBloc.onReset).called(1);
},
);
flameTester.test(
'adds FlipperKeyControllingBehavior to Flippers',
(game) async {

@ -75,7 +75,7 @@ void main() {
flameTester.testGameWidget(
'adds GameBonus.googleWord to the game when all letters '
'in google word are activated and calls onBonusAwarded',
'in google word are activated and calls onReset',
setUp: (game, tester) async {
final behavior = GoogleWordBonusBehavior();
final parent = GoogleGallery.test();
@ -114,7 +114,7 @@ void main() {
verify(
() => gameBloc.add(const BonusActivated(GameBonus.googleWord)),
).called(1);
verify(googleWordBloc.onBonusAwarded).called(1);
verify(googleWordBloc.onReset).called(1);
},
);

@ -307,6 +307,23 @@ void main() {
expect(find.byType(MobileControls), findsOneWidget);
});
testWidgets(
'ReplayButtonOverlay when the overlay is added',
(tester) async {
await tester.pumpApp(
PinballGameView(game),
gameBloc: gameBloc,
startGameBloc: startGameBloc,
);
game.overlays.add(PinballGame.replayButtonOverlay);
await tester.pump();
expect(find.byType(ReplayButtonOverlay), findsOneWidget);
},
);
group('info icon', () {
testWidgets('renders on game over', (tester) async {
final gameState = GameState.initial().copyWith(

@ -8,24 +8,32 @@ import '../../../helpers/helpers.dart';
class _MockStartGameBloc extends Mock implements StartGameBloc {}
class _MockGameBloc extends Mock implements GameBloc {}
void main() {
group('ReplayButtonOverlay', () {
late StartGameBloc startGameBloc;
late _MockGameBloc gameBloc;
setUp(() async {
await mockFlameImages();
startGameBloc = _MockStartGameBloc();
gameBloc = _MockGameBloc();
whenListen(
startGameBloc,
Stream.value(const StartGameState.initial()),
initialState: const StartGameState.initial(),
);
whenListen(
gameBloc,
Stream.value(const GameState.initial()),
initialState: const GameState.initial(),
);
});
testWidgets('renders correctly', (tester) async {
await tester.pumpApp(const ReplayButtonOverlay());
expect(find.text('Replay'), findsOneWidget);
});
@ -33,6 +41,7 @@ void main() {
(tester) async {
await tester.pumpApp(
const ReplayButtonOverlay(),
gameBloc: gameBloc,
startGameBloc: startGameBloc,
);
@ -41,5 +50,19 @@ void main() {
verify(() => startGameBloc.add(const ReplayTapped())).called(1);
});
testWidgets('adds GameStarted event to GameBloc when tapped',
(tester) async {
await tester.pumpApp(
const ReplayButtonOverlay(),
gameBloc: gameBloc,
startGameBloc: startGameBloc,
);
await tester.tap(find.text('Replay'));
await tester.pump();
verify(() => gameBloc.add(const GameStarted())).called(1);
});
});
}

Loading…
Cancel
Save