diff --git a/packages/pinball_components/lib/src/components/spaceship.dart b/packages/pinball_components/lib/src/components/spaceship.dart index 976f2aea..8122e934 100644 --- a/packages/pinball_components/lib/src/components/spaceship.dart +++ b/packages/pinball_components/lib/src/components/spaceship.dart @@ -36,8 +36,8 @@ class Spaceship extends Forge2DBlueprint { SpaceshipEntrance()..initialPosition = position, AndroidHead()..initialPosition = position, SpaceshipHole( - onExitLayer: Layer.spaceshipExitRail, - onExitElevation: 2, + outsideLayer: Layer.spaceshipExitRail, + outsidePriority: 2, )..initialPosition = position - Vector2(5.2, 4.8), SpaceshipHole()..initialPosition = position - Vector2(-7.2, 0.8), SpaceshipWall()..initialPosition = position, @@ -181,19 +181,16 @@ class SpaceshipEntrance extends RampOpening { /// {@endtemplate} class SpaceshipHole extends RampOpening { /// {@macro spaceship_hole} - SpaceshipHole({Layer? onExitLayer, this.onExitElevation = 1}) + SpaceshipHole({Layer? outsideLayer, int? outsidePriority = 1}) : super( pathwayLayer: Layer.spaceship, - outsideLayer: onExitLayer, - outsidePriority: onExitElevation, + outsideLayer: outsideLayer, + outsidePriority: outsidePriority, orientation: RampOrientation.up, ) { layer = Layer.spaceship; } - /// Priority order for [SpaceshipHole] on exit. - final int onExitElevation; - @override Shape get shape { return ArcShape( diff --git a/packages/pinball_components/test/helpers/mocks.dart b/packages/pinball_components/test/helpers/mocks.dart index 8d6f45b3..c36afff2 100644 --- a/packages/pinball_components/test/helpers/mocks.dart +++ b/packages/pinball_components/test/helpers/mocks.dart @@ -1,5 +1,6 @@ import 'dart:ui'; +import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball_components/pinball_components.dart'; @@ -24,3 +25,5 @@ class MockContact extends Mock implements Contact {} class MockContactCallback extends Mock implements ContactCallback {} + +class MockComponent extends Mock implements Component {} diff --git a/packages/pinball_components/test/src/components/failures/spaceship_isolatedDiff.png b/packages/pinball_components/test/src/components/failures/spaceship_isolatedDiff.png new file mode 100644 index 00000000..86fe8c6a Binary files /dev/null and b/packages/pinball_components/test/src/components/failures/spaceship_isolatedDiff.png differ diff --git a/packages/pinball_components/test/src/components/failures/spaceship_maskedDiff.png b/packages/pinball_components/test/src/components/failures/spaceship_maskedDiff.png new file mode 100644 index 00000000..8d64a796 Binary files /dev/null and b/packages/pinball_components/test/src/components/failures/spaceship_maskedDiff.png differ diff --git a/packages/pinball_components/test/src/components/failures/spaceship_masterImage.png b/packages/pinball_components/test/src/components/failures/spaceship_masterImage.png new file mode 100644 index 00000000..da665718 Binary files /dev/null and b/packages/pinball_components/test/src/components/failures/spaceship_masterImage.png differ diff --git a/packages/pinball_components/test/src/components/failures/spaceship_testImage.png b/packages/pinball_components/test/src/components/failures/spaceship_testImage.png new file mode 100644 index 00000000..5e419c9c Binary files /dev/null and b/packages/pinball_components/test/src/components/failures/spaceship_testImage.png differ diff --git a/packages/pinball_components/test/src/components/spaceship_test.dart b/packages/pinball_components/test/src/components/spaceship_test.dart index 87987ca3..0a990d8f 100644 --- a/packages/pinball_components/test/src/components/spaceship_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_test.dart @@ -59,7 +59,7 @@ void main() { group('SpaceshipEntranceBallContactCallback', () { test('changes the ball priority on contact', () { when(() => ball.priority).thenReturn(2); - when(() => entrance.priority).thenReturn(3); + when(() => entrance.pathwayPriority).thenReturn(3); SpaceshipEntranceBallContactCallback().begin( entrance, @@ -67,14 +67,14 @@ void main() { MockContact(), ); - verify(() => ball.showInFrontOf(entrance)).called(1); + verify(() => ball.sendTo(entrance.pathwayPriority)).called(1); }); }); group('SpaceshipHoleBallContactCallback', () { test('changes the ball priority on contact', () { when(() => hole.outsideLayer).thenReturn(Layer.board); - when(() => hole.onExitElevation).thenReturn(1); + when(() => hole.outsidePriority).thenReturn(1); SpaceshipHoleBallContactCallback().begin( hole, @@ -82,20 +82,7 @@ void main() { MockContact(), ); - verify(() => ball.priority = hole.onExitElevation).called(1); - }); - - test('re order the game children', () { - when(() => hole.outsideLayer).thenReturn(Layer.board); - when(() => hole.onExitElevation).thenReturn(1); - - SpaceshipHoleBallContactCallback().begin( - hole, - ball, - MockContact(), - ); - - verify(() => ball.sendToBack()).called(1); + verify(() => ball.sendTo(hole.outsidePriority)).called(1); }); }); }); diff --git a/test/flame/priority_test.dart b/packages/pinball_components/test/src/flame/priority_test.dart similarity index 82% rename from test/flame/priority_test.dart rename to packages/pinball_components/test/src/flame/priority_test.dart index c4f61569..231c7744 100644 --- a/test/flame/priority_test.dart +++ b/packages/pinball_components/test/src/flame/priority_test.dart @@ -2,10 +2,10 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:mockingjay/mockingjay.dart'; +import 'package:mocktail/mocktail.dart'; import 'package:pinball_components/src/flame/priority.dart'; -import '../helpers/helpers.dart'; +import '../../helpers/helpers.dart'; class TestBodyComponent extends BodyComponent { @override @@ -19,13 +19,52 @@ void main() { final flameTester = FlameTester(Forge2DGame.new); group('ComponentPriorityX', () { + group('sendTo', () { + flameTester.test( + 'changes the priority correctly to other level', + (game) async { + const newPriority = 5; + final component = TestBodyComponent()..priority = 4; + + component.sendTo(newPriority); + + expect(component.priority, equals(newPriority)); + }, + ); + + flameTester.test( + 'calls reorderChildren if the new priority is different', + (game) async { + const newPriority = 5; + final component = MockComponent(); + when(() => component.priority).thenReturn(4); + + component.sendTo(newPriority); + + verify(component.reorderChildren).called(1); + }, + ); + + flameTester.test( + "doesn't call reorderChildren if the priority is the same", + (game) async { + const newPriority = 5; + final component = MockComponent(); + when(() => component.priority).thenReturn(newPriority); + + component.sendTo(newPriority); + + verifyNever(component.reorderChildren); + }, + ); + }); + group('sendToBack', () { flameTester.test( 'changes the priority correctly to board level', (game) async { final component = TestBodyComponent()..priority = 4; - await game.ensureAdd(component); component.sendToBack(); expect(component.priority, equals(0)); @@ -38,7 +77,6 @@ void main() { final component = MockComponent(); when(() => component.priority).thenReturn(4); - await game.ensureAdd(component); component.sendToBack(); verify(component.reorderChildren).called(1); @@ -51,7 +89,6 @@ void main() { final component = MockComponent(); when(() => component.priority).thenReturn(0); - await game.ensureAdd(component); component.sendToBack(); verifyNever(component.reorderChildren); @@ -68,7 +105,6 @@ void main() { final otherComponent = TestBodyComponent() ..priority = startPriority - 1; - await game.ensureAddAll([component, otherComponent]); component.showBehindOf(otherComponent); expect(component.priority, equals(otherComponent.priority - 1)); @@ -83,7 +119,6 @@ void main() { final otherComponent = TestBodyComponent() ..priority = startPriority + 1; - await game.ensureAddAll([component, otherComponent]); component.showBehindOf(otherComponent); expect(component.priority, equals(startPriority)); @@ -99,7 +134,6 @@ void main() { when(() => component.priority).thenReturn(startPriority); when(() => otherComponent.priority).thenReturn(startPriority - 1); - await game.ensureAddAll([component, otherComponent]); component.showBehindOf(otherComponent); verify(component.reorderChildren).called(1); @@ -116,7 +150,6 @@ void main() { when(() => component.priority).thenReturn(startPriority); when(() => otherComponent.priority).thenReturn(startPriority + 1); - await game.ensureAddAll([component, otherComponent]); component.showBehindOf(otherComponent); verifyNever(component.reorderChildren); @@ -133,7 +166,6 @@ void main() { final otherComponent = TestBodyComponent() ..priority = startPriority + 1; - await game.ensureAddAll([component, otherComponent]); component.showInFrontOf(otherComponent); expect(component.priority, equals(otherComponent.priority + 1)); @@ -148,7 +180,6 @@ void main() { final otherComponent = TestBodyComponent() ..priority = startPriority - 1; - await game.ensureAddAll([component, otherComponent]); component.showInFrontOf(otherComponent); expect(component.priority, equals(startPriority)); @@ -164,7 +195,6 @@ void main() { when(() => component.priority).thenReturn(startPriority); when(() => otherComponent.priority).thenReturn(startPriority + 1); - await game.ensureAddAll([component, otherComponent]); component.showInFrontOf(otherComponent); verify(component.reorderChildren).called(1); @@ -181,7 +211,6 @@ void main() { when(() => component.priority).thenReturn(startPriority); when(() => otherComponent.priority).thenReturn(startPriority - 1); - await game.ensureAddAll([component, otherComponent]); component.showInFrontOf(otherComponent); verifyNever(component.reorderChildren); diff --git a/test/helpers/mocks.dart b/test/helpers/mocks.dart index 69983d10..fbe8edfb 100644 --- a/test/helpers/mocks.dart +++ b/test/helpers/mocks.dart @@ -70,6 +70,4 @@ class MockSpaceshipExitRailEnd extends Mock implements SpaceshipExitRailEnd {} class MockComponentSet extends Mock implements ComponentSet {} -class MockComponent extends Mock implements Component {} - class MockDashNestBumper extends Mock implements DashNestBumper {}