refactor: initial position to ramps and cleaned ramp callbacks

pull/40/head
RuiAlonso 4 years ago
parent 1b0775cd51
commit 99a394ca18

@ -34,25 +34,25 @@ class JetpackRamp extends Component with HasGameRef<PinballGame> {
angle: _angle, angle: _angle,
rotation: _rotation, rotation: _rotation,
layer: Layer.jetpack, layer: Layer.jetpack,
), )..initialPosition = position,
); );
await add( await add(
JetpackRampOpening( JetpackRampOpening(
position: position + Vector2(-11, 1),
orientation: RampOrientation.down, orientation: RampOrientation.down,
rotation: radians(15), rotation: radians(15),
), )..initialPosition = position + Vector2(-11, 1),
); );
await add( await add(
JetpackRampOpening( JetpackRampOpening(
position: position + Vector2(20.5, 3.4),
orientation: RampOrientation.down, orientation: RampOrientation.down,
rotation: radians(-9), rotation: radians(-9),
), )..initialPosition = position + Vector2(20.5, 3.4),
); );
gameRef.addContactCallback(JetpackRampOpeningBallContactCallback()); gameRef.addContactCallback(
RampOpeningBallContactCallback<JetpackRampOpening>(),
);
} }
} }
@ -63,13 +63,11 @@ class JetpackRamp extends Component with HasGameRef<PinballGame> {
class JetpackRampOpening extends RampOpening { class JetpackRampOpening extends RampOpening {
/// {@macro jetpack_ramp_opening} /// {@macro jetpack_ramp_opening}
JetpackRampOpening({ JetpackRampOpening({
required Vector2 position,
required RampOrientation orientation, required RampOrientation orientation,
double rotation = 0, double rotation = 0,
}) : _rotation = rotation, }) : _rotation = rotation,
_orientation = orientation, _orientation = orientation,
super( super(
position: position,
pathwayLayer: Layer.jetpack, pathwayLayer: Layer.jetpack,
openingLayer: Layer.opening, openingLayer: Layer.opening,
); );
@ -97,19 +95,3 @@ class JetpackRampOpening extends RampOpening {
Vector2(_size / 2, -.1)..rotate(_rotation), 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<JetpackRampOpening> {
/// {@macro jetpack_ramp_opening_ball_contact_callback}
JetpackRampOpeningBallContactCallback() : super();
/// Collection of balls inside [JetpackRamp].
final _ballsInsideJetpack = <Ball>{};
@override
Set get ballsInside => _ballsInsideJetpack;
}

@ -33,7 +33,7 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
end: Vector2(0, 600), end: Vector2(0, 600),
width: 80, width: 80,
layer: Layer.launcher, layer: Layer.launcher,
), )..initialPosition = position,
); );
await add( await add(
@ -44,23 +44,23 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
angle: _angle, angle: _angle,
width: _width, width: _width,
layer: Layer.launcher, layer: Layer.launcher,
), )..initialPosition = position + Vector2(-28.8, -6),
); );
await add( await add(
LauncherRampOpening( LauncherRampOpening(
position: position + Vector2(-46.5, -8.5),
orientation: RampOrientation.down, orientation: RampOrientation.down,
rotation: radians(13), rotation: radians(13),
), )..initialPosition = position + Vector2(-46.5, -8.5),
); );
await add( await add(
LauncherRampOpening( LauncherRampOpening(
position: position + Vector2(4, 0),
orientation: RampOrientation.down, orientation: RampOrientation.down,
), )..initialPosition = position + Vector2(4, 0),
); );
gameRef.addContactCallback(LauncherRampOpeningBallContactCallback()); gameRef.addContactCallback(
RampOpeningBallContactCallback<LauncherRampOpening>(),
);
} }
} }
@ -71,13 +71,11 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
class LauncherRampOpening extends RampOpening { class LauncherRampOpening extends RampOpening {
/// {@macro launcher_ramp_opening} /// {@macro launcher_ramp_opening}
LauncherRampOpening({ LauncherRampOpening({
required Vector2 position,
double rotation = 0, double rotation = 0,
required RampOrientation orientation, required RampOrientation orientation,
}) : _rotation = rotation, }) : _rotation = rotation,
_orientation = orientation, _orientation = orientation,
super( super(
position: position,
pathwayLayer: Layer.launcher, pathwayLayer: Layer.launcher,
openingLayer: Layer.opening, openingLayer: Layer.opening,
); );
@ -105,19 +103,3 @@ class LauncherRampOpening extends RampOpening {
Vector2(_size / 2, -.1)..rotate(_rotation), 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<LauncherRampOpening> {
/// {@macro launcher_ramp_opening_ball_contact_callback}
LauncherRampOpeningBallContactCallback() : super();
/// Collection of balls inside [LauncherRamp].
final _ballsInsideLauncher = <Ball>{};
@override
Set get ballsInside => _ballsInsideLauncher;
}

@ -79,27 +79,24 @@ enum RampOrientation {
/// through this opening. By default openings are [Layer.board] that /// through this opening. By default openings are [Layer.board] that
/// means opening are at ground level, not over board. /// means opening are at ground level, not over board.
/// {@endtemplate} /// {@endtemplate}
abstract class RampOpening extends BodyComponent { abstract class RampOpening extends BodyComponent with InitialPosition {
/// {@macro ramp_opening} /// {@macro ramp_opening}
RampOpening({ RampOpening({
required Vector2 position,
required Layer pathwayLayer, required Layer pathwayLayer,
Layer? openingLayer, Layer? openingLayer,
}) : _position = position, }) : _pathwayLayer = pathwayLayer,
_pathwayLayer = pathwayLayer,
_openingLayer = openingLayer ?? Layer.board; _openingLayer = openingLayer ?? Layer.board;
final Vector2 _position;
final Layer _openingLayer; final Layer _openingLayer;
final Layer _pathwayLayer; final Layer _pathwayLayer;
/// Mask of category bits for collision with [RampOpening] /// Mask of category bits for collision with [RampOpening].
Layer get openingLayer => _openingLayer; Layer get openingLayer => _openingLayer;
/// Mask of category bits for collision inside [Pathway] /// Mask of category bits for collision inside [Pathway].
Layer get pathwayLayer => _pathwayLayer; Layer get pathwayLayer => _pathwayLayer;
/// The [Shape] of the [RampOpening] /// The [Shape] of the [RampOpening].
Shape get shape; Shape get shape;
/// Orientation of the [RampOpening] entrance/exit /// Orientation of the [RampOpening] entrance/exit
@ -115,7 +112,7 @@ abstract class RampOpening extends BodyComponent {
final bodyDef = BodyDef() final bodyDef = BodyDef()
..userData = this ..userData = this
..position = _position ..position = initialPosition
..type = BodyType.static; ..type = BodyType.static;
return world.createBody(bodyDef)..createFixture(fixtureDef); 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, /// Modifies [Ball]'s maskBits while it is inside the ramp. When [Ball] exits,
/// sets maskBits to collide with all elements. /// sets maskBits to collide with all elements.
/// {@endtemplate} /// {@endtemplate}
abstract class RampOpeningBallContactCallback<Opening extends RampOpening> class RampOpeningBallContactCallback<Opening extends RampOpening>
extends ContactCallback<Ball, Opening> { extends ContactCallback<Ball, Opening> {
/// Collection of balls inside ramp pathway. /// Collection of balls inside ramp pathway.
Set get ballsInside; final _ballsInside = <Ball>{};
@override @override
void begin( void begin(
@ -141,12 +138,12 @@ abstract class RampOpeningBallContactCallback<Opening extends RampOpening>
Contact _, Contact _,
) { ) {
Layer layer; Layer layer;
if (!ballsInside.contains(ball)) { if (!_ballsInside.contains(ball)) {
layer = opening.pathwayLayer; layer = opening.pathwayLayer;
ballsInside.add(ball); _ballsInside.add(ball);
} else { } else {
layer = Layer.board; layer = Layer.board;
ballsInside.remove(ball); _ballsInside.remove(ball);
} }
ball.layer = layer; ball.layer = layer;
@ -158,12 +155,12 @@ abstract class RampOpeningBallContactCallback<Opening extends RampOpening>
switch (opening.orientation) { switch (opening.orientation) {
case RampOrientation.up: case RampOrientation.up:
if (ball.body.position.y > opening._position.y) { if (ball.body.position.y > opening.body.position.y) {
layer = Layer.board; layer = Layer.board;
} }
break; break;
case RampOrientation.down: case RampOrientation.down:
if (ball.body.position.y < opening._position.y) { if (ball.body.position.y < opening.body.position.y) {
layer = Layer.board; layer = Layer.board;
} }
break; break;

Loading…
Cancel
Save