From 425e5292b9e1982ca44fe6719b16f5145ed2a077 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Thu, 17 Mar 2022 11:02:56 +0100 Subject: [PATCH] refactor: improved ramp contact callback --- lib/game/components/layer.dart | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/lib/game/components/layer.dart b/lib/game/components/layer.dart index 108daf70..ef27e01f 100644 --- a/lib/game/components/layer.dart +++ b/lib/game/components/layer.dart @@ -132,12 +132,8 @@ class RampOpeningBallContactCallback final _ballsInside = {}; @override - void begin( - Ball ball, - Opening opening, - Contact _, - ) { - Layer layer; + void begin(Ball ball, Opening opening, Contact _) { + late final Layer layer; if (!_ballsInside.contains(ball)) { layer = opening.pathwayLayer; _ballsInside.add(ball); @@ -150,24 +146,11 @@ class RampOpeningBallContactCallback } @override - void end(Ball ball, Opening opening, Contact contact) { - Layer? layer; - - switch (opening.orientation) { - case RampOrientation.up: - if (ball.body.position.y > opening.body.position.y) { - layer = Layer.board; - } - break; - case RampOrientation.down: - if (ball.body.position.y < opening.body.position.y) { - layer = Layer.board; - } - break; - } + void end(Ball ball, Opening opening, Contact _) { + final isBallOutsideOpening = opening.orientation == RampOrientation.up + ? ball.body.position.y > opening.body.position.y + : ball.body.position.y < opening.body.position.y; - if (layer != null) { - ball.layer = layer; - } + if (isBallOutsideOpening) ball.layer = Layer.board; } }