From 1c509e151e69b2fdcce6effc79d3cc14710f253c Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Tue, 15 Mar 2022 15:12:21 +0100 Subject: [PATCH] refactor: hide maskbits and manage only with layer param --- lib/game/components/ball.dart | 8 +-- lib/game/components/crossing_ramp.dart | 34 ++++++------ lib/game/components/jetpack_ramp.dart | 2 +- lib/game/components/sparky_ramp.dart | 2 +- test/game/components/ball_test.dart | 12 ++--- test/game/components/crossing_ramp_test.dart | 54 ++++++++++---------- test/game/components/jetpack_ramp_test.dart | 6 ++- test/game/components/sparky_ramp_test.dart | 4 +- 8 files changed, 63 insertions(+), 59 deletions(-) diff --git a/lib/game/components/ball.dart b/lib/game/components/ball.dart index 94ffa2ad..e166bd40 100644 --- a/lib/game/components/ball.dart +++ b/lib/game/components/ball.dart @@ -10,13 +10,13 @@ class Ball extends BodyComponent with Layer { /// {@macro ball} Ball({ required Vector2 position, - int? maskBits, + RampType? layer, }) : _position = position, - _maskBits = maskBits ?? Filter().maskBits; + _layer = layer ?? RampType.all; /// The initial position of the [Ball] body. final Vector2 _position; - final int _maskBits; + final RampType _layer; /// The size of the [Ball] final Vector2 size = Vector2.all(2); @@ -46,7 +46,7 @@ class Ball extends BodyComponent with Layer { final fixtureDef = FixtureDef(shape) ..density = 1 - ..filter.maskBits = _maskBits; + ..filter.maskBits = _layer.maskBits; final bodyDef = BodyDef() ..userData = this diff --git a/lib/game/components/crossing_ramp.dart b/lib/game/components/crossing_ramp.dart index c093670f..65a5db43 100644 --- a/lib/game/components/crossing_ramp.dart +++ b/lib/game/components/crossing_ramp.dart @@ -11,11 +11,11 @@ import 'package:pinball/game/game.dart'; /// same bits and ignore others. /// {@endtemplate} mixin Layer on BodyComponent { - void setMaskBits(int maskBits) { + void setLayer(RampType layer) { for (final fixture in body.fixtures) { fixture - ..filterData.categoryBits = maskBits - ..filterData.maskBits = maskBits; + ..filterData.categoryBits = layer.maskBits + ..filterData.maskBits = layer.maskBits; } } } @@ -75,18 +75,18 @@ abstract class RampOpening extends BodyComponent { /// {@macro ramp_opening} RampOpening({ required Vector2 position, - required int categoryBits, + required RampType layer, }) : _position = position, - _categoryBits = categoryBits { + _layer = layer { // TODO(ruialonso): remove paint color for BodyComponent. // Left white for dev and testing. } final Vector2 _position; - final int _categoryBits; + final RampType _layer; /// Mask of category bits for collision with [RampOpening] - int get categoryBits => _categoryBits; + RampType get layer => _layer; /// The [Shape] of the [RampOpening] Shape get shape; @@ -98,7 +98,7 @@ abstract class RampOpening extends BodyComponent { Body createBody() { final fixtureDef = FixtureDef(shape) ..isSensor = true - ..filter.categoryBits = _categoryBits; + ..filter.categoryBits = _layer.maskBits; final bodyDef = BodyDef() ..userData = this @@ -126,37 +126,37 @@ abstract class RampOpeningBallContactCallback Area area, Contact _, ) { - int maskBits; + RampType layer; if (!ballsInside.contains(ball)) { - maskBits = area.categoryBits; + layer = area.layer; ballsInside.add(ball); } else { - maskBits = RampType.all.maskBits; + layer = RampType.all; ballsInside.remove(ball); } - ball.setMaskBits(maskBits); + ball.setLayer(layer); } @override void end(Ball ball, Area area, Contact contact) { - int? maskBits; + RampType? layer; switch (area.orientation) { case RampOrientation.up: if (ball.body.position.y > area._position.y) { - maskBits = RampType.all.maskBits; + layer = RampType.all; } break; case RampOrientation.down: if (ball.body.position.y < area._position.y) { - maskBits = RampType.all.maskBits; + layer = RampType.all; } break; } - if (maskBits != null) { - ball.setMaskBits(maskBits); + if (layer != null) { + ball.setLayer(layer); } } } diff --git a/lib/game/components/jetpack_ramp.dart b/lib/game/components/jetpack_ramp.dart index 2ffd6c32..ed08e540 100644 --- a/lib/game/components/jetpack_ramp.dart +++ b/lib/game/components/jetpack_ramp.dart @@ -74,7 +74,7 @@ class JetpackRampOpening extends RampOpening { _orientation = orientation, super( position: position, - categoryBits: RampType.jetpack.maskBits, + layer: RampType.jetpack, ); /// Orientation of entrance/exit of [JetpackRamp] where diff --git a/lib/game/components/sparky_ramp.dart b/lib/game/components/sparky_ramp.dart index 2a976787..bae9ca0c 100644 --- a/lib/game/components/sparky_ramp.dart +++ b/lib/game/components/sparky_ramp.dart @@ -69,7 +69,7 @@ class SparkyRampOpening extends RampOpening { _orientation = orientation, super( position: position, - categoryBits: RampType.sparky.maskBits, + layer: RampType.sparky, ); /// Orientation of entrance/exit of [SparkyRamp] where diff --git a/test/game/components/ball_test.dart b/test/game/components/ball_test.dart index 6fedd6df..8bdcc99b 100644 --- a/test/game/components/ball_test.dart +++ b/test/game/components/ball_test.dart @@ -166,13 +166,13 @@ void main() { ); }); - group('setMaskBits', () { + group('setLayer', () { final flameTester = FlameTester(PinballGameTest.create); flameTester.test( - 'modifies maskBits correctly', + 'modifies layer correctly', (game) async { - const newMaskBits = 1234; + const newLayer = RampType.jetpack; final ball = Ball(position: Vector2.zero()); await game.ensureAdd(ball); @@ -182,10 +182,10 @@ void main() { expect(fixture.filterData.categoryBits, equals(1)); expect(fixture.filterData.maskBits, equals(Filter().maskBits)); - ball.setMaskBits(newMaskBits); + ball.setLayer(newLayer); - expect(fixture.filterData.categoryBits, equals(newMaskBits)); - expect(fixture.filterData.maskBits, equals(newMaskBits)); + expect(fixture.filterData.categoryBits, equals(newLayer.maskBits)); + expect(fixture.filterData.maskBits, equals(newLayer.maskBits)); }, ); }); diff --git a/test/game/components/crossing_ramp_test.dart b/test/game/components/crossing_ramp_test.dart index e98daac1..b4f76579 100644 --- a/test/game/components/crossing_ramp_test.dart +++ b/test/game/components/crossing_ramp_test.dart @@ -11,9 +11,9 @@ class TestRampArea extends RampOpening { TestRampArea({ required Vector2 position, required RampOrientation orientation, - required int categoryBits, + required RampType layer, }) : _orientation = orientation, - super(position: position, categoryBits: categoryBits); + super(position: position, layer: layer); final RampOrientation _orientation; @@ -71,7 +71,7 @@ void main() { final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, - categoryBits: RampType.all.maskBits, + layer: RampType.all, ); await game.ready(); await game.ensureAdd(ramp); @@ -88,7 +88,7 @@ void main() { final ramp = TestRampArea( position: position, orientation: RampOrientation.down, - categoryBits: RampType.all.maskBits, + layer: RampType.all, ); await game.ensureAdd(ramp); @@ -103,7 +103,7 @@ void main() { final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, - categoryBits: RampType.all.maskBits, + layer: RampType.all, ); await game.ensureAdd(ramp); @@ -112,7 +112,7 @@ void main() { ); group('fixtures', () { - const maskBits = 1234; + const layer = RampType.jetpack; flameTester.test( 'exists', @@ -120,7 +120,7 @@ void main() { final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, - categoryBits: maskBits, + layer: layer, ); await game.ensureAdd(ramp); @@ -134,7 +134,7 @@ void main() { final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, - categoryBits: maskBits, + layer: layer, ); await game.ensureAdd(ramp); @@ -149,7 +149,7 @@ void main() { final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, - categoryBits: maskBits, + layer: layer, ); await game.ensureAdd(ramp); @@ -164,7 +164,7 @@ void main() { final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, - categoryBits: maskBits, + layer: layer, ); await game.ensureAdd(ramp); @@ -172,7 +172,7 @@ void main() { final fixture = ramp.body.fixtures[0]; expect( fixture.filterData.categoryBits, - equals(maskBits), + equals(layer.maskBits), ); }, ); @@ -181,7 +181,7 @@ void main() { }); group('RampAreaCallback', () { - const categoryBits = 1234; + const layer = RampType.jetpack; test( 'a ball enters from bottom into a down oriented path and keeps inside, ' @@ -191,7 +191,7 @@ void main() { final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.down, - categoryBits: categoryBits, + layer: layer, ); final callback = TestRampAreaCallback(); @@ -203,11 +203,11 @@ void main() { expect(callback._ballsInside.length, equals(1)); expect(callback._ballsInside.first, ball); - verify(() => ball.setMaskBits(categoryBits)).called(1); + verify(() => ball.setLayer(layer)).called(1); callback.end(ball, area, MockContact()); - verifyNever(() => ball.setMaskBits(RampType.all.maskBits)); + verifyNever(() => ball.setLayer(RampType.all)); }); test( @@ -218,7 +218,7 @@ void main() { final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.up, - categoryBits: categoryBits, + layer: layer, ); final callback = TestRampAreaCallback(); @@ -230,11 +230,11 @@ void main() { expect(callback._ballsInside.length, equals(1)); expect(callback._ballsInside.first, ball); - verify(() => ball.setMaskBits(categoryBits)).called(1); + verify(() => ball.setLayer(layer)).called(1); callback.end(ball, area, MockContact()); - verifyNever(() => ball.setMaskBits(RampType.all.maskBits)); + verifyNever(() => ball.setLayer(RampType.all)); }); test( @@ -245,7 +245,7 @@ void main() { final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.down, - categoryBits: categoryBits, + layer: layer, ); final callback = TestRampAreaCallback(); @@ -257,11 +257,11 @@ void main() { expect(callback._ballsInside.length, equals(1)); expect(callback._ballsInside.first, ball); - verify(() => ball.setMaskBits(categoryBits)).called(1); + verify(() => ball.setLayer(layer)).called(1); callback.end(ball, area, MockContact()); - verify(() => ball.setMaskBits(RampType.all.maskBits)); + verify(() => ball.setLayer(RampType.all)); }); test( @@ -272,7 +272,7 @@ void main() { final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.down, - categoryBits: categoryBits, + layer: layer, ); final callback = TestRampAreaCallback()..ballsInside.add(ball); @@ -282,11 +282,11 @@ void main() { callback.begin(ball, area, MockContact()); expect(callback._ballsInside.isEmpty, isTrue); - verify(() => ball.setMaskBits(RampType.all.maskBits)).called(1); + verify(() => ball.setLayer(RampType.all)).called(1); callback.end(ball, area, MockContact()); - verify(() => ball.setMaskBits(RampType.all.maskBits)).called(1); + verify(() => ball.setLayer(RampType.all)).called(1); }); test( @@ -297,7 +297,7 @@ void main() { final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.up, - categoryBits: categoryBits, + layer: layer, ); final callback = TestRampAreaCallback()..ballsInside.add(ball); @@ -307,11 +307,11 @@ void main() { callback.begin(ball, area, MockContact()); expect(callback._ballsInside.isEmpty, isTrue); - verify(() => ball.setMaskBits(RampType.all.maskBits)).called(1); + verify(() => ball.setLayer(RampType.all)).called(1); callback.end(ball, area, MockContact()); - verify(() => ball.setMaskBits(RampType.all.maskBits)).called(1); + verify(() => ball.setLayer(RampType.all)).called(1); }); }); } diff --git a/test/game/components/jetpack_ramp_test.dart b/test/game/components/jetpack_ramp_test.dart index 434ab9bf..26779aaf 100644 --- a/test/game/components/jetpack_ramp_test.dart +++ b/test/game/components/jetpack_ramp_test.dart @@ -99,8 +99,10 @@ void main() { group('JetpackRampAreaCallback', () { test('has no ball inside on creation', () { - expect(JetpackRampOpeningBallContactCallback().ballsInside, - equals({})); + expect( + JetpackRampOpeningBallContactCallback().ballsInside, + equals({}), + ); }); }); } diff --git a/test/game/components/sparky_ramp_test.dart b/test/game/components/sparky_ramp_test.dart index 84cb3a7f..366d150d 100644 --- a/test/game/components/sparky_ramp_test.dart +++ b/test/game/components/sparky_ramp_test.dart @@ -100,7 +100,9 @@ void main() { group('SparkyRampAreaCallback', () { test('has no ball inside on creation', () { expect( - SparkyRampOpeningBallContactCallback().ballsInside, equals({})); + SparkyRampOpeningBallContactCallback().ballsInside, + equals({}), + ); }); }); }