From f6db096ac60dfef0af78f49cd47b3f30c942a53f Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Fri, 26 Feb 2021 09:45:50 -0500 Subject: [PATCH] Small fixes and nits to make null-safe simpler (#686) --- README.md | 12 ++++++------ analysis_options.yaml | 2 +- doc/examples/gestures/lib/main_mouse_movement.dart | 2 +- doc/examples/gestures/lib/main_multitap.dart | 8 +------- .../gestures/lib/main_multitap_advanced.dart | 10 ++-------- .../gestures/lib/main_overlapping_tapables.dart | 7 +++---- doc/examples/keyboard/pubspec.yaml | 2 -- doc/examples/layers/pubspec.yaml | 2 -- doc/examples/particles/lib/main.dart | 10 +++------- doc/examples/timer/lib/main.dart | 3 ++- doc/examples/widgets/pubspec.yaml | 1 - lib/components.dart | 7 ------- lib/src/anchor.dart | 8 ++++---- lib/src/components/isometric_tile_map_component.dart | 4 +--- lib/src/components/joystick/joystick_component.dart | 4 +--- lib/src/widgets/animation_widget.dart | 2 +- lib/src/widgets/sprite_button.dart | 2 +- test/anchor_test.dart | 7 +++++++ test/components/position_component_test.dart | 2 +- test/util/mock_canvas.dart | 8 ++++++-- 20 files changed, 41 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 582da47dd..1aef815b3 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,13 @@ A minimalistic Flutter game engine. ## About 1.0.0 -This is a release candidate for version 1.0.0 of Flame. This is not ready for production use and the documentation found here is not completely updated according to the code, also everything here can be changed before the full release. +Our goal is to release v1 soon. We are periodically launching RCs (release candidates) as we evolve the code, and we are already quite happy with where we are (but there might still be further changes). Our documentation is still not completely updated to v1, and things are still open to change. -Use this version to get a preview of how the new Flame version will be and also to give the team feedback about the new structure and/or features. +Please use this version to get a preview of the new Flame version and also to give the team feedback about the new structure and/or features. -Checkout the branches `develop-v0.x` and `master-v0.x` for the current last 0.x version. However no new updates will be added to v0 except for bugfixes. +The `master` branch is the bleeding edge of the v1 migration. The `master-v0.x` branch is the latest v0 release (where we are still merging some patches and crucial fixes). -The current v1 release is `1.0.0-rc6` on pub. The last stable version so far is `0.29.0`. Feel free to choose the one that better suits your need. +The current v1 release is on pub. The latest stable version so far is . Feel free to choose the one that better suits your need. --- @@ -99,13 +99,13 @@ You can also show on your repository that your game is made with Flame by using ## Contributing -__Warning__: We are working on bringing Flame to its first stable version, updates on `0.x` versions are frozen, except for bugs. If you want to contribute on that version, by sure that it is a bugfix. For contributions for the stable version, your PR must point to the `v1.0.0` branch and by sure to talk about your contribution to the team, which is accessible on [Discord](https://discord.gg/pxrBmy4). +__Warning__: We are working on bringing Flame to its first stable version, updates on `0.x` versions are frozen, except for crucial bug fixes. If you want to contribute to that version, please be mindful of that, and use the `master-v0.x` branch. For contributions for v1, your PR must point to the `master` branch. If in doubt, make sure to talk about your contribution to the team, either via an issue or [Discord](https://discord.gg/pxrBmy4). Any help is appreciated! Comments, suggestions, issues, PRs. Have you found a bug or have a suggestion of how to enhance Flame, open an issue and we will take a look at it as soon as possible. -Do you want to contribute with a PR? PRs are always welcome, just be sure to create it from the `develop` branch and follow the [checklist](.github/pull_request_template.md) which will appear when you open it. +Do you want to contribute with a PR? PRs are always welcome, just be sure to create it from the correct branch (see above) and follow the [checklist](.github/pull_request_template.md) which will appear when you open it. ## Getting started diff --git a/analysis_options.yaml b/analysis_options.yaml index d66de7320..bea71c834 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -143,7 +143,7 @@ dart_code_metrics: - test/** metrics: number-of-arguments: 8 - number-of-methods: 28 + number-of-methods: 32 lines-of-executable-code: 200 cyclomatic-complexity: 36 diff --git a/doc/examples/gestures/lib/main_mouse_movement.dart b/doc/examples/gestures/lib/main_mouse_movement.dart index 0ec1c7d36..d688ce317 100644 --- a/doc/examples/gestures/lib/main_mouse_movement.dart +++ b/doc/examples/gestures/lib/main_mouse_movement.dart @@ -16,7 +16,7 @@ void main() { class MyGame extends BaseGame with MouseMovementDetector { static const speed = 200.0; - Vector2 position = Vector2(0, 0); + Vector2 position = Vector2.zero(); Vector2 target; final Paint _blue = Paint()..color = const Color(0xFF0000FF); diff --git a/doc/examples/gestures/lib/main_multitap.dart b/doc/examples/gestures/lib/main_multitap.dart index cd0455c6c..42c1837b9 100644 --- a/doc/examples/gestures/lib/main_multitap.dart +++ b/doc/examples/gestures/lib/main_multitap.dart @@ -15,14 +15,8 @@ void main() { class MyGame extends BaseGame with MultiTouchTapDetector { final _whitePaint = BasicPalette.white.paint; - Paint _paint; - final Map _taps = {}; - MyGame() { - _paint = _whitePaint; - } - @override void onTapDown(int pointerId, TapDownDetails details) { _taps[pointerId] = Rect.fromLTWH( @@ -47,7 +41,7 @@ class MyGame extends BaseGame with MultiTouchTapDetector { void render(Canvas canvas) { super.render(canvas); _taps.values.forEach((rect) { - canvas.drawRect(rect, _paint); + canvas.drawRect(rect, _whitePaint); }); } } diff --git a/doc/examples/gestures/lib/main_multitap_advanced.dart b/doc/examples/gestures/lib/main_multitap_advanced.dart index 44d9c2536..2517caa49 100644 --- a/doc/examples/gestures/lib/main_multitap_advanced.dart +++ b/doc/examples/gestures/lib/main_multitap_advanced.dart @@ -17,18 +17,12 @@ class MyGame extends BaseGame with MultiTouchTapDetector, MultiTouchDragDetector { final _whitePaint = BasicPalette.white.paint; - Paint _paint; - final Map _taps = {}; Vector2 _start; Vector2 _end; Rect _panRect; - MyGame() { - _paint = _whitePaint; - } - @override void onTapDown(int pointerId, TapDownDetails details) { _taps[pointerId] = Rect.fromLTWH( @@ -83,11 +77,11 @@ class MyGame extends BaseGame void render(Canvas canvas) { super.render(canvas); _taps.values.forEach((rect) { - canvas.drawRect(rect, _paint); + canvas.drawRect(rect, _whitePaint); }); if (_panRect != null) { - canvas.drawRect(_panRect, _paint); + canvas.drawRect(_panRect, _whitePaint); } } } diff --git a/doc/examples/gestures/lib/main_overlapping_tapables.dart b/doc/examples/gestures/lib/main_overlapping_tapables.dart index 9cb217698..1abbd33ef 100644 --- a/doc/examples/gestures/lib/main_overlapping_tapables.dart +++ b/doc/examples/gestures/lib/main_overlapping_tapables.dart @@ -20,7 +20,7 @@ void main() { } class TapableSquare extends PositionComponent with Tapable { - Paint _randomPaint() { + static Paint _randomPaint() { final rng = math.Random(); final color = Color.fromRGBO( rng.nextInt(256), @@ -31,10 +31,9 @@ class TapableSquare extends PositionComponent with Tapable { return PaletteEntry(color).paint; } - Paint currentPaint; + final Paint _paint = _randomPaint(); TapableSquare({Vector2 position}) { - currentPaint = _randomPaint(); size = Vector2.all(100); this.position = position ?? Vector2.all(100); } @@ -42,7 +41,7 @@ class TapableSquare extends PositionComponent with Tapable { @override void render(Canvas canvas) { super.render(canvas); - canvas.drawRect(size.toRect(), currentPaint); + canvas.drawRect(size.toRect(), _paint); } @override diff --git a/doc/examples/keyboard/pubspec.yaml b/doc/examples/keyboard/pubspec.yaml index 0d499be84..5641c9609 100644 --- a/doc/examples/keyboard/pubspec.yaml +++ b/doc/examples/keyboard/pubspec.yaml @@ -14,8 +14,6 @@ dependencies: flame: path: ../../../ - cupertino_icons: ^0.1.2 - dev_dependencies: flutter_test: sdk: flutter diff --git a/doc/examples/layers/pubspec.yaml b/doc/examples/layers/pubspec.yaml index d1a64a83c..6f78bcd7a 100644 --- a/doc/examples/layers/pubspec.yaml +++ b/doc/examples/layers/pubspec.yaml @@ -14,8 +14,6 @@ dependencies: flame: path: ../../../ - cupertino_icons: ^0.1.3 - dev_dependencies: flutter_test: sdk: flutter diff --git a/doc/examples/particles/lib/main.dart b/doc/examples/particles/lib/main.dart index 13b8eb701..f61735d30 100644 --- a/doc/examples/particles/lib/main.dart +++ b/doc/examples/particles/lib/main.dart @@ -37,17 +37,14 @@ class MyGame extends BaseGame { /// Defines the lifespan of all the particles in these examples final sceneDuration = const Duration(seconds: 1); - Vector2 cellSize; - Vector2 halfCellSize; + Vector2 get cellSize => size / gridSize; + Vector2 get halfCellSize => cellSize / 2; @override Future onLoad() async { await images.load('zap.png'); await images.load('boom3.png'); - cellSize = size / gridSize; - halfCellSize = cellSize * .5; - // Spawn new particles every second Timer.periodic(sceneDuration, (_) => spawnParticles()); } @@ -87,8 +84,7 @@ class MyGame extends BaseGame { final particle = particles.removeLast(); final col = particles.length % gridSize; final row = (particles.length ~/ gridSize).toDouble(); - final cellCenter = - (cellSize.clone()..multiply(Vector2(col, row))) + (cellSize * .5); + final cellCenter = (cellSize..multiply(Vector2(col, row))) + halfCellSize; add( // Bind all the particles to a [Component] update diff --git a/doc/examples/timer/lib/main.dart b/doc/examples/timer/lib/main.dart index e71d99970..ddd6d735f 100644 --- a/doc/examples/timer/lib/main.dart +++ b/doc/examples/timer/lib/main.dart @@ -65,7 +65,8 @@ class MyGame extends Game with TapDetector { int elapsedSecs = 0; - MyGame() { + @override + Future onLoad() async { countdown = Timer(2); interval = Timer( 1, diff --git a/doc/examples/widgets/pubspec.yaml b/doc/examples/widgets/pubspec.yaml index 838dee395..8a5f87641 100644 --- a/doc/examples/widgets/pubspec.yaml +++ b/doc/examples/widgets/pubspec.yaml @@ -15,7 +15,6 @@ dependencies: path: ../../../ dashbook: ^0.0.6 - cupertino_icons: ^0.1.2 flutter: uses-material-design: true assets: diff --git a/lib/components.dart b/lib/components.dart index d4cbca5cc..edfd4a28c 100644 --- a/lib/components.dart +++ b/lib/components.dart @@ -1,20 +1,14 @@ export 'joystick.dart'; -export 'joystick.dart'; export 'src/anchor.dart'; export 'src/components/base_component.dart'; export 'src/components/component.dart'; export 'src/components/isometric_tile_map_component.dart'; export 'src/components/mixins/collidable.dart'; export 'src/components/mixins/draggable.dart'; -export 'src/components/mixins/draggable.dart'; export 'src/components/mixins/has_collidables.dart'; export 'src/components/mixins/has_game_ref.dart'; -export 'src/components/mixins/has_game_ref.dart'; -export 'src/components/mixins/hitbox.dart'; export 'src/components/mixins/hitbox.dart'; export 'src/components/mixins/single_child_particle.dart'; -export 'src/components/mixins/single_child_particle.dart'; -export 'src/components/mixins/tapable.dart'; export 'src/components/mixins/tapable.dart'; export 'src/components/nine_tile_box_component.dart'; export 'src/components/parallax_component.dart'; @@ -28,4 +22,3 @@ export 'src/components/text_component.dart'; export 'src/extensions/vector2.dart'; export 'src/text_config.dart'; export 'src/timer.dart'; -export 'src/timer.dart'; diff --git a/lib/src/anchor.dart b/lib/src/anchor.dart index dc1a0ff6d..22f0b0f00 100644 --- a/lib/src/anchor.dart +++ b/lib/src/anchor.dart @@ -52,7 +52,7 @@ class Anchor { /// /// This should only be used for serialization purposes. String get name { - return _valueNames.containsKey(this) ? _valueNames[this] : 'Anchor($x, $y)'; + return _valueNames[this] ?? 'Anchor($x, $y)'; } /// Returns a string representation of this Anchor. @@ -86,9 +86,9 @@ class Anchor { return _valueNames.entries.singleWhere((e) => e.value == name).key; } else { final regexp = RegExp(r'^\Anchor\(([^,]+), ([^\)]+)\)'); - final matches = regexp.allMatches(name).first.group; - assert(matches != null, 'Bad Anchor format'); - return Anchor(double.parse(matches(1)), double.parse(matches(2))); + final matches = regexp.firstMatch(name)?.groups([1, 2]); + assert(matches != null && matches.length == 2, 'Bad anchor format'); + return Anchor(double.parse(matches[0]), double.parse(matches[1])); } } diff --git a/lib/src/components/isometric_tile_map_component.dart b/lib/src/components/isometric_tile_map_component.dart index 8b658d801..01f8de924 100644 --- a/lib/src/components/isometric_tile_map_component.dart +++ b/lib/src/components/isometric_tile_map_component.dart @@ -37,9 +37,7 @@ class IsometricTileMapComponent extends PositionComponent { this.matrix, { this.destTileSize, Vector2 position, - }) { - this.position = position ?? Vector2.zero(); - } + }) : super(position: position); /// This is the size the tiles will be drawn (either original or overwritten). Vector2 get effectiveTileSize => destTileSize ?? tileset.srcSize; diff --git a/lib/src/components/joystick/joystick_component.dart b/lib/src/components/joystick/joystick_component.dart index 6c90aed4c..1e9839ce4 100644 --- a/lib/src/components/joystick/joystick_component.dart +++ b/lib/src/components/joystick/joystick_component.dart @@ -55,8 +55,6 @@ class JoystickComponent extends JoystickController { void removeAction(int actionId) { final action = children .firstWhere((e) => e is JoystickAction && e.actionId == actionId); - if (action != null) { - removeChild(action); - } + removeChild(action); } } diff --git a/lib/src/widgets/animation_widget.dart b/lib/src/widgets/animation_widget.dart index 4f3e24216..4829958c5 100644 --- a/lib/src/widgets/animation_widget.dart +++ b/lib/src/widgets/animation_widget.dart @@ -20,7 +20,7 @@ class SpriteAnimationWidget extends StatefulWidget { final bool playing; const SpriteAnimationWidget({ - this.animation, + @required this.animation, this.playing = true, this.anchor = Anchor.topLeft, }); diff --git a/lib/src/widgets/sprite_button.dart b/lib/src/widgets/sprite_button.dart index 646824a96..e8a0bb0b1 100644 --- a/lib/src/widgets/sprite_button.dart +++ b/lib/src/widgets/sprite_button.dart @@ -42,7 +42,7 @@ class _ButtonState extends State { onTapUp: (_) { setState(() => _pressed = false); - widget.onPressed?.call(); + widget.onPressed.call(); }, child: Container( width: width, diff --git a/test/anchor_test.dart b/test/anchor_test.dart index 840bc578c..94c9f5dac 100644 --- a/test/anchor_test.dart +++ b/test/anchor_test.dart @@ -19,5 +19,12 @@ void main() { expect(const Anchor(0.2, 0.2).toString(), 'Anchor(0.2, 0.2)'); expect(Anchor.valueOf('Anchor(0.2, 0.2)'), const Anchor(0.2, 0.2)); }); + + test('fail to parse invalid anchor', () async { + expect( + () => Anchor.valueOf('foobar'), + throwsA(const TypeMatcher()), + ); + }); }); } diff --git a/test/components/position_component_test.dart b/test/components/position_component_test.dart index 9bed3d6fd..d619c5d7f 100644 --- a/test/components/position_component_test.dart +++ b/test/components/position_component_test.dart @@ -1,7 +1,7 @@ import 'dart:math' as math; import 'package:flame/components.dart'; -import 'package:flame/src/geometry/polygon.dart'; +import 'package:flame/geometry.dart'; import 'package:test/test.dart'; class MyComponent extends PositionComponent {} diff --git a/test/util/mock_canvas.dart b/test/util/mock_canvas.dart index d8e152b02..41c5f9cc9 100644 --- a/test/util/mock_canvas.dart +++ b/test/util/mock_canvas.dart @@ -16,8 +16,12 @@ class MockCanvas extends Fake implements Canvas { @override void scale(double sx, [double sy]) { final ssx = _normalize(sx); - final ssy = _normalize(sy); - methodCalls.add('scale($ssx, $ssy)'); + if (sy == null) { + methodCalls.add('scale($ssx)'); + } else { + final ssy = _normalize(sy); + methodCalls.add('scale($ssx, $ssy)'); + } } double _normalize(double d) {