diff --git a/lib/game/components/board.dart b/lib/game/components/board.dart index 80db1711..674c3e43 100644 --- a/lib/game/components/board.dart +++ b/lib/game/components/board.dart @@ -6,7 +6,6 @@ import 'package:pinball/game/game.dart'; /// /// The bottom [Component]s are the [Flipper]s and the [Baseboard]s. /// {@endtemplate} -// TODO(alestiago): Add [SlingShot] once provided. // TODO(alestiago): Consider renaming once entire Board is defined. class BottomGroup extends Component { /// {@macro bottom_group} @@ -70,7 +69,15 @@ class _BottomGroupSide extends Component { Flipper.height, ), ); + final slingShot = SlingShot( + side: _side, + position: _position + + Vector2( + (Flipper.width) * direction, + Flipper.height + SlingShot.size.y, + ), + ); - await addAll([flipper, baseboard]); + await addAll([flipper, baseboard, slingShot]); } } diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart index aa50227d..4e165148 100644 --- a/lib/game/components/components.dart +++ b/lib/game/components/components.dart @@ -4,6 +4,7 @@ export 'board.dart'; export 'board_side.dart'; export 'bonus_word.dart'; export 'flipper.dart'; +export 'initial_position.dart'; export 'jetpack_ramp.dart'; export 'joint_anchor.dart'; export 'launcher_ramp.dart'; diff --git a/lib/game/components/initial_position.dart b/lib/game/components/initial_position.dart new file mode 100644 index 00000000..aa4acb46 --- /dev/null +++ b/lib/game/components/initial_position.dart @@ -0,0 +1,17 @@ +import 'package:flame_forge2d/flame_forge2d.dart'; + +/// Forces a given [BodyComponent] to position their [body] to an +/// [initialPosition]. +mixin InitialPosition on BodyComponent { + /// The initial position of the [body]. + late final Vector2 initialPosition; + + @override + void onMount() { + super.onMount(); + assert( + body.position == initialPosition, + 'Body position is not equal to initial position.', + ); + } +} diff --git a/lib/game/components/sling_shot.dart b/lib/game/components/sling_shot.dart index dacf1ee5..0791cf4f 100644 --- a/lib/game/components/sling_shot.dart +++ b/lib/game/components/sling_shot.dart @@ -33,18 +33,19 @@ class SlingShot extends BodyComponent { /// left. final BoardSide _side; + /// The size of the [SlingShot] body. + // TODO(alestiago): Use size from PositionedBodyComponent instead, + // once a sprite is given. + static final Vector2 size = Vector2(6, 8); + List _createFixtureDefs() { final fixtures = []; - // TODO(alestiago): Use size from PositionedBodyComponent instead, - // once a sprite is given. - final size = Vector2(10, 10); - // TODO(alestiago): This magic number can be deduced by specifying the // angle and using polar coordinate system to place the bottom right // vertex. // Something as: y = -size.y * math.cos(angle) - const additionalIncrement = 2; + const additionalIncrement = 3; final triangleVertices = _side.isLeft ? [ Vector2(0, 0), @@ -78,7 +79,7 @@ class SlingShot extends BodyComponent { ); // TODO(alestiago): Play with restitution value once game is bundled. final kickerFixtureDef = FixtureDef(kicker) - ..restitution = 20.0 + ..restitution = 10.0 ..friction = 0; fixtures.add(kickerFixtureDef); diff --git a/lib/game/components/wall.dart b/lib/game/components/wall.dart index c433365c..017f8c4d 100644 --- a/lib/game/components/wall.dart +++ b/lib/game/components/wall.dart @@ -26,7 +26,7 @@ class Wall extends BodyComponent { final fixtureDef = FixtureDef(shape) ..restitution = 0.1 - ..friction = 0.3; + ..friction = 0; final bodyDef = BodyDef() ..userData = this diff --git a/test/game/components/board_test.dart b/test/game/components/board_test.dart index eaa916c3..34a628a8 100644 --- a/test/game/components/board_test.dart +++ b/test/game/components/board_test.dart @@ -45,10 +45,10 @@ void main() { await game.ready(); await game.ensureAdd(bottomGroup); - final leftFlippers = bottomGroup.findNestedChildren( + final rightFlippers = bottomGroup.findNestedChildren( condition: (flipper) => flipper.side.isRight, ); - expect(leftFlippers.length, equals(1)); + expect(rightFlippers.length, equals(1)); }, ); @@ -59,8 +59,20 @@ void main() { await game.ready(); await game.ensureAdd(bottomGroup); - final leftFlippers = bottomGroup.findNestedChildren(); - expect(leftFlippers.length, equals(2)); + final baseboards = bottomGroup.findNestedChildren(); + expect(baseboards.length, equals(2)); + }, + ); + + flameTester.test( + 'has two SlingShots', + (game) async { + final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + await game.ready(); + await game.ensureAdd(bottomGroup); + + final slingShots = bottomGroup.findNestedChildren(); + expect(slingShots.length, equals(2)); }, ); }); diff --git a/test/game/components/initial_position_test.dart b/test/game/components/initial_position_test.dart new file mode 100644 index 00000000..393d780a --- /dev/null +++ b/test/game/components/initial_position_test.dart @@ -0,0 +1,66 @@ +// 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/game/game.dart'; + +class TestBodyComponent extends BodyComponent with InitialPosition { + @override + Body createBody() { + return world.createBody(BodyDef()); + } +} + +class TestPositionedBodyComponent extends BodyComponent with InitialPosition { + @override + Body createBody() { + return world.createBody(BodyDef()..position = initialPosition); + } +} + +void main() { + final flameTester = FlameTester(Forge2DGame.new); + group('InitialPosition', () { + test('correctly sets and gets', () { + final component = TestBodyComponent()..initialPosition = Vector2(1, 2); + expect(component.initialPosition, Vector2(1, 2)); + }); + + test('can only be set once', () { + final component = TestBodyComponent()..initialPosition = Vector2(1, 2); + expect( + () => component.initialPosition = Vector2(3, 4), + throwsA(isA()), + ); + }); + + flameTester.test( + 'returns normally ' + 'when the body sets the position to initial position', + (game) async { + final component = TestPositionedBodyComponent() + ..initialPosition = Vector2.zero(); + + await expectLater( + () async => game.ensureAdd(component), + returnsNormally, + ); + }, + ); + + flameTester.test( + 'throws AssertionError ' + 'when not setting initialPosition to body', + (game) async { + final component = TestBodyComponent()..initialPosition = Vector2.zero(); + await game.ensureAdd(component); + + await expectLater( + () async => game.ensureAdd(component), + throwsAssertionError, + ); + }, + ); + }); +} diff --git a/test/game/components/wall_test.dart b/test/game/components/wall_test.dart index 774cd675..53d387fa 100644 --- a/test/game/components/wall_test.dart +++ b/test/game/components/wall_test.dart @@ -106,7 +106,7 @@ void main() { ); flameTester.test( - 'has friction', + 'has no friction', (game) async { final wall = Wall( start: Vector2.zero(), @@ -115,7 +115,7 @@ void main() { await game.ensureAdd(wall); final fixture = wall.body.fixtures[0]; - expect(fixture.friction, greaterThan(0)); + expect(fixture.friction, equals(0)); }, ); });