diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 7e03f894..259825ee 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -9,7 +9,7 @@ import 'package:flutter/services.dart'; import 'package:pinball/game/game.dart'; /// {@template flipper_group} -/// +/// Adds a [Flipper.right] and a [Flipper.left]. /// {@endtemplate} class FlipperGroup extends PositionComponent with HasGameRef { /// @macro {flipper_group} @@ -30,17 +30,6 @@ class FlipperGroup extends PositionComponent with HasGameRef { ), ); await add(leftFlipper); - final leftFlipperAnchor = FlipperAnchor(flipper: leftFlipper); - await add(leftFlipperAnchor); - - final leftFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( - flipper: leftFlipper, - anchor: leftFlipperAnchor, - ); - // TODO(alestiago): Remove casting once the following is closed: - // https://github.com/flame-engine/forge2d/issues/36 - final leftFlipperRevoluteJoint = - gameRef.world.createJoint(leftFlipperRevoluteJointDef) as RevoluteJoint; final rightFlipper = Flipper.right( position: Vector2( @@ -49,37 +38,6 @@ class FlipperGroup extends PositionComponent with HasGameRef { ), ); await add(rightFlipper); - final rightFlipperAnchor = FlipperAnchor(flipper: rightFlipper); - await add(rightFlipperAnchor); - final rightFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( - flipper: rightFlipper, - anchor: rightFlipperAnchor, - ); - // TODO(alestiago): Remove casting once the following is closed: - // https://github.com/flame-engine/forge2d/issues/36 - final rightFlipperRevoluteJoint = gameRef.world - .createJoint(rightFlipperRevoluteJointDef) as RevoluteJoint; - - // TODO(erickzanardo): Clean this once the issue is solved: - // https://github.com/flame-engine/flame/issues/1417 - // FIXME(erickzanardo): when mounted the initial position is not fully - // reached. - unawaited( - leftFlipper.hasMounted.future.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock( - leftFlipperRevoluteJoint, - leftFlipper.side, - ), - ), - ); - unawaited( - rightFlipper.hasMounted.future.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock( - rightFlipperRevoluteJoint, - rightFlipper.side, - ), - ), - ); } } @@ -152,9 +110,31 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { /// [onKeyEvent] method listens to when one of these keys is pressed. final List _keys; + late final RevoluteJoint _joint; + + /// Applies downward linear velocity to the [Flipper], moving it to its + /// resting position. + void _moveDown() { + body.linearVelocity = Vector2(0, -_speed); + } + + /// Applies upward linear velocity to the [Flipper], moving it to its highest + /// position. + void _moveUp() { + body.linearVelocity = Vector2(0, _speed); + } + @override Future onLoad() async { await super.onLoad(); + await Future.wait([ + _loadSprite(), + _anchorToJoint(), + ]); + } + + /// Loads the sprite that renders with the [Flipper]. + Future _loadSprite() async { final sprite = await gameRef.loadSprite(spritePath); positionComponent = SpriteComponent( sprite: sprite, @@ -166,16 +146,39 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { } } - /// Applies downward linear velocity to the [Flipper], moving it to its - /// resting position. - void _moveDown() { - body.linearVelocity = Vector2(0, -_speed); + /// Anchors the [Flipper] to the [RevoluteJoint] that controls its arc motion. + Future _anchorToJoint() async { + final anchor = FlipperAnchor(flipper: this); + await add(anchor); + + final jointDef = FlipperAnchorRevoluteJointDef( + flipper: this, + anchor: anchor, + ); + // TODO(alestiago): Remove casting once the following is closed: + // https://github.com/flame-engine/forge2d/issues/36 + _joint = world.createJoint(jointDef) as RevoluteJoint; + + // FIXME(erickzanardo): when mounted the initial position is not fully + // reached. + unawaited( + mounted.whenComplete( + () => FlipperAnchorRevoluteJointDef.unlock(_joint, side), + ), + ); } - /// Applies upward linear velocity to the [Flipper], moving it to its highest - /// position. - void _moveUp() { - body.linearVelocity = Vector2(0, _speed); + @override + Body createBody() { + final bodyDef = BodyDef() + ..gravityScale = 0 + ..type = BodyType.dynamic + ..position = _position; + + final body = world.createBody(bodyDef); + _createFixtureDefs().forEach(body.createFixture); + + return body; } List _createFixtureDefs() { @@ -224,30 +227,6 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { return fixtures; } - @override - Body createBody() { - final bodyDef = BodyDef() - ..gravityScale = 0 - ..type = BodyType.dynamic - ..position = _position; - - final body = world.createBody(bodyDef); - _createFixtureDefs().forEach(body.createFixture); - - return body; - } - - // TODO(erickzanardo): Remove this once the issue is solved: - // https://github.com/flame-engine/flame/issues/1417 - // ignore: public_member_api_docs - final Completer hasMounted = Completer(); - - @override - void onMount() { - super.onMount(); - hasMounted.complete(); - } - @override bool onKeyEvent( RawKeyEvent event,