refactor: changed jetpack to shapes

pull/87/head
RuiAlonso 4 years ago
parent a8e43386e8
commit a701d99e9a

@ -1,63 +1,83 @@
// ignore_for_file: public_member_api_docs
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
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} /// {@template jetpack_ramp}
/// Represents the upper left blue ramp of the [Board]. /// Represents the upper left blue ramp of the [Board].
/// {@endtemplate} /// {@endtemplate}
class JetpackRamp extends Component with HasGameRef<PinballGame> { class Jetpack extends Forge2DBlueprint {
/// {@macro jetpack_ramp}
JetpackRamp({
required this.position,
});
/// The position of this [JetpackRamp].
final Vector2 position;
@override @override
Future<void> onLoad() async { void build(_) {
const layer = Layer.jetpack; final position = Vector2(
PinballGame.boardBounds.left + 40.5,
gameRef.addContactCallback( PinballGame.boardBounds.top - 31.5,
RampOpeningBallContactCallback<_JetpackRampOpening>(),
); );
final curvePath = Pathway.arc( addAllContactCallback([
// TODO(ruialonso): Remove color when not needed. RampOpeningBallContactCallback<_JetpackRampOpening>(),
// TODO(ruialonso): Use a bezier curve once control points are defined. ]);
color: const Color.fromARGB(255, 8, 218, 241),
center: position,
width: 5,
radius: 18,
angle: math.pi,
rotation: math.pi,
)..layer = layer;
final leftOpening = _JetpackRampOpening( final _leftOpening = _JetpackRampOpening(
outsideLayer: Layer.spaceship, outsideLayer: Layer.spaceship,
rotation: math.pi, rotation: math.pi,
) )
..initialPosition = position + Vector2(-2.5, -20.2) ..initialPosition = position + Vector2(-2.5, -20.2)
..layer = Layer.jetpack; ..layer = Layer.jetpack;
final rightOpening = _JetpackRampOpening( final _curve = JetpackRamp()
..initialPosition = position
..layer = Layer.jetpack;
final _rightOpening = _JetpackRampOpening(
rotation: math.pi, rotation: math.pi,
) )
..initialPosition = position + Vector2(12.9, -20.2) ..initialPosition = position + Vector2(12.9, -20.2)
..layer = Layer.opening; ..layer = Layer.opening;
await addAll([ addAll([
curvePath, _leftOpening,
leftOpening, _curve,
rightOpening, _rightOpening,
]); ]);
} }
} }
class JetpackRamp extends BodyComponent with InitialPosition, Layered {
JetpackRamp() : super(priority: 2) {
layer = Layer.jetpack;
paint = Paint()
..color = const Color.fromARGB(255, 8, 218, 241)
..style = PaintingStyle.stroke;
}
@override
Body createBody() {
final curveShape = ArcShape(
center: initialPosition,
arcRadius: 18,
angle: math.pi,
rotation: math.pi,
);
final bodyDef = BodyDef()
..userData = this
..position = initialPosition;
return world.createBody(bodyDef)
..createFixture(
FixtureDef(curveShape),
);
}
}
/// {@template jetpack_ramp_opening} /// {@template jetpack_ramp_opening}
/// [RampOpening] with [Layer.jetpack] to filter [Ball] collisions /// [RampOpening] with [Layer.jetpack] to filter [Ball] collisions
/// inside [JetpackRamp]. /// inside [JetpackRamp].

@ -82,12 +82,8 @@ class PinballGame extends Forge2DGame
} }
Future<void> _addPaths() async { Future<void> _addPaths() async {
final jetpackRamp = JetpackRamp( unawaited(addFromBlueprint(Jetpack()));
position: Vector2(
PinballGame.boardBounds.left + 40.5,
PinballGame.boardBounds.top - 31.5,
),
);
final launcherRamp = LauncherRamp( final launcherRamp = LauncherRamp(
position: Vector2( position: Vector2(
PinballGame.boardBounds.right - 30, PinballGame.boardBounds.right - 30,
@ -96,7 +92,6 @@ class PinballGame extends Forge2DGame
); );
await addAll([ await addAll([
jetpackRamp,
launcherRamp, launcherRamp,
]); ]);
} }

@ -1,3 +1,4 @@
export 'ball.dart'; export 'ball.dart';
export 'initial_position.dart'; export 'initial_position.dart';
export 'layer.dart'; export 'layer.dart';
export 'shapes/shapes.dart';

Loading…
Cancel
Save