refactor: renamed LayerSensor to use zIndex

pull/282/head
alestiago 3 years ago
parent 349551d852
commit 7555bfe1a2

@ -194,8 +194,8 @@ class _SpaceshipHole extends LayerSensor {
insideLayer: Layer.spaceship,
outsideLayer: outsideLayer,
orientation: LayerEntranceOrientation.down,
insidePriority: RenderPriority.ballOnSpaceship,
outsidePriority: outsidePriority,
insideZIndex: RenderPriority.ballOnSpaceship,
outsideZIndex: outsidePriority,
) {
layer = Layer.spaceship;
}

@ -230,8 +230,8 @@ class _LaunchRampExit extends LayerSensor {
insideLayer: Layer.launcher,
outsideLayer: Layer.board,
orientation: LayerEntranceOrientation.down,
insidePriority: RenderPriority.ballOnLaunchRamp,
outsidePriority: RenderPriority.ballOnBoard,
insideZIndex: RenderPriority.ballOnLaunchRamp,
outsideZIndex: RenderPriority.ballOnBoard,
) {
layer = Layer.launcher;
}

@ -18,7 +18,7 @@ enum LayerEntranceOrientation {
/// [BodyComponent] located at the entrance and exit of a [Layer].
///
/// By default the base [layer] is set to [Layer.board] and the
/// [outsidePriority] is set to the lowest possible [Layer].
/// [_outsideZIndex] is set to [RenderPriority.ballOnBoard].
/// {@endtemplate}
abstract class LayerSensor extends BodyComponent
with InitialPosition, Layered, ContactCallbacks {
@ -26,32 +26,21 @@ abstract class LayerSensor extends BodyComponent
LayerSensor({
required Layer insideLayer,
Layer? outsideLayer,
required int insidePriority,
int? outsidePriority,
required int insideZIndex,
int? outsideZIndex,
required this.orientation,
}) : _insideLayer = insideLayer,
_outsideLayer = outsideLayer ?? Layer.board,
_insidePriority = insidePriority,
_outsidePriority = outsidePriority ?? RenderPriority.ballOnBoard,
_insideZIndex = insideZIndex,
_outsideZIndex = outsideZIndex ?? RenderPriority.ballOnBoard,
super(renderBody: false) {
layer = Layer.opening;
}
final Layer _insideLayer;
final Layer _outsideLayer;
final int _insidePriority;
final int _outsidePriority;
/// Mask bits value for collisions on [Layer].
Layer get insideLayer => _insideLayer;
/// Mask bits value for collisions outside of [Layer].
Layer get outsideLayer => _outsideLayer;
/// Render priority for the [Ball] on [Layer].
int get insidePriority => _insidePriority;
/// Render priority for the [Ball] outside of [Layer].
int get outsidePriority => _outsidePriority;
final int _insideZIndex;
final int _outsideZIndex;
/// The [Shape] of the [LayerSensor].
Shape get shape;
@ -80,7 +69,7 @@ abstract class LayerSensor extends BodyComponent
super.beginContact(other, contact);
if (other is! Ball) return;
if (other.layer != insideLayer) {
if (other.layer != _insideLayer) {
final isBallEnteringOpening =
(orientation == LayerEntranceOrientation.down &&
other.body.linearVelocity.y < 0) ||
@ -89,13 +78,13 @@ abstract class LayerSensor extends BodyComponent
if (isBallEnteringOpening) {
other
..layer = insideLayer
..zIndex = insidePriority;
..layer = _insideLayer
..zIndex = _insideZIndex;
}
} else {
other
..layer = outsideLayer
..zIndex = outsidePriority;
..layer = _outsideLayer
..zIndex = _outsideZIndex;
}
}
}

@ -153,7 +153,7 @@ class _SpaceshipRailExit extends LayerSensor {
: super(
orientation: LayerEntranceOrientation.down,
insideLayer: Layer.spaceshipExitRail,
insidePriority: RenderPriority.ballOnSpaceshipRail,
insideZIndex: RenderPriority.ballOnSpaceshipRail,
) {
layer = Layer.spaceshipExitRail;
}

@ -354,8 +354,8 @@ class _SpaceshipRampOpening extends LayerSensor {
insideLayer: Layer.spaceshipEntranceRamp,
outsideLayer: outsideLayer,
orientation: LayerEntranceOrientation.down,
insidePriority: RenderPriority.ballOnSpaceshipRamp,
outsidePriority: outsidePriority,
insideZIndex: RenderPriority.ballOnSpaceshipRamp,
outsideZIndex: outsidePriority,
);
final double _rotation;

@ -10,11 +10,11 @@ import '../../helpers/helpers.dart';
class TestLayerSensor extends LayerSensor {
TestLayerSensor({
required LayerEntranceOrientation orientation,
required int insidePriority,
required int insideZIndex,
required Layer insideLayer,
}) : super(
insideLayer: insideLayer,
insidePriority: insidePriority,
insideZIndex: insideZIndex,
orientation: orientation,
);
@ -33,7 +33,7 @@ void main() {
(game) async {
final layerSensor = TestLayerSensor(
orientation: LayerEntranceOrientation.down,
insidePriority: insidePriority,
insideZIndex: insidePriority,
insideLayer: Layer.spaceshipEntranceRamp,
);
await game.ensureAdd(layerSensor);
@ -48,7 +48,7 @@ void main() {
(game) async {
final layerSensor = TestLayerSensor(
orientation: LayerEntranceOrientation.down,
insidePriority: insidePriority,
insideZIndex: insidePriority,
insideLayer: Layer.spaceshipEntranceRamp,
);
await game.ensureAdd(layerSensor);
@ -66,7 +66,7 @@ void main() {
(game) async {
final layerSensor = TestLayerSensor(
orientation: LayerEntranceOrientation.down,
insidePriority: insidePriority,
insideZIndex: insidePriority,
insideLayer: pathwayLayer,
)..layer = openingLayer;
await game.ensureAdd(layerSensor);
@ -80,7 +80,7 @@ void main() {
(game) async {
final layerSensor = TestLayerSensor(
orientation: LayerEntranceOrientation.down,
insidePriority: insidePriority,
insideZIndex: insidePriority,
insideLayer: pathwayLayer,
)..layer = openingLayer;
await game.ensureAdd(layerSensor);
@ -95,7 +95,7 @@ void main() {
(game) async {
final layerSensor = TestLayerSensor(
orientation: LayerEntranceOrientation.down,
insidePriority: insidePriority,
insideZIndex: insidePriority,
insideLayer: pathwayLayer,
)..layer = openingLayer;
await game.ensureAdd(layerSensor);
@ -111,10 +111,14 @@ void main() {
group('beginContact', () {
late Ball ball;
late Body body;
late int insideZIndex;
late Layer insideLayer;
setUp(() {
ball = MockBall();
body = MockBody();
insideZIndex = 1;
insideLayer = Layer.spaceshipEntranceRamp;
when(() => ball.body).thenReturn(body);
when(() => ball.priority).thenReturn(1);
@ -127,23 +131,21 @@ void main() {
(game) async {
final sensor = TestLayerSensor(
orientation: LayerEntranceOrientation.down,
insidePriority: insidePriority,
insideLayer: Layer.spaceshipEntranceRamp,
insideZIndex: insidePriority,
insideLayer: insideLayer,
)..initialPosition = Vector2(0, 10);
when(() => body.linearVelocity).thenReturn(Vector2(0, -1));
sensor.beginContact(ball, MockContact());
verify(() => ball.layer = sensor.insideLayer).called(1);
verify(() => ball.priority = sensor.insidePriority).called(1);
verify(ball.reorderChildren).called(1);
verify(() => ball.layer = insideLayer).called(1);
verify(() => ball.zIndex = insideZIndex).called(1);
when(() => ball.layer).thenReturn(sensor.insideLayer);
when(() => ball.layer).thenReturn(insideLayer);
sensor.beginContact(ball, MockContact());
verify(() => ball.layer = Layer.board);
verify(() => ball.priority = RenderPriority.ballOnBoard).called(1);
verify(ball.reorderChildren).called(1);
verify(() => ball.zIndex = RenderPriority.ballOnBoard).called(1);
});
flameTester.test(
@ -152,23 +154,21 @@ void main() {
(game) async {
final sensor = TestLayerSensor(
orientation: LayerEntranceOrientation.up,
insidePriority: insidePriority,
insideLayer: Layer.spaceshipEntranceRamp,
insideZIndex: insidePriority,
insideLayer: insideLayer,
)..initialPosition = Vector2(0, 10);
when(() => body.linearVelocity).thenReturn(Vector2(0, 1));
sensor.beginContact(ball, MockContact());
verify(() => ball.layer = sensor.insideLayer).called(1);
verify(() => ball.priority = sensor.insidePriority).called(1);
verify(ball.reorderChildren).called(1);
verify(() => ball.layer = insideLayer).called(1);
verify(() => ball.zIndex = insidePriority).called(1);
when(() => ball.layer).thenReturn(sensor.insideLayer);
when(() => ball.layer).thenReturn(insideLayer);
sensor.beginContact(ball, MockContact());
verify(() => ball.layer = Layer.board);
verify(() => ball.priority = RenderPriority.ballOnBoard).called(1);
verify(ball.reorderChildren).called(1);
verify(() => ball.zIndex = RenderPriority.ballOnBoard).called(1);
});
});
}

@ -1,6 +1,5 @@
// ignore_for_file: cascade_invocations
import 'dart:math';
import 'dart:typed_data';
import 'dart:ui';

Loading…
Cancel
Save