feat: add launcher asset and dimension

pull/120/head
Allison Ryan 4 years ago
parent c4012b1c65
commit 52f272cefc

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

@ -1,105 +1,213 @@
// ignore_for_file: public_member_api_docs, avoid_renaming_method_parameters // ignore_for_file: avoid_renaming_method_parameters
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';
import 'package:pinball/flame/blueprint.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/gen/assets.gen.dart';
import 'package:pinball_components/pinball_components.dart' hide Assets;
/// A [Blueprint] which creates the [LauncherRamp]. /// {@template launcher}
/// A [Blueprint] which creates the [LauncherRamp] and
/// [LauncherForegroundRailing].
/// {@endtemplate}
class Launcher extends Forge2DBlueprint { class Launcher extends Forge2DBlueprint {
@override @override
void build(_) { void build(_) {
final position = Vector2(
PinballGame.boardBounds.right - 31.3,
PinballGame.boardBounds.bottom + 33,
);
addAllContactCallback([ addAllContactCallback([
RampOpeningBallContactCallback<_LauncherRampOpening>(), RampOpeningBallContactCallback<_LauncherExit>(),
]); ]);
final leftOpening = _LauncherRampOpening(rotation: math.pi / 2) final launcherRamp = LauncherRamp()..layer = Layer.launcher;
..initialPosition = position + Vector2(-11.8, 72.7)
..layer = Layer.opening;
final rightOpening = _LauncherRampOpening(rotation: 0)
..initialPosition = position + Vector2(-5.4, 65.4)
..layer = Layer.opening;
final launcherRamp = LauncherRamp() final launcherForegroundRailing = LauncherForegroundRailing()
..initialPosition = position + Vector2(1.7, 0)
..layer = Layer.launcher; ..layer = Layer.launcher;
final launcherExit = _LauncherExit(rotation: math.pi / 2)
..initialPosition = Vector2(1.8, 34.2)
..layer = Layer.opening
..renderBody = false;
addAll([ addAll([
leftOpening,
rightOpening,
launcherRamp, launcherRamp,
launcherForegroundRailing,
launcherExit,
]); ]);
} }
} }
/// {@template launcher_ramp} /// {@template launcher_ramp}
/// The yellow right ramp, where the [Ball] goes through when launched from the /// Ramp the [Ball] is launched from at the beginning of each ball life.
/// [Plunger].
/// {@endtemplate} /// {@endtemplate}
class LauncherRamp extends BodyComponent with InitialPosition, Layered { class LauncherRamp extends BodyComponent with InitialPosition, Layered {
/// {@macro launcher_ramp} /// {@macro launcher_ramp}
LauncherRamp() : super(priority: 2) { LauncherRamp() : super(priority: -1) {
layer = Layer.launcher; layer = Layer.launcher;
paint = Paint() paint = Paint()
..color = const Color.fromARGB(255, 251, 255, 0) ..color = const Color.fromARGB(255, 251, 255, 0)
..style = PaintingStyle.stroke; ..style = PaintingStyle.stroke;
} }
/// Width between walls of the ramp.
static const width = 5.0;
/// Radius of the external arc at the top of the ramp.
static const _externalRadius = 16.3;
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[]; final fixturesDef = <FixtureDef>[];
final startPosition = initialPosition + Vector2(0, 3); final elevatedRightStraightShape = EdgeShape()
final endPosition = initialPosition + Vector2(0, 130); ..set(
Vector2(31.4, 61.4),
Vector2(46.5, -68.4),
);
final elevatedRightStraightFixtureDef =
FixtureDef(elevatedRightStraightShape);
fixturesDef.add(elevatedRightStraightFixtureDef);
final rightStraightShape = EdgeShape() final elevatedLeftStraightShape = EdgeShape()
..set( ..set(
startPosition..rotate(PinballGame.boardPerspectiveAngle), Vector2(27.8, 61.4),
endPosition..rotate(PinballGame.boardPerspectiveAngle), Vector2(41.5, -68.4),
);
final elevatedLeftStraightFixtureDef =
FixtureDef(elevatedLeftStraightShape);
fixturesDef.add(elevatedLeftStraightFixtureDef);
final elevatedTopCurveShape = ArcShape(
center: Vector2(20.5, 61.1),
arcRadius: 11,
angle: 1.6,
rotation: -1.65,
); );
final rightStraightFixtureDef = FixtureDef(rightStraightShape); final elevatedTopCurveFixtureDef = FixtureDef(elevatedTopCurveShape);
fixturesDef.add(rightStraightFixtureDef); fixturesDef.add(elevatedTopCurveFixtureDef);
final leftStraightShape = EdgeShape() final elevatedTopStraightShape = EdgeShape()
..set( ..set(
startPosition - Vector2(width, 0), Vector2(3.7, 70.1),
endPosition - Vector2(width, 0), Vector2(19.1, 72.1),
); );
final leftStraightFixtureDef = FixtureDef(leftStraightShape); final elevatedTopStraightFixtureDef = FixtureDef(elevatedTopStraightShape);
fixturesDef.add(leftStraightFixtureDef); fixturesDef.add(elevatedTopStraightFixtureDef);
final externalCurveShape = ArcShape( final elevatedBottomCurveShape = ArcShape(
center: initialPosition + Vector2(-28.2, 132), center: Vector2(19.3, 60.3),
arcRadius: _externalRadius, arcRadius: 8.5,
angle: math.pi / 2, angle: 1.48,
rotation: 3 * math.pi / 2, rotation: -1.58,
); );
final externalCurveFixtureDef = FixtureDef(externalCurveShape); final elevatedBottomCurveFixtureDef = FixtureDef(elevatedBottomCurveShape);
fixturesDef.add(externalCurveFixtureDef); fixturesDef.add(elevatedBottomCurveFixtureDef);
final internalCurveShape = externalCurveShape.copyWith( final elevatedBottomStraightShape = EdgeShape()
arcRadius: _externalRadius - width, ..set(
Vector2(3.7, 66.9),
Vector2(19.1, 68.8),
); );
final internalCurveFixtureDef = FixtureDef(internalCurveShape); final elevatedBottomStraightFixtureDef =
fixturesDef.add(internalCurveFixtureDef); FixtureDef(elevatedBottomStraightShape);
fixturesDef.add(elevatedBottomStraightFixtureDef);
return fixturesDef; return fixturesDef;
} }
@override
Future<void> onLoad() async {
await super.onLoad();
final sprite = await gameRef.loadSprite(
Assets.images.components.launchRamp.launchRamp.path,
);
final spriteComponent = SpriteComponent(
sprite: sprite,
size: Vector2(44.7, 144.1),
anchor: Anchor.center,
position: Vector2(25.65, 0),
);
await gameRef.add(spriteComponent);
renderBody = false;
}
@override
Body createBody() {
final bodyDef = BodyDef()
..userData = this
..position = initialPosition;
final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture);
return body;
}
}
/// {@template launcher_ramp}
/// The yellow right ramp, where the [Ball] goes through when launched from the
/// [Plunger].
/// {@endtemplate}
class LauncherForegroundRailing extends BodyComponent
with InitialPosition, Layered {
/// {@macro launcher_ramp}
LauncherForegroundRailing() : super(priority: 5) {
layer = Layer.launcher;
paint = Paint()
..color = const Color.fromARGB(255, 251, 255, 0)
..style = PaintingStyle.stroke;
}
List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
final groundRightStraightShape = EdgeShape()
..set(
Vector2(27.6, 57.9),
Vector2(30, 35.1),
);
final groundRightStraightFixtureDef = FixtureDef(groundRightStraightShape);
fixturesDef.add(groundRightStraightFixtureDef);
final groundTopStraightShape = EdgeShape()
..set(
Vector2(3.7, 66.8),
Vector2(19.7, 66.8),
);
final groundTopStraightFixtureDef = FixtureDef(groundTopStraightShape);
fixturesDef.add(groundTopStraightFixtureDef);
final groundCurveShape = ArcShape(
center: Vector2(20.1, 59.3),
arcRadius: 7.5,
angle: 1.8,
rotation: -1.63,
);
final groundCurveFixtureDef = FixtureDef(groundCurveShape);
fixturesDef.add(groundCurveFixtureDef);
return fixturesDef;
}
@override
Future<void> onLoad() async {
await super.onLoad();
final sprite = await gameRef.loadSprite(
Assets.images.components.launchRamp.launchRailFG.path,
);
final spriteComponent = SpriteComponent(
sprite: sprite,
size: Vector2(38.1, 138.6),
anchor: Anchor.center,
position: Vector2(22.8, 0),
priority: 5,
);
await gameRef.add(spriteComponent);
renderBody = false;
}
@override @override
Body createBody() { Body createBody() {
final bodyDef = BodyDef() final bodyDef = BodyDef()
@ -113,13 +221,13 @@ class LauncherRamp extends BodyComponent with InitialPosition, Layered {
} }
} }
/// {@template launcher_ramp_opening} /// {@template launcher_exit}
/// [RampOpening] with [Layer.launcher] to filter [Ball]s collisions /// [RampOpening] with [Layer.launcher] to filter [Ball]s exiting the
/// inside [LauncherRamp]. /// [Launcher].
/// {@endtemplate} /// {@endtemplate}
class _LauncherRampOpening extends RampOpening { class _LauncherExit extends RampOpening {
/// {@macro launcher_ramp_opening} /// {@macro launcher_exit}
_LauncherRampOpening({ _LauncherExit({
required double rotation, required double rotation,
}) : _rotation = rotation, }) : _rotation = rotation,
super( super(
@ -129,7 +237,7 @@ class _LauncherRampOpening extends RampOpening {
final double _rotation; final double _rotation;
static final Vector2 _size = Vector2(LauncherRamp.width / 3, .1); static final Vector2 _size = Vector2(1.6, 0.1);
@override @override
Shape get shape => PolygonShape() Shape get shape => PolygonShape()

@ -10,6 +10,8 @@ extension PinballGameAssetsX on PinballGame {
images.load(components.Assets.images.ball.keyName), images.load(components.Assets.images.ball.keyName),
images.load(Assets.images.components.flipper.path), images.load(Assets.images.components.flipper.path),
images.load(Assets.images.components.background.path), images.load(Assets.images.components.background.path),
images.load(Assets.images.components.launchRamp.launchRamp.path),
images.load(Assets.images.components.launchRamp.launchRailFG.path),
images.load(Assets.images.components.spaceship.androidTop.path), images.load(Assets.images.components.spaceship.androidTop.path),
images.load(Assets.images.components.spaceship.androidBottom.path), images.load(Assets.images.components.spaceship.androidBottom.path),
images.load(Assets.images.components.spaceship.lower.path), images.load(Assets.images.components.spaceship.lower.path),

@ -118,7 +118,7 @@ class DebugPinballGame extends PinballGame with TapDetector {
anchor: Anchor.center, anchor: Anchor.center,
) )
..position = Vector2(0, -7.8) ..position = Vector2(0, -7.8)
..priority = -1; ..priority = -5;
await add(spriteComponent); await add(spriteComponent);
} }

@ -25,10 +25,25 @@ class $AssetsImagesComponentsGen {
AssetGenImage get flipper => AssetGenImage get flipper =>
const AssetGenImage('assets/images/components/flipper.png'); const AssetGenImage('assets/images/components/flipper.png');
$AssetsImagesComponentsLaunchRampGen get launchRamp =>
const $AssetsImagesComponentsLaunchRampGen();
$AssetsImagesComponentsSpaceshipGen get spaceship => $AssetsImagesComponentsSpaceshipGen get spaceship =>
const $AssetsImagesComponentsSpaceshipGen(); const $AssetsImagesComponentsSpaceshipGen();
} }
class $AssetsImagesComponentsLaunchRampGen {
const $AssetsImagesComponentsLaunchRampGen();
/// File path: assets/images/components/launch_ramp/launch-ramp.png
AssetGenImage get launchRamp => const AssetGenImage(
'assets/images/components/launch_ramp/launch-ramp.png');
/// File path: assets/images/components/launch_ramp/launch-rail-FG.png
AssetGenImage get launchRailFG => const AssetGenImage(
'assets/images/components/launch_ramp/launch-rail-FG.png');
}
class $AssetsImagesComponentsSpaceshipGen { class $AssetsImagesComponentsSpaceshipGen {
const $AssetsImagesComponentsSpaceshipGen(); const $AssetsImagesComponentsSpaceshipGen();

@ -44,6 +44,7 @@ flutter:
assets: assets:
- assets/images/components/ - assets/images/components/
- assets/images/components/spaceship/ - assets/images/components/spaceship/
- assets/images/components/launch_ramp/
flutter_gen: flutter_gen:
line_length: 80 line_length: 80

Loading…
Cancel
Save