feat: implemented DashBumper assets (#119)

* chore: rebase

* chore: rebase

* chore: rebase

* docs: removed extra paragraph

* fix: removed checks

* refactor: removed ephemeral state

* refactor: uncommented code

* feat: automatically sized Sprites

* docs: included TODO comment

* refactor: corrected typo

* docs: updated doc comment

* feat: adjusted tests

* chore: rebase

* feat: included mustCallSuper

* feat: implemented FlutterForest controllers

* feat: improved tests

* fix: analyzer

* refactor: removed unneccessary mock

* feat: include DashNestBumper tests

* docs: improved doc comment

* refactor: fixed test name grammar

* fix: fixed test
pull/133/head
Alejandro Santiago 4 years ago committed by GitHub
parent 48f831264e
commit e5c3708952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter/foundation.dart';
/// {@template component_controller} /// {@template component_controller}
/// A [ComponentController] is a [Component] in charge of handling the logic /// A [ComponentController] is a [Component] in charge of handling the logic
@ -30,6 +31,7 @@ mixin Controls<T extends ComponentController> on Component {
late final T controller; late final T controller;
@override @override
@mustCallSuper
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
await add(controller); await add(controller);

@ -1,11 +1,10 @@
// ignore_for_file: avoid_renaming_method_parameters // ignore_for_file: avoid_renaming_method_parameters
import 'dart:math' as math;
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pinball/flame/flame.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
@ -16,10 +15,47 @@ import 'package:pinball_components/pinball_components.dart';
/// When all [DashNestBumper]s are hit at least once, the [GameBonus.dashNest] /// When all [DashNestBumper]s are hit at least once, the [GameBonus.dashNest]
/// is awarded, and the [BigDashNestBumper] releases a new [Ball]. /// is awarded, and the [BigDashNestBumper] releases a new [Ball].
/// {@endtemplate} /// {@endtemplate}
// TODO(alestiago): Make a [Blueprint] once nesting [Blueprint] is implemented. // TODO(alestiago): Make a [Blueprint] once [Blueprint] inherits from
class FlutterForest extends Component // [Component].
with HasGameRef<PinballGame>, BlocComponent<GameBloc, GameState> { class FlutterForest extends Component with Controls<_FlutterForestController> {
/// {@macro flutter_forest} /// {@macro flutter_forest}
FlutterForest() {
controller = _FlutterForestController(this);
}
@override
Future<void> onLoad() async {
await super.onLoad();
final signPost = FlutterSignPost()..initialPosition = Vector2(8.35, 58.3);
final bigNest = _ControlledBigDashNestBumper(
id: 'big_nest_bumper',
)..initialPosition = Vector2(18.55, 59.35);
final smallLeftNest = _ControlledSmallDashNestBumper.a(
id: 'small_nest_bumper_a',
)..initialPosition = Vector2(8.95, 51.95);
final smallRightNest = _ControlledSmallDashNestBumper.b(
id: 'small_nest_bumper_b',
)..initialPosition = Vector2(23.3, 46.75);
await addAll([
signPost,
smallLeftNest,
smallRightNest,
bigNest,
]);
}
}
class _FlutterForestController extends ComponentController<FlutterForest>
with BlocComponent<GameBloc, GameState>, HasGameRef<PinballGame> {
_FlutterForestController(FlutterForest flutterForest) : super(flutterForest);
@override
Future<void> onLoad() async {
await super.onLoad();
gameRef.addContactCallback(_ControlledDashNestBumperBallContactCallback());
}
@override @override
bool listenWhen(GameState? previousState, GameState newState) { bool listenWhen(GameState? previousState, GameState newState) {
@ -32,117 +68,90 @@ class FlutterForest extends Component
void onNewState(GameState state) { void onNewState(GameState state) {
super.onNewState(state); super.onNewState(state);
add( component.add(
ControlledBall.bonus( ControlledBall.bonus(theme: gameRef.theme)
theme: gameRef.theme, ..initialPosition = Vector2(17.2, 52.7),
)..initialPosition = Vector2(17.2, 52.7),
); );
} }
}
@override class _ControlledBigDashNestBumper extends BigDashNestBumper
Future<void> onLoad() async { with Controls<DashNestBumperController>, ScorePoints {
gameRef.addContactCallback(DashNestBumperBallContactCallback()); _ControlledBigDashNestBumper({required String id}) : super() {
controller = DashNestBumperController(this, id: id);
}
final signPost = FlutterSignPost()..initialPosition = Vector2(8.35, 58.3); @override
int get points => 20;
}
// TODO(alestiago): adjust positioning once sprites are added. class _ControlledSmallDashNestBumper extends SmallDashNestBumper
final smallLeftNest = SmallDashNestBumper(id: 'small_left_nest') with Controls<DashNestBumperController>, ScorePoints {
..initialPosition = Vector2(8.95, 51.95); _ControlledSmallDashNestBumper.a({required String id}) : super.a() {
final smallRightNest = SmallDashNestBumper(id: 'small_right_nest') controller = DashNestBumperController(this, id: id);
..initialPosition = Vector2(23.3, 46.75); }
final bigNest = BigDashNestBumper(id: 'big_nest')
..initialPosition = Vector2(18.55, 59.35);
await addAll([ _ControlledSmallDashNestBumper.b({required String id}) : super.b() {
signPost, controller = DashNestBumperController(this, id: id);
smallLeftNest,
smallRightNest,
bigNest,
]);
} }
@override
int get points => 10;
} }
/// {@template dash_nest_bumper} /// {@template dash_nest_bumper_controller}
/// Bumper located in the [FlutterForest]. /// Controls a [DashNestBumper].
/// {@endtemplate} /// {@endtemplate}
@visibleForTesting @visibleForTesting
abstract class DashNestBumper extends BodyComponent<PinballGame> class DashNestBumperController extends ComponentController<DashNestBumper>
with ScorePoints, InitialPosition { with BlocComponent<GameBloc, GameState>, HasGameRef<PinballGame> {
/// {@macro dash_nest_bumper} /// {@macro dash_nest_bumper_controller}
DashNestBumper({required this.id}) { DashNestBumperController(
paint = Paint() DashNestBumper dashNestBumper, {
..color = Colors.blue.withOpacity(0.5) required this.id,
..style = PaintingStyle.fill; }) : super(dashNestBumper);
}
/// Unique identifier for the controlled [DashNestBumper].
/// Unique identifier for this [DashNestBumper].
/// ///
/// Used to identify [DashNestBumper]s in [GameState.activatedDashNests]. /// Used to identify [DashNestBumper]s in [GameState.activatedDashNests].
final String id; final String id;
}
/// Listens when a [Ball] bounces bounces against a [DashNestBumper].
@visibleForTesting
class DashNestBumperBallContactCallback
extends ContactCallback<DashNestBumper, Ball> {
@override @override
void begin(DashNestBumper dashNestBumper, Ball ball, Contact _) { bool listenWhen(GameState? previousState, GameState newState) {
dashNestBumper.gameRef.read<GameBloc>().add( final wasActive = previousState?.activatedDashNests.contains(id) ?? false;
DashNestActivated(dashNestBumper.id), final isActive = newState.activatedDashNests.contains(id);
);
}
}
/// {@macro dash_nest_bumper} return wasActive != isActive;
@visibleForTesting }
class BigDashNestBumper extends DashNestBumper {
/// {@macro dash_nest_bumper}
BigDashNestBumper({required String id}) : super(id: id);
@override @override
int get points => 20; void onNewState(GameState state) {
super.onNewState(state);
@override if (state.activatedDashNests.contains(id)) {
Body createBody() { component.activate();
final shape = EllipseShape( } else {
center: Vector2.zero(), component.deactivate();
majorRadius: 4.85, }
minorRadius: 3.95,
)..rotate(math.pi / 2);
final fixtureDef = FixtureDef(shape);
final bodyDef = BodyDef()
..position = initialPosition
..userData = this;
return world.createBody(bodyDef)..createFixture(fixtureDef);
} }
}
/// {@macro dash_nest_bumper} /// Registers when a [DashNestBumper] is hit by a [Ball].
@visibleForTesting ///
class SmallDashNestBumper extends DashNestBumper { /// Triggered by [_ControlledDashNestBumperBallContactCallback].
/// {@macro dash_nest_bumper} void hit() {
SmallDashNestBumper({required String id}) : super(id: id); gameRef.read<GameBloc>().add(DashNestActivated(id));
}
@override }
int get points => 10;
/// Listens when a [Ball] bounces bounces against a [DashNestBumper].
class _ControlledDashNestBumperBallContactCallback
extends ContactCallback<Controls<DashNestBumperController>, Ball> {
@override @override
Body createBody() { void begin(
final shape = EllipseShape( Controls<DashNestBumperController> controlledDashNestBumper,
center: Vector2.zero(), Ball _,
majorRadius: 3, Contact __,
minorRadius: 2.25, ) {
)..rotate(math.pi / 2); controlledDashNestBumper.controller.hit();
final fixtureDef = FixtureDef(shape)
..friction = 0
..restitution = 4;
final bodyDef = BodyDef()
..position = initialPosition
..userData = this;
return world.createBody(bodyDef)..createFixture(fixtureDef);
} }
} }

@ -15,6 +15,12 @@ extension PinballGameAssetsX on PinballGame {
images.load(components.Assets.images.baseboard.right.keyName), images.load(components.Assets.images.baseboard.right.keyName),
images.load(components.Assets.images.dino.dinoLandTop.keyName), images.load(components.Assets.images.dino.dinoLandTop.keyName),
images.load(components.Assets.images.dino.dinoLandBottom.keyName), images.load(components.Assets.images.dino.dinoLandBottom.keyName),
images.load(components.Assets.images.dashBumper.a.active.keyName),
images.load(components.Assets.images.dashBumper.a.inactive.keyName),
images.load(components.Assets.images.dashBumper.b.active.keyName),
images.load(components.Assets.images.dashBumper.b.inactive.keyName),
images.load(components.Assets.images.dashBumper.main.active.keyName),
images.load(components.Assets.images.dashBumper.main.inactive.keyName),
images.load(Assets.images.components.background.path), images.load(Assets.images.components.background.path),
]); ]);
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

@ -14,6 +14,8 @@ class $AssetsImagesGen {
AssetGenImage get ball => const AssetGenImage('assets/images/ball.png'); AssetGenImage get ball => const AssetGenImage('assets/images/ball.png');
$AssetsImagesBaseboardGen get baseboard => const $AssetsImagesBaseboardGen(); $AssetsImagesBaseboardGen get baseboard => const $AssetsImagesBaseboardGen();
$AssetsImagesDashBumperGen get dashBumper =>
const $AssetsImagesDashBumperGen();
$AssetsImagesDinoGen get dino => const $AssetsImagesDinoGen(); $AssetsImagesDinoGen get dino => const $AssetsImagesDinoGen();
$AssetsImagesFlipperGen get flipper => const $AssetsImagesFlipperGen(); $AssetsImagesFlipperGen get flipper => const $AssetsImagesFlipperGen();
@ -42,6 +44,15 @@ class $AssetsImagesBaseboardGen {
const AssetGenImage('assets/images/baseboard/right.png'); const AssetGenImage('assets/images/baseboard/right.png');
} }
class $AssetsImagesDashBumperGen {
const $AssetsImagesDashBumperGen();
$AssetsImagesDashBumperAGen get a => const $AssetsImagesDashBumperAGen();
$AssetsImagesDashBumperBGen get b => const $AssetsImagesDashBumperBGen();
$AssetsImagesDashBumperMainGen get main =>
const $AssetsImagesDashBumperMainGen();
}
class $AssetsImagesDinoGen { class $AssetsImagesDinoGen {
const $AssetsImagesDinoGen(); const $AssetsImagesDinoGen();
@ -66,6 +77,42 @@ class $AssetsImagesFlipperGen {
const AssetGenImage('assets/images/flipper/right.png'); const AssetGenImage('assets/images/flipper/right.png');
} }
class $AssetsImagesDashBumperAGen {
const $AssetsImagesDashBumperAGen();
/// File path: assets/images/dash_bumper/a/active.png
AssetGenImage get active =>
const AssetGenImage('assets/images/dash_bumper/a/active.png');
/// File path: assets/images/dash_bumper/a/inactive.png
AssetGenImage get inactive =>
const AssetGenImage('assets/images/dash_bumper/a/inactive.png');
}
class $AssetsImagesDashBumperBGen {
const $AssetsImagesDashBumperBGen();
/// File path: assets/images/dash_bumper/b/active.png
AssetGenImage get active =>
const AssetGenImage('assets/images/dash_bumper/b/active.png');
/// File path: assets/images/dash_bumper/b/inactive.png
AssetGenImage get inactive =>
const AssetGenImage('assets/images/dash_bumper/b/inactive.png');
}
class $AssetsImagesDashBumperMainGen {
const $AssetsImagesDashBumperMainGen();
/// File path: assets/images/dash_bumper/main/active.png
AssetGenImage get active =>
const AssetGenImage('assets/images/dash_bumper/main/active.png');
/// File path: assets/images/dash_bumper/main/inactive.png
AssetGenImage get inactive =>
const AssetGenImage('assets/images/dash_bumper/main/inactive.png');
}
class Assets { class Assets {
Assets._(); Assets._();

@ -2,6 +2,7 @@ export 'ball.dart';
export 'baseboard.dart'; export 'baseboard.dart';
export 'board_dimensions.dart'; export 'board_dimensions.dart';
export 'board_side.dart'; export 'board_side.dart';
export 'dash_nest_bumper.dart';
export 'dino_walls.dart'; export 'dino_walls.dart';
export 'fire_effect.dart'; export 'fire_effect.dart';
export 'flipper.dart'; export 'flipper.dart';

@ -0,0 +1,142 @@
import 'dart:math' as math;
import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball_components/pinball_components.dart';
/// {@template dash_nest_bumper}
/// Bumper with a nest appearance.
/// {@endtemplate}
abstract class DashNestBumper extends BodyComponent with InitialPosition {
/// {@macro dash_nest_bumper}
DashNestBumper._({
required String activeAssetPath,
required String inactiveAssetPath,
required SpriteComponent spriteComponent,
}) : _activeAssetPath = activeAssetPath,
_inactiveAssetPath = inactiveAssetPath,
_spriteComponent = spriteComponent;
final String _activeAssetPath;
late final Sprite _activeSprite;
final String _inactiveAssetPath;
late final Sprite _inactiveSprite;
final SpriteComponent _spriteComponent;
Future<void> _loadSprites() async {
// TODO(alestiago): I think ideally we would like to do:
// Sprite(path).load so we don't require to store the activeAssetPath and
// the inactive assetPath.
_inactiveSprite = await gameRef.loadSprite(_inactiveAssetPath);
_activeSprite = await gameRef.loadSprite(_activeAssetPath);
}
/// Activates the [DashNestBumper].
void activate() {
_spriteComponent
..sprite = _activeSprite
..size = _activeSprite.originalSize / 10;
}
/// Deactivates the [DashNestBumper].
void deactivate() {
_spriteComponent
..sprite = _inactiveSprite
..size = _inactiveSprite.originalSize / 10;
}
@override
Future<void> onLoad() async {
await super.onLoad();
await _loadSprites();
// TODO(erickzanardo): Look into using onNewState instead.
// Currently doing: onNewState(gameRef.read<GameState>()) will throw an
// `Exception: build context is not available yet`
deactivate();
await add(_spriteComponent);
}
}
/// {@macro dash_nest_bumper}
class BigDashNestBumper extends DashNestBumper {
/// {@macro dash_nest_bumper}
BigDashNestBumper()
: super._(
activeAssetPath: Assets.images.dashBumper.main.active.keyName,
inactiveAssetPath: Assets.images.dashBumper.main.inactive.keyName,
spriteComponent: SpriteComponent(
anchor: Anchor.center,
),
);
@override
Body createBody() {
final shape = EllipseShape(
center: Vector2.zero(),
majorRadius: 4.85,
minorRadius: 3.95,
)..rotate(math.pi / 2);
final fixtureDef = FixtureDef(shape);
final bodyDef = BodyDef()
..position = initialPosition
..userData = this;
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}
/// {@macro dash_nest_bumper}
class SmallDashNestBumper extends DashNestBumper {
/// {@macro dash_nest_bumper}
SmallDashNestBumper._({
required String activeAssetPath,
required String inactiveAssetPath,
required SpriteComponent spriteComponent,
}) : super._(
activeAssetPath: activeAssetPath,
inactiveAssetPath: inactiveAssetPath,
spriteComponent: spriteComponent,
);
/// {@macro dash_nest_bumper}
SmallDashNestBumper.a()
: this._(
activeAssetPath: Assets.images.dashBumper.a.active.keyName,
inactiveAssetPath: Assets.images.dashBumper.a.inactive.keyName,
spriteComponent: SpriteComponent(
anchor: Anchor.center,
position: Vector2(0.35, -1.2),
),
);
/// {@macro dash_nest_bumper}
SmallDashNestBumper.b()
: this._(
activeAssetPath: Assets.images.dashBumper.b.active.keyName,
inactiveAssetPath: Assets.images.dashBumper.b.inactive.keyName,
spriteComponent: SpriteComponent(
anchor: Anchor.center,
position: Vector2(0.35, -1.2),
),
);
@override
Body createBody() {
final shape = EllipseShape(
center: Vector2.zero(),
majorRadius: 3,
minorRadius: 2.25,
)..rotate(math.pi / 2);
final fixtureDef = FixtureDef(shape)
..friction = 0
..restitution = 4;
final bodyDef = BodyDef()
..position = initialPosition
..userData = this;
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}

@ -29,6 +29,9 @@ flutter:
- assets/images/baseboard/ - assets/images/baseboard/
- assets/images/dino/ - assets/images/dino/
- assets/images/flipper/ - assets/images/flipper/
- assets/images/dash_bumper/a/
- assets/images/dash_bumper/b/
- assets/images/dash_bumper/main/
flutter_gen: flutter_gen:
line_length: 80 line_length: 80

@ -0,0 +1,116 @@
// ignore_for_file: cascade_invocations
import 'package:flame/components.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_components/pinball_components.dart';
import '../../helpers/helpers.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(TestGame.new);
group('BigDashNestBumper', () {
flameTester.test('loads correctly', (game) async {
final bumper = BigDashNestBumper();
await game.ensureAdd(bumper);
expect(game.contains(bumper), isTrue);
});
flameTester.test('activate returns normally', (game) async {
final bumper = BigDashNestBumper();
await game.ensureAdd(bumper);
expect(bumper.activate, returnsNormally);
});
flameTester.test('deactivate returns normally', (game) async {
final bumper = BigDashNestBumper();
await game.ensureAdd(bumper);
expect(bumper.deactivate, returnsNormally);
});
flameTester.test('changes sprite', (game) async {
final bumper = BigDashNestBumper();
await game.ensureAdd(bumper);
final spriteComponent = bumper.firstChild<SpriteComponent>()!;
final deactivatedSprite = spriteComponent.sprite;
bumper.activate();
expect(
spriteComponent.sprite,
isNot(equals(deactivatedSprite)),
);
final activatedSprite = spriteComponent.sprite;
bumper.deactivate();
expect(
spriteComponent.sprite,
isNot(equals(activatedSprite)),
);
expect(
activatedSprite,
isNot(equals(deactivatedSprite)),
);
});
});
group('SmallDashNestBumper', () {
flameTester.test('"a" loads correctly', (game) async {
final bumper = SmallDashNestBumper.a();
await game.ensureAdd(bumper);
expect(game.contains(bumper), isTrue);
});
flameTester.test('"b" loads correctly', (game) async {
final bumper = SmallDashNestBumper.b();
await game.ensureAdd(bumper);
expect(game.contains(bumper), isTrue);
});
flameTester.test('activate returns normally', (game) async {
final bumper = SmallDashNestBumper.a();
await game.ensureAdd(bumper);
expect(bumper.activate, returnsNormally);
});
flameTester.test('deactivate returns normally', (game) async {
final bumper = SmallDashNestBumper.a();
await game.ensureAdd(bumper);
expect(bumper.deactivate, returnsNormally);
});
flameTester.test('changes sprite', (game) async {
final bumper = SmallDashNestBumper.a();
await game.ensureAdd(bumper);
final spriteComponent = bumper.firstChild<SpriteComponent>()!;
final deactivatedSprite = spriteComponent.sprite;
bumper.activate();
expect(
spriteComponent.sprite,
isNot(equals(deactivatedSprite)),
);
final activatedSprite = spriteComponent.sprite;
bumper.deactivate();
expect(
spriteComponent.sprite,
isNot(equals(activatedSprite)),
);
expect(
activatedSprite,
isNot(equals(deactivatedSprite)),
);
});
});
}

@ -48,8 +48,9 @@ void main() {
final canvas = MockCanvas(); final canvas = MockCanvas();
effect.render(canvas); effect.render(canvas);
verify(() => canvas.drawCircle(any(), any(), any())) verify(() => canvas.drawCircle(any(), any(), any())).called(
.called(greaterThan(0)); greaterThan(0),
);
}); });
}); });
} }

@ -1,7 +1,9 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart'; import 'package:mocktail/mocktail.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
@ -9,6 +11,18 @@ import 'package:pinball_components/pinball_components.dart';
import '../../helpers/helpers.dart'; import '../../helpers/helpers.dart';
void beginContact(Forge2DGame game, BodyComponent bodyA, BodyComponent bodyB) {
assert(
bodyA.body.fixtures.isNotEmpty && bodyB.body.fixtures.isNotEmpty,
'Bodies require fixtures to contact each other.',
);
final fixtureA = bodyA.body.fixtures.first;
final fixtureB = bodyB.body.fixtures.first;
final contact = Contact.init(fixtureA, 0, fixtureB, 0);
game.world.contactManager.contactListener?.beginContact(contact);
}
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(PinballGameTest.create);
@ -30,13 +44,73 @@ void main() {
'a FlutterSignPost', 'a FlutterSignPost',
(game) async { (game) async {
await game.ready(); await game.ready();
final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest);
expect( expect(
game.descendants().whereType<FlutterSignPost>().length, flutterForest.descendants().whereType<FlutterSignPost>().length,
equals(1), equals(1),
); );
}, },
); );
flameTester.test(
'a BigDashNestBumper',
(game) async {
await game.ready();
final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest);
expect(
flutterForest.descendants().whereType<BigDashNestBumper>().length,
equals(1),
);
},
);
flameTester.test(
'two SmallDashNestBumper',
(game) async {
await game.ready();
final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest);
expect(
flutterForest.descendants().whereType<SmallDashNestBumper>().length,
equals(2),
);
},
);
});
group('controller', () {
group('listenWhen', () {
final gameBloc = MockGameBloc();
final tester = flameBlocTester(
game: TestGame.new,
gameBloc: () => gameBloc,
);
tester.testGameWidget(
'listens when a Bonus.dashNest is added',
verify: (game, tester) async {
final flutterForest = FlutterForest();
const state = GameState(
score: 0,
balls: 3,
activatedBonusLetters: [],
activatedDashNests: {},
bonusHistory: [GameBonus.dashNest],
);
expect(
flutterForest.controller
.listenWhen(const GameState.initial(), state),
isTrue,
);
},
);
});
}); });
flameTester.test( flameTester.test(
@ -47,7 +121,7 @@ void main() {
await game.ensureAdd(flutterForest); await game.ensureAdd(flutterForest);
final previousBalls = game.descendants().whereType<Ball>().length; final previousBalls = game.descendants().whereType<Ball>().length;
flutterForest.onNewState(MockGameState()); flutterForest.controller.onNewState(MockGameState());
await game.ready(); await game.ready();
expect( expect(
@ -57,14 +131,13 @@ void main() {
}, },
); );
group('listenWhen', () { group('bumpers', () {
final gameBloc = MockGameBloc(); late Ball ball;
final tester = flameBlocTester( late GameBloc gameBloc;
game: TestGame.new,
gameBloc: () => gameBloc,
);
setUp(() { setUp(() {
ball = Ball(baseColor: const Color(0xFF00FFFF));
gameBloc = MockGameBloc();
whenListen( whenListen(
gameBloc, gameBloc,
const Stream<GameState>.empty(), const Stream<GameState>.empty(),
@ -72,73 +145,167 @@ void main() {
); );
}); });
final tester = flameBlocTester<PinballGame>(
game: PinballGameTest.create,
gameBloc: () => gameBloc,
);
tester.testGameWidget(
'add DashNestActivated event',
setUp: (game, tester) async {
await game.ready();
final flutterForest =
game.descendants().whereType<FlutterForest>().first;
await game.ensureAdd(ball);
final bumpers =
flutterForest.descendants().whereType<DashNestBumper>();
for (final bumper in bumpers) {
beginContact(game, bumper, ball);
final controller = bumper.firstChild<DashNestBumperController>()!;
verify(
() => gameBloc.add(DashNestActivated(controller.id)),
).called(1);
}
},
);
tester.testGameWidget( tester.testGameWidget(
'listens when a Bonus.dashNest is added', 'add Scored event',
verify: (game, tester) async { setUp: (game, tester) async {
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest);
await game.ensureAdd(ball);
const state = GameState( final bumpers =
score: 0, flutterForest.descendants().whereType<DashNestBumper>();
balls: 3,
activatedBonusLetters: [], for (final bumper in bumpers) {
activatedDashNests: {}, beginContact(game, bumper, ball);
bonusHistory: [GameBonus.dashNest], final points = (bumper as ScorePoints).points;
); verify(
expect( () => gameBloc.add(Scored(points: points)),
flutterForest.listenWhen(const GameState.initial(), state), ).called(1);
isTrue, }
);
}, },
); );
}); });
}); });
group('DashNestBumperBallContactCallback', () { group('DashNestBumperController', () {
final gameBloc = MockGameBloc(); late DashNestBumper dashNestBumper;
final tester = flameBlocTester(
// TODO(alestiago): Use TestGame.new once a controller is implemented.
game: PinballGameTest.create,
gameBloc: () => gameBloc,
);
setUp(() { setUp(() {
whenListen( dashNestBumper = MockDashNestBumper();
gameBloc,
const Stream<GameState>.empty(),
initialState: const GameState.initial(),
);
}); });
final dashNestBumper = MockDashNestBumper(); group(
tester.testGameWidget( 'listensWhen',
'adds a DashNestActivated event with DashNestBumper.id', () {
setUp: (game, tester) async { late GameState previousState;
const id = '0'; late GameState newState;
when(() => dashNestBumper.id).thenReturn(id);
when(() => dashNestBumper.gameRef).thenReturn(game);
},
verify: (game, tester) async {
final contactCallback = DashNestBumperBallContactCallback();
contactCallback.begin(dashNestBumper, MockBall(), MockContact());
verify( setUp(
() => gameBloc.add(DashNestActivated(dashNestBumper.id)), () {
).called(1); previousState = MockGameState();
newState = MockGameState();
},
);
test('listens when the id is added to activatedDashNests', () {
const id = '';
final controller = DashNestBumperController(
dashNestBumper,
id: id,
);
when(() => previousState.activatedDashNests).thenReturn({});
when(() => newState.activatedDashNests).thenReturn({id});
expect(controller.listenWhen(previousState, newState), isTrue);
});
test('listens when the id is removed from activatedDashNests', () {
const id = '';
final controller = DashNestBumperController(
dashNestBumper,
id: id,
);
when(() => previousState.activatedDashNests).thenReturn({id});
when(() => newState.activatedDashNests).thenReturn({});
expect(controller.listenWhen(previousState, newState), isTrue);
});
test("doesn't listen when the id is never in activatedDashNests", () {
final controller = DashNestBumperController(
dashNestBumper,
id: '',
);
when(() => previousState.activatedDashNests).thenReturn({});
when(() => newState.activatedDashNests).thenReturn({});
expect(controller.listenWhen(previousState, newState), isFalse);
});
test("doesn't listen when the id still in activatedDashNests", () {
const id = '';
final controller = DashNestBumperController(
dashNestBumper,
id: id,
);
when(() => previousState.activatedDashNests).thenReturn({id});
when(() => newState.activatedDashNests).thenReturn({id});
expect(controller.listenWhen(previousState, newState), isFalse);
});
}, },
); );
});
group('BigDashNestBumper', () { group(
test('has points', () { 'onNewState',
final dashNestBumper = BigDashNestBumper(id: ''); () {
expect(dashNestBumper.points, greaterThan(0)); late GameState state;
});
});
group('SmallDashNestBumper', () { setUp(() {
test('has points', () { state = MockGameState();
final dashNestBumper = SmallDashNestBumper(id: ''); });
expect(dashNestBumper.points, greaterThan(0));
}); test(
'activates the bumper when id in activatedDashNests',
() {
const id = '';
final controller = DashNestBumperController(
dashNestBumper,
id: id,
);
when(() => state.activatedDashNests).thenReturn({id});
controller.onNewState(state);
verify(() => dashNestBumper.activate()).called(1);
},
);
test(
'deactivates the bumper when id not in activatedDashNests',
() {
final controller = DashNestBumperController(
dashNestBumper,
id: '',
);
when(() => state.activatedDashNests).thenReturn({});
controller.onNewState(state);
verify(() => dashNestBumper.deactivate()).called(1);
},
);
},
);
}); });
} }

Loading…
Cancel
Save