From eb8cc4bb6fbb5ae5744236efa8b81997150dc6ec Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Sat, 7 May 2022 17:45:48 +0100 Subject: [PATCH] fix: improved entering SpaceshipRamp consitency (#385) --- .../spaceship_ramp/spaceship_ramp.dart | 31 +++++++++--- .../spaceship_ramp/spaceship_ramp_test.dart | 49 +++++++++++++++++++ .../src/behaviors/layer_contact_behavior.dart | 21 +++++--- .../behaviors/z_index_contact_behavior.dart | 21 +++++--- .../layer_contact_behavior_test.dart | 18 +++++++ .../z_index_contact_behavior_test.dart | 15 ++++++ 6 files changed, 131 insertions(+), 24 deletions(-) diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart index 0796be92..ea7ed2e7 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart @@ -42,7 +42,7 @@ class SpaceshipRamp extends Component { _SpaceshipRampBackground(), _SpaceshipRampBoardOpening()..initialPosition = Vector2(3.4, -39.5), _SpaceshipRampForegroundRailing(), - _SpaceshipRampBase()..initialPosition = Vector2(3.4, -42.5), + SpaceshipRampBase()..initialPosition = Vector2(3.4, -42.5), _SpaceshipRampBackgroundRailingSpriteComponent(), SpaceshipRampArrowSpriteComponent( current: bloc.state.hits, @@ -255,9 +255,14 @@ class _SpaceshipRampBoardOpening extends BodyComponent _SpaceshipRampBoardOpeningSpriteComponent(), LayerContactBehavior(layer: Layer.spaceshipEntranceRamp) ..applyTo(['inside']), - LayerContactBehavior(layer: Layer.board)..applyTo(['outside']), - ZIndexContactBehavior(zIndex: ZIndexes.ballOnBoard) - ..applyTo(['outside']), + LayerContactBehavior( + layer: Layer.board, + onBegin: false, + )..applyTo(['outside']), + ZIndexContactBehavior( + zIndex: ZIndexes.ballOnBoard, + onBegin: false, + )..applyTo(['outside']), ZIndexContactBehavior(zIndex: ZIndexes.ballOnSpaceshipRamp) ..applyTo(['middle', 'inside']), ], @@ -426,9 +431,19 @@ class _SpaceshipRampForegroundRailingSpriteComponent extends SpriteComponent } } -class _SpaceshipRampBase extends BodyComponent with Layered, InitialPosition { - _SpaceshipRampBase() : super(renderBody: false) { - layer = Layer.board; +@visibleForTesting +class SpaceshipRampBase extends BodyComponent + with InitialPosition, ContactCallbacks { + SpaceshipRampBase() : super(renderBody: false); + + @override + void preSolve(Object other, Contact contact, Manifold oldManifold) { + super.preSolve(other, contact, oldManifold); + if (other is! Layered) return; + // Although, the Layer should already be taking care of the contact + // filtering, this is to ensure the ball doesn't collide with the ramp base + // when the filtering is calculated on different time steps. + contact.setEnabled(other.layer == Layer.board); } @override @@ -441,7 +456,7 @@ class _SpaceshipRampBase extends BodyComponent with Layered, InitialPosition { Vector2(4.1, 1.5), ], ); - final bodyDef = BodyDef(position: initialPosition); + final bodyDef = BodyDef(position: initialPosition, userData: this); return world.createBody(bodyDef)..createFixtureFromShape(shape); } } diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart index b74cfb88..7bd18aeb 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart @@ -2,6 +2,7 @@ import 'package:bloc_test/bloc_test.dart'; import 'package:flame/components.dart'; +import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; @@ -12,6 +13,12 @@ import '../../../helpers/helpers.dart'; class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {} +class _MockBall extends Mock implements Ball {} + +class _MockContact extends Mock implements Contact {} + +class _MockManifold extends Mock implements Manifold {} + void main() { TestWidgetsFlutterBinding.ensureInitialized(); final assets = [ @@ -275,4 +282,46 @@ void main() { }); }); }); + + group('SpaceshipRampBase', () { + test('can be instantiated', () { + expect(SpaceshipRampBase(), isA()); + }); + + flameTester.test('can be loaded', (game) async { + final component = SpaceshipRampBase(); + await game.ensureAdd(component); + expect(game.children, contains(component)); + }); + + flameTester.test( + 'postSolves disables contact when ball is not on Layer.board', + (game) async { + final ball = _MockBall(); + final contact = _MockContact(); + when(() => ball.layer).thenReturn(Layer.spaceshipEntranceRamp); + final component = SpaceshipRampBase(); + await game.ensureAdd(component); + + component.preSolve(ball, contact, _MockManifold()); + + verify(() => contact.setEnabled(false)).called(1); + }, + ); + + flameTester.test( + 'postSolves enables contact when ball is on Layer.board', + (game) async { + final ball = _MockBall(); + final contact = _MockContact(); + when(() => ball.layer).thenReturn(Layer.board); + final component = SpaceshipRampBase(); + await game.ensureAdd(component); + + component.preSolve(ball, contact, _MockManifold()); + + verify(() => contact.setEnabled(true)).called(1); + }, + ); + }); } diff --git a/packages/pinball_flame/lib/src/behaviors/layer_contact_behavior.dart b/packages/pinball_flame/lib/src/behaviors/layer_contact_behavior.dart index a73b94a5..ef475bc9 100644 --- a/packages/pinball_flame/lib/src/behaviors/layer_contact_behavior.dart +++ b/packages/pinball_flame/lib/src/behaviors/layer_contact_behavior.dart @@ -6,15 +6,20 @@ import 'package:pinball_flame/pinball_flame.dart'; /// {@endtemplate} class LayerContactBehavior extends ContactBehavior { /// {@macro layer_contact_behavior} - LayerContactBehavior({required Layer layer}) : _layer = layer; - - final Layer _layer; + LayerContactBehavior({ + required Layer layer, + bool onBegin = true, + }) { + if (onBegin) { + onBeginContact = (other, _) => _changeLayer(other, layer); + } else { + onEndContact = (other, _) => _changeLayer(other, layer); + } + } - @override - void beginContact(Object other, Contact contact) { - super.beginContact(other, contact); + void _changeLayer(Object other, Layer layer) { if (other is! Layered) return; - if (other.layer == _layer) return; - other.layer = _layer; + if (other.layer == layer) return; + other.layer = layer; } } diff --git a/packages/pinball_flame/lib/src/behaviors/z_index_contact_behavior.dart b/packages/pinball_flame/lib/src/behaviors/z_index_contact_behavior.dart index ea9bfcad..c763e2cf 100644 --- a/packages/pinball_flame/lib/src/behaviors/z_index_contact_behavior.dart +++ b/packages/pinball_flame/lib/src/behaviors/z_index_contact_behavior.dart @@ -6,15 +6,20 @@ import 'package:pinball_flame/pinball_flame.dart'; /// {@endtemplate} class ZIndexContactBehavior extends ContactBehavior { /// {@macro layer_contact_behavior} - ZIndexContactBehavior({required int zIndex}) : _zIndex = zIndex; - - final int _zIndex; + ZIndexContactBehavior({ + required int zIndex, + bool onBegin = true, + }) { + if (onBegin) { + onBeginContact = (other, _) => _changeZIndex(other, zIndex); + } else { + onEndContact = (other, _) => _changeZIndex(other, zIndex); + } + } - @override - void beginContact(Object other, Contact contact) { - super.beginContact(other, contact); + void _changeZIndex(Object other, int zIndex) { if (other is! ZIndex) return; - if (other.zIndex == _zIndex) return; - other.zIndex = _zIndex; + if (other.zIndex == zIndex) return; + other.zIndex = zIndex; } } diff --git a/packages/pinball_flame/test/src/behaviors/layer_contact_behavior_test.dart b/packages/pinball_flame/test/src/behaviors/layer_contact_behavior_test.dart index 49040977..d4b7ba18 100644 --- a/packages/pinball_flame/test/src/behaviors/layer_contact_behavior_test.dart +++ b/packages/pinball_flame/test/src/behaviors/layer_contact_behavior_test.dart @@ -56,5 +56,23 @@ void main() { expect(component.layer, newLayer); }); + + flameTester.test('endContact changes layer', (game) async { + const oldLayer = Layer.all; + const newLayer = Layer.board; + final behavior = LayerContactBehavior( + layer: newLayer, + onBegin: false, + ); + final parent = _TestBodyComponent(); + await game.ensureAdd(parent); + await parent.ensureAdd(behavior); + + final component = _TestLayeredBodyComponent(layer: oldLayer); + + behavior.endContact(component, _MockContact()); + + expect(component.layer, newLayer); + }); }); } diff --git a/packages/pinball_flame/test/src/behaviors/z_index_contact_behavior_test.dart b/packages/pinball_flame/test/src/behaviors/z_index_contact_behavior_test.dart index ad09004c..292a51fc 100644 --- a/packages/pinball_flame/test/src/behaviors/z_index_contact_behavior_test.dart +++ b/packages/pinball_flame/test/src/behaviors/z_index_contact_behavior_test.dart @@ -56,5 +56,20 @@ void main() { expect(component.zIndex, newIndex); }); + + flameTester.test('endContact changes zIndex', (game) async { + const oldIndex = 0; + const newIndex = 1; + final behavior = ZIndexContactBehavior(zIndex: newIndex, onBegin: false); + final parent = _TestBodyComponent(); + await game.ensureAdd(parent); + await parent.ensureAdd(behavior); + + final component = _TestZIndexBodyComponent(zIndex: oldIndex); + + behavior.endContact(component, _MockContact()); + + expect(component.zIndex, newIndex); + }); }); }