refactor: ball layer and ramp addAll components

pull/40/head
RuiAlonso 4 years ago
parent 425e5292b9
commit c20c4a550a

@ -8,12 +8,9 @@ import 'package:pinball/game/game.dart';
/// {@endtemplate} /// {@endtemplate}
class Ball extends BodyComponent<PinballGame> with InitialPosition, Layered { class Ball extends BodyComponent<PinballGame> with InitialPosition, Layered {
/// {@macro ball} /// {@macro ball}
Ball({ Ball() {
Layer? layer, layer = Layer.board;
}) : _layer = layer ?? Layer.board; }
/// [Layer] of the board that the [Ball] will interact with.
final Layer _layer;
/// The size of the [Ball] /// The size of the [Ball]
final Vector2 size = Vector2.all(2); final Vector2 size = Vector2.all(2);
@ -41,9 +38,7 @@ class Ball extends BodyComponent<PinballGame> with InitialPosition, Layered {
Body createBody() { Body createBody() {
final shape = CircleShape()..radius = size.x / 2; final shape = CircleShape()..radius = size.x / 2;
final fixtureDef = FixtureDef(shape) final fixtureDef = FixtureDef(shape)..density = 1;
..density = 1
..filter.maskBits = _layer.maskBits;
final bodyDef = BodyDef() final bodyDef = BodyDef()
..position = initialPosition ..position = initialPosition

@ -23,36 +23,39 @@ class JetpackRamp extends Component with HasGameRef<PinballGame> {
/// The position of this [JetpackRamp] /// The position of this [JetpackRamp]
final Vector2 position; final Vector2 position;
static const _layer = Layer.jetpack;
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await add( gameRef.addContactCallback(
Pathway.arc( RampOpeningBallContactCallback<JetpackRampOpening>(),
);
final curvePath = Pathway.arc(
// TODO(ruialonso): Remove color when not needed.
// TODO(ruialonso): Use a bezier curve once control points are defined.
color: const Color.fromARGB(255, 8, 218, 241), color: const Color.fromARGB(255, 8, 218, 241),
center: position, center: position,
width: _width, width: _width,
radius: _radius, radius: _radius,
angle: _angle, angle: _angle,
rotation: _rotation, rotation: _rotation,
layer: Layer.jetpack, layer: _layer,
)..initialPosition = position, )..initialPosition = position;
); final leftOpening = JetpackRampOpening(
await add(
JetpackRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
rotation: radians(15), rotation: radians(15),
)..initialPosition = position + Vector2(-11, 1), )..initialPosition = position + Vector2(-11, 1);
); final rightOpening = JetpackRampOpening(
await add(
JetpackRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
rotation: radians(-9), rotation: radians(-9),
)..initialPosition = position + Vector2(20.5, 3.4), )..initialPosition = position + Vector2(20.5, 3.4);
);
gameRef.addContactCallback( await addAll([
RampOpeningBallContactCallback<JetpackRampOpening>(), curvePath,
); leftOpening,
rightOpening,
]);
} }
} }

@ -26,41 +26,39 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await add( gameRef.addContactCallback(
Pathway.straight( RampOpeningBallContactCallback<LauncherRampOpening>(),
);
final straightPath = Pathway.straight(
color: const Color.fromARGB(255, 34, 255, 0), color: const Color.fromARGB(255, 34, 255, 0),
start: Vector2(0, 0), start: Vector2(0, 0),
end: Vector2(0, 600), end: Vector2(0, 600),
width: 80, width: 80,
layer: Layer.launcher, layer: Layer.launcher,
)..initialPosition = position, )..initialPosition = position;
); final curvedPath = Pathway.arc(
await add(
Pathway.arc(
color: const Color.fromARGB(255, 251, 255, 0), color: const Color.fromARGB(255, 251, 255, 0),
center: position + Vector2(-28.8, -6), center: position + Vector2(-28.8, -6),
radius: _radius, radius: _radius,
angle: _angle, angle: _angle,
width: _width, width: _width,
layer: Layer.launcher, layer: Layer.launcher,
)..initialPosition = position + Vector2(-28.8, -6), )..initialPosition = position + Vector2(-28.8, -6);
); final leftOpening = LauncherRampOpening(
await add(
LauncherRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
rotation: radians(13), rotation: radians(13),
)..initialPosition = position + Vector2(-46.5, -8.5), )..initialPosition = position + Vector2(-46.5, -8.5);
); final rightOpening = LauncherRampOpening(
await add(
LauncherRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
)..initialPosition = position + Vector2(4, 0), )..initialPosition = position + Vector2(4, 0);
);
gameRef.addContactCallback( await addAll([
RampOpeningBallContactCallback<LauncherRampOpening>(), straightPath,
); curvedPath,
leftOpening,
rightOpening,
]);
} }
} }

@ -11,14 +11,24 @@ import 'package:pinball/game/game.dart';
/// bodies with a different bit value. /// bodies with a different bit value.
/// {@endtemplate} /// {@endtemplate}
mixin Layered<T extends Forge2DGame> on BodyComponent<T> { mixin Layered<T extends Forge2DGame> on BodyComponent<T> {
/// Sets [Filter] category and mask bits for the [BodyComponent] Layer _layer = Layer.board;
set layer(Layer layer) {
/// Sets [Filter] category and mask bits for the [BodyComponent].
Layer get layer => _layer;
set layer(Layer value) {
_layer = value;
if (!isLoaded) {
// TODO(erickzanardo): Use loaded.whenComplete once provided.
mounted.whenComplete(() => layer = value);
} else {
for (final fixture in body.fixtures) { for (final fixture in body.fixtures) {
fixture fixture
..filterData.categoryBits = layer.maskBits ..filterData.categoryBits = layer.maskBits
..filterData.maskBits = layer.maskBits; ..filterData.maskBits = layer.maskBits;
} }
} }
}
} }
/// Indicates the type of a layer. /// Indicates the type of a layer.

Loading…
Cancel
Save