From 99a394ca1845b20b0cefb5f399d18a12efeb801d Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Thu, 17 Mar 2022 10:49:31 +0100 Subject: [PATCH] refactor: initial position to ramps and cleaned ramp callbacks --- lib/game/components/jetpack_ramp.dart | 30 +++++------------------- lib/game/components/launcher_ramp.dart | 32 ++++++-------------------- lib/game/components/layer.dart | 29 +++++++++++------------ 3 files changed, 26 insertions(+), 65 deletions(-) diff --git a/lib/game/components/jetpack_ramp.dart b/lib/game/components/jetpack_ramp.dart index 88a8ec6e..0180bb64 100644 --- a/lib/game/components/jetpack_ramp.dart +++ b/lib/game/components/jetpack_ramp.dart @@ -34,25 +34,25 @@ class JetpackRamp extends Component with HasGameRef { angle: _angle, rotation: _rotation, layer: Layer.jetpack, - ), + )..initialPosition = position, ); await add( JetpackRampOpening( - position: position + Vector2(-11, 1), orientation: RampOrientation.down, rotation: radians(15), - ), + )..initialPosition = position + Vector2(-11, 1), ); await add( JetpackRampOpening( - position: position + Vector2(20.5, 3.4), orientation: RampOrientation.down, rotation: radians(-9), - ), + )..initialPosition = position + Vector2(20.5, 3.4), ); - gameRef.addContactCallback(JetpackRampOpeningBallContactCallback()); + gameRef.addContactCallback( + RampOpeningBallContactCallback(), + ); } } @@ -63,13 +63,11 @@ class JetpackRamp extends Component with HasGameRef { class JetpackRampOpening extends RampOpening { /// {@macro jetpack_ramp_opening} JetpackRampOpening({ - required Vector2 position, required RampOrientation orientation, double rotation = 0, }) : _rotation = rotation, _orientation = orientation, super( - position: position, pathwayLayer: Layer.jetpack, openingLayer: Layer.opening, ); @@ -97,19 +95,3 @@ class JetpackRampOpening extends RampOpening { Vector2(_size / 2, -.1)..rotate(_rotation), ]); } - -/// {@template jetpack_ramp_opening_ball_contact_callback} -/// Detects when a [Ball] enters or exits the [JetpackRamp] through a -/// [JetpackRampOpening]. -/// {@endtemplate} -class JetpackRampOpeningBallContactCallback - extends RampOpeningBallContactCallback { - /// {@macro jetpack_ramp_opening_ball_contact_callback} - JetpackRampOpeningBallContactCallback() : super(); - - /// Collection of balls inside [JetpackRamp]. - final _ballsInsideJetpack = {}; - - @override - Set get ballsInside => _ballsInsideJetpack; -} diff --git a/lib/game/components/launcher_ramp.dart b/lib/game/components/launcher_ramp.dart index 94a768b3..7969617c 100644 --- a/lib/game/components/launcher_ramp.dart +++ b/lib/game/components/launcher_ramp.dart @@ -33,7 +33,7 @@ class LauncherRamp extends Component with HasGameRef { end: Vector2(0, 600), width: 80, layer: Layer.launcher, - ), + )..initialPosition = position, ); await add( @@ -44,23 +44,23 @@ class LauncherRamp extends Component with HasGameRef { angle: _angle, width: _width, layer: Layer.launcher, - ), + )..initialPosition = position + Vector2(-28.8, -6), ); await add( LauncherRampOpening( - position: position + Vector2(-46.5, -8.5), orientation: RampOrientation.down, rotation: radians(13), - ), + )..initialPosition = position + Vector2(-46.5, -8.5), ); await add( LauncherRampOpening( - position: position + Vector2(4, 0), orientation: RampOrientation.down, - ), + )..initialPosition = position + Vector2(4, 0), ); - gameRef.addContactCallback(LauncherRampOpeningBallContactCallback()); + gameRef.addContactCallback( + RampOpeningBallContactCallback(), + ); } } @@ -71,13 +71,11 @@ class LauncherRamp extends Component with HasGameRef { class LauncherRampOpening extends RampOpening { /// {@macro launcher_ramp_opening} LauncherRampOpening({ - required Vector2 position, double rotation = 0, required RampOrientation orientation, }) : _rotation = rotation, _orientation = orientation, super( - position: position, pathwayLayer: Layer.launcher, openingLayer: Layer.opening, ); @@ -105,19 +103,3 @@ class LauncherRampOpening extends RampOpening { Vector2(_size / 2, -.1)..rotate(_rotation), ]); } - -/// {@template launcher_ramp_opening_ball_contact_callback} -/// Detects when a [Ball] enters or exits the [LauncherRamp] through a -/// [LauncherRampOpening]. -/// {@endtemplate} -class LauncherRampOpeningBallContactCallback - extends RampOpeningBallContactCallback { - /// {@macro launcher_ramp_opening_ball_contact_callback} - LauncherRampOpeningBallContactCallback() : super(); - - /// Collection of balls inside [LauncherRamp]. - final _ballsInsideLauncher = {}; - - @override - Set get ballsInside => _ballsInsideLauncher; -} diff --git a/lib/game/components/layer.dart b/lib/game/components/layer.dart index 3a84ea1a..108daf70 100644 --- a/lib/game/components/layer.dart +++ b/lib/game/components/layer.dart @@ -79,27 +79,24 @@ enum RampOrientation { /// through this opening. By default openings are [Layer.board] that /// means opening are at ground level, not over board. /// {@endtemplate} -abstract class RampOpening extends BodyComponent { +abstract class RampOpening extends BodyComponent with InitialPosition { /// {@macro ramp_opening} RampOpening({ - required Vector2 position, required Layer pathwayLayer, Layer? openingLayer, - }) : _position = position, - _pathwayLayer = pathwayLayer, + }) : _pathwayLayer = pathwayLayer, _openingLayer = openingLayer ?? Layer.board; - final Vector2 _position; final Layer _openingLayer; final Layer _pathwayLayer; - /// Mask of category bits for collision with [RampOpening] + /// Mask of category bits for collision with [RampOpening]. Layer get openingLayer => _openingLayer; - /// Mask of category bits for collision inside [Pathway] + /// Mask of category bits for collision inside [Pathway]. Layer get pathwayLayer => _pathwayLayer; - /// The [Shape] of the [RampOpening] + /// The [Shape] of the [RampOpening]. Shape get shape; /// Orientation of the [RampOpening] entrance/exit @@ -115,7 +112,7 @@ abstract class RampOpening extends BodyComponent { final bodyDef = BodyDef() ..userData = this - ..position = _position + ..position = initialPosition ..type = BodyType.static; return world.createBody(bodyDef)..createFixture(fixtureDef); @@ -129,10 +126,10 @@ abstract class RampOpening extends BodyComponent { /// Modifies [Ball]'s maskBits while it is inside the ramp. When [Ball] exits, /// sets maskBits to collide with all elements. /// {@endtemplate} -abstract class RampOpeningBallContactCallback +class RampOpeningBallContactCallback extends ContactCallback { /// Collection of balls inside ramp pathway. - Set get ballsInside; + final _ballsInside = {}; @override void begin( @@ -141,12 +138,12 @@ abstract class RampOpeningBallContactCallback Contact _, ) { Layer layer; - if (!ballsInside.contains(ball)) { + if (!_ballsInside.contains(ball)) { layer = opening.pathwayLayer; - ballsInside.add(ball); + _ballsInside.add(ball); } else { layer = Layer.board; - ballsInside.remove(ball); + _ballsInside.remove(ball); } ball.layer = layer; @@ -158,12 +155,12 @@ abstract class RampOpeningBallContactCallback switch (opening.orientation) { case RampOrientation.up: - if (ball.body.position.y > opening._position.y) { + if (ball.body.position.y > opening.body.position.y) { layer = Layer.board; } break; case RampOrientation.down: - if (ball.body.position.y < opening._position.y) { + if (ball.body.position.y < opening.body.position.y) { layer = Layer.board; } break;