mirror of https://github.com/flutter/pinball.git
feat: implement GoogleWord (#176)
parent
437dc07ba7
commit
a65fb692b5
@ -1,208 +0,0 @@
|
||||
// ignore_for_file: avoid_renaming_method_parameters
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/effects.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template bonus_word}
|
||||
/// Loads all [BonusLetter]s to compose a [BonusWord].
|
||||
/// {@endtemplate}
|
||||
class BonusWord extends Component
|
||||
with BlocComponent<GameBloc, GameState>, HasGameRef<PinballGame> {
|
||||
/// {@macro bonus_word}
|
||||
BonusWord({required Vector2 position}) : _position = position;
|
||||
|
||||
final Vector2 _position;
|
||||
|
||||
@override
|
||||
bool listenWhen(GameState? previousState, GameState newState) {
|
||||
return (previousState?.bonusHistory.length ?? 0) <
|
||||
newState.bonusHistory.length &&
|
||||
newState.bonusHistory.last == GameBonus.word;
|
||||
}
|
||||
|
||||
@override
|
||||
void onNewState(GameState state) {
|
||||
if (state.bonusHistory.last == GameBonus.word) {
|
||||
gameRef.audio.googleBonus();
|
||||
|
||||
final letters = children.whereType<BonusLetter>().toList();
|
||||
|
||||
for (var i = 0; i < letters.length; i++) {
|
||||
final letter = letters[i];
|
||||
letter
|
||||
..isEnabled = false
|
||||
..add(
|
||||
SequenceEffect(
|
||||
[
|
||||
ColorEffect(
|
||||
i.isOdd
|
||||
? BonusLetter._activeColor
|
||||
: BonusLetter._disableColor,
|
||||
const Offset(0, 1),
|
||||
EffectController(duration: 0.25),
|
||||
),
|
||||
ColorEffect(
|
||||
i.isOdd
|
||||
? BonusLetter._disableColor
|
||||
: BonusLetter._activeColor,
|
||||
const Offset(0, 1),
|
||||
EffectController(duration: 0.25),
|
||||
),
|
||||
],
|
||||
repeatCount: 4,
|
||||
)..onFinishCallback = () {
|
||||
letter
|
||||
..isEnabled = true
|
||||
..add(
|
||||
ColorEffect(
|
||||
BonusLetter._disableColor,
|
||||
const Offset(0, 1),
|
||||
EffectController(duration: 0.25),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
|
||||
final offsets = [
|
||||
Vector2(-12.92, 1.82),
|
||||
Vector2(-8.33, -0.65),
|
||||
Vector2(-2.88, -1.75),
|
||||
];
|
||||
offsets.addAll(
|
||||
offsets.reversed
|
||||
.map(
|
||||
(offset) => Vector2(-offset.x, offset.y),
|
||||
)
|
||||
.toList(),
|
||||
);
|
||||
assert(offsets.length == GameBloc.bonusWord.length, 'Invalid positions');
|
||||
|
||||
final letters = <BonusLetter>[];
|
||||
for (var i = 0; i < GameBloc.bonusWord.length; i++) {
|
||||
letters.add(
|
||||
BonusLetter(
|
||||
letter: GameBloc.bonusWord[i],
|
||||
index: i,
|
||||
)..initialPosition = _position + offsets[i],
|
||||
);
|
||||
}
|
||||
|
||||
await addAll(letters);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template bonus_letter}
|
||||
/// [BodyType.static] sensor component, part of a word bonus,
|
||||
/// which will activate its letter after contact with a [Ball].
|
||||
/// {@endtemplate}
|
||||
class BonusLetter extends BodyComponent<PinballGame>
|
||||
with BlocComponent<GameBloc, GameState>, InitialPosition {
|
||||
/// {@macro bonus_letter}
|
||||
BonusLetter({
|
||||
required String letter,
|
||||
required int index,
|
||||
}) : _letter = letter,
|
||||
_index = index {
|
||||
paint = Paint()..color = _disableColor;
|
||||
}
|
||||
|
||||
/// The size of the [BonusLetter].
|
||||
static final size = Vector2.all(3.7);
|
||||
|
||||
static const _activeColor = Colors.green;
|
||||
static const _disableColor = Colors.red;
|
||||
|
||||
final String _letter;
|
||||
final int _index;
|
||||
|
||||
/// Indicates if a [BonusLetter] can be activated on [Ball] contact.
|
||||
///
|
||||
/// It is disabled whilst animating and enabled again once the animation
|
||||
/// completes. The animation is triggered when [GameBonus.word] is
|
||||
/// awarded.
|
||||
bool isEnabled = true;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
|
||||
await add(
|
||||
TextComponent(
|
||||
position: Vector2(-1, -1),
|
||||
text: _letter,
|
||||
textRenderer: TextPaint(
|
||||
style: const TextStyle(fontSize: 2, color: Colors.white),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final shape = CircleShape()..radius = size.x / 2;
|
||||
|
||||
final fixtureDef = FixtureDef(shape)..isSensor = true;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..position = initialPosition
|
||||
..userData = this
|
||||
..type = BodyType.static;
|
||||
|
||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||
}
|
||||
|
||||
@override
|
||||
bool listenWhen(GameState? previousState, GameState newState) {
|
||||
final wasActive = previousState?.isLetterActivated(_index) ?? false;
|
||||
final isActive = newState.isLetterActivated(_index);
|
||||
|
||||
return wasActive != isActive;
|
||||
}
|
||||
|
||||
@override
|
||||
void onNewState(GameState state) {
|
||||
final isActive = state.isLetterActivated(_index);
|
||||
|
||||
add(
|
||||
ColorEffect(
|
||||
isActive ? _activeColor : _disableColor,
|
||||
const Offset(0, 1),
|
||||
EffectController(duration: 0.25),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Activates this [BonusLetter], if it's not already activated.
|
||||
void activate() {
|
||||
final isActive = state?.isLetterActivated(_index) ?? false;
|
||||
if (!isActive) {
|
||||
gameRef.read<GameBloc>().add(BonusLetterActivated(_index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Triggers [BonusLetter.activate] method when a [BonusLetter] and a [Ball]
|
||||
/// come in contact.
|
||||
class BonusLetterBallContactCallback
|
||||
extends ContactCallback<Ball, BonusLetter> {
|
||||
@override
|
||||
void begin(Ball ball, BonusLetter bonusLetter, Contact contact) {
|
||||
if (bonusLetter.isEnabled) {
|
||||
bonusLetter.activate();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
// ignore_for_file: avoid_renaming_method_parameters
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball/flame/flame.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template google_word}
|
||||
/// Loads all [GoogleLetter]s to compose a [GoogleWord].
|
||||
/// {@endtemplate}
|
||||
class GoogleWord extends Component
|
||||
with HasGameRef<PinballGame>, Controls<_GoogleWordController> {
|
||||
/// {@macro google_word}
|
||||
GoogleWord({
|
||||
required Vector2 position,
|
||||
}) : _position = position {
|
||||
controller = _GoogleWordController(this);
|
||||
}
|
||||
|
||||
final Vector2 _position;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
gameRef.addContactCallback(_GoogleLetterBallContactCallback());
|
||||
|
||||
final offsets = [
|
||||
Vector2(-12.92, 1.82),
|
||||
Vector2(-8.33, -0.65),
|
||||
Vector2(-2.88, -1.75),
|
||||
Vector2(2.88, -1.75),
|
||||
Vector2(8.33, -0.65),
|
||||
Vector2(12.92, 1.82),
|
||||
];
|
||||
|
||||
final letters = <GoogleLetter>[];
|
||||
for (var index = 0; index < offsets.length; index++) {
|
||||
letters.add(
|
||||
GoogleLetter(index)..initialPosition = _position + offsets[index],
|
||||
);
|
||||
}
|
||||
|
||||
await addAll(letters);
|
||||
}
|
||||
}
|
||||
|
||||
class _GoogleWordController extends ComponentController<GoogleWord>
|
||||
with HasGameRef<PinballGame> {
|
||||
_GoogleWordController(GoogleWord googleWord) : super(googleWord);
|
||||
|
||||
final _activatedLetters = <GoogleLetter>{};
|
||||
|
||||
void activate(GoogleLetter googleLetter) {
|
||||
if (!_activatedLetters.add(googleLetter)) return;
|
||||
|
||||
googleLetter.activate();
|
||||
|
||||
final activatedBonus = _activatedLetters.length == 6;
|
||||
if (activatedBonus) {
|
||||
gameRef.audio.googleBonus();
|
||||
gameRef.read<GameBloc>().add(const BonusActivated(GameBonus.googleWord));
|
||||
component.children.whereType<GoogleLetter>().forEach(
|
||||
(letter) => letter.deactivate(),
|
||||
);
|
||||
_activatedLetters.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Activates a [GoogleLetter] when it contacts with a [Ball].
|
||||
class _GoogleLetterBallContactCallback
|
||||
extends ContactCallback<GoogleLetter, Ball> {
|
||||
@override
|
||||
void begin(GoogleLetter googleLetter, _, __) {
|
||||
final parent = googleLetter.parent;
|
||||
if (parent is GoogleWord) {
|
||||
parent.controller.activate(googleLetter);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,376 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame/effects.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_audio/pinball_audio.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(EmptyPinballGameTest.new);
|
||||
|
||||
group('BonusWord', () {
|
||||
flameTester.test(
|
||||
'loads the letters correctly',
|
||||
(game) async {
|
||||
final bonusWord = BonusWord(
|
||||
position: Vector2.zero(),
|
||||
);
|
||||
await game.ensureAdd(bonusWord);
|
||||
|
||||
final letters = bonusWord.descendants().whereType<BonusLetter>();
|
||||
expect(letters.length, equals(GameBloc.bonusWord.length));
|
||||
},
|
||||
);
|
||||
|
||||
group('listenWhen', () {
|
||||
final previousState = MockGameState();
|
||||
final currentState = MockGameState();
|
||||
|
||||
test(
|
||||
'returns true when there is a new word bonus awarded',
|
||||
() {
|
||||
when(() => previousState.bonusHistory).thenReturn([]);
|
||||
when(() => currentState.bonusHistory).thenReturn([GameBonus.word]);
|
||||
|
||||
expect(
|
||||
BonusWord(position: Vector2.zero()).listenWhen(
|
||||
previousState,
|
||||
currentState,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'returns false when there is no new word bonus awarded',
|
||||
() {
|
||||
when(() => previousState.bonusHistory).thenReturn([GameBonus.word]);
|
||||
when(() => currentState.bonusHistory).thenReturn([GameBonus.word]);
|
||||
|
||||
expect(
|
||||
BonusWord(position: Vector2.zero()).listenWhen(
|
||||
previousState,
|
||||
currentState,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('onNewState', () {
|
||||
final state = MockGameState();
|
||||
flameTester.test(
|
||||
'adds sequence effect to the letters when the player receives a bonus',
|
||||
(game) async {
|
||||
when(() => state.bonusHistory).thenReturn([GameBonus.word]);
|
||||
|
||||
final bonusWord = BonusWord(position: Vector2.zero());
|
||||
await game.ensureAdd(bonusWord);
|
||||
await game.ready();
|
||||
|
||||
bonusWord.onNewState(state);
|
||||
game.update(0); // Run one frame so the effects are added
|
||||
|
||||
final letters = bonusWord.children.whereType<BonusLetter>();
|
||||
expect(letters.length, equals(GameBloc.bonusWord.length));
|
||||
|
||||
for (final letter in letters) {
|
||||
expect(
|
||||
letter.children.whereType<SequenceEffect>().length,
|
||||
equals(1),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'plays the google bonus sound',
|
||||
(game) async {
|
||||
when(() => state.bonusHistory).thenReturn([GameBonus.word]);
|
||||
|
||||
final bonusWord = BonusWord(position: Vector2.zero());
|
||||
await game.ensureAdd(bonusWord);
|
||||
await game.ready();
|
||||
|
||||
bonusWord.onNewState(state);
|
||||
|
||||
verify(bonusWord.gameRef.audio.googleBonus).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'adds a color effect to reset the color when the sequence is finished',
|
||||
(game) async {
|
||||
when(() => state.bonusHistory).thenReturn([GameBonus.word]);
|
||||
|
||||
final bonusWord = BonusWord(position: Vector2.zero());
|
||||
await game.ensureAdd(bonusWord);
|
||||
await game.ready();
|
||||
|
||||
bonusWord.onNewState(state);
|
||||
// Run the amount of time necessary for the animation to finish
|
||||
game.update(3);
|
||||
game.update(0); // Run one additional frame so the effects are added
|
||||
|
||||
final letters = bonusWord.children.whereType<BonusLetter>();
|
||||
expect(letters.length, equals(GameBloc.bonusWord.length));
|
||||
|
||||
for (final letter in letters) {
|
||||
expect(
|
||||
letter.children.whereType<ColorEffect>().length,
|
||||
equals(1),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('BonusLetter', () {
|
||||
final flameTester = FlameTester(EmptyPinballGameTest.new);
|
||||
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final bonusLetter = BonusLetter(
|
||||
letter: 'G',
|
||||
index: 0,
|
||||
);
|
||||
await game.ensureAdd(bonusLetter);
|
||||
await game.ready();
|
||||
|
||||
expect(game.contains(bonusLetter), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
group('body', () {
|
||||
flameTester.test(
|
||||
'is static',
|
||||
(game) async {
|
||||
final bonusLetter = BonusLetter(
|
||||
letter: 'G',
|
||||
index: 0,
|
||||
);
|
||||
await game.ensureAdd(bonusLetter);
|
||||
|
||||
expect(bonusLetter.body.bodyType, equals(BodyType.static));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('fixture', () {
|
||||
flameTester.test(
|
||||
'exists',
|
||||
(game) async {
|
||||
final bonusLetter = BonusLetter(
|
||||
letter: 'G',
|
||||
index: 0,
|
||||
);
|
||||
await game.ensureAdd(bonusLetter);
|
||||
|
||||
expect(bonusLetter.body.fixtures[0], isA<Fixture>());
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'is sensor',
|
||||
(game) async {
|
||||
final bonusLetter = BonusLetter(
|
||||
letter: 'G',
|
||||
index: 0,
|
||||
);
|
||||
await game.ensureAdd(bonusLetter);
|
||||
|
||||
final fixture = bonusLetter.body.fixtures[0];
|
||||
expect(fixture.isSensor, isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'shape is circular',
|
||||
(game) async {
|
||||
final bonusLetter = BonusLetter(
|
||||
letter: 'G',
|
||||
index: 0,
|
||||
);
|
||||
await game.ensureAdd(bonusLetter);
|
||||
|
||||
final fixture = bonusLetter.body.fixtures[0];
|
||||
expect(fixture.shape.shapeType, equals(ShapeType.circle));
|
||||
expect(fixture.shape.radius, equals(1.85));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('bonus letter activation', () {
|
||||
late GameBloc gameBloc;
|
||||
late PinballAudio pinballAudio;
|
||||
|
||||
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
|
||||
gameBuilder: EmptyPinballGameTest.new,
|
||||
blocBuilder: () => gameBloc,
|
||||
repositories: () => [
|
||||
RepositoryProvider<PinballAudio>.value(value: pinballAudio),
|
||||
],
|
||||
);
|
||||
|
||||
setUp(() {
|
||||
gameBloc = MockGameBloc();
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial(),
|
||||
);
|
||||
|
||||
pinballAudio = MockPinballAudio();
|
||||
when(pinballAudio.googleBonus).thenAnswer((_) {});
|
||||
});
|
||||
|
||||
flameBlocTester.testGameWidget(
|
||||
'adds BonusLetterActivated to GameBloc when not activated',
|
||||
setUp: (game, tester) async {
|
||||
final bonusWord = BonusWord(
|
||||
position: Vector2.zero(),
|
||||
);
|
||||
await game.ensureAdd(bonusWord);
|
||||
|
||||
final bonusLetters =
|
||||
game.descendants().whereType<BonusLetter>().toList();
|
||||
for (var index = 0; index < bonusLetters.length; index++) {
|
||||
final bonusLetter = bonusLetters[index];
|
||||
bonusLetter.activate();
|
||||
await game.ready();
|
||||
|
||||
verify(() => gameBloc.add(BonusLetterActivated(index))).called(1);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
flameBlocTester.testGameWidget(
|
||||
"doesn't add BonusLetterActivated to GameBloc when already activated",
|
||||
setUp: (game, tester) async {
|
||||
const state = GameState(
|
||||
score: 0,
|
||||
balls: 2,
|
||||
activatedBonusLetters: [0],
|
||||
activatedDashNests: {},
|
||||
bonusHistory: [],
|
||||
);
|
||||
whenListen(
|
||||
gameBloc,
|
||||
Stream.value(state),
|
||||
initialState: state,
|
||||
);
|
||||
|
||||
final bonusLetter = BonusLetter(letter: '', index: 0);
|
||||
await game.add(bonusLetter);
|
||||
await game.ready();
|
||||
|
||||
bonusLetter.activate();
|
||||
await game.ready();
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
verifyNever(() => gameBloc.add(const BonusLetterActivated(0)));
|
||||
},
|
||||
);
|
||||
|
||||
flameBlocTester.testGameWidget(
|
||||
'adds a ColorEffect',
|
||||
setUp: (game, tester) async {
|
||||
const state = GameState(
|
||||
score: 0,
|
||||
balls: 2,
|
||||
activatedBonusLetters: [0],
|
||||
activatedDashNests: {},
|
||||
bonusHistory: [],
|
||||
);
|
||||
|
||||
final bonusLetter = BonusLetter(letter: '', index: 0);
|
||||
await game.add(bonusLetter);
|
||||
await game.ready();
|
||||
|
||||
bonusLetter.activate();
|
||||
|
||||
bonusLetter.onNewState(state);
|
||||
await tester.pump();
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
// TODO(aleastiago): Look into making `testGameWidget` pass the
|
||||
// subject.
|
||||
final bonusLetter = game.descendants().whereType<BonusLetter>().last;
|
||||
expect(
|
||||
bonusLetter.children.whereType<ColorEffect>().length,
|
||||
equals(1),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameBlocTester.testGameWidget(
|
||||
'listens when there is a change on the letter status',
|
||||
setUp: (game, tester) async {
|
||||
final bonusWord = BonusWord(
|
||||
position: Vector2.zero(),
|
||||
);
|
||||
await game.ensureAdd(bonusWord);
|
||||
|
||||
final bonusLetters =
|
||||
game.descendants().whereType<BonusLetter>().toList();
|
||||
for (var index = 0; index < bonusLetters.length; index++) {
|
||||
final bonusLetter = bonusLetters[index];
|
||||
bonusLetter.activate();
|
||||
await game.ready();
|
||||
|
||||
final state = GameState(
|
||||
score: 0,
|
||||
balls: 2,
|
||||
activatedBonusLetters: [index],
|
||||
activatedDashNests: const {},
|
||||
bonusHistory: const [],
|
||||
);
|
||||
|
||||
expect(
|
||||
bonusLetter.listenWhen(const GameState.initial(), state),
|
||||
isTrue,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('BonusLetterBallContactCallback', () {
|
||||
test('calls ball.activate', () {
|
||||
final ball = MockBall();
|
||||
final bonusLetter = MockBonusLetter();
|
||||
final contactCallback = BonusLetterBallContactCallback();
|
||||
|
||||
when(() => bonusLetter.isEnabled).thenReturn(true);
|
||||
|
||||
contactCallback.begin(ball, bonusLetter, MockContact());
|
||||
|
||||
verify(bonusLetter.activate).called(1);
|
||||
});
|
||||
|
||||
test("doesn't call ball.activate when letter is disabled", () {
|
||||
final ball = MockBall();
|
||||
final bonusLetter = MockBonusLetter();
|
||||
final contactCallback = BonusLetterBallContactCallback();
|
||||
|
||||
when(() => bonusLetter.isEnabled).thenReturn(false);
|
||||
|
||||
contactCallback.begin(ball, bonusLetter, MockContact());
|
||||
|
||||
verifyNever(bonusLetter.activate);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockingjay/mockingjay.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('GoogleWord', () {
|
||||
late GameBloc gameBloc;
|
||||
|
||||
setUp(() {
|
||||
gameBloc = MockGameBloc();
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial(),
|
||||
);
|
||||
});
|
||||
|
||||
final flameTester = FlameTester(EmptyPinballGameTest.new);
|
||||
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
|
||||
gameBuilder: EmptyPinballGameTest.new,
|
||||
blocBuilder: () => gameBloc,
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'loads the letters correctly',
|
||||
(game) async {
|
||||
const word = 'Google';
|
||||
final googleWord = GoogleWord(position: Vector2.zero());
|
||||
await game.ensureAdd(googleWord);
|
||||
|
||||
final letters = googleWord.children.whereType<GoogleLetter>();
|
||||
expect(letters.length, equals(word.length));
|
||||
},
|
||||
);
|
||||
|
||||
flameBlocTester.testGameWidget(
|
||||
'adds GameBonus.googleWord to the game when all letters are activated',
|
||||
setUp: (game, _) async {
|
||||
final ball = Ball(baseColor: const Color(0xFFFF0000));
|
||||
final googleWord = GoogleWord(position: Vector2.zero());
|
||||
await game.ensureAddAll([googleWord, ball]);
|
||||
|
||||
final letters = googleWord.children.whereType<GoogleLetter>();
|
||||
expect(letters, isNotEmpty);
|
||||
for (final letter in letters) {
|
||||
beginContact(game, letter, ball);
|
||||
await game.ready();
|
||||
|
||||
if (letter == letters.last) {
|
||||
verify(
|
||||
() => gameBloc.add(const BonusActivated(GameBonus.googleWord)),
|
||||
).called(1);
|
||||
} else {
|
||||
verifyNever(
|
||||
() => gameBloc.add(const BonusActivated(GameBonus.googleWord)),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue