From 7abb01ed5b3dbc7313a25e5f4734cbc969e7b237 Mon Sep 17 00:00:00 2001 From: horoli Date: Wed, 28 Jan 2026 23:16:37 +0900 Subject: [PATCH] aa --- lib/widgets/battle/character_status_card.dart | 4 + lib/widgets/test/sprite_animation_widget.dart | 98 ++++++++++++------- 2 files changed, 64 insertions(+), 38 deletions(-) diff --git a/lib/widgets/battle/character_status_card.dart b/lib/widgets/battle/character_status_card.dart index d5ab7cc..20a6f1b 100644 --- a/lib/widgets/battle/character_status_card.dart +++ b/lib/widgets/battle/character_status_card.dart @@ -128,6 +128,10 @@ class CharacterStatusCard extends StatelessWidget { assetPath: overrideImage ?? character.image!, scale: 5.0, // Zoomed in (300x300 in 200x200 box) frameCount: 6, + flip: !isPlayer, + fallbackAssetPath: !isPlayer + ? 'assets/images/enemies/Orc.png' + : null, ), ) : Icon( diff --git a/lib/widgets/test/sprite_animation_widget.dart b/lib/widgets/test/sprite_animation_widget.dart index eb199ca..f944ce7 100644 --- a/lib/widgets/test/sprite_animation_widget.dart +++ b/lib/widgets/test/sprite_animation_widget.dart @@ -9,6 +9,8 @@ class SpriteAnimationWidget extends StatefulWidget { final double tileHeight; final int frameCount; final double scale; + final bool flip; + final String? fallbackAssetPath; const SpriteAnimationWidget({ super.key, @@ -18,6 +20,8 @@ class SpriteAnimationWidget extends StatefulWidget { this.frameCount = 6, // Default guess, will adjust logic to use actual image width if possible this.scale = 1.0, + this.flip = false, + this.fallbackAssetPath, }); @override @@ -43,33 +47,47 @@ class _SpriteAnimationWidgetState extends State Future _loadImage() async { try { - final ByteData data = await rootBundle.load(widget.assetPath); - final List bytes = data.buffer.asUint8List(); - final Completer completer = Completer(); - ui.decodeImageFromList(Uint8List.fromList(bytes), (ui.Image img) { - completer.complete(img); - }); - final image = await completer.future; - - if (mounted) { - setState(() { - _image = image; - _isLoading = false; - // Use provided frameCount, but clamp to available frames in image - int maxFrames = (image.width / widget.tileWidth).floor(); - _calculatedFrameCount = widget.frameCount > maxFrames - ? maxFrames - : widget.frameCount; - - // Adjust duration based on frame count - _controller.duration = Duration( - milliseconds: _calculatedFrameCount * 100, - ); - _controller.repeat(); - }); - } + await _loadAsset(widget.assetPath); } catch (e) { debugPrint('Failed to load sprite image: $e'); + if (widget.fallbackAssetPath != null) { + debugPrint( + 'Attempting to load fallback image: ${widget.fallbackAssetPath}', + ); + try { + await _loadAsset(widget.fallbackAssetPath!); + } catch (e2) { + debugPrint('Failed to load fallback image: $e2'); + } + } + } + } + + Future _loadAsset(String path) async { + final ByteData data = await rootBundle.load(path); + final List bytes = data.buffer.asUint8List(); + final Completer completer = Completer(); + ui.decodeImageFromList(Uint8List.fromList(bytes), (ui.Image img) { + completer.complete(img); + }); + final image = await completer.future; + + if (mounted) { + setState(() { + _image = image; + _isLoading = false; + // Use provided frameCount, but clamp to available frames in image + int maxFrames = (image.width / widget.tileWidth).floor(); + _calculatedFrameCount = widget.frameCount > maxFrames + ? maxFrames + : widget.frameCount; + + // Adjust duration based on frame count + _controller.duration = Duration( + milliseconds: _calculatedFrameCount * 100, + ); + _controller.repeat(); + }); } } @@ -92,19 +110,23 @@ class _SpriteAnimationWidgetState extends State return AnimatedBuilder( animation: _controller, builder: (context, child) { - return CustomPaint( - size: Size( - widget.tileWidth * widget.scale, - widget.tileHeight * widget.scale, - ), - painter: SpriteSheetPainter( - image: _image!, - currentFrame: - (_controller.value * _calculatedFrameCount).floor() % - _calculatedFrameCount, - tileWidth: widget.tileWidth, - tileHeight: widget.tileHeight, - scale: widget.scale, + return Transform.scale( + scaleX: widget.flip ? -1.0 : 1.0, + alignment: Alignment.center, + child: CustomPaint( + size: Size( + widget.tileWidth * widget.scale, + widget.tileHeight * widget.scale, + ), + painter: SpriteSheetPainter( + image: _image!, + currentFrame: + (_controller.value * _calculatedFrameCount).floor() % + _calculatedFrameCount, + tileWidth: widget.tileWidth, + tileHeight: widget.tileHeight, + scale: widget.scale, + ), ), ); },