diff --git a/.github/workflows/flame_behaviors.yaml b/.github/workflows/flame_behaviors.yaml index 7fe2748..46446f5 100644 --- a/.github/workflows/flame_behaviors.yaml +++ b/.github/workflows/flame_behaviors.yaml @@ -13,8 +13,8 @@ on: paths: - ".github/workflows/flame_behaviors.yaml" - "packages/flame_behaviors/lib/**" - - packages/flame_behaviors/"test/**" - - packages/flame_behaviors/"pubspec.yaml" + - "packages/flame_behaviors/test/**" + - "packages/flame_behaviors/pubspec.yaml" jobs: build: @@ -22,7 +22,7 @@ jobs: with: working_directory: packages/flame_behaviors flutter_channel: stable - flutter_version: 3.7.3 + flutter_version: 3.13.5 pana: uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/pana.yml@v1 diff --git a/docs/input-behaviors.md b/docs/event-behaviors.md similarity index 71% rename from docs/input-behaviors.md rename to docs/event-behaviors.md index ba23513..f473d0c 100644 --- a/docs/input-behaviors.md +++ b/docs/event-behaviors.md @@ -1,7 +1,7 @@ -# Input Behaviors ⌨ +# Event Behaviors ⌨ -The `flame_behaviors` package also provides input behaviors. These behaviors are a layer over the -existing Flame input mixins for components. These behaviors will trigger when the user interacts with their parent entity. So these events are always relative to the parent entity. +The `flame_behaviors` package also provides event behaviors. These behaviors are a layer over the +existing Flame event mixins for components. These behaviors will trigger when the user interacts with their parent entity. So these events are always relative to the parent entity. ## TappableBehavior @@ -30,5 +30,5 @@ class MyDraggableBehavior extends DraggableBehavior { } ``` -[flame_drag_docs]: https://docs.flame-engine.org/1.7.0/flame/inputs/drag_events.html -[flame_tap_docs]: https://docs.flame-engine.org/1.7.0/flame/inputs/tap_events.html +[flame_drag_docs]: https://docs.flame-engine.org/1.10.0/flame/inputs/drag_events.html +[flame_tap_docs]: https://docs.flame-engine.org/1.10.0/flame/inputs/tap_events.html diff --git a/docs/getting-started.md b/docs/getting-started.md index 18d9380..cf1c4b5 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -115,6 +115,6 @@ To learn more about how to use Flame Behaviors, check out our [article][article_ [flame_package_link]: https://pub.dev/packages/flame [flame_behaviors_package_link]: https://pub.dev/packages/flame_behaviors [article_link]: https://verygood.ventures/blog/build-games-with-flame-behaviors -[input_behaviors_link]: https://github.com/VeryGoodOpenSource/flame_behaviors/blob/docs/main/docs/input-behaviors.md +[input_behaviors_link]: https://github.com/VeryGoodOpenSource/flame_behaviors/blob/docs/main/docs/event-behaviors.md [naming_conventions_link]: https://github.com/VeryGoodOpenSource/flame_behaviors/blob/docs/main/docs/conventions/naming-conventions.md [code_conventions_link]: https://github.com/VeryGoodOpenSource/flame_behaviors/blob/docs/main/docs/conventions/coding-conventions.md diff --git a/packages/flame_behaviors/README.md b/packages/flame_behaviors/README.md index c52b690..04f6e44 100644 --- a/packages/flame_behaviors/README.md +++ b/packages/flame_behaviors/README.md @@ -20,7 +20,7 @@ Developed with 💙 and 🔥 by [Very Good Ventures][very_good_ventures_link] In order to use Flame Behaviors you must have the [Flame package][flame_package_link] added to your project. -> **Note**: Flame Behaviors requires Flame `">=1.6.0 <2.0.0"` +> **Note**: Flame Behaviors requires Flame `">=1.10.0 <2.0.0"` ### Adding the package diff --git a/packages/flame_behaviors/analysis_options.yaml b/packages/flame_behaviors/analysis_options.yaml index 84e34fb..799268d 100644 --- a/packages/flame_behaviors/analysis_options.yaml +++ b/packages/flame_behaviors/analysis_options.yaml @@ -1 +1 @@ -include: package:very_good_analysis/analysis_options.4.0.0.yaml +include: package:very_good_analysis/analysis_options.5.1.0.yaml diff --git a/packages/flame_behaviors/example/lib/behaviors/spawning_behavior.dart b/packages/flame_behaviors/example/lib/behaviors/spawning_behavior.dart index ce077a4..231d8ab 100644 --- a/packages/flame_behaviors/example/lib/behaviors/spawning_behavior.dart +++ b/packages/flame_behaviors/example/lib/behaviors/spawning_behavior.dart @@ -2,7 +2,7 @@ import 'dart:math'; import 'package:example/entities/entities.dart'; import 'package:example/main.dart'; -import 'package:flame/experimental.dart' hide Circle, Rectangle; +import 'package:flame/events.dart'; import 'package:flame/extensions.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; @@ -20,6 +20,7 @@ class SpawningBehavior extends TappableBehavior { @override void onTapDown(TapDownEvent event) { + if (event.handled) return; parent.add(nextRandomEntity(event.canvasPosition)); } @@ -29,26 +30,21 @@ class SpawningBehavior extends TappableBehavior { final velocity = (Vector2.random(_rng) - Vector2.random(_rng)) * 300; final shapeType = Shapes.values[_rng.nextInt(Shapes.values.length)]; - switch (shapeType) { - case Shapes.circle: - return Circle( + return switch (shapeType) { + Shapes.circle => Circle( position: position, size: size, velocity: velocity, rotationSpeed: rotationSpeed, - ); - case Shapes.rectangle: - return Rectangle( + ), + Shapes.rectangle => Rectangle( position: position, size: size, velocity: velocity, rotationSpeed: rotationSpeed, - ); - } + ), + }; } } -enum Shapes { - circle, - rectangle, -} +enum Shapes { circle, rectangle } diff --git a/packages/flame_behaviors/example/lib/entities/behaviors/rotating_behavior.dart b/packages/flame_behaviors/example/lib/entities/behaviors/rotating_behavior.dart index 64ecb7f..7a4a285 100644 --- a/packages/flame_behaviors/example/lib/entities/behaviors/rotating_behavior.dart +++ b/packages/flame_behaviors/example/lib/entities/behaviors/rotating_behavior.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:math'; import 'package:flame/components.dart'; @@ -11,7 +12,7 @@ class RotatingBehavior extends Behavior with HasGameRef { late final ScreenHitbox screenHitbox; @override - Future onLoad() async { + FutureOr onLoad() { screenHitbox = gameRef.children.whereType().first; } diff --git a/packages/flame_behaviors/example/lib/entities/circle/behaviors/behaviors.dart b/packages/flame_behaviors/example/lib/entities/circle/behaviors/behaviors.dart index e9b4872..b573881 100644 --- a/packages/flame_behaviors/example/lib/entities/circle/behaviors/behaviors.dart +++ b/packages/flame_behaviors/example/lib/entities/circle/behaviors/behaviors.dart @@ -1,3 +1,4 @@ export 'circle_collision_behavior.dart'; export 'dragging_behavior.dart'; export 'rectangle_collision_behavior.dart'; +export 'tapping_behavior.dart'; diff --git a/packages/flame_behaviors/example/lib/entities/circle/behaviors/dragging_behavior.dart b/packages/flame_behaviors/example/lib/entities/circle/behaviors/dragging_behavior.dart index a9b31b4..a348810 100644 --- a/packages/flame_behaviors/example/lib/entities/circle/behaviors/dragging_behavior.dart +++ b/packages/flame_behaviors/example/lib/entities/circle/behaviors/dragging_behavior.dart @@ -1,5 +1,5 @@ import 'package:example/entities/entities.dart'; -import 'package:flame/experimental.dart' hide Circle; +import 'package:flame/events.dart'; import 'package:flame/extensions.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; @@ -9,8 +9,9 @@ class DraggingBehavior extends DraggableBehavior { Vector2? originalVelocity; @override - Future onLoad() async { + void onMount() { movement = parent.findBehavior(); + return super.onMount(); } @override diff --git a/packages/flame_behaviors/example/lib/entities/circle/behaviors/tapping_behavior.dart b/packages/flame_behaviors/example/lib/entities/circle/behaviors/tapping_behavior.dart new file mode 100644 index 0000000..bfde6a4 --- /dev/null +++ b/packages/flame_behaviors/example/lib/entities/circle/behaviors/tapping_behavior.dart @@ -0,0 +1,12 @@ +import 'package:example/entities/entities.dart'; +import 'package:flame/events.dart'; +import 'package:flame_behaviors/flame_behaviors.dart'; + +/// This behavior ensures that SpawningBehavior of the game does not spawn +/// anything when we click on a circle (for dragging). +class TappingBehavior extends TappableBehavior { + @override + void onTapDown(TapDownEvent event) { + event.handled = true; + } +} diff --git a/packages/flame_behaviors/example/lib/entities/circle/circle.dart b/packages/flame_behaviors/example/lib/entities/circle/circle.dart index afa684c..61ae53c 100644 --- a/packages/flame_behaviors/example/lib/entities/circle/circle.dart +++ b/packages/flame_behaviors/example/lib/entities/circle/circle.dart @@ -22,7 +22,8 @@ class Circle extends PositionedEntity with HasPaint { ScreenCollidingBehavior(), MovingBehavior(velocity: velocity), RotatingBehavior(rotationSpeed: rotationSpeed), - DraggingBehavior() + TappingBehavior(), + DraggingBehavior(), ], ); diff --git a/packages/flame_behaviors/example/lib/entities/rectangle/behaviors/freezing_behavior.dart b/packages/flame_behaviors/example/lib/entities/rectangle/behaviors/freezing_behavior.dart index 75ada1d..3f63f30 100644 --- a/packages/flame_behaviors/example/lib/entities/rectangle/behaviors/freezing_behavior.dart +++ b/packages/flame_behaviors/example/lib/entities/rectangle/behaviors/freezing_behavior.dart @@ -1,5 +1,5 @@ import 'package:example/entities/entities.dart'; -import 'package:flame/experimental.dart' hide Rectangle; +import 'package:flame/events.dart'; import 'package:flame/extensions.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; @@ -9,8 +9,9 @@ class FreezingBehavior extends TappableBehavior { Vector2? originalVelocity; @override - Future onLoad() async { + void onMount() { movement = parent.findBehavior(); + return super.onMount(); } @override @@ -21,5 +22,6 @@ class FreezingBehavior extends TappableBehavior { originalVelocity = movement?.velocity.clone(); movement?.velocity.setFrom(Vector2.zero()); } + event.handled = true; } } diff --git a/packages/flame_behaviors/example/pubspec.yaml b/packages/flame_behaviors/example/pubspec.yaml index 86fcd48..59c5e49 100644 --- a/packages/flame_behaviors/example/pubspec.yaml +++ b/packages/flame_behaviors/example/pubspec.yaml @@ -4,10 +4,10 @@ version: 1.0.0+1 publish_to: none environment: - sdk: ">=2.17.0 <3.0.0" + sdk: ">=3.0.0 <4.0.0" dependencies: - flame: ^1.5.0 + flame: ^1.10.0 flame_behaviors: path: ../ flutter: diff --git a/packages/flame_behaviors/lib/src/behaviors/behaviors.dart b/packages/flame_behaviors/lib/src/behaviors/behaviors.dart index 7461b16..5b4eaea 100644 --- a/packages/flame_behaviors/lib/src/behaviors/behaviors.dart +++ b/packages/flame_behaviors/lib/src/behaviors/behaviors.dart @@ -1,3 +1,3 @@ export 'behavior.dart'; -export 'input/input.dart'; +export 'events/events.dart'; export 'propagating_collision_behavior.dart'; diff --git a/packages/flame_behaviors/lib/src/behaviors/input/draggable_behavior.dart b/packages/flame_behaviors/lib/src/behaviors/events/draggable_behavior.dart similarity index 86% rename from packages/flame_behaviors/lib/src/behaviors/input/draggable_behavior.dart rename to packages/flame_behaviors/lib/src/behaviors/events/draggable_behavior.dart index 8d55469..e74d702 100644 --- a/packages/flame_behaviors/lib/src/behaviors/input/draggable_behavior.dart +++ b/packages/flame_behaviors/lib/src/behaviors/events/draggable_behavior.dart @@ -1,4 +1,4 @@ -import 'package:flame/experimental.dart'; +import 'package:flame/events.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; /// {@template draggable_behavior} diff --git a/packages/flame_behaviors/lib/src/behaviors/input/input.dart b/packages/flame_behaviors/lib/src/behaviors/events/events.dart similarity index 87% rename from packages/flame_behaviors/lib/src/behaviors/input/input.dart rename to packages/flame_behaviors/lib/src/behaviors/events/events.dart index 54418cc..ed3c748 100644 --- a/packages/flame_behaviors/lib/src/behaviors/input/input.dart +++ b/packages/flame_behaviors/lib/src/behaviors/events/events.dart @@ -1,4 +1,4 @@ -export 'package:flame/input.dart' +export 'package:flame/events.dart' show DragEndInfo, DragStartInfo, diff --git a/packages/flame_behaviors/lib/src/behaviors/input/hoverable_behavior.dart b/packages/flame_behaviors/lib/src/behaviors/events/hoverable_behavior.dart similarity index 81% rename from packages/flame_behaviors/lib/src/behaviors/input/hoverable_behavior.dart rename to packages/flame_behaviors/lib/src/behaviors/events/hoverable_behavior.dart index ebbf169..1be3458 100644 --- a/packages/flame_behaviors/lib/src/behaviors/input/hoverable_behavior.dart +++ b/packages/flame_behaviors/lib/src/behaviors/events/hoverable_behavior.dart @@ -1,4 +1,4 @@ -import 'package:flame/components.dart'; +import 'package:flame/events.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; /// {@template hoverable_behavior} @@ -9,4 +9,4 @@ import 'package:flame_behaviors/flame_behaviors.dart'; /// behaviors. /// {@endtemplate} abstract class HoverableBehavior - extends Behavior with Hoverable {} + extends Behavior with HoverCallbacks {} diff --git a/packages/flame_behaviors/lib/src/behaviors/input/tappable_behavior.dart b/packages/flame_behaviors/lib/src/behaviors/events/tappable_behavior.dart similarity index 86% rename from packages/flame_behaviors/lib/src/behaviors/input/tappable_behavior.dart rename to packages/flame_behaviors/lib/src/behaviors/events/tappable_behavior.dart index 0f53884..57ac4c8 100644 --- a/packages/flame_behaviors/lib/src/behaviors/input/tappable_behavior.dart +++ b/packages/flame_behaviors/lib/src/behaviors/events/tappable_behavior.dart @@ -1,4 +1,4 @@ -import 'package:flame/experimental.dart'; +import 'package:flame/events.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; /// {@template tappable_behavior} diff --git a/packages/flame_behaviors/lib/src/entity.dart b/packages/flame_behaviors/lib/src/entity.dart index d402251..dd6b479 100644 --- a/packages/flame_behaviors/lib/src/entity.dart +++ b/packages/flame_behaviors/lib/src/entity.dart @@ -67,6 +67,7 @@ abstract class Entity extends Component with EntityMixin { Entity({ super.children, super.priority, + super.key, Iterable? behaviors, }) : assert( children?.whereType().isEmpty ?? true, @@ -91,9 +92,11 @@ abstract class PositionedEntity extends PositionComponent with EntityMixin { super.size, super.scale, super.angle, + super.nativeAngle, super.anchor, super.children, super.priority, + super.key, Iterable? behaviors, }) : assert( children?.whereType().isEmpty ?? true, diff --git a/packages/flame_behaviors/pubspec.yaml b/packages/flame_behaviors/pubspec.yaml index f2d8dfd..e42386b 100644 --- a/packages/flame_behaviors/pubspec.yaml +++ b/packages/flame_behaviors/pubspec.yaml @@ -7,16 +7,16 @@ issue_tracker: https://github.com/VeryGoodOpenSource/flame_behaviors/issues documentation: https://github.com/VeryGoodOpenSource/flame_behaviors/tree/main/docs environment: - sdk: ">=2.17.0 <3.0.0" + sdk: ">=3.0.0 <4.0.0" dependencies: - flame: ">=1.7.0 <2.0.0" + flame: ">=1.10.0 <2.0.0" flutter: sdk: flutter dev_dependencies: - flame_test: ^1.9.1 + flame_test: ^1.13.2 flutter_test: sdk: flutter mocktail: ^0.3.0 - very_good_analysis: ^4.0.0+1 + very_good_analysis: ^5.1.0 diff --git a/packages/flame_behaviors/test/helpers/helpers.dart b/packages/flame_behaviors/test/helpers/helpers.dart index 17bb95c..9418ab1 100644 --- a/packages/flame_behaviors/test/helpers/helpers.dart +++ b/packages/flame_behaviors/test/helpers/helpers.dart @@ -1,2 +1 @@ -export 'test_game.dart'; export 'throws_behavior_not_found_for.dart'; diff --git a/packages/flame_behaviors/test/helpers/test_game.dart b/packages/flame_behaviors/test/helpers/test_game.dart deleted file mode 100644 index cbe3b46..0000000 --- a/packages/flame_behaviors/test/helpers/test_game.dart +++ /dev/null @@ -1,4 +0,0 @@ -import 'package:flame/game.dart'; - -class TestGame extends FlameGame - with HasCollisionDetection, HasDraggables, HasHoverables, HasTappables {} diff --git a/packages/flame_behaviors/test/src/behaviors/behavior_test.dart b/packages/flame_behaviors/test/src/behaviors/behavior_test.dart index efc88b6..ce66c5d 100644 --- a/packages/flame_behaviors/test/src/behaviors/behavior_test.dart +++ b/packages/flame_behaviors/test/src/behaviors/behavior_test.dart @@ -5,8 +5,6 @@ import 'package:flame_behaviors/flame_behaviors.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; -import '../../helpers/test_game.dart'; - class _TestEntity extends PositionedEntity { _TestEntity({super.behaviors}) : super(size: Vector2.all(32)); } @@ -14,10 +12,8 @@ class _TestEntity extends PositionedEntity { class _TestBehavior extends Behavior<_TestEntity> {} void main() { - final flameTester = FlameTester(TestGame.new); - group('Behavior', () { - flameTester.testGameWidget( + flameGame.testGameWidget( 'can be added to an Entity', setUp: (game, tester) async { final behavior = _TestBehavior(); @@ -32,7 +28,7 @@ void main() { }, ); - flameTester.testGameWidget( + flameGame.testGameWidget( 'contains point is relative to parent', setUp: (game, tester) async { final behavior = _TestBehavior(); @@ -49,7 +45,7 @@ void main() { }, ); - flameTester.testGameWidget( + flameGame.testGameWidget( 'contains local point is relative to parent', setUp: (game, tester) async { final behavior = _TestBehavior(); @@ -66,7 +62,7 @@ void main() { }, ); - flameTester.testGameWidget( + flameGame.testGameWidget( 'debugMode is provided by the parent', setUp: (game, tester) async { final behavior = _TestBehavior(); @@ -94,7 +90,7 @@ void main() { ); }); - flameTester.testGameWidget( + flameGame.testGameWidget( 'can have its own children', setUp: (game, tester) async { await game.ensureAdd(testEntity); @@ -104,7 +100,7 @@ void main() { }, ); - flameTester.testGameWidget( + flameGame.testGameWidget( 'can not have behaviors as children', setUp: (game, tester) async { await game.ensureAdd(testEntity); @@ -117,7 +113,7 @@ void main() { }, ); - flameTester.testGameWidget( + flameGame.testGameWidget( 'can not have entities as children', setUp: (game, tester) async { await game.ensureAdd(testEntity); diff --git a/packages/flame_behaviors/test/src/behaviors/propagating_collision_behavior_test.dart b/packages/flame_behaviors/test/src/behaviors/propagating_collision_behavior_test.dart index 00f03c6..fcf8f95 100644 --- a/packages/flame_behaviors/test/src/behaviors/propagating_collision_behavior_test.dart +++ b/packages/flame_behaviors/test/src/behaviors/propagating_collision_behavior_test.dart @@ -2,12 +2,11 @@ import 'package:flame/collisions.dart'; import 'package:flame/components.dart'; +import 'package:flame/game.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; -import '../../helpers/helpers.dart'; - class _EntityA extends PositionedEntity { _EntityA({ super.behaviors, @@ -60,8 +59,10 @@ class _CollisionBehaviorAtoC extends _CollisionBehavior<_EntityC, _EntityA> {} class _CollisionBehaviorAtoComponent extends _CollisionBehavior {} +class _TestGame extends FlameGame with HasCollisionDetection {} + void main() { - final flameTester = FlameTester(TestGame.new); + final flameTester = FlameTester(_TestGame.new); group('$CollisionBehavior', () { flameTester.testGameWidget( diff --git a/packages/flame_behaviors/test/src/entity_test.dart b/packages/flame_behaviors/test/src/entity_test.dart index e00b4f6..d98370d 100644 --- a/packages/flame_behaviors/test/src/entity_test.dart +++ b/packages/flame_behaviors/test/src/entity_test.dart @@ -15,10 +15,8 @@ class _TestEntity extends Entity { class _TestBehavior extends Behavior<_TestEntity> {} void main() { - final flameTester = FlameTester(TestGame.new); - group('Entity', () { - flameTester.testGameWidget( + flameGame.testGameWidget( 'adds behaviors directly to itself', setUp: (game, tester) async { final behavior = _TestBehavior(); @@ -33,7 +31,7 @@ void main() { }, ); - flameTester.testGameWidget( + flameGame.testGameWidget( 'adds behaviors to itself', setUp: (game, tester) async { final behavior = _TestBehavior(); @@ -49,7 +47,7 @@ void main() { }, ); - flameTester.testGameWidget( + flameGame.testGameWidget( 'behavior can be removed from entity and the internal cache', setUp: (game, tester) async { final entity = _TestEntity(behaviors: []); @@ -74,7 +72,7 @@ void main() { }, ); - flameTester.testGameWidget( + flameGame.testGameWidget( 'can correctly confirm if it has a behavior', setUp: (game, tester) async { final entity = _TestEntity(behaviors: []);