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 : /// A [PositionComponent] that can have mutiple [Sprite]s and render 13 : /// the one mapped with the [current] key. 14 : class SpriteGroupComponent<T> extends PositionComponent with HasPaint { 15 : /// Key with the current playing animation 16 : T? current; 17 : 18 : /// Map with the available states for this sprite group 19 : Map<T, Sprite>? sprites; 20 : 21 : /// Creates a component with an empty animation which can be set later 22 1 : SpriteGroupComponent({ 23 : this.sprites, 24 : Vector2? position, 25 : Vector2? size, 26 : int? priority, 27 : this.current, 28 : Paint? paint, 29 1 : }) : super( 30 : position: position, 31 : size: size, 32 : priority: priority, 33 : ) { 34 : if (paint != null) { 35 0 : this.paint = paint; 36 : } 37 : } 38 : 39 4 : Sprite? get sprite => sprites?[current]; 40 : 41 0 : @mustCallSuper 42 : @override 43 : void render(Canvas canvas) { 44 0 : super.render(canvas); 45 0 : sprite?.render( 46 : canvas, 47 0 : size: size, 48 0 : overridePaint: paint, 49 : ); 50 : } 51 : }