From 7bbc46acd98c8ef0949153d9970702efa3155238 Mon Sep 17 00:00:00 2001 From: alestiago Date: Fri, 22 Apr 2022 03:35:37 +0100 Subject: [PATCH] refactor: moved SparkyComputerSensor --- lib/game/components/sparky_fire_zone.dart | 52 +++++++------------ .../lib/src/components/sparky_computer.dart | 25 --------- .../components/sparky_fire_zone_test.dart | 33 ++++++++++++ 3 files changed, 52 insertions(+), 58 deletions(-) diff --git a/lib/game/components/sparky_fire_zone.dart b/lib/game/components/sparky_fire_zone.dart index 965d8ee0..c71209f0 100644 --- a/lib/game/components/sparky_fire_zone.dart +++ b/lib/game/components/sparky_fire_zone.dart @@ -21,7 +21,7 @@ class SparkyFireZone extends Blueprint with HasGameRef { _SparkyBumper.a()..initialPosition = Vector2(-22.9, -41.65), _SparkyBumper.b()..initialPosition = Vector2(-21.25, -57.9), _SparkyBumper.c()..initialPosition = Vector2(-3.3, -52.55), - SparkyTurboChargeSensor()..initialPosition = Vector2(-13, -49.8), + SparkyComputerSensor()..initialPosition = Vector2(-13, -49.8), SparkyAnimatronic()..position = Vector2(-13.8, -58.2), ], blueprints: [ @@ -47,9 +47,7 @@ class _SparkyBumper extends SparkyBumper with ScorePoints { await super.onLoad(); // TODO(alestiago): Revisit once this has been merged: // https://github.com/flame-engine/flame/pull/1547 - gameRef - ..removeContactCallback(SparkyBumperBallContactCallback()) - ..addContactCallback(SparkyBumperBallContactCallback()); + gameRef.addContactCallback(SparkyBumperBallContactCallback()); } } @@ -67,59 +65,47 @@ class SparkyBumperBallContactCallback } } -/// {@template sparky_turbo_charge_sensor} +/// {@template sparky_computer_sensor} /// Small sensor body used to detect when a ball has entered the -/// [SparkyComputer] with the [SparkyTurboChargeSensorBallContactCallback]. +/// [SparkyComputer]. /// {@endtemplate} -@visibleForTesting // TODO(alestiago): Revisit once this has been merged: // https://github.com/flame-engine/flame/pull/1547 -class SparkyTurboChargeSensor extends BodyComponent with InitialPosition { - /// {@macro sparky_turbo_charge_sensor} - SparkyTurboChargeSensor() { +class SparkyComputerSensor extends BodyComponent with InitialPosition { + /// {@macro sparky_computer_sensor} + SparkyComputerSensor() { renderBody = false; } @override Body createBody() { final shape = CircleShape()..radius = 0.1; - final fixtureDef = FixtureDef( - shape, - isSensor: true, - ); + final fixtureDef = FixtureDef(shape, isSensor: true); final bodyDef = BodyDef( position: initialPosition, userData: this, ); - return world.createBody(bodyDef)..createFixture(fixtureDef); } @override Future onLoad() async { await super.onLoad(); - gameRef.addContactCallback(SparkyTurboChargeSensorBallContactCallback()); + // TODO(alestiago): Revisit once this has been merged: + // https://github.com/flame-engine/flame/pull/1547 + gameRef.addContactCallback(SparkyComputerSensorBallContactCallback()); } } -/// {@template sparky_turbo_charge_sensor_ball_contact_callback} -/// Turbo charges the [Ball] on contact with [SparkyTurboChargeSensor]. -/// {@endtemplate} @visibleForTesting -class SparkyTurboChargeSensorBallContactCallback - extends ContactCallback { - /// {@macro sparky_turbo_charge_sensor_ball_contact_callback} - SparkyTurboChargeSensorBallContactCallback(); - +// TODO(alestiago): Revisit once this has been merged: +// https://github.com/flame-engine/flame/pull/1547 +// ignore: public_member_api_docs +class SparkyComputerSensorBallContactCallback + extends ContactCallback { @override - void begin( - SparkyTurboChargeSensor sparkyTurboChargeSensor, - ControlledBall ball, - _, - ) { - // TODO(alestiago): Revisit once this has been merged: - // https://github.com/flame-engine/flame/pull/1547 - ball.controller.turboCharge(); - ball.gameRef.firstChild()?.playing = true; + void begin(_, ControlledBall controlledBall, __) { + controlledBall.controller.turboCharge(); + // ball.gameRef.firstChild()?.playing = true; } } diff --git a/packages/pinball_components/lib/src/components/sparky_computer.dart b/packages/pinball_components/lib/src/components/sparky_computer.dart index 4cec4c59..481de63d 100644 --- a/packages/pinball_components/lib/src/components/sparky_computer.dart +++ b/packages/pinball_components/lib/src/components/sparky_computer.dart @@ -7,9 +7,6 @@ import 'package:pinball_flame/pinball_flame.dart'; /// {@template sparky_computer} /// A computer owned by Sparky. -/// -/// Register a [ContactCallback] for [SparkyComputerSensor] to listen when -/// something enters the [SparkyComputer]. /// {@endtemplate} class SparkyComputer extends Blueprint { /// {@macro sparky_computer} @@ -18,7 +15,6 @@ class SparkyComputer extends Blueprint { components: [ _ComputerBase(), _ComputerTopSpriteComponent(), - SparkyComputerSensor(), ], ); } @@ -104,24 +100,3 @@ class _ComputerTopSpriteComponent extends SpriteComponent with HasGameRef { size = sprite.originalSize / 10; } } - -/// {@template sparky_computer_sensor} -/// Small sensor body used to detect when a ball has entered the -/// [SparkyComputer]. -/// {@endtemplate} -class SparkyComputerSensor extends BodyComponent with InitialPosition { - /// {@macro sparky_computer_sensor} - SparkyComputerSensor() { - renderBody = false; - } - - @override - Body createBody() { - final shape = CircleShape()..radius = 0.1; - final fixtureDef = FixtureDef(shape, isSensor: true); - final bodyDef = BodyDef() - ..position = initialPosition - ..userData = this; - return world.createBody(bodyDef)..createFixture(fixtureDef); - } -} diff --git a/test/game/components/sparky_fire_zone_test.dart b/test/game/components/sparky_fire_zone_test.dart index 55de64d2..bc47106a 100644 --- a/test/game/components/sparky_fire_zone_test.dart +++ b/test/game/components/sparky_fire_zone_test.dart @@ -127,4 +127,37 @@ void main() { ); }); }); + + group( + 'SparkyTurboChargeSensorBallContactCallback', + () { + // TODO(alestiago): Make tests pass. + flameTester.test('calls turboCharge', (game) async { + final callback = SparkyComputerSensorBallContactCallback(); + final ball = MockControlledBall(); + when(() => ball.controller).thenReturn(MockBallController()); + when(() => ball.gameRef).thenReturn(game); + + await game.ready(); + callback.begin(MockSparkyComputerSensor(), ball, MockContact()); + + verify(() => ball.controller.turboCharge()).called(1); + }); + + flameTester.test('plays DashAnimatronic', (game) async { + final callback = SparkyComputerSensorBallContactCallback(); + final ball = MockControlledBall(); + when(() => ball.gameRef).thenReturn(game); + when(() => ball.controller).thenReturn(MockBallController()); + final dashAnimatronic = DashAnimatronic(); + await game.ensureAdd(dashAnimatronic); + + expect(dashAnimatronic.playing, isFalse); + callback.begin(MockSparkyComputerSensor(), ball, MockContact()); + await game.ready(); + + expect(dashAnimatronic.playing, isTrue); + }); + }, + ); }