This commit is contained in:
parent
8297ce980f
commit
7abb01ed5b
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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<SpriteAnimationWidget>
|
|||
|
||||
Future<void> _loadImage() async {
|
||||
try {
|
||||
final ByteData data = await rootBundle.load(widget.assetPath);
|
||||
final List<int> bytes = data.buffer.asUint8List();
|
||||
final Completer<ui.Image> 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<void> _loadAsset(String path) async {
|
||||
final ByteData data = await rootBundle.load(path);
|
||||
final List<int> bytes = data.buffer.asUint8List();
|
||||
final Completer<ui.Image> 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<SpriteAnimationWidget>
|
|||
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,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue