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,
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<JetpackRampOpening>(),
);
}
}
@ -63,13 +63,11 @@ class JetpackRamp extends Component with HasGameRef<PinballGame> {
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<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),
width: 80,
layer: Layer.launcher,
),
)..initialPosition = position,
);
await add(
@ -44,23 +44,23 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
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<LauncherRampOpening>(),
);
}
}
@ -71,13 +71,11 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
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<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
/// 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<Opening extends RampOpening>
class RampOpeningBallContactCallback<Opening extends RampOpening>
extends ContactCallback<Ball, Opening> {
/// Collection of balls inside ramp pathway.
Set get ballsInside;
final _ballsInside = <Ball>{};
@override
void begin(
@ -141,12 +138,12 @@ abstract class RampOpeningBallContactCallback<Opening extends RampOpening>
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<Opening extends RampOpening>
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;

Loading…
Cancel
Save