mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-04 21:17:13 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			64 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
import 'dart:ui';
 | 
						|
 | 
						|
import 'package:flutter/foundation.dart';
 | 
						|
 | 
						|
import '../extensions/vector2.dart';
 | 
						|
import '../sprite_animation.dart';
 | 
						|
import 'position_component.dart';
 | 
						|
 | 
						|
class SpriteAnimationComponent extends PositionComponent {
 | 
						|
  SpriteAnimation animation;
 | 
						|
  Paint overridePaint;
 | 
						|
  bool removeOnFinish = false;
 | 
						|
 | 
						|
  /// Creates a component with an empty animation which can be set later
 | 
						|
  SpriteAnimationComponent();
 | 
						|
 | 
						|
  /// Creates an [SpriteAnimationComponent] from an [animation] and a [size]
 | 
						|
  ///
 | 
						|
  /// Optionally [removeOnFinish] can be set to true to have this component be auto removed from the [BaseGame] when the animation is finished.
 | 
						|
  SpriteAnimationComponent.fromSpriteAnimation(
 | 
						|
    Vector2 size,
 | 
						|
    this.animation, {
 | 
						|
    this.removeOnFinish = false,
 | 
						|
  }) : assert(animation != null) {
 | 
						|
    super.size.setFrom(size);
 | 
						|
  }
 | 
						|
 | 
						|
  /// Creates a SpriteAnimationComponent from a [size], an [image] and [data]. Check [SpriteAnimationData] for more info on the available options.
 | 
						|
  ///
 | 
						|
  /// Optionally [removeOnFinish] can be set to true to have this component be auto removed from the [BaseGame] when the animation is finished.
 | 
						|
  SpriteAnimationComponent.fromFrameData(
 | 
						|
    Vector2 size,
 | 
						|
    Image image,
 | 
						|
    SpriteAnimationData data, {
 | 
						|
    this.removeOnFinish = false,
 | 
						|
  }) {
 | 
						|
    super.size.setFrom(size);
 | 
						|
    animation = SpriteAnimation.fromFrameData(
 | 
						|
      image,
 | 
						|
      data,
 | 
						|
    );
 | 
						|
  }
 | 
						|
 | 
						|
  @override
 | 
						|
  bool get shouldRemove => removeOnFinish && (animation?.isLastFrame ?? false);
 | 
						|
 | 
						|
  @mustCallSuper
 | 
						|
  @override
 | 
						|
  void render(Canvas canvas) {
 | 
						|
    super.render(canvas);
 | 
						|
    animation?.getSprite()?.render(
 | 
						|
          canvas,
 | 
						|
          size: size,
 | 
						|
          overridePaint: overridePaint,
 | 
						|
        );
 | 
						|
  }
 | 
						|
 | 
						|
  @override
 | 
						|
  void update(double t) {
 | 
						|
    super.update(t);
 | 
						|
    animation?.update(t);
 | 
						|
  }
 | 
						|
}
 |