feat: refactor to allow jetpack ramp to be above board and launcher ramp

pull/40/head
RuiAlonso 4 years ago
parent 688941b9fd
commit 3e70ad9ea7

@ -39,7 +39,7 @@ class JetpackRamp extends Component with HasGameRef<PinballGame> {
await add(
JetpackRampOpening(
position: position + Vector2(-11, .5),
position: position + Vector2(-11, 1),
orientation: RampOrientation.down,
rotation: radians(15),
),
@ -66,13 +66,12 @@ class JetpackRampOpening extends RampOpening {
required Vector2 position,
required RampOrientation orientation,
double rotation = 0,
Layer? openingLayer,
}) : _rotation = rotation,
_orientation = orientation,
super(
position: position,
pathwayLayer: Layer.jetpack,
openingLayer: openingLayer,
openingLayer: Layer.opening,
);
/// Orientation of entrance/exit of [JetpackRamp] where
@ -84,7 +83,7 @@ class JetpackRampOpening extends RampOpening {
final double _rotation;
/// Size of the [RampOpening] placed at the entrance/exit of [JetpackRamp].
final int _size = 7;
final int _size = 6;
@override
RampOrientation get orientation => _orientation;

@ -80,6 +80,7 @@ class LauncherRampOpening extends RampOpening {
super(
position: position,
pathwayLayer: Layer.launcher,
openingLayer: Layer.opening,
);
/// Orientation of entrance/exit of [LauncherRamp] where
@ -91,7 +92,7 @@ class LauncherRampOpening extends RampOpening {
final double _rotation;
/// Size of the [RampOpening] placed at the entrance/exit of [LauncherRamp].
final int _size = 7;
final int _size = 6;
@override
RampOrientation get orientation => _orientation;

@ -32,6 +32,9 @@ enum Layer {
/// Collide only with board elements (the ground level).
board,
/// Collide only with ramps opening elements.
opening,
/// Collide only with Jetpack group elements.
jetpack,
@ -41,17 +44,19 @@ enum Layer {
/// Utility methods for [Layer].
extension LayerX on Layer {
/// Mask of bits for each [Layer].
/// Mask of bits for each [Layer] to filter collisions.
int get maskBits {
switch (this) {
case Layer.all:
return 0xFFFF;
case Layer.board:
return 0xFF0F;
return 0x0001;
case Layer.opening:
return 0x0007;
case Layer.jetpack:
return 0x0010;
return 0x0002;
case Layer.launcher:
return 0x0100;
return 0x0005;
}
}
}
@ -139,11 +144,9 @@ abstract class RampOpeningBallContactCallback<Opening extends RampOpening>
Layer layer;
if (!ballsInside.contains(ball)) {
layer = opening.pathwayLayer;
print('TOUCH begin (add) layer=$layer');
ballsInside.add(ball);
} else {
layer = Layer.board;
print('TOUCH begin (remove) layer=$layer');
ballsInside.remove(ball);
}
@ -156,19 +159,13 @@ abstract class RampOpeningBallContactCallback<Opening extends RampOpening>
switch (opening.orientation) {
case RampOrientation.up:
print('TOUCH end up');
if (ball.body.position.y > opening._position.y) {
print('layer=$layer');
layer = Layer.board;
print('layer=$layer');
}
break;
case RampOrientation.down:
print('TOUCH end down');
if (ball.body.position.y < opening._position.y) {
print('layer=$layer');
layer = Layer.board;
print('layer=$layer');
}
break;
}

@ -181,7 +181,7 @@ class Pathway extends BodyComponent {
final fixtureDef = FixtureDef(chain);
body.createFixture(fixtureDef).filterData.categoryBits =
_layer?.maskBits ?? Filter().categoryBits;
_layer?.maskBits ?? Layer.board.maskBits;
}
return body;

@ -94,7 +94,7 @@ void main() {
await game.ensureAdd(ball);
final fixture = ball.body.fixtures[0];
expect(fixture.filterData.maskBits, equals(Filter().maskBits));
expect(fixture.filterData.maskBits, equals(Layer.board.maskBits));
},
);
});
@ -180,7 +180,7 @@ void main() {
final fixture = ball.body.fixtures[0];
expect(fixture.filterData.categoryBits, equals(1));
expect(fixture.filterData.maskBits, equals(Filter().maskBits));
expect(fixture.filterData.maskBits, equals(Layer.board.maskBits));
ball.layer = newLayer;

@ -11,9 +11,14 @@ class TestRampOpening extends RampOpening {
TestRampOpening({
required Vector2 position,
required RampOrientation orientation,
required Layer layer,
required Layer pathwayLayer,
required Layer openingLayer,
}) : _orientation = orientation,
super(position: position, pathwayLayer: layer);
super(
position: position,
pathwayLayer: pathwayLayer,
openingLayer: openingLayer,
);
final RampOrientation _orientation;
@ -43,31 +48,36 @@ class TestRampOpeningBallContactCallback
void main() {
group('Layer', () {
test('has four values', () {
expect(Layer.values.length, equals(4));
expect(Layer.values.length, equals(5));
});
});
group('LayerX', () {
test('all types are different', () {
expect(Layer.all.maskBits, isNot(equals(Layer.board.maskBits)));
expect(Layer.board.maskBits, isNot(equals(Layer.jetpack.maskBits)));
expect(Layer.board.maskBits, isNot(equals(Layer.opening.maskBits)));
expect(Layer.opening.maskBits, isNot(equals(Layer.jetpack.maskBits)));
expect(Layer.jetpack.maskBits, isNot(equals(Layer.launcher.maskBits)));
expect(Layer.launcher.maskBits, isNot(equals(Layer.board.maskBits)));
});
test('all type has 0xFFFF maskBits', () {
expect(Layer.all.maskBits, equals(0xFF0F));
expect(Layer.all.maskBits, equals(0xFFFF));
});
test('board type has 0x0001 maskBits', () {
expect(Layer.board.maskBits, equals(0x0001));
});
test('board type has 0xFF0F maskBits', () {
expect(Layer.board.maskBits, equals(0xFF0F));
test('opening type has 0x0007 maskBits', () {
expect(Layer.opening.maskBits, equals(0x0007));
});
test('jetpack type has 0x010 maskBits', () {
expect(Layer.jetpack.maskBits, equals(0x0010));
test('jetpack type has 0x0002 maskBits', () {
expect(Layer.jetpack.maskBits, equals(0x0002));
});
test('launcher type has 0x0100 maskBits', () {
expect(Layer.launcher.maskBits, equals(0x0100));
test('launcher type has 0x0005 maskBits', () {
expect(Layer.launcher.maskBits, equals(0x0005));
});
});
@ -81,7 +91,8 @@ void main() {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
layer: Layer.board,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
await game.ready();
await game.ensureAdd(ramp);
@ -98,7 +109,8 @@ void main() {
final ramp = TestRampOpening(
position: position,
orientation: RampOrientation.down,
layer: Layer.board,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
await game.ensureAdd(ramp);
@ -113,7 +125,8 @@ void main() {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
layer: Layer.board,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
await game.ensureAdd(ramp);
@ -130,7 +143,8 @@ void main() {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
layer: layer,
pathwayLayer: layer,
openingLayer: layer,
);
await game.ensureAdd(ramp);
@ -144,7 +158,8 @@ void main() {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
layer: layer,
pathwayLayer: layer,
openingLayer: layer,
);
await game.ensureAdd(ramp);
@ -159,7 +174,8 @@ void main() {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
layer: layer,
pathwayLayer: layer,
openingLayer: layer,
);
await game.ensureAdd(ramp);
@ -174,7 +190,8 @@ void main() {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
layer: layer,
pathwayLayer: layer,
openingLayer: layer,
);
await game.ensureAdd(ramp);
@ -191,8 +208,6 @@ void main() {
});
group('RampOpeningBallContactCallback', () {
const layer = Layer.jetpack;
test(
'a ball enters from bottom into a down oriented path and keeps inside, '
'is saved into collection and set maskBits to path', () {
@ -201,7 +216,8 @@ void main() {
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.down,
layer: layer,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback();
@ -213,7 +229,7 @@ void main() {
expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball);
verify(() => ball.layer = layer).called(1);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
@ -228,7 +244,8 @@ void main() {
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.up,
layer: layer,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback();
@ -240,7 +257,7 @@ void main() {
expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball);
verify(() => ball.layer = layer).called(1);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
@ -255,7 +272,8 @@ void main() {
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.down,
layer: layer,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback();
@ -267,7 +285,7 @@ void main() {
expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball);
verify(() => ball.layer = layer).called(1);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
@ -282,7 +300,8 @@ void main() {
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.down,
layer: layer,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback()
..ballsInside.add(ball);
@ -308,7 +327,8 @@ void main() {
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.up,
layer: layer,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback()
..ballsInside.add(ball);

@ -167,7 +167,7 @@ void main() {
expect(fixture, isA<Fixture>());
expect(
fixture.filterData.categoryBits,
equals(Filter().categoryBits),
equals(Layer.board.maskBits),
);
}
},

Loading…
Cancel
Save