From e412b55f0115f85726d2bcd3dc73f91cbcb0335d Mon Sep 17 00:00:00 2001 From: alestiago Date: Wed, 30 Mar 2022 16:40:33 +0100 Subject: [PATCH] feat: defined component controller --- lib/flame/component_controller.dart | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/flame/component_controller.dart diff --git a/lib/flame/component_controller.dart b/lib/flame/component_controller.dart new file mode 100644 index 00000000..c52e46bf --- /dev/null +++ b/lib/flame/component_controller.dart @@ -0,0 +1,31 @@ +import 'package:flame/components.dart'; +import 'package:flame_bloc/flame_bloc.dart'; + +/// {@template component_controller} +/// A [ComponentController] is a [Component] in charge of handling the logic +/// associated with another [Component]. +/// +/// [ComponentController]s usually implement [BlocComponent]. +/// {@endtemplate} +abstract class ComponentController extends Component { + /// {@macro component_controller} + ComponentController(this.component); + + /// The [Component] controlled by this [ComponentController]. + final T component; + + /// Ads the [ComponentController] to its [component]. + Future attach() async { + await component.add(this); + } + + @override + Future onLoad() async { + await super.onLoad(); + assert( + parent! == component, + 'ComponentController should be child of $component. ' + 'Use attach() instead.', + ); + } +}