From fd110feef3f0e8200df1a6de523380b66da60541 Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Sun, 1 May 2022 22:52:13 -0500 Subject: [PATCH] refactor: adjust physics and add tests --- lib/game/components/launcher.dart | 4 +- lib/game/components/sparky_scorch.dart | 10 ++- lib/game/pinball_game.dart | 2 +- .../android_bumper/android_bumper.dart | 5 +- .../lib/src/components/android_spaceship.dart | 8 +- .../lib/src/components/ball.dart | 2 +- .../lib/src/components/kicker/kicker.dart | 2 +- .../lib/src/components/plunger.dart | 2 +- .../lib/src/components/slingshot.dart | 22 ++---- .../lib/src/components/z_indexes.dart | 4 +- .../android_bumper/android_bumper_test.dart | 10 +++ .../dash_nest_bumper_test.dart | 10 +++ .../test/src/components/slingshot_test.dart | 73 ++----------------- .../sparky_bumper/sparky_bumper_test.dart | 10 +++ 14 files changed, 66 insertions(+), 98 deletions(-) diff --git a/lib/game/components/launcher.dart b/lib/game/components/launcher.dart index 2663dfd4..ffac6507 100644 --- a/lib/game/components/launcher.dart +++ b/lib/game/components/launcher.dart @@ -12,8 +12,8 @@ class Launcher extends Component { : super( children: [ LaunchRamp(), - ControlledPlunger(compressionDistance: 10.5) - ..initialPosition = Vector2(41.1, 43), + ControlledPlunger(compressionDistance: 9.2) + ..initialPosition = Vector2(41.2, 43.7), RocketSpriteComponent()..position = Vector2(43, 62.3), ], ); diff --git a/lib/game/components/sparky_scorch.dart b/lib/game/components/sparky_scorch.dart index 271e3527..d461f95f 100644 --- a/lib/game/components/sparky_scorch.dart +++ b/lib/game/components/sparky_scorch.dart @@ -29,7 +29,7 @@ class SparkyScorch extends Component { ScoringBehavior(points: 20000), ], )..initialPosition = Vector2(-3.3, -52.55), - SparkyComputerSensor()..initialPosition = Vector2(-13, -49.8), + SparkyComputerSensor()..initialPosition = Vector2(-13, -49.9), SparkyAnimatronic()..position = Vector2(-13.8, -58.2), SparkyComputer(), ], @@ -53,7 +53,13 @@ class SparkyComputerSensor extends BodyComponent @override Body createBody() { - final shape = CircleShape()..radius = 0.1; + final shape = PolygonShape() + ..setAsBox( + 1, + 0.1, + Vector2.zero(), + -0.18, + ); final fixtureDef = FixtureDef(shape, isSensor: true); final bodyDef = BodyDef( position: initialPosition, diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index e090e9a8..6715430b 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -23,7 +23,7 @@ class PinballGame extends Forge2DGame PinballGame({ required this.characterTheme, required this.audio, - }) : super(gravity: Vector2(0, 35)) { + }) : super(gravity: Vector2(0, 30)) { images.prefix = ''; controller = _GameBallsController(this); } diff --git a/packages/pinball_components/lib/src/components/android_bumper/android_bumper.dart b/packages/pinball_components/lib/src/components/android_bumper/android_bumper.dart index d96cf649..7ddabee8 100644 --- a/packages/pinball_components/lib/src/components/android_bumper/android_bumper.dart +++ b/packages/pinball_components/lib/src/components/android_bumper/android_bumper.dart @@ -68,7 +68,10 @@ class AndroidBumper extends BodyComponent with InitialPosition, ZIndex { dimmedAssetPath: Assets.images.android.bumper.b.dimmed.keyName, spritePosition: Vector2(0, -0.1), bloc: AndroidBumperCubit(), - children: children, + children: [ + ...?children, + BumpingBehavior(strength: 20), + ], ); /// {@macro android_bumper} diff --git a/packages/pinball_components/lib/src/components/android_spaceship.dart b/packages/pinball_components/lib/src/components/android_spaceship.dart index 81a564e1..d7de59e7 100644 --- a/packages/pinball_components/lib/src/components/android_spaceship.dart +++ b/packages/pinball_components/lib/src/components/android_spaceship.dart @@ -7,6 +7,7 @@ import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball_components/gen/assets.gen.dart'; import 'package:pinball_components/pinball_components.dart' hide Assets; +import 'package:pinball_components/src/components/bumping_behavior.dart'; import 'package:pinball_flame/pinball_flame.dart'; class AndroidSpaceship extends Component { @@ -141,14 +142,9 @@ class _AndroidHead extends BodyComponent with InitialPosition, Layered, ZIndex { majorRadius: 3.1, minorRadius: 2, )..rotate(1.4); - // TODO(allisonryan0002): use bumping behavior. - final fixtureDef = FixtureDef( - shape, - restitution: 0.1, - ); final bodyDef = BodyDef(position: initialPosition); - return world.createBody(bodyDef)..createFixture(fixtureDef); + return world.createBody(bodyDef)..createFixtureFromShape(shape); } } diff --git a/packages/pinball_components/lib/src/components/ball.dart b/packages/pinball_components/lib/src/components/ball.dart index 81a57e7c..63b6faf0 100644 --- a/packages/pinball_components/lib/src/components/ball.dart +++ b/packages/pinball_components/lib/src/components/ball.dart @@ -116,7 +116,7 @@ class Ball extends BodyComponent math.pow(defaultGravity, 2) - math.pow(positionalXForce, 2), ); - body.gravityOverride = Vector2(-positionalXForce, positionalYForce); + body.gravityOverride = Vector2(positionalXForce, positionalYForce); } } diff --git a/packages/pinball_components/lib/src/components/kicker/kicker.dart b/packages/pinball_components/lib/src/components/kicker/kicker.dart index 3301e2ba..570f2990 100644 --- a/packages/pinball_components/lib/src/components/kicker/kicker.dart +++ b/packages/pinball_components/lib/src/components/kicker/kicker.dart @@ -36,7 +36,7 @@ class Kicker extends BodyComponent with InitialPosition { }) : _side = side, super( children: [ - BumpingBehavior(strength: 15)..applyTo(['bouncy_edge']), + BumpingBehavior(strength: 20)..applyTo(['bouncy_edge']), KickerBallContactBehavior()..applyTo(['bouncy_edge']), KickerBlinkingBehavior(), _KickerSpriteGroupComponent( diff --git a/packages/pinball_components/lib/src/components/plunger.dart b/packages/pinball_components/lib/src/components/plunger.dart index e9fbfdff..fa81c783 100644 --- a/packages/pinball_components/lib/src/components/plunger.dart +++ b/packages/pinball_components/lib/src/components/plunger.dart @@ -79,7 +79,7 @@ class Plunger extends BodyComponent with InitialPosition, Layered, ZIndex { /// The velocity's magnitude depends on how far the [Plunger] has been pulled /// from its original [initialPosition]. void release() { - final velocity = (initialPosition.y - body.position.y) * 20; + final velocity = (initialPosition.y - body.position.y) * 11; body.linearVelocity = Vector2(0, velocity); _spriteComponent.release(); } diff --git a/packages/pinball_components/lib/src/components/slingshot.dart b/packages/pinball_components/lib/src/components/slingshot.dart index 42163d80..e203c082 100644 --- a/packages/pinball_components/lib/src/components/slingshot.dart +++ b/packages/pinball_components/lib/src/components/slingshot.dart @@ -29,7 +29,7 @@ class Slingshots extends Component with ZIndex { } /// {@template slingshot} -/// Elastic bumper that bounces the [Ball] off of its straight sides. +/// Elastic bumper that bounces the [Ball] off of its sides. /// {@endtemplate} class Slingshot extends BodyComponent with InitialPosition { /// {@macro slingshot} @@ -42,7 +42,7 @@ class Slingshot extends BodyComponent with InitialPosition { super( children: [ _SlinghsotSpriteComponent(spritePath, angle: angle), - BumpingBehavior(strength: 10), + BumpingBehavior(strength: 20), ], renderBody: false, ); @@ -56,37 +56,27 @@ class Slingshot extends BodyComponent with InitialPosition { final topCircleShape = CircleShape()..radius = circleRadius; topCircleShape.position.setValues(0, -_length / 2); - final topCircleFixtureDef = FixtureDef(topCircleShape); final bottomCircleShape = CircleShape()..radius = circleRadius; bottomCircleShape.position.setValues(0, _length / 2); - final bottomCircleFixtureDef = FixtureDef(bottomCircleShape); final leftEdgeShape = EdgeShape() ..set( Vector2(circleRadius, _length / 2), Vector2(circleRadius, -_length / 2), ); - final leftEdgeShapeFixtureDef = FixtureDef( - leftEdgeShape, - restitution: 5, - ); final rightEdgeShape = EdgeShape() ..set( Vector2(-circleRadius, _length / 2), Vector2(-circleRadius, -_length / 2), ); - final rightEdgeShapeFixtureDef = FixtureDef( - rightEdgeShape, - restitution: 5, - ); return [ - topCircleFixtureDef, - bottomCircleFixtureDef, - leftEdgeShapeFixtureDef, - rightEdgeShapeFixtureDef, + FixtureDef(topCircleShape), + FixtureDef(bottomCircleShape), + FixtureDef(leftEdgeShape), + FixtureDef(rightEdgeShape), ]; } diff --git a/packages/pinball_components/lib/src/components/z_indexes.dart b/packages/pinball_components/lib/src/components/z_indexes.dart index e38683a2..04dd02c7 100644 --- a/packages/pinball_components/lib/src/components/z_indexes.dart +++ b/packages/pinball_components/lib/src/components/z_indexes.dart @@ -23,7 +23,7 @@ abstract class ZIndexes { // TODO(allisonryan0002): fix this magic zindex. Could bump all priorities so // there are no negatives. - static const boardBackground = 3 * _below + _base; + static const boardBackground = 5 * _below + _base; static const decal = _above + boardBackground; @@ -61,7 +61,7 @@ abstract class ZIndexes { // Flutter Forest - static const flutterForest = _above + launchRampForegroundRailing; + static const flutterForest = _above + ballOnBoard; // Sparky Scorch diff --git a/packages/pinball_components/test/src/components/android_bumper/android_bumper_test.dart b/packages/pinball_components/test/src/components/android_bumper/android_bumper_test.dart index a5256b79..d5be2639 100644 --- a/packages/pinball_components/test/src/components/android_bumper/android_bumper_test.dart +++ b/packages/pinball_components/test/src/components/android_bumper/android_bumper_test.dart @@ -7,6 +7,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/src/components/android_bumper/behaviors/behaviors.dart'; +import 'package:pinball_components/src/components/bumping_behavior.dart'; import '../../../helpers/helpers.dart'; @@ -81,6 +82,15 @@ void main() { isNotNull, ); }); + + flameTester.test('a BumpingBehavior', (game) async { + final androidBumper = AndroidBumper.a(); + await game.ensureAdd(androidBumper); + expect( + androidBumper.children.whereType().single, + isNotNull, + ); + }); }); }); } diff --git a/packages/pinball_components/test/src/components/dash_nest_bumper/dash_nest_bumper_test.dart b/packages/pinball_components/test/src/components/dash_nest_bumper/dash_nest_bumper_test.dart index 67764951..deed97ac 100644 --- a/packages/pinball_components/test/src/components/dash_nest_bumper/dash_nest_bumper_test.dart +++ b/packages/pinball_components/test/src/components/dash_nest_bumper/dash_nest_bumper_test.dart @@ -6,6 +6,7 @@ import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball_components/pinball_components.dart'; +import 'package:pinball_components/src/components/bumping_behavior.dart'; import 'package:pinball_components/src/components/dash_nest_bumper/behaviors/behaviors.dart'; import '../../../helpers/helpers.dart'; @@ -83,6 +84,15 @@ void main() { isNotNull, ); }); + + flameTester.test('a BumpingBehavior', (game) async { + final dashNestBumper = DashNestBumper.a(); + await game.ensureAdd(dashNestBumper); + expect( + dashNestBumper.children.whereType().single, + isNotNull, + ); + }); }); }); } diff --git a/packages/pinball_components/test/src/components/slingshot_test.dart b/packages/pinball_components/test/src/components/slingshot_test.dart index 0c7a29e0..21885550 100644 --- a/packages/pinball_components/test/src/components/slingshot_test.dart +++ b/packages/pinball_components/test/src/components/slingshot_test.dart @@ -4,6 +4,7 @@ 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 'package:pinball_components/src/components/bumping_behavior.dart'; import '../../helpers/helpers.dart'; @@ -14,8 +15,6 @@ void main() { Assets.images.slingshot.lower.keyName, ]; final flameTester = FlameTester(() => TestGame(assets)); - const length = 2.0; - const angle = 0.0; flameTester.test('loads correctly', (game) async { final component = Slingshots(); @@ -40,68 +39,12 @@ void main() { }, ); - flameTester.test( - 'loads correctly', - (game) async { - final slingshot = Slingshot( - length: length, - angle: angle, - spritePath: assets.first, - ); - await game.ensureAdd(slingshot); - - expect(game.contains(slingshot), isTrue); - }, - ); - - flameTester.test( - 'body is static', - (game) async { - final slingshot = Slingshot( - length: length, - angle: angle, - spritePath: assets.first, - ); - await game.ensureAdd(slingshot); - - expect(slingshot.body.bodyType, equals(BodyType.static)); - }, - ); - - flameTester.test( - 'has restitution', - (game) async { - final slingshot = Slingshot( - length: length, - angle: angle, - spritePath: assets.first, - ); - await game.ensureAdd(slingshot); - - final totalRestitution = slingshot.body.fixtures.fold( - 0, - (total, fixture) => total + fixture.restitution, - ); - expect(totalRestitution, greaterThan(0)); - }, - ); - - flameTester.test( - 'has no friction', - (game) async { - final slingshot = Slingshot( - length: length, - angle: angle, - spritePath: assets.first, - ); - await game.ensureAdd(slingshot); - - final totalFriction = slingshot.body.fixtures.fold( - 0, - (total, fixture) => total + fixture.friction, - ); - expect(totalFriction, equals(0)); - }, - ); + flameTester.test('adds BumpingBehavior', (game) async { + final slingshots = Slingshots(); + await game.ensureAdd(slingshots); + for (final slingshot in slingshots.children) { + expect(slingshot.firstChild(), isNotNull); + } + }); }); } diff --git a/packages/pinball_components/test/src/components/sparky_bumper/sparky_bumper_test.dart b/packages/pinball_components/test/src/components/sparky_bumper/sparky_bumper_test.dart index 0d255454..922e37b7 100644 --- a/packages/pinball_components/test/src/components/sparky_bumper/sparky_bumper_test.dart +++ b/packages/pinball_components/test/src/components/sparky_bumper/sparky_bumper_test.dart @@ -6,6 +6,7 @@ import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball_components/pinball_components.dart'; +import 'package:pinball_components/src/components/bumping_behavior.dart'; import 'package:pinball_components/src/components/sparky_bumper/behaviors/behaviors.dart'; import '../../../helpers/helpers.dart'; @@ -81,6 +82,15 @@ void main() { isNotNull, ); }); + + flameTester.test('a BumpingBehavior', (game) async { + final sparkyBumper = SparkyBumper.a(); + await game.ensureAdd(sparkyBumper); + expect( + sparkyBumper.children.whereType().single, + isNotNull, + ); + }); }); }); }