import 'package:flame/components.dart'; import 'package:flutter/material.dart'; import 'body_component.dart'; import 'forge2d_game.dart'; /// A [PositionBodyComponent] handles a [PositionComponent] on top of a /// [BodyComponent]. You have to keep track of the size of the /// [PositionComponent] and it can only have its anchor in the center. abstract class PositionBodyComponent extends BodyComponent { PositionComponent positionComponent; Vector2 size; @override bool debugMode = false; /// Make sure that the [size] of the position component matches the bounding /// shape of the body that is create in createBody() PositionBodyComponent( this.positionComponent, this.size, ); @mustCallSuper @override Future onLoad() async { await super.onLoad(); updatePositionComponent(); positionComponent.anchor = Anchor.center; gameRef.add(positionComponent); } @override void update(double dt) { super.update(dt); updatePositionComponent(); } @override void onRemove() { // Since the PositionComponent was added to the game in this class it should // also be removed by this class when the BodyComponent is removed. positionComponent.removeFromParent(); super.onRemove(); } void updatePositionComponent() { positionComponent.position.setFrom(body.position); positionComponent.position.y *= -1; positionComponent ..angle = -angle ..size = size; } }