refactor: ball priority (#183)

* refactor: ball priority

* refactor: ramp opening and score text
pull/186/head
Allison Ryan 3 years ago committed by GitHub
parent 64f3bbea5e
commit bc53fb64d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,6 +18,7 @@ class ControlledBall extends Ball with Controls<BallController> {
required PinballTheme theme, required PinballTheme theme,
}) : super(baseColor: theme.characterTheme.ballColor) { }) : super(baseColor: theme.characterTheme.ballColor) {
controller = BallController(this); controller = BallController(this);
priority = Ball.launchRampPriority;
} }
/// {@template bonus_ball} /// {@template bonus_ball}
@ -29,11 +30,13 @@ class ControlledBall extends Ball with Controls<BallController> {
required PinballTheme theme, required PinballTheme theme,
}) : super(baseColor: theme.characterTheme.ballColor) { }) : super(baseColor: theme.characterTheme.ballColor) {
controller = BallController(this); controller = BallController(this);
priority = Ball.boardPriority;
} }
/// [Ball] used in [DebugPinballGame]. /// [Ball] used in [DebugPinballGame].
ControlledBall.debug() : super(baseColor: const Color(0xFFFF0000)) { ControlledBall.debug() : super(baseColor: const Color(0xFFFF0000)) {
controller = DebugBallController(this); controller = DebugBallController(this);
priority = Ball.boardPriority;
} }
} }

@ -22,6 +22,21 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
layer = Layer.board; layer = Layer.board;
} }
/// Render priority for the [Ball] while it's on the board.
static const int boardPriority = 0;
/// Render priority for the [Ball] while it's on the [SpaceshipRamp].
static const int spaceshipRampPriority = 4;
/// Render priority for the [Ball] while it's on the [Spaceship].
static const int spaceshipPriority = 4;
/// Render priority for the [Ball] while it's on the [SpaceshipRail].
static const int spaceshipRailPriority = 2;
/// Render priority for the [Ball] while it's on the [LaunchRamp].
static const int launchRampPriority = 0;
/// The size of the [Ball]. /// The size of the [Ball].
static final Vector2 size = Vector2.all(4.13); static final Vector2 size = Vector2.all(4.13);

@ -11,9 +11,6 @@ import 'package:pinball_components/pinball_components.dart';
/// [_LaunchRampForegroundRailing]. /// [_LaunchRampForegroundRailing].
/// {@endtemplate} /// {@endtemplate}
class LaunchRamp extends Forge2DBlueprint { class LaunchRamp extends Forge2DBlueprint {
/// Base priority for [Ball] while inside [LaunchRamp].
static const ballPriorityInsideRamp = 0;
@override @override
void build(_) { void build(_) {
addAllContactCallback([ addAllContactCallback([
@ -45,7 +42,7 @@ class _LaunchRampBase extends BodyComponent with InitialPosition, Layered {
/// {@macro launch_ramp_base} /// {@macro launch_ramp_base}
_LaunchRampBase() _LaunchRampBase()
: super( : super(
priority: LaunchRamp.ballPriorityInsideRamp - 1, priority: Ball.launchRampPriority - 1,
) { ) {
layer = Layer.launcher; layer = Layer.launcher;
} }
@ -151,7 +148,7 @@ class _LaunchRampForegroundRailing extends BodyComponent
/// {@macro launch_ramp_foreground_railing} /// {@macro launch_ramp_foreground_railing}
_LaunchRampForegroundRailing() _LaunchRampForegroundRailing()
: super( : super(
priority: LaunchRamp.ballPriorityInsideRamp + 1, priority: Ball.launchRampPriority + 1,
) { ) {
layer = Layer.launcher; layer = Layer.launcher;
} }
@ -236,7 +233,7 @@ class _LaunchRampExit extends RampOpening {
super( super(
insideLayer: Layer.launcher, insideLayer: Layer.launcher,
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: LaunchRamp.ballPriorityInsideRamp, insidePriority: Ball.launchRampPriority,
); );
final double _rotation; final double _rotation;

@ -29,13 +29,13 @@ abstract class RampOpening extends BodyComponent with InitialPosition, Layered {
RampOpening({ RampOpening({
required Layer insideLayer, required Layer insideLayer,
Layer? outsideLayer, Layer? outsideLayer,
int? insidePriority, required int insidePriority,
int? outsidePriority, int? outsidePriority,
required this.orientation, required this.orientation,
}) : _insideLayer = insideLayer, }) : _insideLayer = insideLayer,
_outsideLayer = outsideLayer ?? Layer.board, _outsideLayer = outsideLayer ?? Layer.board,
_insidePriority = insidePriority ?? 0, _insidePriority = insidePriority,
_outsidePriority = outsidePriority ?? 0 { _outsidePriority = outsidePriority ?? Ball.boardPriority {
layer = Layer.opening; layer = Layer.opening;
} }
final Layer _insideLayer; final Layer _insideLayer;

@ -18,7 +18,7 @@ class ScoreText extends TextComponent {
text: text, text: text,
position: position, position: position,
anchor: Anchor.center, anchor: Anchor.center,
priority: 100, priority: Ball.spaceshipRampPriority + 1,
); );
late final Effect _effect; late final Effect _effect;

@ -21,9 +21,6 @@ class Spaceship extends Forge2DBlueprint {
/// The [position] where the elements will be created /// The [position] where the elements will be created
final Vector2 position; final Vector2 position;
/// Base priority for wall while be on spaceship.
static const ballPriorityWhenOnSpaceship = 4;
@override @override
void build(_) { void build(_) {
addAllContactCallback([ addAllContactCallback([
@ -37,7 +34,7 @@ class Spaceship extends Forge2DBlueprint {
AndroidHead()..initialPosition = position, AndroidHead()..initialPosition = position,
SpaceshipHole( SpaceshipHole(
outsideLayer: Layer.spaceshipExitRail, outsideLayer: Layer.spaceshipExitRail,
outsidePriority: SpaceshipRail.ballPriorityInsideRail, outsidePriority: Ball.spaceshipRailPriority,
)..initialPosition = position - Vector2(5.2, 4.8), )..initialPosition = position - Vector2(5.2, 4.8),
SpaceshipHole()..initialPosition = position - Vector2(-7.2, 0.8), SpaceshipHole()..initialPosition = position - Vector2(-7.2, 0.8),
SpaceshipWall()..initialPosition = position, SpaceshipWall()..initialPosition = position,
@ -50,8 +47,7 @@ class Spaceship extends Forge2DBlueprint {
/// {@endtemplate} /// {@endtemplate}
class SpaceshipSaucer extends BodyComponent with InitialPosition, Layered { class SpaceshipSaucer extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_saucer} /// {@macro spaceship_saucer}
SpaceshipSaucer() SpaceshipSaucer() : super(priority: Ball.spaceshipPriority - 1) {
: super(priority: Spaceship.ballPriorityWhenOnSpaceship - 1) {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
@ -95,7 +91,7 @@ class SpaceshipSaucer extends BodyComponent with InitialPosition, Layered {
/// {@endtemplate} /// {@endtemplate}
class AndroidHead extends BodyComponent with InitialPosition, Layered { class AndroidHead extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_bridge} /// {@macro spaceship_bridge}
AndroidHead() : super(priority: Spaceship.ballPriorityWhenOnSpaceship + 1) { AndroidHead() : super(priority: Ball.spaceshipPriority + 1) {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
@ -157,7 +153,7 @@ class SpaceshipEntrance extends RampOpening {
: super( : super(
insideLayer: Layer.spaceship, insideLayer: Layer.spaceship,
orientation: RampOrientation.up, orientation: RampOrientation.up,
insidePriority: Spaceship.ballPriorityWhenOnSpaceship, insidePriority: Ball.spaceshipPriority,
) { ) {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
@ -242,7 +238,7 @@ class _SpaceshipWallShape extends ChainShape {
/// {@endtemplate} /// {@endtemplate}
class SpaceshipWall extends BodyComponent with InitialPosition, Layered { class SpaceshipWall extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_wall} /// {@macro spaceship_wall}
SpaceshipWall() : super(priority: Spaceship.ballPriorityWhenOnSpaceship + 1) { SpaceshipWall() : super(priority: Ball.spaceshipPriority + 1) {
layer = Layer.spaceship; layer = Layer.spaceship;
} }

@ -14,9 +14,6 @@ class SpaceshipRail extends Forge2DBlueprint {
/// {@macro spaceship_rail} /// {@macro spaceship_rail}
SpaceshipRail(); SpaceshipRail();
/// Base priority for [Ball] while inside [SpaceshipRail].
static const ballPriorityInsideRail = 2;
@override @override
void build(_) { void build(_) {
addAllContactCallback([ addAllContactCallback([
@ -45,7 +42,7 @@ class SpaceshipRail extends Forge2DBlueprint {
class _SpaceshipRailRamp extends BodyComponent with InitialPosition, Layered { class _SpaceshipRailRamp extends BodyComponent with InitialPosition, Layered {
_SpaceshipRailRamp() _SpaceshipRailRamp()
: super( : super(
priority: SpaceshipRail.ballPriorityInsideRail - 1, priority: Ball.spaceshipRailPriority - 1,
) { ) {
layer = Layer.spaceshipExitRail; layer = Layer.spaceshipExitRail;
} }
@ -161,8 +158,7 @@ class _SpaceshipRailRampSpriteComponent extends SpriteComponent
} }
class _SpaceshipRailForeground extends SpriteComponent with HasGameRef { class _SpaceshipRailForeground extends SpriteComponent with HasGameRef {
_SpaceshipRailForeground() _SpaceshipRailForeground() : super(priority: Ball.spaceshipRailPriority + 1);
: super(priority: SpaceshipRail.ballPriorityInsideRail + 1);
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
@ -182,7 +178,7 @@ class _SpaceshipRailForeground extends SpriteComponent with HasGameRef {
class _SpaceshipRailBase extends BodyComponent with InitialPosition, Layered { class _SpaceshipRailBase extends BodyComponent with InitialPosition, Layered {
_SpaceshipRailBase({required this.radius}) _SpaceshipRailBase({required this.radius})
: super( : super(
priority: SpaceshipRail.ballPriorityInsideRail + 1, priority: Ball.spaceshipRailPriority + 1,
) { ) {
renderBody = false; renderBody = false;
layer = Layer.board; layer = Layer.board;
@ -214,7 +210,7 @@ class SpaceshipRailExit extends RampOpening {
: super( : super(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insideLayer: Layer.spaceshipExitRail, insideLayer: Layer.spaceshipExitRail,
insidePriority: SpaceshipRail.ballPriorityInsideRail, insidePriority: Ball.spaceshipRailPriority,
) { ) {
renderBody = false; renderBody = false;
layer = Layer.spaceshipExitRail; layer = Layer.spaceshipExitRail;

@ -14,9 +14,6 @@ class SpaceshipRamp extends Forge2DBlueprint {
/// {@macro spaceship_ramp} /// {@macro spaceship_ramp}
SpaceshipRamp(); SpaceshipRamp();
/// Base priority for the [Ball] while inside the ramp.
static const int ballPriorityInsideRamp = 4;
@override @override
void build(_) { void build(_) {
addAllContactCallback([ addAllContactCallback([
@ -32,7 +29,7 @@ class SpaceshipRamp extends Forge2DBlueprint {
..layer = Layer.opening; ..layer = Layer.opening;
final leftOpening = _SpaceshipRampOpening( final leftOpening = _SpaceshipRampOpening(
outsideLayer: Layer.spaceship, outsideLayer: Layer.spaceship,
outsidePriority: Spaceship.ballPriorityWhenOnSpaceship, outsidePriority: Ball.spaceshipPriority,
rotation: math.pi, rotation: math.pi,
) )
..initialPosition = Vector2(-13.7, 18.6) ..initialPosition = Vector2(-13.7, 18.6)
@ -61,8 +58,7 @@ class SpaceshipRamp extends Forge2DBlueprint {
class _SpaceshipRampBackground extends BodyComponent class _SpaceshipRampBackground extends BodyComponent
with InitialPosition, Layered { with InitialPosition, Layered {
_SpaceshipRampBackground() _SpaceshipRampBackground() : super(priority: Ball.spaceshipRampPriority - 1) {
: super(priority: SpaceshipRamp.ballPriorityInsideRamp - 1) {
layer = Layer.spaceshipEntranceRamp; layer = Layer.spaceshipEntranceRamp;
} }
@ -174,7 +170,7 @@ class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent
class _SpaceshipRampForegroundRailing extends BodyComponent class _SpaceshipRampForegroundRailing extends BodyComponent
with InitialPosition, Layered { with InitialPosition, Layered {
_SpaceshipRampForegroundRailing() _SpaceshipRampForegroundRailing()
: super(priority: SpaceshipRamp.ballPriorityInsideRamp + 1) { : super(priority: Ball.spaceshipRampPriority + 1) {
layer = Layer.spaceshipEntranceRamp; layer = Layer.spaceshipEntranceRamp;
} }
@ -294,7 +290,7 @@ class _SpaceshipRampOpening extends RampOpening {
insideLayer: Layer.spaceshipEntranceRamp, insideLayer: Layer.spaceshipEntranceRamp,
outsideLayer: outsideLayer, outsideLayer: outsideLayer,
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: SpaceshipRamp.ballPriorityInsideRamp, insidePriority: Ball.spaceshipRampPriority,
outsidePriority: outsidePriority, outsidePriority: outsidePriority,
) { ) {
renderBody = false; renderBody = false;

@ -9,7 +9,7 @@ class LaunchRampGame extends BasicBallGame {
LaunchRampGame() LaunchRampGame()
: super( : super(
color: Colors.blue, color: Colors.blue,
ballPriority: LaunchRamp.ballPriorityInsideRamp, ballPriority: Ball.launchRampPriority,
ballLayer: Layer.launcher, ballLayer: Layer.launcher,
); );

@ -9,7 +9,7 @@ class SpaceshipRailGame extends BasicBallGame {
SpaceshipRailGame() SpaceshipRailGame()
: super( : super(
color: Colors.blue, color: Colors.blue,
ballPriority: SpaceshipRail.ballPriorityInsideRail, ballPriority: Ball.spaceshipRailPriority,
ballLayer: Layer.spaceshipExitRail, ballLayer: Layer.spaceshipExitRail,
); );

@ -9,7 +9,7 @@ class SpaceshipRampGame extends BasicBallGame {
SpaceshipRampGame() SpaceshipRampGame()
: super( : super(
color: Colors.blue, color: Colors.blue,
ballPriority: SpaceshipRamp.ballPriorityInsideRamp, ballPriority: Ball.spaceshipRampPriority,
ballLayer: Layer.spaceshipEntranceRamp, ballLayer: Layer.spaceshipEntranceRamp,
); );

@ -10,9 +10,11 @@ import '../../helpers/helpers.dart';
class TestRampOpening extends RampOpening { class TestRampOpening extends RampOpening {
TestRampOpening({ TestRampOpening({
required RampOrientation orientation, required RampOrientation orientation,
required int insidePriority,
required Layer pathwayLayer, required Layer pathwayLayer,
}) : super( }) : super(
insideLayer: pathwayLayer, insideLayer: pathwayLayer,
insidePriority: insidePriority,
orientation: orientation, orientation: orientation,
); );
@ -34,16 +36,15 @@ class TestRampOpeningBallContactCallback
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(TestGame.new); final flameTester = FlameTester(TestGame.new);
const insidePriority = 1;
group('RampOpening', () { group('RampOpening', () {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(TestGame.new);
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final ramp = TestRampOpening( final ramp = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: insidePriority,
pathwayLayer: Layer.spaceshipEntranceRamp, pathwayLayer: Layer.spaceshipEntranceRamp,
); );
await game.ready(); await game.ready();
@ -59,6 +60,7 @@ void main() {
(game) async { (game) async {
final ramp = TestRampOpening( final ramp = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: insidePriority,
pathwayLayer: Layer.spaceshipEntranceRamp, pathwayLayer: Layer.spaceshipEntranceRamp,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -76,6 +78,7 @@ void main() {
(game) async { (game) async {
final ramp = TestRampOpening( final ramp = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: insidePriority,
pathwayLayer: pathwayLayer, pathwayLayer: pathwayLayer,
)..layer = openingLayer; )..layer = openingLayer;
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -89,6 +92,7 @@ void main() {
(game) async { (game) async {
final ramp = TestRampOpening( final ramp = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: insidePriority,
pathwayLayer: pathwayLayer, pathwayLayer: pathwayLayer,
)..layer = openingLayer; )..layer = openingLayer;
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -103,6 +107,7 @@ void main() {
(game) async { (game) async {
final ramp = TestRampOpening( final ramp = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: insidePriority,
pathwayLayer: pathwayLayer, pathwayLayer: pathwayLayer,
)..layer = openingLayer; )..layer = openingLayer;
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -124,6 +129,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: insidePriority,
pathwayLayer: Layer.spaceshipEntranceRamp, pathwayLayer: Layer.spaceshipEntranceRamp,
); );
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();
@ -145,6 +151,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.up, orientation: RampOrientation.up,
insidePriority: insidePriority,
pathwayLayer: Layer.spaceshipEntranceRamp, pathwayLayer: Layer.spaceshipEntranceRamp,
); );
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();
@ -165,6 +172,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: insidePriority,
pathwayLayer: Layer.spaceshipEntranceRamp, pathwayLayer: Layer.spaceshipEntranceRamp,
)..initialPosition = Vector2(0, 10); )..initialPosition = Vector2(0, 10);
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();
@ -189,6 +197,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.up, orientation: RampOrientation.up,
insidePriority: insidePriority,
pathwayLayer: Layer.spaceshipEntranceRamp, pathwayLayer: Layer.spaceshipEntranceRamp,
)..initialPosition = Vector2(0, 10); )..initialPosition = Vector2(0, 10);
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();
@ -213,6 +222,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: insidePriority,
pathwayLayer: Layer.spaceshipEntranceRamp, pathwayLayer: Layer.spaceshipEntranceRamp,
)..initialPosition = Vector2(0, 10); )..initialPosition = Vector2(0, 10);
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();

Loading…
Cancel
Save