feat: implemented `Multipliers` (#231)
* feat: added multipliers * feat: added multipliers to sandbox * test: tests and golden tests for multipliers * feat: multipliers controller * fix: missed assets load * refactor: multiplier refactored and tested * refactor: changed sandbox for multipliers * feat: multiplier controller * refactor: moved assets to constructor at sandbox * test: coverage for multipliers * test: removed test * refactor: coverage for multiplier * test: coverage for multiplier * chore: multiplier doc * Update packages/pinball_components/sandbox/lib/stories/multipliers/multipliers_game.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * chore: little code refactor, doc * refactor: multiplier creation refactored * test: fixed tests for multipliers * refactor: changed properties names * refactor: multipliers cubit * test: refactored tests for multipliers * refactored: sandbow for multipliers * refactor: multipliers controller * test: tests for multiplier behavior * chore: analysis errors * feat: multipliers behavior with multiplier from gamebloc * refactor: changed toggle to next at multipliers * test: coverage for cubit * refactor: refactored multipliers and coverage * refactor: sandbox * test: flamebloctester * Update lib/game/components/multipliers/multipliers.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update packages/pinball_components/lib/src/components/multiplier/cubit/multiplier_cubit.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update lib/game/components/multipliers/multipliers.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update packages/pinball_components/lib/src/components/multiplier/cubit/multiplier_state.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update packages/pinball_components/lib/src/components/multiplier/cubit/multiplier_state.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update test/game/components/multipliers/behaviors/multipliers_behavior_test.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * test: fixed test names and golden files * test: golden tests * fix: fixed bloc bug for tests on multiplier behavior * chore: renamed rotation * chore: doc ignore * test: grouped * test: golden tests dir * Update packages/pinball_components/sandbox/lib/stories/multipliers/stories.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * refactor: moved extension for multiplier value * chore: multiplier extension equalsTo name * fixed: missed method rename Co-authored-by: Alejandro Santiago <dev@alestiago.com> Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>pull/269/head
@ -0,0 +1 @@
|
||||
export 'multipliers_behavior.dart';
|
@ -0,0 +1,25 @@
|
||||
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';
|
||||
|
||||
/// Toggle each [Multiplier] when GameState.multiplier changes.
|
||||
class MultipliersBehavior extends Component
|
||||
with
|
||||
HasGameRef<PinballGame>,
|
||||
ParentIsA<Multipliers>,
|
||||
BlocComponent<GameBloc, GameState> {
|
||||
@override
|
||||
bool listenWhen(GameState? previousState, GameState newState) {
|
||||
return previousState?.multiplier != newState.multiplier;
|
||||
}
|
||||
|
||||
@override
|
||||
void onNewState(GameState state) {
|
||||
final multipliers = parent.children.whereType<Multiplier>();
|
||||
for (final multiplier in multipliers) {
|
||||
multiplier.bloc.next(state.multiplier);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pinball/game/components/multipliers/behaviors/behaviors.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template multipliers}
|
||||
/// A group for the multipliers on the board.
|
||||
/// {@endtemplate}
|
||||
class Multipliers extends Component {
|
||||
/// {@macro multipliers}
|
||||
Multipliers()
|
||||
: super(
|
||||
children: [
|
||||
Multiplier.x2(
|
||||
position: Vector2(-19.5, -2),
|
||||
angle: -15 * math.pi / 180,
|
||||
),
|
||||
Multiplier.x3(
|
||||
position: Vector2(13, -9.4),
|
||||
angle: 15 * math.pi / 180,
|
||||
),
|
||||
Multiplier.x4(
|
||||
position: Vector2(0, -21.2),
|
||||
angle: 0,
|
||||
),
|
||||
Multiplier.x5(
|
||||
position: Vector2(-8.5, -28),
|
||||
angle: -3 * math.pi / 180,
|
||||
),
|
||||
Multiplier.x6(
|
||||
position: Vector2(10, -30.7),
|
||||
angle: 8 * math.pi / 180,
|
||||
),
|
||||
MultipliersBehavior(),
|
||||
],
|
||||
);
|
||||
|
||||
/// Creates [Multipliers] without any children.
|
||||
///
|
||||
/// This can be used for testing [Multipliers]'s behaviors in isolation.
|
||||
@visibleForTesting
|
||||
Multipliers.test();
|
||||
}
|
After Width: | Height: | Size: 5.1 KiB |
After Width: | Height: | Size: 5.6 KiB |
After Width: | Height: | Size: 6.2 KiB |
After Width: | Height: | Size: 6.9 KiB |
After Width: | Height: | Size: 9.0 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 13 KiB |
@ -0,0 +1,25 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
part 'multiplier_state.dart';
|
||||
|
||||
class MultiplierCubit extends Cubit<MultiplierState> {
|
||||
MultiplierCubit(MultiplierValue multiplierValue)
|
||||
: super(MultiplierState.initial(multiplierValue));
|
||||
|
||||
/// Event added when the game's current multiplier changes.
|
||||
void next(int multiplier) {
|
||||
if (state.value.equals(multiplier)) {
|
||||
if (state.spriteState == MultiplierSpriteState.dimmed) {
|
||||
emit(state.copyWith(spriteState: MultiplierSpriteState.lit));
|
||||
}
|
||||
} else {
|
||||
if (state.spriteState == MultiplierSpriteState.lit) {
|
||||
emit(state.copyWith(spriteState: MultiplierSpriteState.dimmed));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
part of 'multiplier_cubit.dart';
|
||||
|
||||
enum MultiplierSpriteState {
|
||||
lit,
|
||||
dimmed,
|
||||
}
|
||||
|
||||
class MultiplierState extends Equatable {
|
||||
const MultiplierState({
|
||||
required this.value,
|
||||
required this.spriteState,
|
||||
});
|
||||
|
||||
const MultiplierState.initial(MultiplierValue multiplierValue)
|
||||
: this(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
);
|
||||
|
||||
/// Current value for the [Multiplier]
|
||||
final MultiplierValue value;
|
||||
|
||||
/// The [MultiplierSpriteGroupComponent] current sprite state
|
||||
final MultiplierSpriteState spriteState;
|
||||
|
||||
MultiplierState copyWith({
|
||||
MultiplierSpriteState? spriteState,
|
||||
}) {
|
||||
return MultiplierState(
|
||||
value: value,
|
||||
spriteState: spriteState ?? this.spriteState,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [value, spriteState];
|
||||
}
|
||||
|
||||
extension MultiplierValueX on MultiplierValue {
|
||||
bool equals(int value) {
|
||||
switch (this) {
|
||||
case MultiplierValue.x2:
|
||||
return value == 2;
|
||||
case MultiplierValue.x3:
|
||||
return value == 3;
|
||||
case MultiplierValue.x4:
|
||||
return value == 4;
|
||||
case MultiplierValue.x5:
|
||||
return value == 5;
|
||||
case MultiplierValue.x6:
|
||||
return value == 6;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,204 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pinball_components/gen/assets.gen.dart';
|
||||
import 'package:pinball_components/src/components/multiplier/cubit/multiplier_cubit.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
export 'cubit/multiplier_cubit.dart';
|
||||
|
||||
/// {@template multiplier}
|
||||
/// Backlit multiplier decal displayed on the board.
|
||||
/// {@endtemplate}
|
||||
class Multiplier extends Component {
|
||||
/// {@macro multiplier}
|
||||
Multiplier._({
|
||||
required MultiplierValue value,
|
||||
required Vector2 position,
|
||||
required double angle,
|
||||
required this.bloc,
|
||||
}) : _value = value,
|
||||
_position = position,
|
||||
_angle = angle,
|
||||
super();
|
||||
|
||||
/// {@macro multiplier}
|
||||
Multiplier.x2({
|
||||
required Vector2 position,
|
||||
required double angle,
|
||||
}) : this._(
|
||||
value: MultiplierValue.x2,
|
||||
position: position,
|
||||
angle: angle,
|
||||
bloc: MultiplierCubit(MultiplierValue.x2),
|
||||
);
|
||||
|
||||
/// {@macro multiplier}
|
||||
Multiplier.x3({
|
||||
required Vector2 position,
|
||||
required double angle,
|
||||
}) : this._(
|
||||
value: MultiplierValue.x3,
|
||||
position: position,
|
||||
angle: angle,
|
||||
bloc: MultiplierCubit(MultiplierValue.x3),
|
||||
);
|
||||
|
||||
/// {@macro multiplier}
|
||||
Multiplier.x4({
|
||||
required Vector2 position,
|
||||
required double angle,
|
||||
}) : this._(
|
||||
value: MultiplierValue.x4,
|
||||
position: position,
|
||||
angle: angle,
|
||||
bloc: MultiplierCubit(MultiplierValue.x4),
|
||||
);
|
||||
|
||||
/// {@macro multiplier}
|
||||
Multiplier.x5({
|
||||
required Vector2 position,
|
||||
required double angle,
|
||||
}) : this._(
|
||||
value: MultiplierValue.x5,
|
||||
position: position,
|
||||
angle: angle,
|
||||
bloc: MultiplierCubit(MultiplierValue.x5),
|
||||
);
|
||||
|
||||
/// {@macro multiplier}
|
||||
Multiplier.x6({
|
||||
required Vector2 position,
|
||||
required double angle,
|
||||
}) : this._(
|
||||
value: MultiplierValue.x6,
|
||||
position: position,
|
||||
angle: angle,
|
||||
bloc: MultiplierCubit(MultiplierValue.x6),
|
||||
);
|
||||
|
||||
/// Creates a [Multiplier] without any children.
|
||||
///
|
||||
/// This can be used for testing [Multiplier]'s behaviors in isolation.
|
||||
// TODO(alestiago): Refactor injecting bloc once the following is merged:
|
||||
// https://github.com/flame-engine/flame/pull/1538
|
||||
@visibleForTesting
|
||||
Multiplier.test({
|
||||
required MultiplierValue value,
|
||||
required this.bloc,
|
||||
}) : _value = value,
|
||||
_position = Vector2.zero(),
|
||||
_angle = 0;
|
||||
|
||||
// TODO(ruimiguel): Consider refactoring once the following is merged:
|
||||
// https://github.com/flame-engine/flame/pull/1538
|
||||
final MultiplierCubit bloc;
|
||||
|
||||
final MultiplierValue _value;
|
||||
final Vector2 _position;
|
||||
final double _angle;
|
||||
late final MultiplierSpriteGroupComponent _sprite;
|
||||
|
||||
@override
|
||||
void onRemove() {
|
||||
bloc.close();
|
||||
super.onRemove();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
_sprite = MultiplierSpriteGroupComponent(
|
||||
position: _position,
|
||||
litAssetPath: _value.litAssetPath,
|
||||
dimmedAssetPath: _value.dimmedAssetPath,
|
||||
angle: _angle,
|
||||
current: bloc.state,
|
||||
);
|
||||
await add(_sprite);
|
||||
}
|
||||
}
|
||||
|
||||
/// Available multiplier values.
|
||||
enum MultiplierValue {
|
||||
x2,
|
||||
x3,
|
||||
x4,
|
||||
x5,
|
||||
x6,
|
||||
}
|
||||
|
||||
extension on MultiplierValue {
|
||||
String get litAssetPath {
|
||||
switch (this) {
|
||||
case MultiplierValue.x2:
|
||||
return Assets.images.multiplier.x2.lit.keyName;
|
||||
case MultiplierValue.x3:
|
||||
return Assets.images.multiplier.x3.lit.keyName;
|
||||
case MultiplierValue.x4:
|
||||
return Assets.images.multiplier.x4.lit.keyName;
|
||||
case MultiplierValue.x5:
|
||||
return Assets.images.multiplier.x5.lit.keyName;
|
||||
case MultiplierValue.x6:
|
||||
return Assets.images.multiplier.x6.lit.keyName;
|
||||
}
|
||||
}
|
||||
|
||||
String get dimmedAssetPath {
|
||||
switch (this) {
|
||||
case MultiplierValue.x2:
|
||||
return Assets.images.multiplier.x2.dimmed.keyName;
|
||||
case MultiplierValue.x3:
|
||||
return Assets.images.multiplier.x3.dimmed.keyName;
|
||||
case MultiplierValue.x4:
|
||||
return Assets.images.multiplier.x4.dimmed.keyName;
|
||||
case MultiplierValue.x5:
|
||||
return Assets.images.multiplier.x5.dimmed.keyName;
|
||||
case MultiplierValue.x6:
|
||||
return Assets.images.multiplier.x6.dimmed.keyName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template multiplier_sprite_group_component}
|
||||
/// A [SpriteGroupComponent] for a [Multiplier] with lit and dimmed states.
|
||||
/// {@endtemplate}
|
||||
@visibleForTesting
|
||||
class MultiplierSpriteGroupComponent
|
||||
extends SpriteGroupComponent<MultiplierSpriteState>
|
||||
with HasGameRef, ParentIsA<Multiplier> {
|
||||
/// {@macro multiplier_sprite_group_component}
|
||||
MultiplierSpriteGroupComponent({
|
||||
required Vector2 position,
|
||||
required String litAssetPath,
|
||||
required String dimmedAssetPath,
|
||||
required double angle,
|
||||
required MultiplierState current,
|
||||
}) : _litAssetPath = litAssetPath,
|
||||
_dimmedAssetPath = dimmedAssetPath,
|
||||
super(
|
||||
anchor: Anchor.center,
|
||||
position: position,
|
||||
angle: angle,
|
||||
current: current.spriteState,
|
||||
);
|
||||
|
||||
final String _litAssetPath;
|
||||
final String _dimmedAssetPath;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
parent.bloc.stream.listen((state) => current = state.spriteState);
|
||||
|
||||
final sprites = {
|
||||
MultiplierSpriteState.lit:
|
||||
Sprite(gameRef.images.fromCache(_litAssetPath)),
|
||||
MultiplierSpriteState.dimmed:
|
||||
Sprite(gameRef.images.fromCache(_dimmedAssetPath)),
|
||||
};
|
||||
this.sprites = sprites;
|
||||
size = sprites[current]!.originalSize / 10;
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:sandbox/stories/ball/basic_ball_game.dart';
|
||||
|
||||
class MultipliersGame extends BallGame with KeyboardEvents {
|
||||
MultipliersGame()
|
||||
: super(
|
||||
imagesFileNames: [
|
||||
Assets.images.multiplier.x2.lit.keyName,
|
||||
Assets.images.multiplier.x2.dimmed.keyName,
|
||||
Assets.images.multiplier.x3.lit.keyName,
|
||||
Assets.images.multiplier.x3.dimmed.keyName,
|
||||
Assets.images.multiplier.x4.lit.keyName,
|
||||
Assets.images.multiplier.x4.dimmed.keyName,
|
||||
Assets.images.multiplier.x5.lit.keyName,
|
||||
Assets.images.multiplier.x5.dimmed.keyName,
|
||||
Assets.images.multiplier.x6.lit.keyName,
|
||||
Assets.images.multiplier.x6.dimmed.keyName,
|
||||
],
|
||||
);
|
||||
|
||||
static const description = '''
|
||||
Shows how the Multipliers are rendered.
|
||||
|
||||
- Tap anywhere on the screen to spawn a ball into the game.
|
||||
- Press digits 2 to 6 for toggle state multipliers 2 to 6.
|
||||
''';
|
||||
|
||||
final List<Multiplier> multipliers = [
|
||||
Multiplier.x2(
|
||||
position: Vector2(-20, 0),
|
||||
angle: -15 * math.pi / 180,
|
||||
),
|
||||
Multiplier.x3(
|
||||
position: Vector2(20, -5),
|
||||
angle: 15 * math.pi / 180,
|
||||
),
|
||||
Multiplier.x4(
|
||||
position: Vector2(0, -15),
|
||||
angle: 0,
|
||||
),
|
||||
Multiplier.x5(
|
||||
position: Vector2(-10, -25),
|
||||
angle: -3 * math.pi / 180,
|
||||
),
|
||||
Multiplier.x6(
|
||||
position: Vector2(10, -35),
|
||||
angle: 8 * math.pi / 180,
|
||||
),
|
||||
];
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
|
||||
camera.followVector2(Vector2.zero());
|
||||
|
||||
await addAll(multipliers);
|
||||
await traceAllBodies();
|
||||
}
|
||||
|
||||
@override
|
||||
KeyEventResult onKeyEvent(
|
||||
RawKeyEvent event,
|
||||
Set<LogicalKeyboardKey> keysPressed,
|
||||
) {
|
||||
if (event is RawKeyDownEvent) {
|
||||
var currentMultiplier = 1;
|
||||
|
||||
if (event.logicalKey == LogicalKeyboardKey.digit2) {
|
||||
currentMultiplier = 2;
|
||||
}
|
||||
if (event.logicalKey == LogicalKeyboardKey.digit3) {
|
||||
currentMultiplier = 3;
|
||||
}
|
||||
if (event.logicalKey == LogicalKeyboardKey.digit4) {
|
||||
currentMultiplier = 4;
|
||||
}
|
||||
if (event.logicalKey == LogicalKeyboardKey.digit5) {
|
||||
currentMultiplier = 5;
|
||||
}
|
||||
if (event.logicalKey == LogicalKeyboardKey.digit6) {
|
||||
currentMultiplier = 6;
|
||||
}
|
||||
|
||||
for (final multiplier in multipliers) {
|
||||
multiplier.bloc.next(currentMultiplier);
|
||||
}
|
||||
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
import 'package:dashbook/dashbook.dart';
|
||||
import 'package:sandbox/common/common.dart';
|
||||
import 'package:sandbox/stories/multipliers/multipliers_game.dart';
|
||||
|
||||
void addMultipliersStories(Dashbook dashbook) {
|
||||
dashbook.storiesOf('Multipliers').addGame(
|
||||
title: 'Multipliers',
|
||||
description: MultipliersGame.description,
|
||||
gameBuilder: (_) => MultipliersGame(),
|
||||
);
|
||||
}
|
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 26 KiB |
@ -0,0 +1,118 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
void main() {
|
||||
group(
|
||||
'MultiplierCubit',
|
||||
() {
|
||||
blocTest<MultiplierCubit, MultiplierState>(
|
||||
"emits [lit] when 'next' on x2 dimmed with x2 multiplier value",
|
||||
build: () => MultiplierCubit(MultiplierValue.x2),
|
||||
act: (bloc) => bloc.next(2),
|
||||
expect: () => [
|
||||
isA<MultiplierState>()
|
||||
..having(
|
||||
(state) => state.spriteState,
|
||||
'spriteState',
|
||||
MultiplierSpriteState.lit,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<MultiplierCubit, MultiplierState>(
|
||||
"emits [lit] when 'next' on x3 dimmed with x3 multiplier value",
|
||||
build: () => MultiplierCubit(MultiplierValue.x3),
|
||||
act: (bloc) => bloc.next(3),
|
||||
expect: () => [
|
||||
isA<MultiplierState>()
|
||||
..having(
|
||||
(state) => state.spriteState,
|
||||
'spriteState',
|
||||
MultiplierSpriteState.lit,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<MultiplierCubit, MultiplierState>(
|
||||
"emits [lit] when 'next' on x4 dimmed with x4 multiplier value",
|
||||
build: () => MultiplierCubit(MultiplierValue.x4),
|
||||
act: (bloc) => bloc.next(4),
|
||||
expect: () => [
|
||||
isA<MultiplierState>()
|
||||
..having(
|
||||
(state) => state.spriteState,
|
||||
'spriteState',
|
||||
MultiplierSpriteState.lit,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<MultiplierCubit, MultiplierState>(
|
||||
"emits [lit] when 'next' on x5 dimmed with x5 multiplier value",
|
||||
build: () => MultiplierCubit(MultiplierValue.x5),
|
||||
act: (bloc) => bloc.next(5),
|
||||
expect: () => [
|
||||
isA<MultiplierState>()
|
||||
..having(
|
||||
(state) => state.spriteState,
|
||||
'spriteState',
|
||||
MultiplierSpriteState.lit,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<MultiplierCubit, MultiplierState>(
|
||||
"emits [lit] when 'next' on x6 dimmed with x6 multiplier value",
|
||||
build: () => MultiplierCubit(MultiplierValue.x6),
|
||||
act: (bloc) => bloc.next(6),
|
||||
expect: () => [
|
||||
isA<MultiplierState>()
|
||||
..having(
|
||||
(state) => state.spriteState,
|
||||
'spriteState',
|
||||
MultiplierSpriteState.lit,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<MultiplierCubit, MultiplierState>(
|
||||
"emits [dimmed] when 'next' on lit with different multiplier value",
|
||||
build: () => MultiplierCubit(MultiplierValue.x2),
|
||||
seed: () => MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
act: (bloc) => bloc.next(3),
|
||||
expect: () => [
|
||||
isA<MultiplierState>()
|
||||
..having(
|
||||
(state) => state.spriteState,
|
||||
'spriteState',
|
||||
MultiplierSpriteState.dimmed,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<MultiplierCubit, MultiplierState>(
|
||||
"emits nothing when 'next' on lit with same multiplier value",
|
||||
build: () => MultiplierCubit(MultiplierValue.x2),
|
||||
seed: () => MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
act: (bloc) => bloc.next(2),
|
||||
expect: () => <MultiplierState>[],
|
||||
);
|
||||
|
||||
blocTest<MultiplierCubit, MultiplierState>(
|
||||
"emits nothing when 'next' on dimmed with different multiplier value",
|
||||
build: () => MultiplierCubit(MultiplierValue.x2),
|
||||
act: (bloc) => bloc.next(3),
|
||||
expect: () => <MultiplierState>[],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/src/pinball_components.dart';
|
||||
|
||||
void main() {
|
||||
group('MultiplierState', () {
|
||||
test('supports value equality', () {
|
||||
expect(
|
||||
MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
equals(
|
||||
MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
group('constructor', () {
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('copyWith', () {
|
||||
test(
|
||||
'copies correctly '
|
||||
'when no argument specified',
|
||||
() {
|
||||
const multiplierState = MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
);
|
||||
expect(
|
||||
multiplierState.copyWith(),
|
||||
equals(multiplierState),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'copies correctly '
|
||||
'when all arguments specified',
|
||||
() {
|
||||
const multiplierState = MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
);
|
||||
final otherMultiplierState = MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
);
|
||||
expect(multiplierState, isNot(equals(otherMultiplierState)));
|
||||
|
||||
expect(
|
||||
multiplierState.copyWith(
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
),
|
||||
equals(otherMultiplierState),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,517 @@
|
||||
// ignore_for_file: cascade_invocations, prefer_const_constructors
|
||||
|
||||
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 '../../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
final bloc = MockMultiplierCubit();
|
||||
|
||||
group('Multiplier', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final assets = [
|
||||
Assets.images.multiplier.x2.lit.keyName,
|
||||
Assets.images.multiplier.x2.dimmed.keyName,
|
||||
Assets.images.multiplier.x3.lit.keyName,
|
||||
Assets.images.multiplier.x3.dimmed.keyName,
|
||||
Assets.images.multiplier.x4.lit.keyName,
|
||||
Assets.images.multiplier.x4.dimmed.keyName,
|
||||
Assets.images.multiplier.x5.lit.keyName,
|
||||
Assets.images.multiplier.x5.dimmed.keyName,
|
||||
Assets.images.multiplier.x6.lit.keyName,
|
||||
Assets.images.multiplier.x6.dimmed.keyName,
|
||||
];
|
||||
final flameTester = FlameTester(() => TestGame(assets));
|
||||
|
||||
flameTester.test('"x2" loads correctly', (game) async {
|
||||
final multiplier = Multiplier.x2(
|
||||
position: Vector2.zero(),
|
||||
angle: 0,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
expect(game.contains(multiplier), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"x3" loads correctly', (game) async {
|
||||
final multiplier = Multiplier.x3(
|
||||
position: Vector2.zero(),
|
||||
angle: 0,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
expect(game.contains(multiplier), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"x4" loads correctly', (game) async {
|
||||
final multiplier = Multiplier.x4(
|
||||
position: Vector2.zero(),
|
||||
angle: 0,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
expect(game.contains(multiplier), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"x5" loads correctly', (game) async {
|
||||
final multiplier = Multiplier.x5(
|
||||
position: Vector2.zero(),
|
||||
angle: 0,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
expect(game.contains(multiplier), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"x6" loads correctly', (game) async {
|
||||
final multiplier = Multiplier.x6(
|
||||
position: Vector2.zero(),
|
||||
angle: 0,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
expect(game.contains(multiplier), isTrue);
|
||||
});
|
||||
|
||||
group('renders correctly', () {
|
||||
group('x2', () {
|
||||
const multiplierValue = MultiplierValue.x2;
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'lit when bloc state is lit',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.lit,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x2-lit.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'dimmed when bloc state is dimmed',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.dimmed,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x2-dimmed.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('x3', () {
|
||||
const multiplierValue = MultiplierValue.x3;
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'lit when bloc state is lit',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.lit,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x3-lit.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'dimmed when bloc state is dimmed',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.dimmed,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x3-dimmed.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('x4', () {
|
||||
const multiplierValue = MultiplierValue.x4;
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'lit when bloc state is lit',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.lit,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x4-lit.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'dimmed when bloc state is dimmed',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.dimmed,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x4-dimmed.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('x5', () {
|
||||
const multiplierValue = MultiplierValue.x5;
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'lit when bloc state is lit',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.lit,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x5-lit.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'dimmed when bloc state is dimmed',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.dimmed,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x5-dimmed.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('x6', () {
|
||||
const multiplierValue = MultiplierValue.x6;
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'lit when bloc state is lit',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.lit,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.lit,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x6-lit.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.testGameWidget(
|
||||
'dimmed when bloc state is dimmed',
|
||||
setUp: (game, tester) async {
|
||||
await game.images.loadAll(assets);
|
||||
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: multiplierValue,
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
),
|
||||
);
|
||||
|
||||
final multiplier = Multiplier.test(
|
||||
value: multiplierValue,
|
||||
bloc: bloc,
|
||||
);
|
||||
await game.ensureAdd(multiplier);
|
||||
await tester.pump();
|
||||
|
||||
game.camera.followVector2(Vector2.zero());
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
expect(
|
||||
game
|
||||
.descendants()
|
||||
.whereType<MultiplierSpriteGroupComponent>()
|
||||
.first
|
||||
.current,
|
||||
MultiplierSpriteState.dimmed,
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
find.byGame<TestGame>(),
|
||||
matchesGoldenFile('../golden/multipliers/x6-dimmed.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
flameTester.test('closes bloc when removed', (game) async {
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState(
|
||||
value: MultiplierValue.x2,
|
||||
spriteState: MultiplierSpriteState.dimmed,
|
||||
),
|
||||
);
|
||||
when(bloc.close).thenAnswer((_) async {});
|
||||
final multiplier = Multiplier.test(value: MultiplierValue.x2, bloc: bloc);
|
||||
|
||||
await game.ensureAdd(multiplier);
|
||||
game.remove(multiplier);
|
||||
await game.ready();
|
||||
|
||||
verify(bloc.close).called(1);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
// ignore_for_file: cascade_invocations, prefer_const_constructors
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockingjay/mockingjay.dart';
|
||||
import 'package:pinball/game/components/multipliers/behaviors/behaviors.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final assets = [
|
||||
Assets.images.multiplier.x2.lit.keyName,
|
||||
Assets.images.multiplier.x2.dimmed.keyName,
|
||||
Assets.images.multiplier.x3.lit.keyName,
|
||||
Assets.images.multiplier.x3.dimmed.keyName,
|
||||
Assets.images.multiplier.x4.lit.keyName,
|
||||
Assets.images.multiplier.x4.dimmed.keyName,
|
||||
Assets.images.multiplier.x5.lit.keyName,
|
||||
Assets.images.multiplier.x5.dimmed.keyName,
|
||||
Assets.images.multiplier.x6.lit.keyName,
|
||||
Assets.images.multiplier.x6.dimmed.keyName,
|
||||
];
|
||||
|
||||
group('MultipliersBehavior', () {
|
||||
late GameBloc gameBloc;
|
||||
|
||||
setUp(() {
|
||||
registerFallbackValue(MockComponent());
|
||||
gameBloc = MockGameBloc();
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial(),
|
||||
);
|
||||
});
|
||||
|
||||
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
|
||||
gameBuilder: EmptyPinballTestGame.new,
|
||||
blocBuilder: () => gameBloc,
|
||||
assets: assets,
|
||||
);
|
||||
|
||||
group('listenWhen', () {
|
||||
test('is true when the multiplier has changed', () {
|
||||
final state = GameState(
|
||||
score: 10,
|
||||
multiplier: 2,
|
||||
rounds: 0,
|
||||
bonusHistory: const [],
|
||||
);
|
||||
|
||||
final previous = GameState.initial();
|
||||
expect(
|
||||
MultipliersBehavior().listenWhen(previous, state),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('is false when the multiplier state is the same', () {
|
||||
final state = GameState(
|
||||
score: 10,
|
||||
multiplier: 1,
|
||||
rounds: 0,
|
||||
bonusHistory: const [],
|
||||
);
|
||||
|
||||
final previous = GameState.initial();
|
||||
expect(
|
||||
MultipliersBehavior().listenWhen(previous, state),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('onNewState', () {
|
||||
flameBlocTester.testGameWidget(
|
||||
"calls 'next' once per each multiplier when GameBloc emit state",
|
||||
setUp: (game, tester) async {
|
||||
final behavior = MultipliersBehavior();
|
||||
final parent = Multipliers.test();
|
||||
final multiplierX2Cubit = MockMultiplierCubit();
|
||||
final multiplierX3Cubit = MockMultiplierCubit();
|
||||
final multipliers = [
|
||||
Multiplier.test(
|
||||
value: MultiplierValue.x2,
|
||||
bloc: multiplierX2Cubit,
|
||||
),
|
||||
Multiplier.test(
|
||||
value: MultiplierValue.x3,
|
||||
bloc: multiplierX3Cubit,
|
||||
),
|
||||
];
|
||||
|
||||
whenListen(
|
||||
multiplierX2Cubit,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState.initial(MultiplierValue.x2),
|
||||
);
|
||||
when(() => multiplierX2Cubit.next(any())).thenAnswer((_) async {});
|
||||
|
||||
whenListen(
|
||||
multiplierX3Cubit,
|
||||
const Stream<MultiplierState>.empty(),
|
||||
initialState: MultiplierState.initial(MultiplierValue.x2),
|
||||
);
|
||||
when(() => multiplierX3Cubit.next(any())).thenAnswer((_) async {});
|
||||
|
||||
await parent.addAll(multipliers);
|
||||
await game.ensureAdd(parent);
|
||||
await parent.ensureAdd(behavior);
|
||||
|
||||
await tester.pump();
|
||||
|
||||
behavior.onNewState(
|
||||
GameState.initial().copyWith(multiplier: 2),
|
||||
);
|
||||
|
||||
for (final multiplier in multipliers) {
|
||||
verify(
|
||||
() => multiplier.bloc.next(any()),
|
||||
).called(1);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final assets = [
|
||||
Assets.images.multiplier.x2.lit.keyName,
|
||||
Assets.images.multiplier.x2.dimmed.keyName,
|
||||
Assets.images.multiplier.x3.lit.keyName,
|
||||
Assets.images.multiplier.x3.dimmed.keyName,
|
||||
Assets.images.multiplier.x4.lit.keyName,
|
||||
Assets.images.multiplier.x4.dimmed.keyName,
|
||||
Assets.images.multiplier.x5.lit.keyName,
|
||||
Assets.images.multiplier.x5.dimmed.keyName,
|
||||
Assets.images.multiplier.x6.lit.keyName,
|
||||
Assets.images.multiplier.x6.dimmed.keyName,
|
||||
];
|
||||
|
||||
late GameBloc gameBloc;
|
||||
|
||||
setUp(() {
|
||||
gameBloc = GameBloc();
|
||||
});
|
||||
|
||||
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
|
||||
gameBuilder: EmptyPinballTestGame.new,
|
||||
blocBuilder: () => gameBloc,
|
||||
assets: assets,
|
||||
);
|
||||
|
||||
group('Multipliers', () {
|
||||
flameBlocTester.testGameWidget(
|
||||
'loads correctly',
|
||||
setUp: (game, tester) async {
|
||||
final multipliersGroup = Multipliers();
|
||||
await game.ensureAdd(multipliersGroup);
|
||||
|
||||
expect(game.contains(multipliersGroup), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
group('loads', () {
|
||||
flameBlocTester.testGameWidget(
|
||||
'five Multiplier',
|
||||
setUp: (game, tester) async {
|
||||
final multipliersGroup = Multipliers();
|
||||
await game.ensureAdd(multipliersGroup);
|
||||
|
||||
expect(
|
||||
multipliersGroup.descendants().whereType<Multiplier>().length,
|
||||
equals(5),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|