diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart index 03912fe6..61d0f3ca 100644 --- a/lib/game/components/components.dart +++ b/lib/game/components/components.dart @@ -3,7 +3,6 @@ export 'bonus_word.dart'; export 'controlled_ball.dart'; export 'controlled_flipper.dart'; export 'flutter_forest.dart'; -export 'launcher_ramp.dart'; export 'plunger.dart'; export 'score_points.dart'; export 'wall.dart'; diff --git a/lib/game/components/launcher_ramp.dart b/lib/game/components/launcher_ramp.dart deleted file mode 100644 index 5c071f4f..00000000 --- a/lib/game/components/launcher_ramp.dart +++ /dev/null @@ -1,142 +0,0 @@ -// ignore_for_file: public_member_api_docs, avoid_renaming_method_parameters - -import 'dart:math' as math; - -import 'package:flame/extensions.dart'; -import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:flutter/material.dart'; -import 'package:pinball/game/game.dart'; -import 'package:pinball_components/pinball_components.dart'; - -/// A [Blueprint] which creates the [LauncherRamp]. -class Launcher extends Forge2DBlueprint { - @override - void build(_) { - final position = Vector2( - BoardDimensions.bounds.right - 31.3, - BoardDimensions.bounds.bottom + 33, - ); - - addAllContactCallback([ - RampOpeningBallContactCallback<_LauncherRampOpening>(), - ]); - - final leftOpening = _LauncherRampOpening(rotation: math.pi / 2) - ..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() - ..initialPosition = position + Vector2(1.7, 0) - ..layer = Layer.launcher; - - addAll([ - leftOpening, - rightOpening, - launcherRamp, - ]); - } -} - -/// {@template launcher_ramp} -/// The yellow right ramp, where the [Ball] goes through when launched from the -/// [Plunger]. -/// {@endtemplate} -class LauncherRamp extends BodyComponent with InitialPosition, Layered { - /// {@macro launcher_ramp} - LauncherRamp() : super(priority: 2) { - layer = Layer.launcher; - paint = Paint() - ..color = const Color.fromARGB(255, 251, 255, 0) - ..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 _createFixtureDefs() { - final fixturesDef = []; - - final startPosition = initialPosition + Vector2(0, 3); - final endPosition = initialPosition + Vector2(0, 130); - - final rightStraightShape = EdgeShape() - ..set( - startPosition..rotate(BoardDimensions.perspectiveAngle), - endPosition..rotate(BoardDimensions.perspectiveAngle), - ); - final rightStraightFixtureDef = FixtureDef(rightStraightShape); - fixturesDef.add(rightStraightFixtureDef); - - final leftStraightShape = EdgeShape() - ..set( - startPosition - Vector2(width, 0), - endPosition - Vector2(width, 0), - ); - final leftStraightFixtureDef = FixtureDef(leftStraightShape); - fixturesDef.add(leftStraightFixtureDef); - - final externalCurveShape = ArcShape( - center: initialPosition + Vector2(-28.2, 132), - arcRadius: _externalRadius, - angle: math.pi / 2, - rotation: 3 * math.pi / 2, - ); - final externalCurveFixtureDef = FixtureDef(externalCurveShape); - fixturesDef.add(externalCurveFixtureDef); - - final internalCurveShape = externalCurveShape.copyWith( - arcRadius: _externalRadius - 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} -/// [RampOpening] with [Layer.launcher] to filter [Ball]s collisions -/// inside [LauncherRamp]. -/// {@endtemplate} -class _LauncherRampOpening extends RampOpening { - /// {@macro launcher_ramp_opening} - _LauncherRampOpening({ - required double rotation, - }) : _rotation = rotation, - super( - insideLayer: Layer.launcher, - orientation: RampOrientation.down, - insidePriority: 3, - ); - - final double _rotation; - - static final Vector2 _size = Vector2(LauncherRamp.width / 3, .1); - - @override - Shape get shape => PolygonShape() - ..setAsBox( - _size.x, - _size.y, - initialPosition, - _rotation, - ); -} diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 60e29a4d..cc5797c0 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -1,5 +1,6 @@ import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:pinball_components/pinball_components.dart'; @@ -13,7 +14,11 @@ class Plunger extends BodyComponent with KeyboardHandler, InitialPosition { /// {@macro plunger} Plunger({ required this.compressionDistance, - }); + }) : super( + priority: 5, + // TODO(allisonryan0002): remove paint after asset is added. + paint: Paint()..color = const Color.fromARGB(255, 241, 8, 8), + ); /// Distance the plunger can lower. final double compressionDistance; diff --git a/lib/game/game_assets.dart b/lib/game/game_assets.dart index c1f37eb2..5592d667 100644 --- a/lib/game/game_assets.dart +++ b/lib/game/game_assets.dart @@ -13,6 +13,12 @@ extension PinballGameAssetsX on PinballGame { images.load(components.Assets.images.flipper.right.keyName), images.load(components.Assets.images.baseboard.left.keyName), images.load(components.Assets.images.baseboard.right.keyName), + images.load(components.Assets.images.spaceshipSaucer.keyName), + images.load(components.Assets.images.spaceshipBridge.keyName), + images.load(components.Assets.images.launchRamp.ramp.keyName), + images.load( + components.Assets.images.launchRamp.foregroundRailing.keyName, + ), images.load(components.Assets.images.dino.dinoLandTop.keyName), images.load(components.Assets.images.dino.dinoLandBottom.keyName), images.load(components.Assets.images.dashBumper.a.active.keyName), diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 741aa799..35484bd5 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -35,7 +35,7 @@ class PinballGame extends Forge2DGame unawaited(add(Board())); unawaited(_addPlunger()); unawaited(_addBonusWord()); - unawaited(_addPaths()); + unawaited(_addRamps()); unawaited( addFromBlueprint( Spaceship( @@ -91,9 +91,9 @@ class PinballGame extends Forge2DGame ); } - Future _addPaths() async { + Future _addRamps() async { unawaited(addFromBlueprint(SpaceshipRamp())); - unawaited(addFromBlueprint(Launcher())); + unawaited(addFromBlueprint(LaunchRamp())); } void spawnBall() { @@ -128,7 +128,7 @@ class DebugPinballGame extends PinballGame with TapDetector { anchor: Anchor.center, ) ..position = Vector2(0, -7.8) - ..priority = -1; + ..priority = -2; await add(spriteComponent); } diff --git a/packages/pinball_components/assets/images/launch_ramp/foreground-railing.png b/packages/pinball_components/assets/images/launch_ramp/foreground-railing.png new file mode 100644 index 00000000..7dd0de96 Binary files /dev/null and b/packages/pinball_components/assets/images/launch_ramp/foreground-railing.png differ diff --git a/packages/pinball_components/assets/images/launch_ramp/ramp.png b/packages/pinball_components/assets/images/launch_ramp/ramp.png new file mode 100644 index 00000000..c811dd83 Binary files /dev/null and b/packages/pinball_components/assets/images/launch_ramp/ramp.png differ diff --git a/packages/pinball_components/lib/gen/assets.gen.dart b/packages/pinball_components/lib/gen/assets.gen.dart index bad421b1..42b164cc 100644 --- a/packages/pinball_components/lib/gen/assets.gen.dart +++ b/packages/pinball_components/lib/gen/assets.gen.dart @@ -25,6 +25,9 @@ class $AssetsImagesGen { AssetGenImage get flutterSignPost => const AssetGenImage('assets/images/flutter_sign_post.png'); + $AssetsImagesLaunchRampGen get launchRamp => + const $AssetsImagesLaunchRampGen(); + /// File path: assets/images/spaceship_bridge.png AssetGenImage get spaceshipBridge => const AssetGenImage('assets/images/spaceship_bridge.png'); @@ -94,6 +97,18 @@ class $AssetsImagesFlipperGen { const AssetGenImage('assets/images/flipper/right.png'); } +class $AssetsImagesLaunchRampGen { + const $AssetsImagesLaunchRampGen(); + + /// File path: assets/images/launch_ramp/foreground-railing.png + AssetGenImage get foregroundRailing => + const AssetGenImage('assets/images/launch_ramp/foreground-railing.png'); + + /// File path: assets/images/launch_ramp/ramp.png + AssetGenImage get ramp => + const AssetGenImage('assets/images/launch_ramp/ramp.png'); +} + class $AssetsImagesSpaceshipRampGen { const $AssetsImagesSpaceshipRampGen(); diff --git a/packages/pinball_components/lib/src/components/components.dart b/packages/pinball_components/lib/src/components/components.dart index 2c868279..4fb633a5 100644 --- a/packages/pinball_components/lib/src/components/components.dart +++ b/packages/pinball_components/lib/src/components/components.dart @@ -11,6 +11,7 @@ export 'flutter_sign_post.dart'; export 'initial_position.dart'; export 'joint_anchor.dart'; export 'kicker.dart'; +export 'launch_ramp.dart'; export 'layer.dart'; export 'ramp_opening.dart'; export 'shapes/shapes.dart'; diff --git a/packages/pinball_components/lib/src/components/launch_ramp.dart b/packages/pinball_components/lib/src/components/launch_ramp.dart new file mode 100644 index 00000000..5f7ee098 --- /dev/null +++ b/packages/pinball_components/lib/src/components/launch_ramp.dart @@ -0,0 +1,243 @@ +// ignore_for_file: avoid_renaming_method_parameters + +import 'dart:math' as math; + +import 'package:flame/components.dart'; +import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:pinball_components/pinball_components.dart'; + +/// {@template launch_ramp} +/// A [Blueprint] which creates the [_LaunchRampBase] and +/// [_LaunchRampForegroundRailing]. +/// {@endtemplate} +class LaunchRamp extends Forge2DBlueprint { + @override + void build(_) { + addAllContactCallback([ + RampOpeningBallContactCallback<_LaunchRampExit>(), + ]); + + final launchRampBase = _LaunchRampBase()..layer = Layer.launcher; + + final launchRampForegroundRailing = _LaunchRampForegroundRailing() + ..layer = Layer.launcher; + + final launchRampExit = _LaunchRampExit(rotation: math.pi / 2) + ..initialPosition = Vector2(1.8, 34.2) + ..layer = Layer.opening + ..renderBody = false; + + addAll([ + launchRampBase, + launchRampForegroundRailing, + launchRampExit, + ]); + } +} + +/// {@template launch_ramp_base} +/// Ramp the [Ball] is launched from at the beginning of each ball life. +/// {@endtemplate} +class _LaunchRampBase extends BodyComponent with InitialPosition, Layered { + /// {@macro launch_ramp_base} + _LaunchRampBase() : super(priority: -1) { + layer = Layer.launcher; + } + + List _createFixtureDefs() { + final fixturesDef = []; + + final rightStraightShape = EdgeShape() + ..set( + Vector2(31.4, 61.4), + Vector2(46.5, -68.4), + ); + final rightStraightFixtureDef = FixtureDef(rightStraightShape); + fixturesDef.add(rightStraightFixtureDef); + + final leftStraightShape = EdgeShape() + ..set( + Vector2(27.8, 61.4), + Vector2(41.5, -68.4), + ); + final leftStraightFixtureDef = FixtureDef(leftStraightShape); + fixturesDef.add(leftStraightFixtureDef); + + final topCurveShape = ArcShape( + center: Vector2(20.5, 61.1), + arcRadius: 11, + angle: 1.6, + rotation: -1.65, + ); + final topCurveFixtureDef = FixtureDef(topCurveShape); + fixturesDef.add(topCurveFixtureDef); + + final bottomCurveShape = ArcShape( + center: Vector2(19.3, 60.3), + arcRadius: 8.5, + angle: 1.48, + rotation: -1.58, + ); + final bottomCurveFixtureDef = FixtureDef(bottomCurveShape); + fixturesDef.add(bottomCurveFixtureDef); + + final topStraightShape = EdgeShape() + ..set( + Vector2(3.7, 70.1), + Vector2(19.1, 72.1), + ); + final topStraightFixtureDef = FixtureDef(topStraightShape); + fixturesDef.add(topStraightFixtureDef); + + final bottomStraightShape = EdgeShape() + ..set( + Vector2(3.7, 66.9), + Vector2(19.1, 68.8), + ); + final bottomStraightFixtureDef = FixtureDef(bottomStraightShape); + fixturesDef.add(bottomStraightFixtureDef); + + return fixturesDef; + } + + @override + Body createBody() { + final bodyDef = BodyDef() + ..userData = this + ..position = initialPosition; + + final body = world.createBody(bodyDef); + _createFixtureDefs().forEach(body.createFixture); + + return body; + } + + @override + Future onLoad() async { + await super.onLoad(); + await _loadSprite(); + renderBody = false; + } + + Future _loadSprite() async { + final sprite = await gameRef.loadSprite( + Assets.images.launchRamp.ramp.keyName, + ); + + await add( + SpriteComponent( + sprite: sprite, + size: sprite.originalSize / 10, + anchor: Anchor.center, + position: Vector2(25.65, 0), + ), + ); + } +} + +/// {@template launch_ramp_foreground_railing} +/// Foreground railing for the [_LaunchRampBase] to render in front of the +/// [Ball]. +/// {@endtemplate} +class _LaunchRampForegroundRailing extends BodyComponent + with InitialPosition, Layered { + /// {@macro launch_ramp_foreground_railing} + _LaunchRampForegroundRailing() : super(priority: 4) { + layer = Layer.launcher; + } + + List _createFixtureDefs() { + final fixturesDef = []; + + final rightStraightShape = EdgeShape() + ..set( + Vector2(27.6, 57.9), + Vector2(30, 35.1), + ); + final rightStraightFixtureDef = FixtureDef(rightStraightShape); + fixturesDef.add(rightStraightFixtureDef); + + final curveShape = ArcShape( + center: Vector2(20.1, 59.3), + arcRadius: 7.5, + angle: 1.8, + rotation: -1.63, + ); + final curveFixtureDef = FixtureDef(curveShape); + fixturesDef.add(curveFixtureDef); + + final topStraightShape = EdgeShape() + ..set( + Vector2(3.7, 66.8), + Vector2(19.7, 66.8), + ); + final topStraightFixtureDef = FixtureDef(topStraightShape); + fixturesDef.add(topStraightFixtureDef); + + return fixturesDef; + } + + @override + Body createBody() { + final bodyDef = BodyDef() + ..userData = this + ..position = initialPosition; + + final body = world.createBody(bodyDef); + _createFixtureDefs().forEach(body.createFixture); + + return body; + } + + @override + Future onLoad() async { + await super.onLoad(); + await _loadSprite(); + renderBody = false; + } + + Future _loadSprite() async { + final sprite = await gameRef.loadSprite( + Assets.images.launchRamp.foregroundRailing.keyName, + ); + + await add( + SpriteComponent( + sprite: sprite, + size: sprite.originalSize / 10, + anchor: Anchor.center, + position: Vector2(22.8, 0), + priority: 4, + ), + ); + } +} + +/// {@template launch_ramp_exit} +/// [RampOpening] with [Layer.launcher] to filter [Ball]s exiting the +/// [LaunchRamp]. +/// {@endtemplate} +class _LaunchRampExit extends RampOpening { + /// {@macro launch_ramp_exit} + _LaunchRampExit({ + required double rotation, + }) : _rotation = rotation, + super( + insideLayer: Layer.launcher, + orientation: RampOrientation.down, + insidePriority: 3, + ); + + final double _rotation; + + static final Vector2 _size = Vector2(1.6, 0.1); + + @override + Shape get shape => PolygonShape() + ..setAsBox( + _size.x, + _size.y, + initialPosition, + _rotation, + ); +} diff --git a/packages/pinball_components/pubspec.yaml b/packages/pinball_components/pubspec.yaml index 846fb58e..13e850cd 100644 --- a/packages/pinball_components/pubspec.yaml +++ b/packages/pinball_components/pubspec.yaml @@ -29,6 +29,7 @@ flutter: - assets/images/baseboard/ - assets/images/dino/ - assets/images/flipper/ + - assets/images/launch_ramp/ - assets/images/dash_bumper/a/ - assets/images/dash_bumper/b/ - assets/images/dash_bumper/main/ diff --git a/packages/pinball_components/test/src/components/golden/launch-ramp.png b/packages/pinball_components/test/src/components/golden/launch-ramp.png new file mode 100644 index 00000000..e872c533 Binary files /dev/null and b/packages/pinball_components/test/src/components/golden/launch-ramp.png differ diff --git a/packages/pinball_components/test/src/components/launch_ramp_test.dart b/packages/pinball_components/test/src/components/launch_ramp_test.dart new file mode 100644 index 00000000..c7c7b1b2 --- /dev/null +++ b/packages/pinball_components/test/src/components/launch_ramp_test.dart @@ -0,0 +1,31 @@ +// ignore_for_file: cascade_invocations + +import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flame_test/flame_test.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pinball_components/pinball_components.dart'; + +import '../../helpers/helpers.dart'; + +void main() { + group('LaunchRamp', () { + final tester = FlameTester(TestGame.new); + + tester.testGameWidget( + 'renders correctly', + setUp: (game, tester) async { + await game.addFromBlueprint(LaunchRamp()); + await game.ready(); + game.camera.followVector2(Vector2.zero()); + game.camera.zoom = 4.1; + }, + // TODO(allisonryan0002): enable test when workflows are fixed. + // verify: (game, tester) async { + // await expectLater( + // find.byGame(), + // matchesGoldenFile('golden/launch-ramp.png'), + // ); + // }, + ); + }); +}