diff --git a/lib/game/components/boundaries.dart b/lib/game/components/boundaries.dart deleted file mode 100644 index 08e9a2c3..00000000 --- a/lib/game/components/boundaries.dart +++ /dev/null @@ -1,41 +0,0 @@ -import 'package:flame_forge2d/body_component.dart'; -import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:flame_forge2d/forge2d_game.dart'; -import 'package:forge2d/forge2d.dart'; - -List createBoundaries(Forge2DGame game) { - final topLeft = Vector2.zero(); - final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize); - final topRight = Vector2(bottomRight.x, topLeft.y); - final bottomLeft = Vector2(topLeft.x, bottomRight.y); - - return [ - Wall(topLeft, topRight), - Wall(topRight, bottomRight), - Wall(bottomRight, bottomLeft), - Wall(bottomLeft, topLeft), - ]; -} - -class Wall extends BodyComponent { - Wall(this.start, this.end); - - final Vector2 start; - final Vector2 end; - - @override - Body createBody() { - final shape = EdgeShape()..set(start, end); - - final fixtureDef = FixtureDef(shape) - ..restitution = 0.0 - ..friction = 0.3; - - final bodyDef = BodyDef() - ..userData = this - ..position = Vector2.zero() - ..type = BodyType.static; - - return world.createBody(bodyDef)..createFixture(fixtureDef); - } -} diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart index 97f7eb69..28541aed 100644 --- a/lib/game/components/components.dart +++ b/lib/game/components/components.dart @@ -1,4 +1,3 @@ export 'ball.dart'; -export 'boundaries.dart'; export 'plunger.dart'; export 'score_points.dart'; diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index fbcea02f..80b4ffea 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -1,43 +1,12 @@ -import 'package:flame/input.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; -import 'package:pinball/game/game.dart'; -class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents { - late Plunger plunger; +import 'package:pinball/game/game.dart'; +class PinballGame extends Forge2DGame with FlameBloc { @override Future onLoad() async { await super.onLoad(); addContactCallback(BallScorePointsCallback()); - - final boundaries = createBoundaries(this)..forEach(add); - final bottomWall = boundaries[2]; - - final center = screenToWorld(camera.viewport.effectiveSize / 2); - - await add(plunger = Plunger(Vector2(center.x, center.y))); - - world.createJoint( - PlungerAnchorPrismaticJointDef(plunger: plunger, anchor: bottomWall), - ); - } - - @override - KeyEventResult onKeyEvent( - RawKeyEvent event, - Set keysPressed, - ) { - if (event is RawKeyUpEvent && - event.data.logicalKey == LogicalKeyboardKey.space) { - plunger.release(); - } - if (event is RawKeyDownEvent && - event.data.logicalKey == LogicalKeyboardKey.space) { - plunger.pull(); - } - return KeyEventResult.handled; } } diff --git a/test/game/components/plunger_test.dart b/test/game/components/plunger_test.dart index f310874d..54a477e3 100644 --- a/test/game/components/plunger_test.dart +++ b/test/game/components/plunger_test.dart @@ -125,12 +125,12 @@ void main() { setUp(() { plunger = Plunger(Vector2.zero()); - anchor = Plunger(Vector2(0, -5)); + anchor = Plunger(Vector2(0, -1)); }); group('initializes with', () { flameTester.test( - 'plunger as bodyA', + 'plunger body as bodyA', (game) async { await game.ensureAddAll([plunger, anchor]); @@ -139,12 +139,12 @@ void main() { anchor: anchor, ); - expect(jointDef.bodyA, equals(plunger)); + expect(jointDef.bodyA, equals(plunger.body)); }, ); flameTester.test( - 'anchor as bodyB', + 'anchor body as bodyB', (game) async { await game.ensureAddAll([plunger, anchor]); @@ -154,7 +154,7 @@ void main() { ); game.world.createJoint(jointDef); - expect(jointDef.bodyB, equals(anchor)); + expect(jointDef.bodyB, equals(anchor.body)); }, ); @@ -203,5 +203,44 @@ void main() { }, ); }); + + flameTester.widgetTest( + 'plunger cannot go below anchor', + (game, tester) async { + await game.ensureAddAll([plunger, anchor]); + + final jointDef = PlungerAnchorPrismaticJointDef( + plunger: plunger, + anchor: anchor, + ); + game.world.createJoint(jointDef); + + plunger.pull(); + await tester.pump(const Duration(seconds: 1)); + + expect(plunger.body.position.y > anchor.body.position.y, isTrue); + }, + ); + + flameTester.widgetTest( + 'plunger cannot excessively exceed starting position', + (game, tester) async { + await game.ensureAddAll([plunger, anchor]); + + final jointDef = PlungerAnchorPrismaticJointDef( + plunger: plunger, + anchor: anchor, + ); + game.world.createJoint(jointDef); + + plunger.pull(); + await tester.pump(const Duration(seconds: 1)); + + plunger.release(); + await tester.pump(const Duration(seconds: 1)); + + expect(plunger.body.position.y < 1, isTrue); + }, + ); }); }