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({
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<Opening extends RampOpening>
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<Opening extends RampOpening>
if (isBallOutsideOpening) {
ball
..layer = opening.outsideLayer
..priority = opening.outsidePriority;
..sendTo(opening.outsidePriority)
..layer = opening.outsideLayer;
_ballsInside.remove(ball);
}
}

@ -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<SpaceshipHole, Ball> {
@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;
}
}

@ -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) {

Loading…
Cancel
Save