【Flutter】スプライトの利用

class MySprite extends SpriteComponent {
  late final Vector2 _position;

  MySprite(this._position): super();

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    sprite = await Sprite.load('chara.png');
    position = _position;
    size = Vector2(100, 100);
  }

  @override
  void update(double delta) {
    super.update(delta);
  }
}
class SampleGame extends FlameGame with HasTappableComponents {
  Vector2 _position = Vector2(100, 100);
  final List<PositionComponent> _sprites = <PositionComponent>[];

  @override
  Color backgroundColor() => const Color(0xffCCCCFF);

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    var sp1 = GreenRectSprite(Vector2(200, 100));
    _sprites.add(sp1);
    add(sp1);
    var sp2 = RedCircleSprite(Vector2(100, 200));
    _sprites.add(sp2);
    add(sp2);
    add(WhiteTextSprite(Vector2(25, 25)));
  }

  @override
  void onTapDown(TapDownEvent event) {
    _position = event.canvasPosition - Vector2(50, 50);
    super.onTapDown(event);
  }
}

class RedCircleSprite extends CircleComponent
  with HasGameRef<SampleGame> {
  late Vector2 _position;

  RedCircleSprite(this._position): super();

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    setColor(Colors.red);
    position = _position;
    size = Vector2(100, 100);
  }

  @override
  void update(double delta) {
    final d = (gameRef._position - position) / 10;
    position += d * delta * 100;
    super.update(delta);
  }
}

class GreenRectSprite extends PositionComponent with HasGameRef<SampleGame>{
  late Vector2 _position;
  late Paint _paint;

  GreenRectSprite(this._position): super();

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    position = _position;
    size = Vector2(100, 100);
    _paint = Paint()
      ..style = PaintingStyle.fill
      ..color = Colors.green;
  }

  @override
  void update(double delta) {
    final d = (gameRef._position - position) / 50;
    position += d * delta * 100;
    super.update(delta);
  }
}

class GreenRectSprite extends PositionComponent
  with HasGameRef<SampleGame> {
  late Vector2 _position;
  late Paint _paint;

  GreenRectSprite(this._position): super();

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    position = _position;
    size = Vector2(100, 100);
    _paint = Paint()
      ..style = PaintingStyle.fill
      ..color = Colors.green;
  }

  @override
  void update(double delta) {
    final d = (gameRef._position - position) / 50;
    position += d * delta * 100;
    super.update(delta);
  }

  @override
  void render(Canvas canvas) {
    super.render(canvas);
    final r = Rect.fromLTWH(0, 0, 100, 100);
    canvas.drawRect(r, _paint);
  }
}

class WhiteTextSprite extends TextComponent {
  late Vector2 _position;

  WhiteTextSprite(this._position): super();

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    position = _position;
    text = "Hello Flame";
    textRenderer = TextPaint(
      style: TextStyle(
        fontSize: 48.0,
        fontWeight: FontWeight.bold,
        color: Colors.white,
      ),
    );
  }
}