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}
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<PinballGame> with Layer {
final fixtureDef = FixtureDef(shape)
..density = 1
..filter.maskBits = _maskBits;
..filter.maskBits = _layer.maskBits;
final bodyDef = BodyDef()
..userData = this

@ -11,11 +11,11 @@ import 'package:pinball/game/game.dart';
/// same bits and ignore others.
/// {@endtemplate}
mixin Layer on BodyComponent<PinballGame> {
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 extends RampOpening>
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);
}
}
}

@ -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

@ -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

@ -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));
},
);
});

@ -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);
});
});
}

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

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

Loading…
Cancel
Save