fix: fixed priority changes

pull/83/head
RuiAlonso 4 years ago
parent e164c12f77
commit a0b8bad13c

@ -29,12 +29,12 @@ abstract class RampOpening extends BodyComponent with InitialPosition, Layered {
RampOpening({ RampOpening({
required Layer pathwayLayer, required Layer pathwayLayer,
Layer? outsideLayer, Layer? outsideLayer,
required int pathwayPriority, int? pathwayPriority,
int? outsidePriority, int? outsidePriority,
required this.orientation, required this.orientation,
}) : _pathwayLayer = pathwayLayer, }) : _pathwayLayer = pathwayLayer,
_outsideLayer = outsideLayer ?? Layer.board, _outsideLayer = outsideLayer ?? Layer.board,
_pathwayPriority = pathwayPriority, _pathwayPriority = pathwayPriority ?? 0,
_outsidePriority = outsidePriority ?? 0 { _outsidePriority = outsidePriority ?? 0 {
layer = Layer.opening; layer = Layer.opening;
} }
@ -96,8 +96,8 @@ class RampOpeningBallContactCallback<Opening extends RampOpening>
layer = opening.pathwayLayer; layer = opening.pathwayLayer;
_ballsInside.add(ball); _ballsInside.add(ball);
ball ball
..layer = layer ..sendTo(opening.pathwayPriority)
..priority = opening.pathwayPriority; ..layer = layer;
} else { } else {
_ballsInside.remove(ball); _ballsInside.remove(ball);
} }
@ -119,8 +119,8 @@ class RampOpeningBallContactCallback<Opening extends RampOpening>
if (isBallOutsideOpening) { if (isBallOutsideOpening) {
ball ball
..layer = opening.outsideLayer ..sendTo(opening.outsidePriority)
..priority = opening.outsidePriority; ..layer = opening.outsideLayer;
_ballsInside.remove(ball); _ballsInside.remove(ball);
} }
} }

@ -152,8 +152,8 @@ class SpaceshipEntrance extends RampOpening {
: super( : super(
pathwayLayer: Layer.spaceship, pathwayLayer: Layer.spaceship,
orientation: RampOrientation.up, orientation: RampOrientation.up,
pathwayPriority: Spaceship.ballPriorityWhenOnSpaceship,
) { ) {
priority = Spaceship.ballPriorityWhenOnSpaceship;
layer = Layer.spaceship; layer = Layer.spaceship;
} }
@ -185,6 +185,7 @@ class SpaceshipHole extends RampOpening {
: super( : super(
pathwayLayer: Layer.spaceship, pathwayLayer: Layer.spaceship,
outsideLayer: onExitLayer, outsideLayer: onExitLayer,
outsidePriority: onExitElevation,
orientation: RampOrientation.up, orientation: RampOrientation.up,
) { ) {
layer = Layer.spaceship; layer = Layer.spaceship;
@ -267,7 +268,7 @@ class SpaceshipEntranceBallContactCallback
@override @override
void begin(SpaceshipEntrance entrance, Ball ball, _) { void begin(SpaceshipEntrance entrance, Ball ball, _) {
ball ball
..showInFrontOf(entrance) ..sendTo(entrance.pathwayPriority)
..layer = Layer.spaceship; ..layer = Layer.spaceship;
} }
} }
@ -275,16 +276,14 @@ class SpaceshipEntranceBallContactCallback
/// [ContactCallback] that handles the contact between the [Ball] /// [ContactCallback] that handles the contact between the [Ball]
/// and a [SpaceshipHole]. /// and a [SpaceshipHole].
/// ///
/// It sets the [Ball] priority and filter data so it will "be back" on the /// It sets the [Ball] priority and filter data so it will outside of the
/// board. /// [Spaceship].
class SpaceshipHoleBallContactCallback class SpaceshipHoleBallContactCallback
extends ContactCallback<SpaceshipHole, Ball> { extends ContactCallback<SpaceshipHole, Ball> {
@override @override
void begin(SpaceshipHole hole, Ball ball, _) { void begin(SpaceshipHole hole, Ball ball, _) {
ball ball
// TODO(ruimiguel): apply Elevated when PR merged. ..sendTo(hole.outsidePriority)
..priority = hole.onExitElevation
..gameRef.reorderChildren()
..layer = hole.outsideLayer; ..layer = hole.outsideLayer;
} }
} }

@ -1,4 +1,3 @@
// TODO(ruimiguel): move file to appropiate location.
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flame/components.dart'; import 'package:flame/components.dart';
@ -6,6 +5,14 @@ import 'package:flame/components.dart';
extension ComponentPriorityX on Component { extension ComponentPriorityX on Component {
static const _lowestPriority = 0; 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. /// Changes the priority to the lowest possible.
void sendToBack() { void sendToBack() {
if (priority != _lowestPriority) { if (priority != _lowestPriority) {

Loading…
Cancel
Save