refactor: refactored launcher ramp

pull/87/head
RuiAlonso 4 years ago
parent 58678017b7
commit a3e3b9c215

@ -9,7 +9,7 @@ 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';
/// A [Blueprint] which creates the launcher ramp. /// A [Blueprint] which creates the [LauncherRamp].
class Launcher extends Forge2DBlueprint { class Launcher extends Forge2DBlueprint {
/// Width between walls of the pathway. /// Width between walls of the pathway.
static const width = 5.0; static const width = 5.0;
@ -28,14 +28,10 @@ class Launcher extends Forge2DBlueprint {
RampOpeningBallContactCallback<_LauncherRampOpening>(), RampOpeningBallContactCallback<_LauncherRampOpening>(),
]); ]);
final straightPath = LauncherStraightRamp() final curvedPath = LauncherRamp()
..initialPosition = position + Vector2(1.7, 0) ..initialPosition = position + Vector2(1.7, 0)
..layer = Layer.launcher; ..layer = Layer.launcher;
final curvedPath = LauncherCurveRamp()
..initialPosition = position + Vector2(-12, 59.3)
..layer = Layer.launcher;
final leftOpening = _LauncherRampOpening(rotation: math.pi / 2) final leftOpening = _LauncherRampOpening(rotation: math.pi / 2)
..initialPosition = position + Vector2(-11.6, 66.3) ..initialPosition = position + Vector2(-11.6, 66.3)
..layer = Layer.opening; ..layer = Layer.opening;
@ -44,7 +40,6 @@ class Launcher extends Forge2DBlueprint {
..layer = Layer.opening; ..layer = Layer.opening;
addAll([ addAll([
straightPath,
curvedPath, curvedPath,
leftOpening, leftOpening,
rightOpening, rightOpening,
@ -52,16 +47,16 @@ class Launcher extends Forge2DBlueprint {
} }
} }
/// {@template launcher_straight_ramp} /// {@template launcher_ramp}
/// The green left ramp, where the [Ball] goes through when launched from the /// The yellow right ramp, where the [Ball] goes through when launched from the
/// [Plunger]. /// [Plunger].
/// {@endtemplate} /// {@endtemplate}
class LauncherStraightRamp extends BodyComponent with InitialPosition, Layered { class LauncherRamp extends BodyComponent with InitialPosition, Layered {
/// {@macro launcher_straight_ramp} /// {@macro launcher_ramp}
LauncherStraightRamp() : super(priority: 2) { LauncherRamp() : super(priority: 2) {
layer = Layer.launcher; layer = Layer.launcher;
paint = Paint() paint = Paint()
..color = const Color.fromARGB(255, 34, 255, 0) ..color = const Color.fromARGB(255, 251, 255, 0)
..style = PaintingStyle.stroke; ..style = PaintingStyle.stroke;
} }
@ -90,40 +85,8 @@ class LauncherStraightRamp extends BodyComponent with InitialPosition, Layered {
final internalStraightFixtureDef = FixtureDef(internalStraightShape); final internalStraightFixtureDef = FixtureDef(internalStraightShape);
fixturesDef.add(internalStraightFixtureDef); 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( final externalCurveShape = ArcShape(
center: initialPosition, center: initialPosition + Vector2(-27.5, 119),
arcRadius: Launcher.externalRadius, arcRadius: Launcher.externalRadius,
angle: math.pi / 2, angle: math.pi / 2,
rotation: 3 * math.pi / 2, rotation: 3 * math.pi / 2,
@ -155,7 +118,7 @@ class LauncherCurveRamp extends BodyComponent with InitialPosition, Layered {
/// {@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 launcher ramp. /// inside [LauncherRamp].
/// {@endtemplate} /// {@endtemplate}
class _LauncherRampOpening extends RampOpening { class _LauncherRampOpening extends RampOpening {
/// {@macro launcher_ramp_opening} /// {@macro launcher_ramp_opening}

@ -1,5 +1,3 @@
import 'dart:math' as math;
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';

Loading…
Cancel
Save