From 3e70ad9ea7a46fe4dbd7bb85edc439307c07846c Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Wed, 16 Mar 2022 14:08:49 +0100 Subject: [PATCH] feat: refactor to allow jetpack ramp to be above board and launcher ramp --- lib/game/components/jetpack_ramp.dart | 7 +-- lib/game/components/launcher_ramp.dart | 3 +- lib/game/components/layer.dart | 21 +++---- lib/game/components/pathway.dart | 2 +- test/game/components/ball_test.dart | 4 +- test/game/components/layer_test.dart | 76 ++++++++++++++++---------- test/game/components/pathway_test.dart | 2 +- 7 files changed, 66 insertions(+), 49 deletions(-) diff --git a/lib/game/components/jetpack_ramp.dart b/lib/game/components/jetpack_ramp.dart index aa97a1bf..1761aaf6 100644 --- a/lib/game/components/jetpack_ramp.dart +++ b/lib/game/components/jetpack_ramp.dart @@ -39,7 +39,7 @@ class JetpackRamp extends Component with HasGameRef { 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; diff --git a/lib/game/components/launcher_ramp.dart b/lib/game/components/launcher_ramp.dart index b38323b5..2c6e6700 100644 --- a/lib/game/components/launcher_ramp.dart +++ b/lib/game/components/launcher_ramp.dart @@ -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; diff --git a/lib/game/components/layer.dart b/lib/game/components/layer.dart index af3f8ce0..fc84746c 100644 --- a/lib/game/components/layer.dart +++ b/lib/game/components/layer.dart @@ -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 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 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; } diff --git a/lib/game/components/pathway.dart b/lib/game/components/pathway.dart index 5e4cadf6..e6ec3b5e 100644 --- a/lib/game/components/pathway.dart +++ b/lib/game/components/pathway.dart @@ -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; diff --git a/test/game/components/ball_test.dart b/test/game/components/ball_test.dart index de15c24c..2953cf7b 100644 --- a/test/game/components/ball_test.dart +++ b/test/game/components/ball_test.dart @@ -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; diff --git a/test/game/components/layer_test.dart b/test/game/components/layer_test.dart index cbf53b66..233d9683 100644 --- a/test/game/components/layer_test.dart +++ b/test/game/components/layer_test.dart @@ -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); diff --git a/test/game/components/pathway_test.dart b/test/game/components/pathway_test.dart index 443c1f96..961bb1a7 100644 --- a/test/game/components/pathway_test.dart +++ b/test/game/components/pathway_test.dart @@ -167,7 +167,7 @@ void main() { expect(fixture, isA()); expect( fixture.filterData.categoryBits, - equals(Filter().categoryBits), + equals(Layer.board.maskBits), ); } },