refactor: modified jetpack ramp to use shapes and blueprint

pull/87/head
RuiAlonso 4 years ago
parent a701d99e9a
commit 2629fdac41

@ -10,10 +10,14 @@ import 'package:pinball/flame/blueprint.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
/// {@template jetpack_ramp} /// A [Blueprint] which creates the [JetpackRamp].
/// Represents the upper left blue ramp of the [Board].
/// {@endtemplate}
class Jetpack extends Forge2DBlueprint { class Jetpack extends Forge2DBlueprint {
/// Width between walls of the [Pathway].
static const width = 5.0;
/// Size for the radius of the external wall [Pathway].
static const externalRadius = 18.0;
@override @override
void build(_) { void build(_) {
final position = Vector2( final position = Vector2(
@ -33,9 +37,8 @@ class Jetpack extends Forge2DBlueprint {
..layer = Layer.jetpack; ..layer = Layer.jetpack;
final _curve = JetpackRamp() final _curve = JetpackRamp()
..initialPosition = position ..initialPosition = position + Vector2(5, -20.2)
..layer = Layer.jetpack; ..layer = Layer.jetpack;
final _rightOpening = _JetpackRampOpening( final _rightOpening = _JetpackRampOpening(
rotation: math.pi, rotation: math.pi,
) )
@ -50,6 +53,9 @@ class Jetpack extends Forge2DBlueprint {
} }
} }
/// {@template jetpack_ramp}
/// Represents the upper left blue ramp of the [Board].
/// {@endtemplate}
class JetpackRamp extends BodyComponent with InitialPosition, Layered { class JetpackRamp extends BodyComponent with InitialPosition, Layered {
JetpackRamp() : super(priority: 2) { JetpackRamp() : super(priority: 2) {
layer = Layer.jetpack; layer = Layer.jetpack;
@ -58,23 +64,37 @@ class JetpackRamp extends BodyComponent with InitialPosition, Layered {
..style = PaintingStyle.stroke; ..style = PaintingStyle.stroke;
} }
@override List<FixtureDef> _createFixtureDefs() {
Body createBody() { final fixturesDef = <FixtureDef>[];
final curveShape = ArcShape(
final externalCurveShape = ArcShape(
center: initialPosition, center: initialPosition,
arcRadius: 18, arcRadius: Jetpack.externalRadius,
angle: math.pi, angle: math.pi,
rotation: math.pi, rotation: math.pi,
); );
final externalFixtureDef = FixtureDef(externalCurveShape);
fixturesDef.add(externalFixtureDef);
final internalCurveShape = externalCurveShape.copyWith(
arcRadius: Jetpack.externalRadius - Jetpack.width,
);
final internalFixtureDef = FixtureDef(internalCurveShape);
fixturesDef.add(internalFixtureDef);
return fixturesDef;
}
@override
Body createBody() {
final bodyDef = BodyDef() final bodyDef = BodyDef()
..userData = this ..userData = this
..position = initialPosition; ..position = initialPosition;
return world.createBody(bodyDef) final body = world.createBody(bodyDef);
..createFixture( _createFixtureDefs().forEach(body.createFixture);
FixtureDef(curveShape),
); return body;
} }
} }
@ -96,9 +116,7 @@ class _JetpackRampOpening extends RampOpening {
final double _rotation; final double _rotation;
// TODO(ruialonso): Avoid magic number 3, should be propotional to static final Vector2 _size = Vector2(Jetpack.width / 3, .1);
// [JetpackRamp].
static final Vector2 _size = Vector2(3, .1);
@override @override
Shape get shape => PolygonShape() Shape get shape => PolygonShape()

Loading…
Cancel
Save