LCOV - code coverage report
Current view: top level - lib/src/components - sprite_animation_group_component.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 10 22 45.5 %
Date: 2021-08-10 15:50:53 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:ui';
       2             : 
       3             : import 'package:meta/meta.dart';
       4             : 
       5             : import '../extensions/vector2.dart';
       6             : import '../sprite_animation.dart';
       7             : import 'mixins/has_paint.dart';
       8             : import 'position_component.dart';
       9             : 
      10             : export '../sprite_animation.dart';
      11             : 
      12             : class SpriteAnimationGroupComponent<T> extends PositionComponent with HasPaint {
      13             :   /// Key with the current playing animation
      14             :   T? current;
      15             : 
      16             :   /// Map with the mapping each state to the flag removeOnFinish
      17             :   final Map<T, bool> removeOnFinish;
      18             : 
      19             :   /// Map with the available states for this animation group
      20             :   late Map<T, SpriteAnimation> animations;
      21             : 
      22             :   /// Creates a component with an empty animation which can be set later
      23           1 :   SpriteAnimationGroupComponent({
      24             :     required this.animations,
      25             :     Vector2? position,
      26             :     Vector2? size,
      27             :     int? priority,
      28             :     this.current,
      29             :     Paint? paint,
      30             :     this.removeOnFinish = const {},
      31           1 :   }) : super(
      32             :           position: position,
      33             :           size: size,
      34             :           priority: priority,
      35             :         ) {
      36             :     if (paint != null) {
      37           0 :       this.paint = paint;
      38             :     }
      39             :   }
      40             : 
      41             :   /// Creates a SpriteAnimationGroupComponent from a [size], an [image] and [data].
      42             :   /// Check [SpriteAnimationData] for more info on the available options.
      43             :   ///
      44             :   /// Optionally [removeOnFinish] can be mapped to true to have this component be auto
      45             :   /// removed from the BaseGame when the animation is finished.
      46           0 :   SpriteAnimationGroupComponent.fromFrameData(
      47             :     Image image,
      48             :     Map<T, SpriteAnimationData> data, {
      49             :     this.current,
      50             :     Vector2? position,
      51             :     Vector2? size,
      52             :     int? priority,
      53             :     Paint? paint,
      54             :     this.removeOnFinish = const {},
      55           0 :   }) : super(
      56             :           position: position,
      57             :           size: size,
      58             :           priority: priority,
      59             :         ) {
      60           0 :     animations = data.map((key, value) {
      61           0 :       return MapEntry(
      62             :         key,
      63           0 :         SpriteAnimation.fromFrameData(
      64             :           image,
      65             :           value,
      66             :         ),
      67             :       );
      68             :     });
      69             : 
      70             :     if (paint != null) {
      71           0 :       this.paint = paint;
      72             :     }
      73             :   }
      74             : 
      75           4 :   SpriteAnimation? get animation => animations[current];
      76             : 
      77             :   /// Component will be removed after animation is done and the current state
      78             :   /// is true on [removeOnFinish].
      79             :   ///
      80             :   /// Note: [SpriteAnimationGroupComponent] will not be removed automatically if loop
      81             :   /// property of [SpriteAnimation] of the current state is true.
      82           1 :   @override
      83             :   bool get shouldRemove {
      84           3 :     final stateRemoveOnFinish = removeOnFinish[current] ?? false;
      85           1 :     return super.shouldRemove ||
      86           2 :         (stateRemoveOnFinish && (animation?.done() ?? false));
      87             :   }
      88             : 
      89           0 :   @mustCallSuper
      90             :   @override
      91             :   void render(Canvas canvas) {
      92           0 :     super.render(canvas);
      93           0 :     animation?.getSprite().render(
      94             :           canvas,
      95           0 :           size: size,
      96           0 :           overridePaint: paint,
      97             :         );
      98             :   }
      99             : 
     100           1 :   @override
     101             :   void update(double dt) {
     102           1 :     super.update(dt);
     103           2 :     animation?.update(dt);
     104             :   }
     105             : }

Generated by: LCOV version 1.15