From 1539ab7bd3d15c6a73caf9baa7eab05d1e8a8f35 Mon Sep 17 00:00:00 2001 From: Erick Date: Mon, 14 Mar 2022 11:18:23 -0300 Subject: [PATCH] chore: update project to the latest flame and forge2d (#41) --- lib/game/components/ball.dart | 16 +++++-- lib/game/components/components.dart | 2 +- lib/game/components/flipper.dart | 43 +++++++++++-------- .../{anchor.dart => joint_anchor.dart} | 8 ++-- lib/game/components/plunger.dart | 10 ++--- pubspec.lock | 26 +++++------ pubspec.yaml | 6 +-- test/game/components/flipper_test.dart | 4 +- ...nchor_test.dart => joint_anchor_test.dart} | 10 ++--- 9 files changed, 69 insertions(+), 56 deletions(-) rename lib/game/components/{anchor.dart => joint_anchor.dart} (85%) rename test/game/components/{anchor_test.dart => joint_anchor_test.dart} (83%) diff --git a/lib/game/components/ball.dart b/lib/game/components/ball.dart index d5b05fa1..738ceec6 100644 --- a/lib/game/components/ball.dart +++ b/lib/game/components/ball.dart @@ -6,16 +6,18 @@ import 'package:pinball/game/game.dart'; /// A solid, [BodyType.dynamic] sphere that rolls and bounces along the /// [PinballGame]. /// {@endtemplate} -class Ball extends PositionBodyComponent { +class Ball extends BodyComponent { /// {@macro ball} Ball({ required Vector2 position, - }) : _position = position, - super(size: Vector2.all(2)); + }) : _position = position; /// The initial position of the [Ball] body. final Vector2 _position; + /// The size of the [Ball] + final Vector2 size = Vector2.all(2); + /// Asset location of the sprite that renders with the [Ball]. /// /// Sprite is preloaded by [PinballGameAssetsX]. @@ -26,7 +28,13 @@ class Ball extends PositionBodyComponent { await super.onLoad(); final sprite = await gameRef.loadSprite(spritePath); final tint = gameRef.theme.characterTheme.ballColor.withOpacity(0.5); - positionComponent = SpriteComponent(sprite: sprite, size: size)..tint(tint); + await add( + SpriteComponent( + sprite: sprite, + size: size, + anchor: Anchor.center, + )..tint(tint), + ); } @override diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart index 6e663c26..71108fcc 100644 --- a/lib/game/components/components.dart +++ b/lib/game/components/components.dart @@ -1,9 +1,9 @@ -export 'anchor.dart'; export 'ball.dart'; export 'baseboard.dart'; export 'board_side.dart'; export 'bonus_word.dart'; export 'flipper.dart'; +export 'joint_anchor.dart'; export 'pathway.dart'; export 'plunger.dart'; export 'score_points.dart'; diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 6c184383..9035daaf 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -1,25 +1,29 @@ import 'dart:async'; import 'dart:math' as math; -import 'package:flame/components.dart' show PositionComponent, SpriteComponent; +import 'package:flame/components.dart'; import 'package:flame/input.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:pinball/game/game.dart'; /// {@template flipper_group} /// Loads a [Flipper.right] and a [Flipper.left]. /// {@endtemplate} -class FlipperGroup extends PositionComponent { +class FlipperGroup extends Component { /// {@macro flipper_group} FlipperGroup({ - required Vector2 position, + required this.position, required this.spacing, - }) : super(position: position); + }); /// The amount of space between the [Flipper.right] and [Flipper.left]. final double spacing; + /// The position of this [FlipperGroup] + final Vector2 position; + @override Future onLoad() async { final leftFlipper = Flipper.left( @@ -45,15 +49,14 @@ class FlipperGroup extends PositionComponent { /// /// [Flipper] can be controlled by the player in an arc motion. /// {@endtemplate flipper} -class Flipper extends PositionBodyComponent with KeyboardHandler { +class Flipper extends BodyComponent with KeyboardHandler { /// {@macro flipper} Flipper._({ required Vector2 position, required this.side, required List keys, }) : _position = position, - _keys = keys, - super(size: Vector2(width, height)); + _keys = keys; /// A left positioned [Flipper]. Flipper.left({ @@ -124,14 +127,17 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { /// Loads the sprite that renders with the [Flipper]. Future _loadSprite() async { final sprite = await gameRef.loadSprite(spritePath); - positionComponent = SpriteComponent( + final spriteComponent = SpriteComponent( sprite: sprite, - size: size, + size: Vector2(width, height), + anchor: Anchor.center, ); if (side.isRight) { - positionComponent!.flipHorizontally(); + spriteComponent.flipHorizontally(); } + + await add(spriteComponent); } /// Anchors the [Flipper] to the [RevoluteJoint] that controls its arc motion. @@ -160,11 +166,11 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { final fixtures = []; final isLeft = side.isLeft; - final bigCircleShape = CircleShape()..radius = size.y / 2; + final bigCircleShape = CircleShape()..radius = height / 2; bigCircleShape.position.setValues( isLeft - ? -(size.x / 2) + bigCircleShape.radius - : (size.x / 2) - bigCircleShape.radius, + ? -(width / 2) + bigCircleShape.radius + : (width / 2) - bigCircleShape.radius, 0, ); final bigCircleFixtureDef = FixtureDef(bigCircleShape); @@ -173,8 +179,8 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { final smallCircleShape = CircleShape()..radius = bigCircleShape.radius / 2; smallCircleShape.position.setValues( isLeft - ? (size.x / 2) - smallCircleShape.radius - : -(size.x / 2) + smallCircleShape.radius, + ? (width / 2) - smallCircleShape.radius + : -(width / 2) + smallCircleShape.radius, 0, ); final smallCircleFixtureDef = FixtureDef(smallCircleShape); @@ -205,6 +211,7 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { @override Future onLoad() async { await super.onLoad(); + paint = Paint()..color = Colors.transparent; await Future.wait([ _loadSprite(), _anchorToJoint(), @@ -249,15 +256,15 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { /// /// The end of a [Flipper] depends on its [Flipper.side]. /// {@endtemplate} -class FlipperAnchor extends Anchor { +class FlipperAnchor extends JointAnchor { /// {@macro flipper_anchor} FlipperAnchor({ required Flipper flipper, }) : super( position: Vector2( flipper.side.isLeft - ? flipper.body.position.x - flipper.size.x / 2 - : flipper.body.position.x + flipper.size.x / 2, + ? flipper.body.position.x - Flipper.width / 2 + : flipper.body.position.x + Flipper.width / 2, flipper.body.position.y, ), ); diff --git a/lib/game/components/anchor.dart b/lib/game/components/joint_anchor.dart similarity index 85% rename from lib/game/components/anchor.dart rename to lib/game/components/joint_anchor.dart index 0e78aa1c..05e62b73 100644 --- a/lib/game/components/anchor.dart +++ b/lib/game/components/joint_anchor.dart @@ -1,6 +1,6 @@ import 'package:flame_forge2d/flame_forge2d.dart'; -/// {@template anchor} +/// {@template joint_anchor} /// Non visual [BodyComponent] used to hold a [BodyType.dynamic] in [Joint]s /// with this [BodyType.static]. /// @@ -15,9 +15,9 @@ import 'package:flame_forge2d/flame_forge2d.dart'; /// ); /// ``` /// {@endtemplate} -class Anchor extends BodyComponent { - /// {@macro anchor} - Anchor({ +class JointAnchor extends BodyComponent { + /// {@macro joint_anchor} + JointAnchor({ required Vector2 position, }) : _position = position; diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 2ec2f599..bf63b462 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -1,7 +1,7 @@ import 'package:flame/input.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/services.dart'; -import 'package:pinball/game/game.dart' show Anchor; +import 'package:pinball/game/game.dart'; /// {@template plunger} /// [Plunger] serves as a spring, that shoots the ball on the right side of the @@ -77,9 +77,9 @@ class Plunger extends BodyComponent with KeyboardHandler { } /// {@template plunger_anchor} -/// [Anchor] positioned below a [Plunger]. +/// [JointAnchor] positioned below a [Plunger]. /// {@endtemplate} -class PlungerAnchor extends Anchor { +class PlungerAnchor extends JointAnchor { /// {@macro plunger_anchor} PlungerAnchor({ required Plunger plunger, @@ -92,11 +92,11 @@ class PlungerAnchor extends Anchor { } /// {@template plunger_anchor_prismatic_joint_def} -/// [PrismaticJointDef] between a [Plunger] and an [Anchor] with motion on +/// [PrismaticJointDef] between a [Plunger] and an [JointAnchor] with motion on /// the vertical axis. /// /// The [Plunger] is constrained vertically between its starting position and -/// the [Anchor]. The [Anchor] must be below the [Plunger]. +/// the [JointAnchor]. The [JointAnchor] must be below the [Plunger]. /// {@endtemplate} class PlungerAnchorPrismaticJointDef extends PrismaticJointDef { /// {@macro plunger_anchor_prismatic_joint_def} diff --git a/pubspec.lock b/pubspec.lock index 8ff31de7..73d0fc8d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,14 +7,14 @@ packages: name: _fe_analyzer_shared url: "https://pub.dartlang.org" source: hosted - version: "30.0.0" + version: "31.0.0" analyzer: dependency: transitive description: name: analyzer url: "https://pub.dartlang.org" source: hosted - version: "2.7.0" + version: "2.8.0" args: dependency: transitive description: @@ -35,14 +35,14 @@ packages: name: bloc url: "https://pub.dartlang.org" source: hosted - version: "8.0.2" + version: "8.0.3" bloc_test: dependency: "direct dev" description: name: bloc_test url: "https://pub.dartlang.org" source: hosted - version: "9.0.2" + version: "9.0.3" boolean_selector: dependency: transitive description: @@ -140,21 +140,21 @@ packages: name: flame url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-releasecandidate.4" + version: "1.1.0-releasecandidate.5" flame_bloc: dependency: "direct main" description: name: flame_bloc url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-releasecandidate.4" + version: "1.2.0-releasecandidate.5" flame_forge2d: dependency: "direct main" description: name: flame_forge2d url: "https://pub.dartlang.org" source: hosted - version: "0.9.0-releasecandidate.4" + version: "0.9.0-releasecandidate.5" flame_test: dependency: "direct dev" description: @@ -190,7 +190,7 @@ packages: name: forge2d url: "https://pub.dartlang.org" source: hosted - version: "0.8.1" + version: "0.8.2" frontend_server_client: dependency: transitive description: @@ -218,7 +218,7 @@ packages: name: http_multi_server url: "https://pub.dartlang.org" source: hosted - version: "3.0.1" + version: "3.2.0" http_parser: dependency: transitive description: @@ -246,7 +246,7 @@ packages: name: js url: "https://pub.dartlang.org" source: hosted - version: "0.6.3" + version: "0.6.4" logging: dependency: transitive description: @@ -351,14 +351,14 @@ packages: name: provider url: "https://pub.dartlang.org" source: hosted - version: "6.0.1" + version: "6.0.2" pub_semver: dependency: transitive description: name: pub_semver url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.1" shelf: dependency: transitive description: @@ -489,7 +489,7 @@ packages: name: vm_service url: "https://pub.dartlang.org" source: hosted - version: "7.3.0" + version: "7.5.0" watcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 39b93fe7..18905a10 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,9 +9,9 @@ environment: dependencies: bloc: ^8.0.2 equatable: ^2.0.3 - flame: ^1.1.0-releasecandidate.4 - flame_bloc: ^1.2.0-releasecandidate.4 - flame_forge2d: ^0.9.0-releasecandidate.4 + flame: ^1.1.0-releasecandidate.5 + flame_bloc: ^1.2.0-releasecandidate.5 + flame_forge2d: ^0.9.0-releasecandidate.5 flutter: sdk: flutter flutter_bloc: ^8.0.1 diff --git a/test/game/components/flipper_test.dart b/test/game/components/flipper_test.dart index 070265bc..ead927bf 100644 --- a/test/game/components/flipper_test.dart +++ b/test/game/components/flipper_test.dart @@ -103,9 +103,7 @@ void main() { ) as Flipper; expect( - leftFlipper.body.position.x + - leftFlipper.size.x + - flipperGroup.spacing, + leftFlipper.body.position.x + Flipper.width + flipperGroup.spacing, equals(rightFlipper.body.position.x), ); }, diff --git a/test/game/components/anchor_test.dart b/test/game/components/joint_anchor_test.dart similarity index 83% rename from test/game/components/anchor_test.dart rename to test/game/components/joint_anchor_test.dart index 49721947..a65f9e1a 100644 --- a/test/game/components/anchor_test.dart +++ b/test/game/components/joint_anchor_test.dart @@ -10,13 +10,13 @@ import '../../helpers/helpers.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); - group('Anchor', () { + group('JointAnchor', () { final flameTester = FlameTester(PinballGameTest.create); flameTester.test( 'loads correctly', (game) async { - final anchor = Anchor(position: Vector2.zero()); + final anchor = JointAnchor(position: Vector2.zero()); await game.ready(); await game.ensureAdd(anchor); @@ -30,7 +30,7 @@ void main() { (game) async { await game.ready(); final position = Vector2.all(10); - final anchor = Anchor(position: position); + final anchor = JointAnchor(position: position); await game.ensureAdd(anchor); game.contains(anchor); @@ -42,7 +42,7 @@ void main() { 'is static', (game) async { await game.ready(); - final anchor = Anchor(position: Vector2.zero()); + final anchor = JointAnchor(position: Vector2.zero()); await game.ensureAdd(anchor); expect(anchor.body.bodyType, equals(BodyType.static)); @@ -54,7 +54,7 @@ void main() { flameTester.test( 'has none', (game) async { - final anchor = Anchor(position: Vector2.zero()); + final anchor = JointAnchor(position: Vector2.zero()); await game.ensureAdd(anchor); expect(anchor.body.fixtures, isEmpty);