diff --git a/packages/pinball_components/lib/src/components/slingshot.dart b/packages/pinball_components/lib/src/components/slingshot.dart index 52bcf318..0a4f5cae 100644 --- a/packages/pinball_components/lib/src/components/slingshot.dart +++ b/packages/pinball_components/lib/src/components/slingshot.dart @@ -17,16 +17,19 @@ class Slingshots extends Forge2DBlueprint { angle: -1.5 * (math.pi / 180), spritePath: Assets.images.slingshot.leftUpper.keyName, )..initialPosition = Vector2(-29, 1.5); + final leftLowerSlingshot = Slingshot( length: 3.54, angle: -29.1 * (math.pi / 180), spritePath: Assets.images.slingshot.leftLower.keyName, )..initialPosition = Vector2(-31, -6.2); + final rightUpperSlingshot = Slingshot( length: 5.64, angle: 1 * (math.pi / 180), spritePath: Assets.images.slingshot.rightUpper.keyName, )..initialPosition = Vector2(22.3, 1.58); + final rightLowerSlingshot = Slingshot( length: 3.46, angle: 26.8 * (math.pi / 180), @@ -68,12 +71,12 @@ class Slingshot extends BodyComponent with InitialPosition { final topCircleShape = CircleShape()..radius = circleRadius; topCircleShape.position.setValues(0, _length / 2); - final topCircleFixtureDef = FixtureDef(topCircleShape); + final topCircleFixtureDef = FixtureDef(topCircleShape)..friction = 0; fixturesDef.add(topCircleFixtureDef); final bottomCircleShape = CircleShape()..radius = circleRadius; bottomCircleShape.position.setValues(0, -_length / 2); - final bottomCircleFixtureDef = FixtureDef(bottomCircleShape); + final bottomCircleFixtureDef = FixtureDef(bottomCircleShape)..friction = 0; fixturesDef.add(bottomCircleFixtureDef); final leftEdgeShape = EdgeShape() @@ -81,17 +84,20 @@ class Slingshot extends BodyComponent with InitialPosition { Vector2(circleRadius, _length / 2), Vector2(circleRadius, -_length / 2), ); - final leftEdgeShapeFixtureDef = FixtureDef(leftEdgeShape)..restitution = 5; + final leftEdgeShapeFixtureDef = FixtureDef(leftEdgeShape) + ..friction = 0 + ..restitution = 5; fixturesDef.add(leftEdgeShapeFixtureDef); - final righttEdgeShape = EdgeShape() + final rightEdgeShape = EdgeShape() ..set( Vector2(-circleRadius, _length / 2), Vector2(-circleRadius, -_length / 2), ); - final righttEdgeShapeFixtureDef = FixtureDef(righttEdgeShape) + final rightEdgeShapeFixtureDef = FixtureDef(rightEdgeShape) + ..friction = 0 ..restitution = 5; - fixturesDef.add(righttEdgeShapeFixtureDef); + fixturesDef.add(rightEdgeShapeFixtureDef); return fixturesDef; } diff --git a/packages/pinball_components/test/src/components/golden/slingshots.png b/packages/pinball_components/test/src/components/golden/slingshots.png new file mode 100644 index 00000000..2e4ada7b Binary files /dev/null and b/packages/pinball_components/test/src/components/golden/slingshots.png differ diff --git a/packages/pinball_components/test/src/components/slingshot_test.dart b/packages/pinball_components/test/src/components/slingshot_test.dart new file mode 100644 index 00000000..6f015e13 --- /dev/null +++ b/packages/pinball_components/test/src/components/slingshot_test.dart @@ -0,0 +1,97 @@ +// 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('Slingshot', () { + final flameTester = FlameTester(TestGame.new); + const length = 2.0; + const angle = 0.0; + final spritePath = Assets.images.slingshot.leftUpper.keyName; + + flameTester.testGameWidget( + 'renders correctly', + setUp: (game, tester) async { + await game.addFromBlueprint(Slingshots()); + await game.ready(); + game.camera.followVector2(Vector2.zero()); + }, + // TODO(allisonryan0002): enable test when workflows are fixed. + // verify: (game, tester) async { + // await expectLater( + // find.byGame(), + // matchesGoldenFile('golden/slingshots.png'), + // ); + // }, + ); + + flameTester.test( + 'loads correctly', + (game) async { + final slingshot = Slingshot( + length: length, + angle: angle, + spritePath: spritePath, + ); + await game.ensureAdd(slingshot); + + expect(game.contains(slingshot), isTrue); + }, + ); + + flameTester.test( + 'body is static', + (game) async { + final slingshot = Slingshot( + length: length, + angle: angle, + spritePath: spritePath, + ); + 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: spritePath, + ); + 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: spritePath, + ); + await game.ensureAdd(slingshot); + + final totalFriction = slingshot.body.fixtures.fold( + 0, + (total, fixture) => total + fixture.friction, + ); + expect(totalFriction, equals(0)); + }, + ); + }); +}