diff --git a/packages/pinball_components/lib/src/components/ramp_opening.dart b/packages/pinball_components/lib/src/components/ramp_opening.dart index 3784bf00..1ad07489 100644 --- a/packages/pinball_components/lib/src/components/ramp_opening.dart +++ b/packages/pinball_components/lib/src/components/ramp_opening.dart @@ -29,12 +29,12 @@ abstract class RampOpening extends BodyComponent with InitialPosition, Layered { RampOpening({ required Layer pathwayLayer, Layer? outsideLayer, - required int pathwayPriority, + int? pathwayPriority, int? outsidePriority, required this.orientation, }) : _pathwayLayer = pathwayLayer, _outsideLayer = outsideLayer ?? Layer.board, - _pathwayPriority = pathwayPriority, + _pathwayPriority = pathwayPriority ?? 0, _outsidePriority = outsidePriority ?? 0 { layer = Layer.opening; } @@ -96,8 +96,8 @@ class RampOpeningBallContactCallback layer = opening.pathwayLayer; _ballsInside.add(ball); ball - ..layer = layer - ..priority = opening.pathwayPriority; + ..sendTo(opening.pathwayPriority) + ..layer = layer; } else { _ballsInside.remove(ball); } @@ -119,8 +119,8 @@ class RampOpeningBallContactCallback if (isBallOutsideOpening) { ball - ..layer = opening.outsideLayer - ..priority = opening.outsidePriority; + ..sendTo(opening.outsidePriority) + ..layer = opening.outsideLayer; _ballsInside.remove(ball); } } diff --git a/packages/pinball_components/lib/src/components/spaceship.dart b/packages/pinball_components/lib/src/components/spaceship.dart index 03166a1b..976f2aea 100644 --- a/packages/pinball_components/lib/src/components/spaceship.dart +++ b/packages/pinball_components/lib/src/components/spaceship.dart @@ -152,8 +152,8 @@ class SpaceshipEntrance extends RampOpening { : super( pathwayLayer: Layer.spaceship, orientation: RampOrientation.up, + pathwayPriority: Spaceship.ballPriorityWhenOnSpaceship, ) { - priority = Spaceship.ballPriorityWhenOnSpaceship; layer = Layer.spaceship; } @@ -185,6 +185,7 @@ class SpaceshipHole extends RampOpening { : super( pathwayLayer: Layer.spaceship, outsideLayer: onExitLayer, + outsidePriority: onExitElevation, orientation: RampOrientation.up, ) { layer = Layer.spaceship; @@ -267,7 +268,7 @@ class SpaceshipEntranceBallContactCallback @override void begin(SpaceshipEntrance entrance, Ball ball, _) { ball - ..showInFrontOf(entrance) + ..sendTo(entrance.pathwayPriority) ..layer = Layer.spaceship; } } @@ -275,16 +276,14 @@ class SpaceshipEntranceBallContactCallback /// [ContactCallback] that handles the contact between the [Ball] /// and a [SpaceshipHole]. /// -/// It sets the [Ball] priority and filter data so it will "be back" on the -/// board. +/// It sets the [Ball] priority and filter data so it will outside of the +/// [Spaceship]. class SpaceshipHoleBallContactCallback extends ContactCallback { @override void begin(SpaceshipHole hole, Ball ball, _) { ball - // TODO(ruimiguel): apply Elevated when PR merged. - ..priority = hole.onExitElevation - ..gameRef.reorderChildren() + ..sendTo(hole.outsidePriority) ..layer = hole.outsideLayer; } } diff --git a/packages/pinball_components/lib/src/flame/priority.dart b/packages/pinball_components/lib/src/flame/priority.dart index c4f9b312..f4dccabf 100644 --- a/packages/pinball_components/lib/src/flame/priority.dart +++ b/packages/pinball_components/lib/src/flame/priority.dart @@ -1,4 +1,3 @@ -// TODO(ruimiguel): move file to appropiate location. import 'dart:math' as math; import 'package:flame/components.dart'; @@ -6,6 +5,14 @@ import 'package:flame/components.dart'; extension ComponentPriorityX on Component { static const _lowestPriority = 0; + /// Changes the priority to a specific one. + void sendTo(int destinationPriority) { + if (priority != destinationPriority) { + priority = math.max(destinationPriority, _lowestPriority); + reorderChildren(); + } + } + /// Changes the priority to the lowest possible. void sendToBack() { if (priority != _lowestPriority) {