mirror of https://github.com/flutter/pinball.git
commit
f09a825c7f
@ -1,76 +0,0 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pinball/l10n/l10n.dart';
|
||||
import 'package:pinball_ui/pinball_ui.dart';
|
||||
|
||||
/// {@template footer}
|
||||
/// Footer widget with links to the main tech stack.
|
||||
/// {@endtemplate}
|
||||
class Footer extends StatelessWidget {
|
||||
/// {@macro footer}
|
||||
const Footer({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(50, 0, 50, 32),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: const [
|
||||
_MadeWithFlutterAndFirebase(),
|
||||
_GoogleIO(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _GoogleIO extends StatelessWidget {
|
||||
const _GoogleIO({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final theme = Theme.of(context);
|
||||
return Text(
|
||||
l10n.footerGoogleIOText,
|
||||
style: theme.textTheme.bodyText1!.copyWith(color: PinballColors.white),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MadeWithFlutterAndFirebase extends StatelessWidget {
|
||||
const _MadeWithFlutterAndFirebase({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final theme = Theme.of(context);
|
||||
return RichText(
|
||||
textAlign: TextAlign.center,
|
||||
text: TextSpan(
|
||||
text: l10n.footerMadeWithText,
|
||||
style: theme.textTheme.bodyText1!.copyWith(color: PinballColors.white),
|
||||
children: <TextSpan>[
|
||||
TextSpan(
|
||||
text: l10n.footerFlutterLinkText,
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () => openLink('https://flutter.dev'),
|
||||
style: const TextStyle(
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
const TextSpan(text: ' & '),
|
||||
TextSpan(
|
||||
text: l10n.footerFirebaseLinkText,
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () => openLink('https://firebase.google.com'),
|
||||
style: const TextStyle(
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
export 'ball_spawning_behavior.dart';
|
||||
export 'ball_theming_behavior.dart';
|
||||
export 'bonus_noise_behavior.dart';
|
||||
export 'bumper_noise_behavior.dart';
|
||||
export 'camera_focusing_behavior.dart';
|
||||
export 'scoring_behavior.dart';
|
||||
|
@ -0,0 +1,41 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_audio/pinball_audio.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
/// Behavior that handles playing a bonus sound effect
|
||||
class BonusNoiseBehavior extends Component {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await add(
|
||||
FlameBlocListener<GameBloc, GameState>(
|
||||
listenWhen: (previous, current) {
|
||||
return previous.bonusHistory.length != current.bonusHistory.length;
|
||||
},
|
||||
onNewState: (state) {
|
||||
final bonus = state.bonusHistory.last;
|
||||
final audioPlayer = readProvider<PinballPlayer>();
|
||||
|
||||
switch (bonus) {
|
||||
case GameBonus.googleWord:
|
||||
audioPlayer.play(PinballAudio.google);
|
||||
break;
|
||||
case GameBonus.sparkyTurboCharge:
|
||||
audioPlayer.play(PinballAudio.sparky);
|
||||
break;
|
||||
case GameBonus.dinoChomp:
|
||||
// TODO(erickzanardo): Add sound
|
||||
break;
|
||||
case GameBonus.androidSpaceship:
|
||||
// TODO(erickzanardo): Add sound
|
||||
break;
|
||||
case GameBonus.dashNest:
|
||||
// TODO(erickzanardo): Add sound
|
||||
break;
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
// ignore_for_file: avoid_renaming_method_parameters
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
import 'package:pinball_theme/pinball_theme.dart';
|
||||
|
||||
/// {@template controlled_ball}
|
||||
/// A [Ball] with a [BallController] attached.
|
||||
///
|
||||
/// When a [Ball] is lost, if there aren't more [Ball]s in play and the game is
|
||||
/// not over, a new [Ball] will be spawned.
|
||||
/// {@endtemplate}
|
||||
class ControlledBall extends Ball with Controls<BallController> {
|
||||
/// A [Ball] that launches from the [Plunger].
|
||||
ControlledBall.launch({
|
||||
required CharacterTheme characterTheme,
|
||||
}) : super(assetPath: characterTheme.ball.keyName) {
|
||||
controller = BallController(this);
|
||||
layer = Layer.launcher;
|
||||
zIndex = ZIndexes.ballOnLaunchRamp;
|
||||
}
|
||||
|
||||
/// {@macro controlled_ball}
|
||||
ControlledBall.bonus({
|
||||
required CharacterTheme characterTheme,
|
||||
}) : super(assetPath: characterTheme.ball.keyName) {
|
||||
controller = BallController(this);
|
||||
zIndex = ZIndexes.ballOnBoard;
|
||||
}
|
||||
|
||||
/// [Ball] used in [DebugPinballGame].
|
||||
ControlledBall.debug() : super() {
|
||||
controller = BallController(this);
|
||||
zIndex = ZIndexes.ballOnBoard;
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template ball_controller}
|
||||
/// Controller attached to a [Ball] that handles its game related logic.
|
||||
/// {@endtemplate}
|
||||
class BallController extends ComponentController<Ball>
|
||||
with FlameBlocReader<GameBloc, GameState> {
|
||||
/// {@macro ball_controller}
|
||||
BallController(Ball ball) : super(ball);
|
||||
|
||||
/// Stops the [Ball] inside of the [SparkyComputer] while the turbo charge
|
||||
/// sequence runs, then boosts the ball out of the computer.
|
||||
Future<void> turboCharge() async {
|
||||
bloc.add(const SparkyTurboChargeActivated());
|
||||
|
||||
component.stop();
|
||||
// TODO(alestiago): Refactor this hard coded duration once the following is
|
||||
// merged:
|
||||
// https://github.com/flame-engine/flame/pull/1564
|
||||
await Future<void>.delayed(
|
||||
const Duration(milliseconds: 2583),
|
||||
);
|
||||
component.resume();
|
||||
await component.add(
|
||||
BallTurboChargingBehavior(impulse: Vector2(40, 110)),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
export 'sparky_computer_bonus_behavior.dart';
|
@ -0,0 +1,28 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
/// Adds a [GameBonus.sparkyTurboCharge] when a [Ball] enters the
|
||||
/// [SparkyComputer].
|
||||
class SparkyComputerBonusBehavior extends Component
|
||||
with ParentIsA<SparkyScorch>, FlameBlocReader<GameBloc, GameState> {
|
||||
@override
|
||||
void onMount() {
|
||||
super.onMount();
|
||||
final sparkyComputer = parent.firstChild<SparkyComputer>()!;
|
||||
final animatronic = parent.firstChild<SparkyAnimatronic>()!;
|
||||
|
||||
// TODO(alestiago): Refactor subscription management once the following is
|
||||
// merged:
|
||||
// https://github.com/flame-engine/flame/pull/1538
|
||||
sparkyComputer.bloc.stream.listen((state) async {
|
||||
final listenWhen = state == SparkyComputerState.withBall;
|
||||
if (!listenWhen) return;
|
||||
|
||||
bloc.add(const BonusActivated(GameBonus.sparkyTurboCharge));
|
||||
animatronic.playing = true;
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
export 'more_information_dialog.dart';
|
@ -0,0 +1,218 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pinball/l10n/l10n.dart';
|
||||
import 'package:pinball_ui/pinball_ui.dart';
|
||||
|
||||
/// Inflates [MoreInformationDialog] using [showDialog].
|
||||
Future<void> showMoreInformationDialog(BuildContext context) {
|
||||
final gameWidgetWidth = MediaQuery.of(context).size.height * 9 / 16;
|
||||
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
barrierColor: PinballColors.transparent,
|
||||
barrierDismissible: true,
|
||||
builder: (_) {
|
||||
return Center(
|
||||
child: SizedBox(
|
||||
height: gameWidgetWidth * 0.87,
|
||||
width: gameWidgetWidth,
|
||||
child: const MoreInformationDialog(),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// {@template more_information_dialog}
|
||||
/// Dialog used to show informational links
|
||||
/// {@endtemplate}
|
||||
class MoreInformationDialog extends StatelessWidget {
|
||||
/// {@macro more_information_dialog}
|
||||
const MoreInformationDialog({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Material(
|
||||
color: PinballColors.transparent,
|
||||
child: _LinkBoxDecoration(
|
||||
child: Column(
|
||||
children: const [
|
||||
SizedBox(height: 16),
|
||||
_LinkBoxHeader(),
|
||||
Expanded(
|
||||
child: _LinkBoxBody(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LinkBoxHeader extends StatelessWidget {
|
||||
const _LinkBoxHeader({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final indent = MediaQuery.of(context).size.width / 5;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Text(
|
||||
l10n.linkBoxTitle,
|
||||
style: Theme.of(context).textTheme.headline3!.copyWith(
|
||||
color: PinballColors.blue,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Divider(
|
||||
color: PinballColors.white,
|
||||
endIndent: indent,
|
||||
indent: indent,
|
||||
thickness: 2,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LinkBoxDecoration extends StatelessWidget {
|
||||
const _LinkBoxDecoration({
|
||||
Key? key,
|
||||
required this.child,
|
||||
}) : super(key: key);
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DecoratedBox(
|
||||
decoration: const CrtBackground().copyWith(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(12)),
|
||||
border: Border.all(
|
||||
color: PinballColors.white,
|
||||
width: 5,
|
||||
),
|
||||
),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LinkBoxBody extends StatelessWidget {
|
||||
const _LinkBoxBody({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
const _MadeWithFlutterAndFirebase(),
|
||||
_TextLink(
|
||||
text: l10n.linkBoxOpenSourceCode,
|
||||
link: _MoreInformationUrl.openSourceCode,
|
||||
),
|
||||
_TextLink(
|
||||
text: l10n.linkBoxGoogleIOText,
|
||||
link: _MoreInformationUrl.googleIOEvent,
|
||||
),
|
||||
_TextLink(
|
||||
text: l10n.linkBoxFlutterGames,
|
||||
link: _MoreInformationUrl.flutterGamesWebsite,
|
||||
),
|
||||
_TextLink(
|
||||
text: l10n.linkBoxHowItsMade,
|
||||
link: _MoreInformationUrl.howItsMadeArticle,
|
||||
),
|
||||
_TextLink(
|
||||
text: l10n.linkBoxTermsOfService,
|
||||
link: _MoreInformationUrl.termsOfService,
|
||||
),
|
||||
_TextLink(
|
||||
text: l10n.linkBoxPrivacyPolicy,
|
||||
link: _MoreInformationUrl.privacyPolicy,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TextLink extends StatelessWidget {
|
||||
const _TextLink({
|
||||
Key? key,
|
||||
required this.text,
|
||||
required this.link,
|
||||
}) : super(key: key);
|
||||
|
||||
final String text;
|
||||
final String link;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return InkWell(
|
||||
onTap: () => openLink(link),
|
||||
child: Text(
|
||||
text,
|
||||
style: theme.textTheme.headline5!.copyWith(
|
||||
color: PinballColors.white,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MadeWithFlutterAndFirebase extends StatelessWidget {
|
||||
const _MadeWithFlutterAndFirebase({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final theme = Theme.of(context);
|
||||
return RichText(
|
||||
textAlign: TextAlign.center,
|
||||
text: TextSpan(
|
||||
text: l10n.linkBoxMadeWithText,
|
||||
style: theme.textTheme.headline5!.copyWith(color: PinballColors.white),
|
||||
children: <TextSpan>[
|
||||
TextSpan(
|
||||
text: l10n.linkBoxFlutterLinkText,
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () => openLink(_MoreInformationUrl.flutterWebsite),
|
||||
style: const TextStyle(
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
const TextSpan(text: ' & '),
|
||||
TextSpan(
|
||||
text: l10n.linkBoxFirebaseLinkText,
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () => openLink(_MoreInformationUrl.firebaseWebsite),
|
||||
style: theme.textTheme.headline5!.copyWith(
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _MoreInformationUrl {
|
||||
static const flutterWebsite = 'https://flutter.dev';
|
||||
static const firebaseWebsite = 'https://firebase.google.com';
|
||||
static const openSourceCode = 'https://github.com/VGVentures/pinball';
|
||||
static const googleIOEvent = 'https://events.google.com/io/';
|
||||
static const flutterGamesWebsite = 'http://flutter.dev/games';
|
||||
static const howItsMadeArticle =
|
||||
'https://medium.com/flutter/i-o-pinball-powered-by-flutter-and-firebase-d22423f3f5d';
|
||||
static const termsOfService = 'https://policies.google.com/terms';
|
||||
static const privacyPolicy = 'https://policies.google.com/privacy';
|
||||
}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
export 'sparky_computer_sensor_ball_contact_behavior.dart';
|
@ -0,0 +1,35 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
/// {@template sparky_computer_sensor_ball_contact_behavior}
|
||||
/// When a [Ball] enters the [SparkyComputer] it is stopped for a period of time
|
||||
/// before a [BallTurboChargingBehavior] is applied to it.
|
||||
/// {@endtemplate}
|
||||
class SparkyComputerSensorBallContactBehavior
|
||||
extends ContactBehavior<SparkyComputer> {
|
||||
@override
|
||||
Future<void> beginContact(Object other, Contact contact) async {
|
||||
super.beginContact(other, contact);
|
||||
if (other is! Ball) return;
|
||||
|
||||
other.stop();
|
||||
parent.bloc.onBallEntered();
|
||||
await parent.add(
|
||||
TimerComponent(
|
||||
period: 1.5,
|
||||
removeOnFinish: true,
|
||||
onTick: () async {
|
||||
other.resume();
|
||||
await other.add(
|
||||
BallTurboChargingBehavior(
|
||||
impulse: Vector2(40, 110),
|
||||
),
|
||||
);
|
||||
parent.bloc.onBallTurboCharged();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
|
||||
part 'sparky_computer_state.dart';
|
||||
|
||||
class SparkyComputerCubit extends Cubit<SparkyComputerState> {
|
||||
SparkyComputerCubit() : super(SparkyComputerState.withoutBall);
|
||||
|
||||
void onBallEntered() {
|
||||
emit(SparkyComputerState.withBall);
|
||||
}
|
||||
|
||||
void onBallTurboCharged() {
|
||||
emit(SparkyComputerState.withoutBall);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
part of 'sparky_computer_cubit.dart';
|
||||
|
||||
enum SparkyComputerState {
|
||||
withoutBall,
|
||||
withBall,
|
||||
}
|
Before Width: | Height: | Size: 154 KiB After Width: | Height: | Size: 154 KiB |
Before Width: | Height: | Size: 126 KiB After Width: | Height: | Size: 126 KiB |
Before Width: | Height: | Size: 97 KiB After Width: | Height: | Size: 95 KiB |
@ -0,0 +1,141 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
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/sparky_computer/behaviors/behaviors.dart';
|
||||
|
||||
import '../../../../helpers/helpers.dart';
|
||||
|
||||
class _MockSparkyComputerCubit extends Mock implements SparkyComputerCubit {}
|
||||
|
||||
class _MockBall extends Mock implements Ball {}
|
||||
|
||||
class _MockContact extends Mock implements Contact {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
|
||||
group(
|
||||
'SparkyComputerSensorBallContactBehavior',
|
||||
() {
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
SparkyComputerSensorBallContactBehavior(),
|
||||
isA<SparkyComputerSensorBallContactBehavior>(),
|
||||
);
|
||||
});
|
||||
|
||||
group('beginContact', () {
|
||||
flameTester.test(
|
||||
'stops a ball',
|
||||
(game) async {
|
||||
final behavior = SparkyComputerSensorBallContactBehavior();
|
||||
final bloc = _MockSparkyComputerCubit();
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<SparkyComputerState>.empty(),
|
||||
initialState: SparkyComputerState.withoutBall,
|
||||
);
|
||||
|
||||
final sparkyComputer = SparkyComputer.test(
|
||||
bloc: bloc,
|
||||
);
|
||||
await sparkyComputer.add(behavior);
|
||||
await game.ensureAdd(sparkyComputer);
|
||||
|
||||
final ball = _MockBall();
|
||||
await behavior.beginContact(ball, _MockContact());
|
||||
|
||||
verify(ball.stop).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'emits onBallEntered when contacts with a ball',
|
||||
(game) async {
|
||||
final behavior = SparkyComputerSensorBallContactBehavior();
|
||||
final bloc = _MockSparkyComputerCubit();
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<SparkyComputerState>.empty(),
|
||||
initialState: SparkyComputerState.withoutBall,
|
||||
);
|
||||
|
||||
final sparkyComputer = SparkyComputer.test(
|
||||
bloc: bloc,
|
||||
);
|
||||
await sparkyComputer.add(behavior);
|
||||
await game.ensureAdd(sparkyComputer);
|
||||
|
||||
await behavior.beginContact(_MockBall(), _MockContact());
|
||||
|
||||
verify(sparkyComputer.bloc.onBallEntered).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'adds TimerComponent when contacts with a ball',
|
||||
(game) async {
|
||||
final behavior = SparkyComputerSensorBallContactBehavior();
|
||||
final bloc = _MockSparkyComputerCubit();
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<SparkyComputerState>.empty(),
|
||||
initialState: SparkyComputerState.withoutBall,
|
||||
);
|
||||
|
||||
final sparkyComputer = SparkyComputer.test(
|
||||
bloc: bloc,
|
||||
);
|
||||
await sparkyComputer.add(behavior);
|
||||
await game.ensureAdd(sparkyComputer);
|
||||
|
||||
await behavior.beginContact(_MockBall(), _MockContact());
|
||||
await game.ready();
|
||||
|
||||
expect(
|
||||
sparkyComputer.firstChild<TimerComponent>(),
|
||||
isA<TimerComponent>(),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'TimerComponent resumes ball and calls onBallTurboCharged onTick',
|
||||
(game) async {
|
||||
final behavior = SparkyComputerSensorBallContactBehavior();
|
||||
final bloc = _MockSparkyComputerCubit();
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<SparkyComputerState>.empty(),
|
||||
initialState: SparkyComputerState.withoutBall,
|
||||
);
|
||||
|
||||
final sparkyComputer = SparkyComputer.test(
|
||||
bloc: bloc,
|
||||
);
|
||||
await sparkyComputer.add(behavior);
|
||||
await game.ensureAdd(sparkyComputer);
|
||||
|
||||
final ball = _MockBall();
|
||||
await behavior.beginContact(ball, _MockContact());
|
||||
await game.ready();
|
||||
game.update(
|
||||
sparkyComputer.firstChild<TimerComponent>()!.timer.limit,
|
||||
);
|
||||
await game.ready();
|
||||
|
||||
verify(ball.resume).called(1);
|
||||
verify(sparkyComputer.bloc.onBallTurboCharged).called(1);
|
||||
},
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
void main() {
|
||||
group(
|
||||
'SparkyComputerCubit',
|
||||
() {
|
||||
blocTest<SparkyComputerCubit, SparkyComputerState>(
|
||||
'onBallEntered emits withBall',
|
||||
build: SparkyComputerCubit.new,
|
||||
act: (bloc) => bloc.onBallEntered(),
|
||||
expect: () => [SparkyComputerState.withBall],
|
||||
);
|
||||
|
||||
blocTest<SparkyComputerCubit, SparkyComputerState>(
|
||||
'onBallTurboCharged emits withoutBall',
|
||||
build: SparkyComputerCubit.new,
|
||||
act: (bloc) => bloc.onBallTurboCharged(),
|
||||
expect: () => [SparkyComputerState.withoutBall],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame/components.dart';
|
||||
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/sparky_computer/behaviors/behaviors.dart';
|
||||
|
||||
import '../../../helpers/helpers.dart';
|
||||
|
||||
class _MockSparkyComputerCubit extends Mock implements SparkyComputerCubit {}
|
||||
|
||||
void main() {
|
||||
group('SparkyComputer', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final assets = [
|
||||
Assets.images.sparky.computer.base.keyName,
|
||||
Assets.images.sparky.computer.top.keyName,
|
||||
Assets.images.sparky.computer.glow.keyName,
|
||||
];
|
||||
final flameTester = FlameTester(() => TestGame(assets));
|
||||
|
||||
flameTester.test('loads correctly', (game) async {
|
||||
final component = SparkyComputer();
|
||||
await game.ensureAdd(component);
|
||||
expect(game.contains(component), isTrue);
|
||||
});
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'renders correctly',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
await game.ensureAdd(SparkyComputer());
|
||||
await tester.pump();
|
||||
|
||||
game.camera
|
||||
..followVector2(Vector2(0, -20))
|
||||
..zoom = 7;
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/sparky-computer.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
// TODO(alestiago): Consider refactoring once the following is merged:
|
||||
// https://github.com/flame-engine/flame/pull/1538
|
||||
// ignore: public_member_api_docs
|
||||
flameTester.test('closes bloc when removed', (game) async {
|
||||
final bloc = _MockSparkyComputerCubit();
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<SparkyComputerState>.empty(),
|
||||
initialState: SparkyComputerState.withoutBall,
|
||||
);
|
||||
when(bloc.close).thenAnswer((_) async {});
|
||||
final sparkyComputer = SparkyComputer.test(bloc: bloc);
|
||||
|
||||
await game.ensureAdd(sparkyComputer);
|
||||
game.remove(sparkyComputer);
|
||||
await game.ready();
|
||||
|
||||
verify(bloc.close).called(1);
|
||||
});
|
||||
|
||||
group('adds', () {
|
||||
flameTester.test('new children', (game) async {
|
||||
final component = Component();
|
||||
final sparkyComputer = SparkyComputer(
|
||||
children: [component],
|
||||
);
|
||||
await game.ensureAdd(sparkyComputer);
|
||||
expect(sparkyComputer.children, contains(component));
|
||||
});
|
||||
|
||||
flameTester.test('a SparkyComputerSensorBallContactBehavior',
|
||||
(game) async {
|
||||
final sparkyComputer = SparkyComputer();
|
||||
await game.ensureAdd(sparkyComputer);
|
||||
expect(
|
||||
sparkyComputer.children
|
||||
.whereType<SparkyComputerSensorBallContactBehavior>()
|
||||
.single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('SparkyComputer', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final assets = [
|
||||
Assets.images.sparky.computer.base.keyName,
|
||||
Assets.images.sparky.computer.top.keyName,
|
||||
Assets.images.sparky.computer.glow.keyName,
|
||||
];
|
||||
final flameTester = FlameTester(() => TestGame(assets));
|
||||
|
||||
flameTester.test('loads correctly', (game) async {
|
||||
final component = SparkyComputer();
|
||||
await game.ensureAdd(component);
|
||||
expect(game.contains(component), isTrue);
|
||||
});
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'renders correctly',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
await game.ensureAdd(SparkyComputer());
|
||||
await tester.pump();
|
||||
|
||||
game.camera
|
||||
..followVector2(Vector2(0, -20))
|
||||
..zoom = 7;
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('golden/sparky-computer.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
@ -0,0 +1,186 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/game/behaviors/behaviors.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_audio/pinball_audio.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
class _TestGame extends Forge2DGame {
|
||||
Future<void> pump(
|
||||
BonusNoiseBehavior child, {
|
||||
required PinballPlayer player,
|
||||
required GameBloc bloc,
|
||||
}) {
|
||||
return ensureAdd(
|
||||
FlameBlocProvider<GameBloc, GameState>.value(
|
||||
value: bloc,
|
||||
children: [
|
||||
FlameProvider<PinballPlayer>.value(
|
||||
player,
|
||||
children: [
|
||||
child,
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MockPinballPlayer extends Mock implements PinballPlayer {}
|
||||
|
||||
class _MockGameBloc extends Mock implements GameBloc {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('BonusNoiseBehavior', () {
|
||||
late PinballPlayer player;
|
||||
late GameBloc bloc;
|
||||
final flameTester = FlameTester(_TestGame.new);
|
||||
|
||||
setUpAll(() {
|
||||
registerFallbackValue(PinballAudio.google);
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
player = _MockPinballPlayer();
|
||||
when(() => player.play(any())).thenAnswer((_) {});
|
||||
bloc = _MockGameBloc();
|
||||
});
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'plays google sound',
|
||||
setUp: (game, _) async {
|
||||
const state = GameState(
|
||||
totalScore: 0,
|
||||
roundScore: 0,
|
||||
multiplier: 1,
|
||||
rounds: 0,
|
||||
bonusHistory: [GameBonus.googleWord],
|
||||
status: GameStatus.playing,
|
||||
);
|
||||
const initialState = GameState.initial();
|
||||
whenListen(
|
||||
bloc,
|
||||
Stream.fromIterable([initialState, state]),
|
||||
initialState: initialState,
|
||||
);
|
||||
final behavior = BonusNoiseBehavior();
|
||||
await game.pump(behavior, player: player, bloc: bloc);
|
||||
},
|
||||
verify: (_, __) async {
|
||||
verify(() => player.play(PinballAudio.google)).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'plays sparky sound',
|
||||
setUp: (game, _) async {
|
||||
const state = GameState(
|
||||
totalScore: 0,
|
||||
roundScore: 0,
|
||||
multiplier: 1,
|
||||
rounds: 0,
|
||||
bonusHistory: [GameBonus.sparkyTurboCharge],
|
||||
status: GameStatus.playing,
|
||||
);
|
||||
const initialState = GameState.initial();
|
||||
whenListen(
|
||||
bloc,
|
||||
Stream.fromIterable([initialState, state]),
|
||||
initialState: initialState,
|
||||
);
|
||||
final behavior = BonusNoiseBehavior();
|
||||
await game.pump(behavior, player: player, bloc: bloc);
|
||||
},
|
||||
verify: (_, __) async {
|
||||
verify(() => player.play(PinballAudio.sparky)).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'plays dino chomp sound',
|
||||
setUp: (game, _) async {
|
||||
const state = GameState(
|
||||
totalScore: 0,
|
||||
roundScore: 0,
|
||||
multiplier: 1,
|
||||
rounds: 0,
|
||||
bonusHistory: [GameBonus.dinoChomp],
|
||||
status: GameStatus.playing,
|
||||
);
|
||||
const initialState = GameState.initial();
|
||||
whenListen(
|
||||
bloc,
|
||||
Stream.fromIterable([initialState, state]),
|
||||
initialState: initialState,
|
||||
);
|
||||
final behavior = BonusNoiseBehavior();
|
||||
await game.pump(behavior, player: player, bloc: bloc);
|
||||
},
|
||||
verify: (_, __) async {
|
||||
// TODO(erickzanardo): Change when the sound is implemented
|
||||
verifyNever(() => player.play(any()));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'plays android spaceship sound',
|
||||
setUp: (game, _) async {
|
||||
const state = GameState(
|
||||
totalScore: 0,
|
||||
roundScore: 0,
|
||||
multiplier: 1,
|
||||
rounds: 0,
|
||||
bonusHistory: [GameBonus.androidSpaceship],
|
||||
status: GameStatus.playing,
|
||||
);
|
||||
const initialState = GameState.initial();
|
||||
whenListen(
|
||||
bloc,
|
||||
Stream.fromIterable([initialState, state]),
|
||||
initialState: initialState,
|
||||
);
|
||||
final behavior = BonusNoiseBehavior();
|
||||
await game.pump(behavior, player: player, bloc: bloc);
|
||||
},
|
||||
verify: (_, __) async {
|
||||
// TODO(erickzanardo): Change when the sound is implemented
|
||||
verifyNever(() => player.play(any()));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'plays dash nest sound',
|
||||
setUp: (game, _) async {
|
||||
const state = GameState(
|
||||
totalScore: 0,
|
||||
roundScore: 0,
|
||||
multiplier: 1,
|
||||
rounds: 0,
|
||||
bonusHistory: [GameBonus.dashNest],
|
||||
status: GameStatus.playing,
|
||||
);
|
||||
const initialState = GameState.initial();
|
||||
whenListen(
|
||||
bloc,
|
||||
Stream.fromIterable([initialState, state]),
|
||||
initialState: initialState,
|
||||
);
|
||||
final behavior = BonusNoiseBehavior();
|
||||
await game.pump(behavior, player: player, bloc: bloc);
|
||||
},
|
||||
verify: (_, __) async {
|
||||
// TODO(erickzanardo): Change when the sound is implemented
|
||||
verifyNever(() => player.play(any()));
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_theme/pinball_theme.dart' as theme;
|
||||
|
||||
class _TestGame extends Forge2DGame {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
images.prefix = '';
|
||||
await images.load(theme.Assets.images.dash.ball.keyName);
|
||||
}
|
||||
|
||||
Future<void> pump(Ball child, {required GameBloc gameBloc}) async {
|
||||
await ensureAdd(
|
||||
FlameBlocProvider<GameBloc, GameState>.value(
|
||||
value: gameBloc,
|
||||
children: [child],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MockGameBloc extends Mock implements GameBloc {}
|
||||
|
||||
class _MockBall extends Mock implements Ball {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('BallController', () {
|
||||
late Ball ball;
|
||||
late GameBloc gameBloc;
|
||||
|
||||
setUp(() {
|
||||
ball = Ball();
|
||||
gameBloc = _MockGameBloc();
|
||||
});
|
||||
|
||||
final flameBlocTester = FlameTester(_TestGame.new);
|
||||
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
BallController(_MockBall()),
|
||||
isA<BallController>(),
|
||||
);
|
||||
});
|
||||
|
||||
flameBlocTester.testGameWidget(
|
||||
'turboCharge adds TurboChargeActivated',
|
||||
setUp: (game, tester) async {
|
||||
await game.onLoad();
|
||||
|
||||
final controller = BallController(ball);
|
||||
await ball.add(controller);
|
||||
await game.pump(ball, gameBloc: gameBloc);
|
||||
|
||||
await controller.turboCharge();
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
verify(() => gameBloc.add(const SparkyTurboChargeActivated()))
|
||||
.called(1);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/game/components/sparky_scorch/behaviors/behaviors.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
class _TestGame extends Forge2DGame {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
images.prefix = '';
|
||||
await images.loadAll([
|
||||
Assets.images.sparky.computer.top.keyName,
|
||||
Assets.images.sparky.computer.base.keyName,
|
||||
Assets.images.sparky.computer.glow.keyName,
|
||||
Assets.images.sparky.animatronic.keyName,
|
||||
Assets.images.sparky.bumper.a.lit.keyName,
|
||||
Assets.images.sparky.bumper.a.dimmed.keyName,
|
||||
Assets.images.sparky.bumper.b.lit.keyName,
|
||||
Assets.images.sparky.bumper.b.dimmed.keyName,
|
||||
Assets.images.sparky.bumper.c.lit.keyName,
|
||||
Assets.images.sparky.bumper.c.dimmed.keyName,
|
||||
]);
|
||||
}
|
||||
|
||||
Future<void> pump(
|
||||
SparkyScorch child, {
|
||||
required GameBloc gameBloc,
|
||||
}) async {
|
||||
// Not needed once https://github.com/flame-engine/flame/issues/1607
|
||||
// is fixed
|
||||
await onLoad();
|
||||
await ensureAdd(
|
||||
FlameBlocProvider<GameBloc, GameState>.value(
|
||||
value: gameBloc,
|
||||
children: [child],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MockGameBloc extends Mock implements GameBloc {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('SparkyComputerBonusBehavior', () {
|
||||
late GameBloc gameBloc;
|
||||
|
||||
setUp(() {
|
||||
gameBloc = _MockGameBloc();
|
||||
});
|
||||
|
||||
final flameTester = FlameTester(_TestGame.new);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'adds GameBonus.sparkyTurboCharge to the game and plays animatronic '
|
||||
'when SparkyComputerState.withBall is emitted',
|
||||
setUp: (game, tester) async {
|
||||
final behavior = SparkyComputerBonusBehavior();
|
||||
final parent = SparkyScorch.test();
|
||||
final sparkyComputer = SparkyComputer();
|
||||
final animatronic = SparkyAnimatronic();
|
||||
|
||||
await parent.addAll([
|
||||
sparkyComputer,
|
||||
animatronic,
|
||||
]);
|
||||
await game.pump(parent, gameBloc: gameBloc);
|
||||
await parent.ensureAdd(behavior);
|
||||
|
||||
sparkyComputer.bloc.onBallEntered();
|
||||
await tester.pump();
|
||||
|
||||
verify(
|
||||
() => gameBloc.add(const BonusActivated(GameBonus.sparkyTurboCharge)),
|
||||
).called(1);
|
||||
expect(animatronic.playing, isTrue);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue