refactor: launcher ramp

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

@ -2,7 +2,6 @@
import 'dart:math' as math; import 'dart:math' as math;
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:flutter/material.dart';

@ -1,62 +1,49 @@
// 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/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 launcher_ramp} /// A [Blueprint] which creates the [LauncherRamp].
/// The yellow left ramp, where the [Ball] goes through when launched from the class Launcher extends Forge2DBlueprint {
/// [Plunger]. /// Width between walls of the [Pathway].
/// {@endtemplate} static const width = 5.0;
class LauncherRamp extends Component with HasGameRef<PinballGame> {
/// {@macro launcher_ramp}
LauncherRamp({
required this.position,
});
/// The position of this [LauncherRamp]. /// Size for the radius of the external wall [Pathway].
final Vector2 position; static const externalRadius = 16.3;
@override @override
Future<void> onLoad() async { void build(_) {
const layer = Layer.launcher; final position = Vector2(
PinballGame.boardBounds.right - 30,
PinballGame.boardBounds.bottom + 40,
);
gameRef.addContactCallback( addAllContactCallback([
RampOpeningBallContactCallback<_LauncherRampOpening>(), RampOpeningBallContactCallback<_LauncherRampOpening>(),
); ]);
final launcherRampRotation = final straightPath = LauncherStraightRamp()
-math.atan(18.6 / PinballGame.boardBounds.height); ..initialPosition = position + Vector2(1.7, 0)
..layer = Layer.launcher;
final straightPath = Pathway.straight( final curvedPath = LauncherCurveRamp()
color: const Color.fromARGB(255, 34, 255, 0), ..initialPosition = position + Vector2(-12, 59.3)
start: position + Vector2(-1.2, 10), ..layer = Layer.launcher;
end: position + Vector2(-1.2, 117),
width: 5,
rotation: launcherRampRotation,
)
..initialPosition = position
..layer = layer;
final curvedPath = Pathway.arc(
color: const Color.fromARGB(255, 251, 255, 0),
center: position + Vector2(-2.8, 87.2),
radius: 16.3,
angle: math.pi / 2,
width: 5,
rotation: 3 * math.pi / 2,
)..layer = layer;
final leftOpening = _LauncherRampOpening(rotation: math.pi / 2) final leftOpening = _LauncherRampOpening(rotation: math.pi / 2)
..initialPosition = position + Vector2(-11.8, 66.3) ..initialPosition = position + Vector2(-11.6, 66.3)
..layer = Layer.opening; ..layer = Layer.opening;
final rightOpening = _LauncherRampOpening(rotation: 0) final rightOpening = _LauncherRampOpening(rotation: 0)
..initialPosition = position + Vector2(-4.9, 59.4) ..initialPosition = position + Vector2(-4.9, 59.4)
..layer = Layer.opening; ..layer = Layer.opening;
await addAll([ addAll([
straightPath, straightPath,
curvedPath, curvedPath,
leftOpening, leftOpening,
@ -65,6 +52,107 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
} }
} }
/// {@template launcher_straight_ramp}
/// The green left ramp, where the [Ball] goes through when launched from the
/// [Plunger].
/// {@endtemplate}
class LauncherStraightRamp extends BodyComponent with InitialPosition, Layered {
/// {@macro launcher_straight_ramp}
LauncherStraightRamp() : super(priority: 2) {
layer = Layer.launcher;
paint = Paint()
..color = const Color.fromARGB(255, 34, 255, 0)
..style = PaintingStyle.stroke;
}
List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
final launcherRampRotation =
-math.atan(18.6 / PinballGame.boardBounds.height);
final startPosition = initialPosition + Vector2(0, 3);
final endPosition = initialPosition + Vector2(0, 117);
final externalStraightShape = EdgeShape()
..set(
startPosition..rotate(launcherRampRotation),
endPosition..rotate(launcherRampRotation),
);
final externalStraightFixtureDef = FixtureDef(externalStraightShape);
fixturesDef.add(externalStraightFixtureDef);
final internalStraightShape = EdgeShape()
..set(
startPosition - Vector2(Launcher.width, 0),
endPosition - Vector2(Launcher.width, 0),
);
final internalStraightFixtureDef = FixtureDef(internalStraightShape);
fixturesDef.add(internalStraightFixtureDef);
return fixturesDef;
}
@override
Body createBody() {
final bodyDef = BodyDef()
..userData = this
..position = initialPosition;
final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture);
return body;
}
}
/// {@template launcher_curve_ramp}
/// The yellow left ramp, where the [Ball] goes through when launched from the
/// [Plunger].
/// {@endtemplate}
class LauncherCurveRamp extends BodyComponent with InitialPosition, Layered {
/// {@macro launcher_curve_ramp}
LauncherCurveRamp() : super(priority: 2) {
layer = Layer.launcher;
paint = Paint()
..color = const Color.fromARGB(255, 251, 255, 0)
..style = PaintingStyle.stroke;
}
List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
final externalCurveShape = ArcShape(
center: initialPosition,
arcRadius: Launcher.externalRadius,
angle: math.pi / 2,
rotation: 3 * math.pi / 2,
);
final externalCurveFixtureDef = FixtureDef(externalCurveShape);
fixturesDef.add(externalCurveFixtureDef);
final internalCurveShape = externalCurveShape.copyWith(
arcRadius: Launcher.externalRadius - Launcher.width,
);
final internalCurveFixtureDef = FixtureDef(internalCurveShape);
fixturesDef.add(internalCurveFixtureDef);
return fixturesDef;
}
@override
Body createBody() {
final bodyDef = BodyDef()
..userData = this
..position = initialPosition;
final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture);
return body;
}
}
/// {@template launcher_ramp_opening} /// {@template launcher_ramp_opening}
/// [RampOpening] with [Layer.launcher] to filter [Ball]s collisions /// [RampOpening] with [Layer.launcher] to filter [Ball]s collisions
/// inside [LauncherRamp]. /// inside [LauncherRamp].
@ -81,9 +169,7 @@ class _LauncherRampOpening extends RampOpening {
final double _rotation; final double _rotation;
// TODO(ruialonso): Avoid magic number 3, should be propotional to static final Vector2 _size = Vector2(Launcher.width / 3, .1);
// [JetpackRamp].
static final Vector2 _size = Vector2(3, .1);
@override @override
Shape get shape => PolygonShape() Shape get shape => PolygonShape()

@ -83,17 +83,7 @@ class PinballGame extends Forge2DGame
Future<void> _addPaths() async { Future<void> _addPaths() async {
unawaited(addFromBlueprint(Jetpack())); unawaited(addFromBlueprint(Jetpack()));
unawaited(addFromBlueprint(Launcher()));
final launcherRamp = LauncherRamp(
position: Vector2(
PinballGame.boardBounds.right - 30,
PinballGame.boardBounds.bottom + 40,
),
);
await addAll([
launcherRamp,
]);
} }
void spawnBall() { void spawnBall() {

Loading…
Cancel
Save