refactor: hide maskbits and manage only with layer param

pull/40/head
RuiAlonso 4 years ago
parent 85c71a2df1
commit 1c509e151e

@ -10,13 +10,13 @@ class Ball extends BodyComponent<PinballGame> with Layer {
/// {@macro ball} /// {@macro ball}
Ball({ Ball({
required Vector2 position, required Vector2 position,
int? maskBits, RampType? layer,
}) : _position = position, }) : _position = position,
_maskBits = maskBits ?? Filter().maskBits; _layer = layer ?? RampType.all;
/// The initial position of the [Ball] body. /// The initial position of the [Ball] body.
final Vector2 _position; final Vector2 _position;
final int _maskBits; final RampType _layer;
/// The size of the [Ball] /// The size of the [Ball]
final Vector2 size = Vector2.all(2); final Vector2 size = Vector2.all(2);
@ -46,7 +46,7 @@ class Ball extends BodyComponent<PinballGame> with Layer {
final fixtureDef = FixtureDef(shape) final fixtureDef = FixtureDef(shape)
..density = 1 ..density = 1
..filter.maskBits = _maskBits; ..filter.maskBits = _layer.maskBits;
final bodyDef = BodyDef() final bodyDef = BodyDef()
..userData = this ..userData = this

@ -11,11 +11,11 @@ import 'package:pinball/game/game.dart';
/// same bits and ignore others. /// same bits and ignore others.
/// {@endtemplate} /// {@endtemplate}
mixin Layer on BodyComponent<PinballGame> { mixin Layer on BodyComponent<PinballGame> {
void setMaskBits(int maskBits) { void setLayer(RampType layer) {
for (final fixture in body.fixtures) { for (final fixture in body.fixtures) {
fixture fixture
..filterData.categoryBits = maskBits ..filterData.categoryBits = layer.maskBits
..filterData.maskBits = maskBits; ..filterData.maskBits = layer.maskBits;
} }
} }
} }
@ -75,18 +75,18 @@ abstract class RampOpening extends BodyComponent {
/// {@macro ramp_opening} /// {@macro ramp_opening}
RampOpening({ RampOpening({
required Vector2 position, required Vector2 position,
required int categoryBits, required RampType layer,
}) : _position = position, }) : _position = position,
_categoryBits = categoryBits { _layer = layer {
// TODO(ruialonso): remove paint color for BodyComponent. // TODO(ruialonso): remove paint color for BodyComponent.
// Left white for dev and testing. // Left white for dev and testing.
} }
final Vector2 _position; final Vector2 _position;
final int _categoryBits; final RampType _layer;
/// Mask of category bits for collision with [RampOpening] /// Mask of category bits for collision with [RampOpening]
int get categoryBits => _categoryBits; RampType get layer => _layer;
/// The [Shape] of the [RampOpening] /// The [Shape] of the [RampOpening]
Shape get shape; Shape get shape;
@ -98,7 +98,7 @@ abstract class RampOpening extends BodyComponent {
Body createBody() { Body createBody() {
final fixtureDef = FixtureDef(shape) final fixtureDef = FixtureDef(shape)
..isSensor = true ..isSensor = true
..filter.categoryBits = _categoryBits; ..filter.categoryBits = _layer.maskBits;
final bodyDef = BodyDef() final bodyDef = BodyDef()
..userData = this ..userData = this
@ -126,37 +126,37 @@ abstract class RampOpeningBallContactCallback<Area extends RampOpening>
Area area, Area area,
Contact _, Contact _,
) { ) {
int maskBits; RampType layer;
if (!ballsInside.contains(ball)) { if (!ballsInside.contains(ball)) {
maskBits = area.categoryBits; layer = area.layer;
ballsInside.add(ball); ballsInside.add(ball);
} else { } else {
maskBits = RampType.all.maskBits; layer = RampType.all;
ballsInside.remove(ball); ballsInside.remove(ball);
} }
ball.setMaskBits(maskBits); ball.setLayer(layer);
} }
@override @override
void end(Ball ball, Area area, Contact contact) { void end(Ball ball, Area area, Contact contact) {
int? maskBits; RampType? layer;
switch (area.orientation) { switch (area.orientation) {
case RampOrientation.up: case RampOrientation.up:
if (ball.body.position.y > area._position.y) { if (ball.body.position.y > area._position.y) {
maskBits = RampType.all.maskBits; layer = RampType.all;
} }
break; break;
case RampOrientation.down: case RampOrientation.down:
if (ball.body.position.y < area._position.y) { if (ball.body.position.y < area._position.y) {
maskBits = RampType.all.maskBits; layer = RampType.all;
} }
break; break;
} }
if (maskBits != null) { if (layer != null) {
ball.setMaskBits(maskBits); ball.setLayer(layer);
} }
} }
} }

@ -74,7 +74,7 @@ class JetpackRampOpening extends RampOpening {
_orientation = orientation, _orientation = orientation,
super( super(
position: position, position: position,
categoryBits: RampType.jetpack.maskBits, layer: RampType.jetpack,
); );
/// Orientation of entrance/exit of [JetpackRamp] where /// Orientation of entrance/exit of [JetpackRamp] where

@ -69,7 +69,7 @@ class SparkyRampOpening extends RampOpening {
_orientation = orientation, _orientation = orientation,
super( super(
position: position, position: position,
categoryBits: RampType.sparky.maskBits, layer: RampType.sparky,
); );
/// Orientation of entrance/exit of [SparkyRamp] where /// Orientation of entrance/exit of [SparkyRamp] where

@ -166,13 +166,13 @@ void main() {
); );
}); });
group('setMaskBits', () { group('setLayer', () {
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(PinballGameTest.create);
flameTester.test( flameTester.test(
'modifies maskBits correctly', 'modifies layer correctly',
(game) async { (game) async {
const newMaskBits = 1234; const newLayer = RampType.jetpack;
final ball = Ball(position: Vector2.zero()); final ball = Ball(position: Vector2.zero());
await game.ensureAdd(ball); await game.ensureAdd(ball);
@ -182,10 +182,10 @@ void main() {
expect(fixture.filterData.categoryBits, equals(1)); expect(fixture.filterData.categoryBits, equals(1));
expect(fixture.filterData.maskBits, equals(Filter().maskBits)); expect(fixture.filterData.maskBits, equals(Filter().maskBits));
ball.setMaskBits(newMaskBits); ball.setLayer(newLayer);
expect(fixture.filterData.categoryBits, equals(newMaskBits)); expect(fixture.filterData.categoryBits, equals(newLayer.maskBits));
expect(fixture.filterData.maskBits, equals(newMaskBits)); expect(fixture.filterData.maskBits, equals(newLayer.maskBits));
}, },
); );
}); });

@ -11,9 +11,9 @@ class TestRampArea extends RampOpening {
TestRampArea({ TestRampArea({
required Vector2 position, required Vector2 position,
required RampOrientation orientation, required RampOrientation orientation,
required int categoryBits, required RampType layer,
}) : _orientation = orientation, }) : _orientation = orientation,
super(position: position, categoryBits: categoryBits); super(position: position, layer: layer);
final RampOrientation _orientation; final RampOrientation _orientation;
@ -71,7 +71,7 @@ void main() {
final ramp = TestRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: RampType.all.maskBits, layer: RampType.all,
); );
await game.ready(); await game.ready();
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -88,7 +88,7 @@ void main() {
final ramp = TestRampArea( final ramp = TestRampArea(
position: position, position: position,
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: RampType.all.maskBits, layer: RampType.all,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -103,7 +103,7 @@ void main() {
final ramp = TestRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: RampType.all.maskBits, layer: RampType.all,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -112,7 +112,7 @@ void main() {
); );
group('fixtures', () { group('fixtures', () {
const maskBits = 1234; const layer = RampType.jetpack;
flameTester.test( flameTester.test(
'exists', 'exists',
@ -120,7 +120,7 @@ void main() {
final ramp = TestRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: maskBits, layer: layer,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -134,7 +134,7 @@ void main() {
final ramp = TestRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: maskBits, layer: layer,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -149,7 +149,7 @@ void main() {
final ramp = TestRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: maskBits, layer: layer,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -164,7 +164,7 @@ void main() {
final ramp = TestRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: maskBits, layer: layer,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -172,7 +172,7 @@ void main() {
final fixture = ramp.body.fixtures[0]; final fixture = ramp.body.fixtures[0];
expect( expect(
fixture.filterData.categoryBits, fixture.filterData.categoryBits,
equals(maskBits), equals(layer.maskBits),
); );
}, },
); );
@ -181,7 +181,7 @@ void main() {
}); });
group('RampAreaCallback', () { group('RampAreaCallback', () {
const categoryBits = 1234; const layer = RampType.jetpack;
test( test(
'a ball enters from bottom into a down oriented path and keeps inside, ' 'a ball enters from bottom into a down oriented path and keeps inside, '
@ -191,7 +191,7 @@ void main() {
final area = TestRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: categoryBits, layer: layer,
); );
final callback = TestRampAreaCallback(); final callback = TestRampAreaCallback();
@ -203,11 +203,11 @@ void main() {
expect(callback._ballsInside.length, equals(1)); expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball); expect(callback._ballsInside.first, ball);
verify(() => ball.setMaskBits(categoryBits)).called(1); verify(() => ball.setLayer(layer)).called(1);
callback.end(ball, area, MockContact()); callback.end(ball, area, MockContact());
verifyNever(() => ball.setMaskBits(RampType.all.maskBits)); verifyNever(() => ball.setLayer(RampType.all));
}); });
test( test(
@ -218,7 +218,7 @@ void main() {
final area = TestRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.up, orientation: RampOrientation.up,
categoryBits: categoryBits, layer: layer,
); );
final callback = TestRampAreaCallback(); final callback = TestRampAreaCallback();
@ -230,11 +230,11 @@ void main() {
expect(callback._ballsInside.length, equals(1)); expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball); expect(callback._ballsInside.first, ball);
verify(() => ball.setMaskBits(categoryBits)).called(1); verify(() => ball.setLayer(layer)).called(1);
callback.end(ball, area, MockContact()); callback.end(ball, area, MockContact());
verifyNever(() => ball.setMaskBits(RampType.all.maskBits)); verifyNever(() => ball.setLayer(RampType.all));
}); });
test( test(
@ -245,7 +245,7 @@ void main() {
final area = TestRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: categoryBits, layer: layer,
); );
final callback = TestRampAreaCallback(); final callback = TestRampAreaCallback();
@ -257,11 +257,11 @@ void main() {
expect(callback._ballsInside.length, equals(1)); expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball); expect(callback._ballsInside.first, ball);
verify(() => ball.setMaskBits(categoryBits)).called(1); verify(() => ball.setLayer(layer)).called(1);
callback.end(ball, area, MockContact()); callback.end(ball, area, MockContact());
verify(() => ball.setMaskBits(RampType.all.maskBits)); verify(() => ball.setLayer(RampType.all));
}); });
test( test(
@ -272,7 +272,7 @@ void main() {
final area = TestRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: categoryBits, layer: layer,
); );
final callback = TestRampAreaCallback()..ballsInside.add(ball); final callback = TestRampAreaCallback()..ballsInside.add(ball);
@ -282,11 +282,11 @@ void main() {
callback.begin(ball, area, MockContact()); callback.begin(ball, area, MockContact());
expect(callback._ballsInside.isEmpty, isTrue); 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()); callback.end(ball, area, MockContact());
verify(() => ball.setMaskBits(RampType.all.maskBits)).called(1); verify(() => ball.setLayer(RampType.all)).called(1);
}); });
test( test(
@ -297,7 +297,7 @@ void main() {
final area = TestRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.up, orientation: RampOrientation.up,
categoryBits: categoryBits, layer: layer,
); );
final callback = TestRampAreaCallback()..ballsInside.add(ball); final callback = TestRampAreaCallback()..ballsInside.add(ball);
@ -307,11 +307,11 @@ void main() {
callback.begin(ball, area, MockContact()); callback.begin(ball, area, MockContact());
expect(callback._ballsInside.isEmpty, isTrue); 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()); callback.end(ball, area, MockContact());
verify(() => ball.setMaskBits(RampType.all.maskBits)).called(1); verify(() => ball.setLayer(RampType.all)).called(1);
}); });
}); });
} }

@ -99,8 +99,10 @@ void main() {
group('JetpackRampAreaCallback', () { group('JetpackRampAreaCallback', () {
test('has no ball inside on creation', () { test('has no ball inside on creation', () {
expect(JetpackRampOpeningBallContactCallback().ballsInside, expect(
equals(<Ball>{})); JetpackRampOpeningBallContactCallback().ballsInside,
equals(<Ball>{}),
);
}); });
}); });
} }

@ -100,7 +100,9 @@ void main() {
group('SparkyRampAreaCallback', () { group('SparkyRampAreaCallback', () {
test('has no ball inside on creation', () { test('has no ball inside on creation', () {
expect( expect(
SparkyRampOpeningBallContactCallback().ballsInside, equals(<Ball>{})); SparkyRampOpeningBallContactCallback().ballsInside,
equals(<Ball>{}),
);
}); });
}); });
} }

Loading…
Cancel
Save