Skip to content

Latest commit

 

History

History
77 lines (54 loc) · 1.55 KB

state_controller.md

File metadata and controls

77 lines (54 loc) · 1.55 KB

StateController

State manager util to separate view and logic with dependency injection.

  • Create your StateController:
class KnightController extends StateController<Knight> {

@override
  void onReady(Knight component) {
    // TODO: implement onReady
    super.onReady(component);
  }

  @override
  void onRemove(Knight component) {
    // TODO: implement onRemove
    super.onRemove(component);
  }
  
  @override
  void update(double dt, Knight component) {
   // if (component.checkInterval('seeEnemy', 250, dt) == true) {
    //   component.seeEnemy(
    //     radiusVision: component!.width * 4,
    //     notObserved: _handleNotObserveEnemy,
    //     observed: (enemies) => _handleObserveEnemy(enemies.first),
    //   );
    // }
  }
}
  • Put the dependencies and controllers:
BonfireInjector().put((i) => KnightController());
 // or
 // BonfireInjector().putFactory(
  //   (i) => KnightController(),
  // );
  • Adds mixin in your component:
class Knight extends SimplePlayer
    with Lighting, ObjectCollision, UseStateController<KnightController> {
}
  • To listen controller with Widget. (Useful in Overlays)
     StateControllerConsumer<BarLifeController>(
        builder: (BuildContext context, BarLifeController controller) {
          return MyWidget(controller);
        },
      );
  • To access any dependency or controller registered:
final barLifeController = BonfireInjector().get<BarLifeController>()

Game example here