diff --git a/lib/flame/component_controller.dart b/lib/flame/component_controller.dart index 851028f0..1d6e0173 100644 --- a/lib/flame/component_controller.dart +++ b/lib/flame/component_controller.dart @@ -23,6 +23,11 @@ abstract class ComponentController extends 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]. diff --git a/test/flame/component_controller_test.dart b/test/flame/component_controller_test.dart index 4e5da210..e1973274 100644 --- a/test/flame/component_controller_test.dart +++ b/test/flame/component_controller_test.dart @@ -31,6 +31,7 @@ void main() { ); }, ); + flameTester.test( 'throws AssertionError when not attached to controlled component', (game) async { @@ -44,6 +45,35 @@ void main() { ); }, ); + + 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', () {