diff --git a/lib/game/components/ball.dart b/lib/game/components/ball.dart index d1721927..d5b05fa1 100644 --- a/lib/game/components/ball.dart +++ b/lib/game/components/ball.dart @@ -37,7 +37,7 @@ class Ball extends PositionBodyComponent { final bodyDef = BodyDef() ..userData = this - ..position = _position + ..position = Vector2(_position.x, _position.y + size.y) ..type = BodyType.dynamic; return world.createBody(bodyDef)..createFixture(fixtureDef); diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 16754ed3..a14679f0 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -218,7 +218,7 @@ class FlipperAnchorRevoluteJointDef extends RevoluteJointDef { /// {@macro flipper_anchor_revolute_joint_def} FlipperAnchorRevoluteJointDef({ required Flipper flipper, - required Anchor anchor, + required FlipperAnchor anchor, }) { initialize( flipper.body, diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 364fc35e..2ec2f599 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -1,23 +1,32 @@ +import 'package:flame/input.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flutter/services.dart'; import 'package:pinball/game/game.dart' show Anchor; /// {@template plunger} /// [Plunger] serves as a spring, that shoots the ball on the right side of the /// playfield. /// -/// [Plunger] ignores gravity so the player controls its downward [pull]. +/// [Plunger] ignores gravity so the player controls its downward [_pull]. /// {@endtemplate} -class Plunger extends BodyComponent { +class Plunger extends BodyComponent with KeyboardHandler { /// {@macro plunger} - Plunger({required Vector2 position}) : _position = position; + Plunger({ + required Vector2 position, + required this.compressionDistance, + }) : _position = position; + /// The initial position of the [Plunger] body. final Vector2 _position; + /// Distance the plunger can lower. + final double compressionDistance; + @override Body createBody() { - final shape = PolygonShape()..setAsBoxXY(2.5, 1.5); + final shape = PolygonShape()..setAsBoxXY(2, 0.75); - final fixtureDef = FixtureDef(shape); + final fixtureDef = FixtureDef(shape)..density = 5; final bodyDef = BodyDef() ..userData = this @@ -29,18 +38,57 @@ class Plunger extends BodyComponent { } /// Set a constant downward velocity on the [Plunger]. - void pull() { - body.linearVelocity = Vector2(0, -7); + void _pull() { + body.linearVelocity = Vector2(0, -3); } /// Set an upward velocity on the [Plunger]. /// /// The velocity's magnitude depends on how far the [Plunger] has been pulled /// from its original [_position]. - void release() { + void _release() { final velocity = (_position.y - body.position.y) * 9; body.linearVelocity = Vector2(0, velocity); } + + @override + bool onKeyEvent( + RawKeyEvent event, + Set keysPressed, + ) { + final keys = [ + LogicalKeyboardKey.space, + LogicalKeyboardKey.arrowDown, + LogicalKeyboardKey.keyS, + ]; + // TODO(alestiago): Check why false cancels the event for other components. + // Investigate why return is of type [bool] expected instead of a type + // [KeyEventResult]. + if (!keys.contains(event.logicalKey)) return true; + + if (event is RawKeyDownEvent) { + _pull(); + } else if (event is RawKeyUpEvent) { + _release(); + } + + return true; + } +} + +/// {@template plunger_anchor} +/// [Anchor] positioned below a [Plunger]. +/// {@endtemplate} +class PlungerAnchor extends Anchor { + /// {@macro plunger_anchor} + PlungerAnchor({ + required Plunger plunger, + }) : super( + position: Vector2( + plunger.body.position.x, + plunger.body.position.y - plunger.compressionDistance, + ), + ); } /// {@template plunger_anchor_prismatic_joint_def} @@ -54,11 +102,8 @@ class PlungerAnchorPrismaticJointDef extends PrismaticJointDef { /// {@macro plunger_anchor_prismatic_joint_def} PlungerAnchorPrismaticJointDef({ required Plunger plunger, - required Anchor anchor, - }) : assert( - anchor.body.position.y < plunger.body.position.y, - 'Anchor must be below the Plunger', - ) { + required PlungerAnchor anchor, + }) { initialize( plunger.body, anchor.body, @@ -67,6 +112,9 @@ class PlungerAnchorPrismaticJointDef extends PrismaticJointDef { ); enableLimit = true; lowerTranslation = double.negativeInfinity; + enableMotor = true; + motorSpeed = 50; + maxMotorForce = motorSpeed; collideConnected = true; } } diff --git a/lib/game/components/wall.dart b/lib/game/components/wall.dart index c307aad4..c433365c 100644 --- a/lib/game/components/wall.dart +++ b/lib/game/components/wall.dart @@ -4,7 +4,7 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/components/components.dart'; /// {@template wall} -/// A continuos generic and [BodyType.static] barrier that divides a game area. +/// A continuous generic and [BodyType.static] barrier that divides a game area. /// {@endtemplate} // TODO(alestiago): Remove [Wall] for [Pathway.straight]. class Wall extends BodyComponent { @@ -25,7 +25,7 @@ class Wall extends BodyComponent { final shape = EdgeShape()..set(start, end); final fixtureDef = FixtureDef(shape) - ..restitution = 0.0 + ..restitution = 0.1 ..friction = 0.3; final bodyDef = BodyDef() @@ -37,6 +37,20 @@ class Wall extends BodyComponent { } } +/// Create top, left, and right [Wall]s for the game board. +List createBoundaries(Forge2DGame game) { + final topLeft = Vector2.zero(); + final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize); + final topRight = Vector2(bottomRight.x, topLeft.y); + final bottomLeft = Vector2(topLeft.x, bottomRight.y); + + return [ + Wall(start: topLeft, end: topRight), + Wall(start: topRight, end: bottomRight), + Wall(start: bottomLeft, end: topLeft), + ]; +} + /// {@template bottom_wall} /// [Wall] located at the bottom of the board. /// diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 7d6fba41..f4ff6757 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -13,17 +13,7 @@ class PinballGame extends Forge2DGame final PinballTheme theme; - // TODO(erickzanardo): Change to the plumber position - late final ballStartingPosition = screenToWorld( - Vector2( - camera.viewport.effectiveSize.x / 2, - camera.viewport.effectiveSize.y - 20, - ), - ) - - Vector2(0, -20); - - // TODO(alestiago): Change to the design position. - late final flippersPosition = ballStartingPosition - Vector2(0, 5); + late final Plunger plunger; @override void onAttach() { @@ -31,21 +21,56 @@ class PinballGame extends Forge2DGame spawnBall(); } + @override + Future onLoad() async { + _addContactCallbacks(); + + await _addGameBoundaries(); + unawaited(_addFlippers()); + unawaited(_addPlunger()); + + // Corner wall above plunger so the ball deflects into the rest of the + // board. + // TODO(allisonryan0002): remove once we have the launch track for the ball. + await add( + Wall( + start: screenToWorld( + Vector2( + camera.viewport.effectiveSize.x, + 100, + ), + ), + end: screenToWorld( + Vector2( + camera.viewport.effectiveSize.x - 100, + 0, + ), + ), + ), + ); + } + void spawnBall() { - add(Ball(position: ballStartingPosition)); + add(Ball(position: plunger.body.position)); } - @override - Future onLoad() async { + void _addContactCallbacks() { addContactCallback(BallScorePointsCallback()); - - await add(BottomWall(this)); addContactCallback(BottomWallBallContactCallback()); + } - unawaited(_addFlippers()); + Future _addGameBoundaries() async { + await add(BottomWall(this)); + createBoundaries(this).forEach(add); } Future _addFlippers() async { + final flippersPosition = screenToWorld( + Vector2( + camera.viewport.effectiveSize.x / 2, + camera.viewport.effectiveSize.y / 1.1, + ), + ); const spaceBetweenFlippers = 2; final leftFlipper = Flipper.left( position: Vector2( @@ -104,6 +129,31 @@ class PinballGame extends Forge2DGame ), ); } + + Future _addPlunger() async { + late PlungerAnchor plungerAnchor; + final compressionDistance = camera.viewport.effectiveSize.y / 12; + + await add( + plunger = Plunger( + position: screenToWorld( + Vector2( + camera.viewport.effectiveSize.x / 1.035, + camera.viewport.effectiveSize.y - compressionDistance, + ), + ), + compressionDistance: compressionDistance, + ), + ); + await add(plungerAnchor = PlungerAnchor(plunger: plunger)); + + world.createJoint( + PlungerAnchorPrismaticJointDef( + plunger: plunger, + anchor: plungerAnchor, + ), + ); + } } class DebugPinballGame extends PinballGame with TapDetector { diff --git a/test/game/components/ball_test.dart b/test/game/components/ball_test.dart index 0b115055..9f473e42 100644 --- a/test/game/components/ball_test.dart +++ b/test/game/components/ball_test.dart @@ -34,7 +34,11 @@ void main() { await game.ensureAdd(ball); game.contains(ball); - expect(ball.body.position, position); + final expectedPosition = Vector2( + position.x, + position.y + ball.size.y, + ); + expect(ball.body.position, equals(expectedPosition)); }, ); @@ -49,7 +53,7 @@ void main() { ); }); - group('first fixture', () { + group('fixture', () { flameTester.test( 'exists', (game) async { diff --git a/test/game/components/flipper_test.dart b/test/game/components/flipper_test.dart index 941090ea..2a92f082 100644 --- a/test/game/components/flipper_test.dart +++ b/test/game/components/flipper_test.dart @@ -255,36 +255,33 @@ void main() { }, ); - group( - 'FlipperAnchor', - () { - flameTester.test( - 'position is at the left of the left Flipper', - (game) async { - final flipper = Flipper.left(position: Vector2.zero()); - await game.ensureAdd(flipper); + group('FlipperAnchor', () { + flameTester.test( + 'position is at the left of the left Flipper', + (game) async { + final flipper = Flipper.left(position: Vector2.zero()); + await game.ensureAdd(flipper); - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); + final flipperAnchor = FlipperAnchor(flipper: flipper); + await game.ensureAdd(flipperAnchor); - expect(flipperAnchor.body.position.x, equals(-Flipper.width / 2)); - }, - ); + expect(flipperAnchor.body.position.x, equals(-Flipper.width / 2)); + }, + ); - flameTester.test( - 'position is at the right of the right Flipper', - (game) async { - final flipper = Flipper.right(position: Vector2.zero()); - await game.ensureAdd(flipper); + flameTester.test( + 'position is at the right of the right Flipper', + (game) async { + final flipper = Flipper.right(position: Vector2.zero()); + await game.ensureAdd(flipper); - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); + final flipperAnchor = FlipperAnchor(flipper: flipper); + await game.ensureAdd(flipperAnchor); - expect(flipperAnchor.body.position.x, equals(Flipper.width / 2)); - }, - ); - }, - ); + expect(flipperAnchor.body.position.x, equals(Flipper.width / 2)); + }, + ); + }); group('FlipperAnchorRevoluteJointDef', () { group('initializes with', () { diff --git a/test/game/components/plunger_test.dart b/test/game/components/plunger_test.dart index 68d02160..02330b31 100644 --- a/test/game/components/plunger_test.dart +++ b/test/game/components/plunger_test.dart @@ -1,8 +1,11 @@ // ignore_for_file: cascade_invocations +import 'dart:collection'; + import 'package:bloc_test/bloc_test.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_test/flame_test.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pinball/game/game.dart'; @@ -13,11 +16,16 @@ void main() { final flameTester = FlameTester(PinballGameTest.create); group('Plunger', () { + const compressionDistance = 0.0; + flameTester.test( 'loads correctly', (game) async { await game.ready(); - final plunger = Plunger(position: Vector2.zero()); + final plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); await game.ensureAdd(plunger); expect(game.contains(plunger), isTrue); @@ -29,7 +37,10 @@ void main() { 'positions correctly', (game) async { final position = Vector2.all(10); - final plunger = Plunger(position: position); + final plunger = Plunger( + position: position, + compressionDistance: compressionDistance, + ); await game.ensureAdd(plunger); game.contains(plunger); @@ -40,7 +51,10 @@ void main() { flameTester.test( 'is dynamic', (game) async { - final plunger = Plunger(position: Vector2.zero()); + final plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); await game.ensureAdd(plunger); expect(plunger.body.bodyType, equals(BodyType.dynamic)); @@ -50,7 +64,10 @@ void main() { flameTester.test( 'ignores gravity', (game) async { - final plunger = Plunger(position: Vector2.zero()); + final plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); await game.ensureAdd(plunger); expect(plunger.body.gravityScale, isZero); @@ -58,11 +75,14 @@ void main() { ); }); - group('first fixture', () { + group('fixture', () { flameTester.test( 'exists', (game) async { - final plunger = Plunger(position: Vector2.zero()); + final plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); await game.ensureAdd(plunger); expect(plunger.body.fixtures[0], isA()); @@ -72,65 +92,128 @@ void main() { flameTester.test( 'shape is a polygon', (game) async { - final plunger = Plunger(position: Vector2.zero()); + final plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); await game.ensureAdd(plunger); final fixture = plunger.body.fixtures[0]; expect(fixture.shape.shapeType, equals(ShapeType.polygon)); }, ); - }); - - flameTester.test( - 'pull sets a negative linear velocity', - (game) async { - final plunger = Plunger(position: Vector2.zero()); - await game.ensureAdd(plunger); - plunger.pull(); - - expect(plunger.body.linearVelocity.y, isNegative); - expect(plunger.body.linearVelocity.x, isZero); - }, - ); - - group('release', () { flameTester.test( - 'does not set a linear velocity ' - 'when plunger is in starting position', + 'has density', (game) async { - final plunger = Plunger(position: Vector2.zero()); + final plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); await game.ensureAdd(plunger); - plunger.release(); - - expect(plunger.body.linearVelocity.y, isZero); - expect(plunger.body.linearVelocity.x, isZero); + final fixture = plunger.body.fixtures[0]; + expect(fixture.density, greaterThan(0)); }, ); + }); - flameTester.test( - 'sets a positive linear velocity ' - 'when plunger is below starting position', - (game) async { - final plunger = Plunger(position: Vector2.zero()); - await game.ensureAdd(plunger); + group('onKeyEvent', () { + final keys = UnmodifiableListView([ + LogicalKeyboardKey.space, + LogicalKeyboardKey.arrowDown, + LogicalKeyboardKey.keyS, + ]); - plunger.body.setTransform(Vector2(0, -1), 0); - plunger.release(); + late Plunger plunger; - expect(plunger.body.linearVelocity.y, isPositive); - expect(plunger.body.linearVelocity.x, isZero); - }, - ); + setUp(() { + plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); + }); + + testRawKeyUpEvents(keys, (event) { + final keyLabel = (event.logicalKey != LogicalKeyboardKey.space) + ? event.logicalKey.keyLabel + : 'Space'; + flameTester.test( + 'moves upwards when $keyLabel is released ' + 'and plunger is below its starting position', + (game) async { + await game.ensureAdd(plunger); + plunger.body.setTransform(Vector2(0, -1), 0); + plunger.onKeyEvent(event, {}); + + expect(plunger.body.linearVelocity.y, isPositive); + expect(plunger.body.linearVelocity.x, isZero); + }, + ); + }); + + testRawKeyUpEvents(keys, (event) { + final keyLabel = (event.logicalKey != LogicalKeyboardKey.space) + ? event.logicalKey.keyLabel + : 'Space'; + flameTester.test( + 'does not move when $keyLabel is released ' + 'and plunger is in its starting position', + (game) async { + await game.ensureAdd(plunger); + plunger.onKeyEvent(event, {}); + + expect(plunger.body.linearVelocity.y, isZero); + expect(plunger.body.linearVelocity.x, isZero); + }, + ); + }); + + testRawKeyDownEvents(keys, (event) { + final keyLabel = (event.logicalKey != LogicalKeyboardKey.space) + ? event.logicalKey.keyLabel + : 'Space'; + flameTester.test( + 'moves downwards when $keyLabel is pressed', + (game) async { + await game.ensureAdd(plunger); + plunger.onKeyEvent(event, {}); + + expect(plunger.body.linearVelocity.y, isNegative); + expect(plunger.body.linearVelocity.x, isZero); + }, + ); + }); }); }); - group('PlungerAnchorPrismaticJointDef', () { - late Plunger plunger; - late Anchor anchor; + group('PlungerAnchor', () { + const compressionDistance = 10.0; + + flameTester.test( + 'position is a compression distance below the Plunger', + (game) async { + final plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); + await game.ensureAdd(plunger); + + final plungerAnchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(plungerAnchor); + + expect( + plungerAnchor.body.position.y, + equals(plunger.body.position.y - compressionDistance), + ); + }, + ); + }); + group('PlungerAnchorPrismaticJointDef', () { + const compressionDistance = 10.0; final gameBloc = MockGameBloc(); + late Plunger plunger; setUp(() { whenListen( @@ -138,51 +221,21 @@ void main() { const Stream.empty(), initialState: const GameState.initial(), ); - plunger = Plunger(position: Vector2.zero()); - anchor = Anchor(position: Vector2(0, -1)); + plunger = Plunger( + position: Vector2.zero(), + compressionDistance: compressionDistance, + ); }); final flameTester = flameBlocTester(gameBloc: gameBloc); - flameTester.test( - 'throws AssertionError ' - 'when anchor is above plunger', - (game) async { - final anchor = Anchor(position: Vector2(0, 1)); - await game.ensureAddAll([plunger, anchor]); - - expect( - () => PlungerAnchorPrismaticJointDef( - plunger: plunger, - anchor: anchor, - ), - throwsAssertionError, - ); - }, - ); - - flameTester.test( - 'throws AssertionError ' - 'when anchor is in same position as plunger', - (game) async { - final anchor = Anchor(position: Vector2.zero()); - await game.ensureAddAll([plunger, anchor]); - - expect( - () => PlungerAnchorPrismaticJointDef( - plunger: plunger, - anchor: anchor, - ), - throwsAssertionError, - ); - }, - ); - group('initializes with', () { flameTester.test( 'plunger body as bodyA', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -196,7 +249,9 @@ void main() { flameTester.test( 'anchor body as bodyB', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -211,7 +266,9 @@ void main() { flameTester.test( 'limits enabled', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -226,7 +283,9 @@ void main() { flameTester.test( 'lower translation limit as negative infinity', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -241,7 +300,9 @@ void main() { flameTester.test( 'connected body collison enabled', (game) async { - await game.ensureAddAll([plunger, anchor]); + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); final jointDef = PlungerAnchorPrismaticJointDef( plunger: plunger, @@ -254,46 +315,51 @@ void main() { ); }); - flameTester.widgetTest( - 'plunger cannot go below anchor', - (game, tester) async { - await game.ensureAddAll([plunger, anchor]); + testRawKeyUpEvents([LogicalKeyboardKey.space], (event) { + flameTester.widgetTest( + 'plunger cannot go below anchor', + (game, tester) async { + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); - // Giving anchor a shape for the plunger to collide with. - anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1)); + // Giving anchor a shape for the plunger to collide with. + anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1)); - final jointDef = PlungerAnchorPrismaticJointDef( - plunger: plunger, - anchor: anchor, - ); - game.world.createJoint(jointDef); + final jointDef = PlungerAnchorPrismaticJointDef( + plunger: plunger, + anchor: anchor, + ); + game.world.createJoint(jointDef); - plunger.pull(); - await tester.pump(const Duration(seconds: 1)); + await tester.pump(const Duration(seconds: 1)); - expect(plunger.body.position.y > anchor.body.position.y, isTrue); - }, - ); + expect(plunger.body.position.y > anchor.body.position.y, isTrue); + }, + ); + }); - flameTester.widgetTest( - 'plunger cannot excessively exceed starting position', - (game, tester) async { - await game.ensureAddAll([plunger, anchor]); + testRawKeyUpEvents([LogicalKeyboardKey.space], (event) { + flameTester.widgetTest( + 'plunger cannot excessively exceed starting position', + (game, tester) async { + await game.ensureAdd(plunger); + final anchor = PlungerAnchor(plunger: plunger); + await game.ensureAdd(anchor); - final jointDef = PlungerAnchorPrismaticJointDef( - plunger: plunger, - anchor: anchor, - ); - game.world.createJoint(jointDef); + final jointDef = PlungerAnchorPrismaticJointDef( + plunger: plunger, + anchor: anchor, + ); + game.world.createJoint(jointDef); - plunger.pull(); - await tester.pump(const Duration(seconds: 1)); + plunger.body.setTransform(Vector2(0, -1), 0); - plunger.release(); - await tester.pump(const Duration(seconds: 1)); + await tester.pump(const Duration(seconds: 1)); - expect(plunger.body.position.y < 1, isTrue); - }, - ); + expect(plunger.body.position.y < 1, isTrue); + }, + ); + }); }); } diff --git a/test/game/components/wall_test.dart b/test/game/components/wall_test.dart index 6d792ea4..e60046ad 100644 --- a/test/game/components/wall_test.dart +++ b/test/game/components/wall_test.dart @@ -77,7 +77,7 @@ void main() { ); }); - group('first fixture', () { + group('fixture', () { flameTester.test( 'exists', (game) async { @@ -92,7 +92,7 @@ void main() { ); flameTester.test( - 'has restitution equals 0', + 'has restitution', (game) async { final wall = Wall( start: Vector2.zero(), @@ -101,7 +101,7 @@ void main() { await game.ensureAdd(wall); final fixture = wall.body.fixtures[0]; - expect(fixture.restitution, equals(0)); + expect(fixture.restitution, greaterThan(0)); }, ); diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index f1d6bb32..d7db79e2 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -17,60 +17,102 @@ void main() { // TODO(alestiago): test if [PinballGame] registers // [BallScorePointsCallback] once the following issue is resolved: // https://github.com/flame-engine/flame/issues/1416 - group( - 'components', - () { - group('Flippers', () { - bool Function(Component) flipperSelector(BoardSide side) => - (component) => component is Flipper && component.side == side; - - flameTester.test( - 'has only one left Flipper', - (game) async { - await game.ready(); - - expect( - () => game.children.singleWhere( - flipperSelector(BoardSide.left), - ), - returnsNormally, - ); - }, - ); - - flameTester.test( - 'has only one right Flipper', - (game) async { - await game.ready(); - - expect( - () => game.children.singleWhere( - flipperSelector(BoardSide.right), - ), - returnsNormally, - ); - }, - ); + group('components', () { + group('Walls', () { + flameTester.test( + 'has three Walls', + (game) async { + await game.ready(); + final walls = game.children + .where( + (component) => component is Wall && component is! BottomWall, + ) + .toList(); + // TODO(allisonryan0002): expect 3 when launch track is added and + // temporary wall is removed. + expect(walls.length, 4); + }, + ); - debugModeFlameTester.test('adds a ball on tap up', (game) async { + flameTester.test( + 'has only one BottomWall', + (game) async { await game.ready(); - final eventPosition = MockEventPosition(); - when(() => eventPosition.game).thenReturn(Vector2.all(10)); + expect( + () => game.children.singleWhere( + (component) => component is BottomWall, + ), + returnsNormally, + ); + }, + ); + }); - final tapUpEvent = MockTapUpInfo(); - when(() => tapUpEvent.eventPosition).thenReturn(eventPosition); + group('Flippers', () { + bool Function(Component) flipperSelector(BoardSide side) => + (component) => component is Flipper && component.side == side; - game.onTapUp(tapUpEvent); + flameTester.test( + 'has only one left Flipper', + (game) async { await game.ready(); expect( - game.children.whereType().length, - equals(1), + () => game.children.singleWhere( + flipperSelector(BoardSide.left), + ), + returnsNormally, ); - }); - }); - }, - ); + }, + ); + + flameTester.test( + 'has only one right Flipper', + (game) async { + await game.ready(); + + expect( + () => game.children.singleWhere( + flipperSelector(BoardSide.right), + ), + returnsNormally, + ); + }, + ); + }); + + flameTester.test( + 'Plunger has only one Plunger', + (game) async { + await game.ready(); + + expect( + () => game.children.singleWhere( + (component) => component is Plunger, + ), + returnsNormally, + ); + }, + ); + }); + + debugModeFlameTester.test('adds a ball on tap up', (game) async { + await game.ready(); + + final eventPosition = MockEventPosition(); + when(() => eventPosition.game).thenReturn(Vector2.all(10)); + + final tapUpEvent = MockTapUpInfo(); + when(() => tapUpEvent.eventPosition).thenReturn(eventPosition); + + game.onTapUp(tapUpEvent); + await game.ready(); + + expect( + game.children.whereType().length, + equals(1), + ); + }); }); }