From 3b41771bdf756de80c87569b0eb573165b325d10 Mon Sep 17 00:00:00 2001 From: alestiago Date: Sun, 8 May 2022 11:09:18 +0100 Subject: [PATCH] refactor: removed ComponentController --- packages/pinball_flame/lib/pinball_flame.dart | 1 - .../lib/src/component_controller.dart | 41 -------- .../test/src/component_controller_test.dart | 96 ------------------- 3 files changed, 138 deletions(-) delete mode 100644 packages/pinball_flame/lib/src/component_controller.dart delete mode 100644 packages/pinball_flame/test/src/component_controller_test.dart diff --git a/packages/pinball_flame/lib/pinball_flame.dart b/packages/pinball_flame/lib/pinball_flame.dart index bc3cae0e..867fa6ba 100644 --- a/packages/pinball_flame/lib/pinball_flame.dart +++ b/packages/pinball_flame/lib/pinball_flame.dart @@ -2,7 +2,6 @@ library pinball_flame; export 'src/behaviors/behaviors.dart'; export 'src/canvas/canvas.dart'; -export 'src/component_controller.dart'; export 'src/flame_provider.dart'; export 'src/keyboard_input_controller.dart'; export 'src/layer.dart'; diff --git a/packages/pinball_flame/lib/src/component_controller.dart b/packages/pinball_flame/lib/src/component_controller.dart deleted file mode 100644 index 6afc1c40..00000000 --- a/packages/pinball_flame/lib/src/component_controller.dart +++ /dev/null @@ -1,41 +0,0 @@ -import 'package:flame/components.dart'; -import 'package:flutter/foundation.dart'; - -/// {@template component_controller} -/// A [ComponentController] is a [Component] in charge of handling the logic -/// associated with another [Component]. -/// {@endtemplate} -abstract class ComponentController extends Component { - /// {@macro component_controller} - ComponentController(this.component); - - /// The [Component] controlled by this [ComponentController]. - final T component; - - @override - Future addToParent(Component parent) async { - assert( - parent == component, - 'ComponentController should be child of $component.', - ); - await super.addToParent(parent); - } - - @override - Future add(Component component) { - throw Exception('ComponentController cannot add other components.'); - } -} - -/// Mixin that attaches a single [ComponentController] to a [Component]. -mixin Controls on Component { - /// The [ComponentController] attached to this [Component]. - late T controller; - - @override - @mustCallSuper - Future onLoad() async { - await super.onLoad(); - await add(controller); - } -} diff --git a/packages/pinball_flame/test/src/component_controller_test.dart b/packages/pinball_flame/test/src/component_controller_test.dart deleted file mode 100644 index addcf2b0..00000000 --- a/packages/pinball_flame/test/src/component_controller_test.dart +++ /dev/null @@ -1,96 +0,0 @@ -// ignore_for_file: cascade_invocations - -import 'package:flame/game.dart'; -import 'package:flame/src/components/component.dart'; -import 'package:flame_test/flame_test.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:pinball_flame/pinball_flame.dart'; - -class TestComponentController extends ComponentController { - TestComponentController(Component component) : super(component); -} - -class ControlledComponent extends Component - with Controls { - ControlledComponent() : super() { - controller = TestComponentController(this); - } -} - -void main() { - TestWidgetsFlutterBinding.ensureInitialized(); - final flameTester = FlameTester(FlameGame.new); - - group('ComponentController', () { - flameTester.test( - 'can be instantiated', - (game) async { - expect( - TestComponentController(Component()), - isA(), - ); - }, - ); - - flameTester.test( - 'throws AssertionError when not attached to controlled component', - (game) async { - final component = Component(); - final controller = TestComponentController(component); - - final anotherComponent = Component(); - await expectLater( - () async => await anotherComponent.add(controller), - throwsAssertionError, - ); - }, - ); - - flameTester.test( - 'throws Exception when adding a component', - (game) async { - final component = ControlledComponent(); - final controller = TestComponentController(component); - - await expectLater( - () async => controller.add(Component()), - throwsException, - ); - }, - ); - - flameTester.test( - 'throws Exception when adding multiple components', - (game) async { - final component = ControlledComponent(); - final controller = TestComponentController(component); - - await expectLater( - () async => controller.addAll([ - Component(), - Component(), - ]), - throwsException, - ); - }, - ); - }); - - group('Controls', () { - flameTester.test( - 'can be instantiated', - (game) async { - expect(ControlledComponent(), isA()); - }, - ); - - flameTester.test('adds controller', (game) async { - final component = ControlledComponent(); - - await game.add(component); - await game.ready(); - - expect(component.contains(component.controller), isTrue); - }); - }); -}