Refactor: Slim down BattleProvider and extract logic services. Restore original animations and positioning.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
export 'battle/battle_bottom_section.dart';
|
||||
export 'battle/battle_arena.dart';
|
||||
export 'battle/battle_animation_widget.dart';
|
||||
export 'battle/battle_controls.dart';
|
||||
export 'battle/battle_header.dart';
|
||||
@@ -7,3 +9,5 @@ export 'battle/explosion_widget.dart';
|
||||
export 'battle/floating_battle_texts.dart';
|
||||
export 'battle/risk_selection_dialog.dart';
|
||||
export 'battle/shake_widget.dart';
|
||||
export 'battle/battle_overlays.dart';
|
||||
export 'battle/effect_sprite_widget.dart';
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../providers/battle_provider.dart';
|
||||
import 'character_status_card.dart';
|
||||
import 'shake_widget.dart';
|
||||
import 'battle_animation_widget.dart';
|
||||
|
||||
class BattleArena extends StatelessWidget {
|
||||
final BattleProvider battleProvider;
|
||||
final GlobalKey playerKey;
|
||||
final GlobalKey enemyKey;
|
||||
final GlobalKey<BattleAnimationWidgetState> playerAnimKey;
|
||||
final GlobalKey<BattleAnimationWidgetState> enemyAnimKey;
|
||||
final GlobalKey<ShakeWidgetState> shakeKey;
|
||||
final GlobalKey stackKey;
|
||||
final bool isPlayerAttacking;
|
||||
final bool isEnemyAttacking;
|
||||
final String? playerOverrideImage;
|
||||
final String? enemyOverrideImage;
|
||||
|
||||
const BattleArena({
|
||||
super.key,
|
||||
required this.battleProvider,
|
||||
required this.playerKey,
|
||||
required this.enemyKey,
|
||||
required this.playerAnimKey,
|
||||
required this.enemyAnimKey,
|
||||
required this.shakeKey,
|
||||
required this.stackKey,
|
||||
required this.isPlayerAttacking,
|
||||
required this.isEnemyAttacking,
|
||||
this.playerOverrideImage,
|
||||
this.enemyOverrideImage,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ShakeWidget(
|
||||
key: shakeKey,
|
||||
child: Stack(
|
||||
key: stackKey,
|
||||
children: [
|
||||
// 1. Background Image
|
||||
Container(
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/images/background/tier_1.jpg'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
),
|
||||
// 1.1 Opacity Layer
|
||||
Container(color: Colors.black.withValues(alpha: 0.7)),
|
||||
|
||||
// 2. Character Area
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Stack(
|
||||
children: [
|
||||
// Player (Bottom Left)
|
||||
Positioned(
|
||||
bottom: 80,
|
||||
left: 16,
|
||||
child: CharacterStatusCard(
|
||||
character: battleProvider.player,
|
||||
isPlayer: true,
|
||||
isTurn: battleProvider.isPlayerTurn,
|
||||
key: playerKey,
|
||||
animationKey: playerAnimKey,
|
||||
hideStats: isPlayerAttacking,
|
||||
overrideImage: playerOverrideImage,
|
||||
),
|
||||
),
|
||||
// Enemy (Top Right)
|
||||
Positioned(
|
||||
top: 16,
|
||||
right: 16,
|
||||
child: CharacterStatusCard(
|
||||
character: battleProvider.enemy,
|
||||
isPlayer: false,
|
||||
isTurn: !battleProvider.isPlayerTurn,
|
||||
key: enemyKey,
|
||||
animationKey: enemyAnimKey,
|
||||
hideStats: isEnemyAttacking,
|
||||
overrideImage: enemyOverrideImage,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../game/enums.dart';
|
||||
import '../../game/config.dart';
|
||||
import '../../providers/battle_provider.dart';
|
||||
import 'battle_controls.dart';
|
||||
import 'battle_log_overlay.dart';
|
||||
|
||||
class BattleBottomSection extends StatelessWidget {
|
||||
final BattleProvider battleProvider;
|
||||
final bool showLogs;
|
||||
final bool isPlayerAttacking;
|
||||
final bool isEnemyAttacking;
|
||||
final VoidCallback onToggleLogs;
|
||||
final VoidCallback onAttackPressed;
|
||||
final VoidCallback onDefendPressed;
|
||||
final VoidCallback onItemPressed;
|
||||
|
||||
// Custom buttons/panels passed from parent to keep their logic there for now
|
||||
final Widget equipmentSwapButton;
|
||||
final Widget? equipmentSwapPanel;
|
||||
|
||||
const BattleBottomSection({
|
||||
super.key,
|
||||
required this.battleProvider,
|
||||
required this.showLogs,
|
||||
required this.isPlayerAttacking,
|
||||
required this.isEnemyAttacking,
|
||||
required this.onToggleLogs,
|
||||
required this.onAttackPressed,
|
||||
required this.onDefendPressed,
|
||||
required this.onItemPressed,
|
||||
required this.equipmentSwapButton,
|
||||
this.equipmentSwapPanel,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Stack(
|
||||
children: [
|
||||
// 1. Logs Overlay
|
||||
if (showLogs && battleProvider.logs.isNotEmpty)
|
||||
Positioned(
|
||||
top: 60,
|
||||
left: 16,
|
||||
right: 16,
|
||||
height: BattleConfig.logsOverlayHeight,
|
||||
child: BattleLogOverlay(logs: battleProvider.logs),
|
||||
),
|
||||
|
||||
// 2. Battle Controls (Bottom Right)
|
||||
Positioned(
|
||||
bottom: 20,
|
||||
right: 20,
|
||||
child: BattleControls(
|
||||
isAttackEnabled: battleProvider.isPlayerTurn &&
|
||||
!battleProvider.player.isDead &&
|
||||
!battleProvider.enemy.isDead &&
|
||||
!battleProvider.showRewardPopup &&
|
||||
!isPlayerAttacking &&
|
||||
!isEnemyAttacking,
|
||||
isDefendEnabled: battleProvider.isPlayerTurn &&
|
||||
!battleProvider.player.isDead &&
|
||||
!battleProvider.enemy.isDead &&
|
||||
!battleProvider.showRewardPopup &&
|
||||
!isPlayerAttacking &&
|
||||
!isEnemyAttacking &&
|
||||
!battleProvider.player.hasStatus(
|
||||
StatusEffectType.defenseForbidden,
|
||||
),
|
||||
onAttackPressed: onAttackPressed,
|
||||
onDefendPressed: onDefendPressed,
|
||||
onItemPressed: onItemPressed,
|
||||
),
|
||||
),
|
||||
|
||||
// 3. Equipment Swap Panel
|
||||
if (equipmentSwapPanel != null)
|
||||
Positioned(
|
||||
bottom: 20,
|
||||
right: 96,
|
||||
width: 260,
|
||||
child: equipmentSwapPanel!,
|
||||
),
|
||||
|
||||
// 4. Log Toggle & Swap Button (Bottom Left)
|
||||
Positioned(
|
||||
bottom: 20,
|
||||
left: 20,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
equipmentSwapButton,
|
||||
const SizedBox(height: 12),
|
||||
FloatingActionButton(
|
||||
heroTag: "logToggle",
|
||||
mini: true,
|
||||
backgroundColor: ThemeConfig.toggleBtnBg,
|
||||
onPressed: onToggleLogs,
|
||||
child: Icon(
|
||||
showLogs ? Icons.visibility_off : Icons.visibility,
|
||||
color: ThemeConfig.textColorWhite,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../game/enums.dart';
|
||||
import '../../game/models.dart';
|
||||
import '../../game/config.dart';
|
||||
import '../../providers/battle_provider.dart';
|
||||
import '../../utils/item_utils.dart';
|
||||
import '../inventory/item_stat_widget.dart';
|
||||
import '../test/sprite_animation_widget.dart';
|
||||
import '../../screens/main_menu_screen.dart';
|
||||
|
||||
class BattleRewardOverlay extends StatefulWidget {
|
||||
final BattleProvider battleProvider;
|
||||
|
||||
const BattleRewardOverlay({super.key, required this.battleProvider});
|
||||
|
||||
@override
|
||||
State<BattleRewardOverlay> createState() => _BattleRewardOverlayState();
|
||||
}
|
||||
|
||||
class _BattleRewardOverlayState extends State<BattleRewardOverlay> {
|
||||
bool _isCompletingReward = false;
|
||||
|
||||
Future<void> _selectReward(Item item) async {
|
||||
if (_isCompletingReward) return;
|
||||
setState(() => _isCompletingReward = true);
|
||||
|
||||
widget.battleProvider.selectReward(item);
|
||||
|
||||
if (mounted) {
|
||||
setState(() => _isCompletingReward = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final battleProvider = widget.battleProvider;
|
||||
return Container(
|
||||
color: ThemeConfig.cardBgColor,
|
||||
child: Center(
|
||||
child: SimpleDialog(
|
||||
title: Row(
|
||||
children: [
|
||||
const Text(
|
||||
"${AppStrings.victory} ${AppStrings.chooseReward}",
|
||||
),
|
||||
const Spacer(),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.monetization_on,
|
||||
color: ThemeConfig.statGoldColor,
|
||||
size: ThemeConfig.itemIconSizeSmall,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
"${battleProvider.lastGoldReward} G",
|
||||
style: TextStyle(
|
||||
color: ThemeConfig.statGoldColor,
|
||||
fontSize: ThemeConfig.fontSizeBody,
|
||||
fontWeight: ThemeConfig.fontWeightBold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
children: battleProvider.rewardOptions.map((item) {
|
||||
bool isSkip = item.id == "reward_skip";
|
||||
return SimpleDialogOption(
|
||||
onPressed: _isCompletingReward ? null : () => _selectReward(item),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
if (!isSkip)
|
||||
Container(
|
||||
padding: const EdgeInsets.all(4),
|
||||
decoration: BoxDecoration(
|
||||
color: ThemeConfig.rewardItemBg,
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
border: Border.all(
|
||||
color: item.rarity != ItemRarity.magic
|
||||
? ItemUtils.getRarityColor(item.rarity)
|
||||
: ThemeConfig.rarityCommon,
|
||||
),
|
||||
),
|
||||
child: Image.asset(
|
||||
ItemUtils.getIconPath(item.slot),
|
||||
width: ThemeConfig.itemIconSizeMedium,
|
||||
height: ThemeConfig.itemIconSizeMedium,
|
||||
fit: BoxFit.contain,
|
||||
filterQuality: FilterQuality.high,
|
||||
),
|
||||
),
|
||||
if (!isSkip) const SizedBox(width: 12),
|
||||
Text(
|
||||
item.name,
|
||||
style: TextStyle(
|
||||
fontWeight: ThemeConfig.fontWeightBold,
|
||||
fontSize: ThemeConfig.fontSizeLarge,
|
||||
color: isSkip
|
||||
? ThemeConfig.textColorGrey
|
||||
: ItemUtils.getRarityColor(item.rarity),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (!isSkip) ItemStatWidget(item: item),
|
||||
Text(
|
||||
item.description,
|
||||
style: const TextStyle(
|
||||
fontSize: ThemeConfig.fontSizeMedium,
|
||||
color: ThemeConfig.textColorGrey,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BattleDefeatOverlay extends StatelessWidget {
|
||||
const BattleDefeatOverlay({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: ThemeConfig.battleBg,
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SpriteAnimationWidget(
|
||||
assetPath: 'assets/images/character/Knight-Death.png',
|
||||
frameCount: 4,
|
||||
scale: 4.0,
|
||||
loop: false,
|
||||
customDuration: Duration(milliseconds: 1500),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
AppStrings.defeat,
|
||||
style: TextStyle(
|
||||
color: ThemeConfig.statHpColor,
|
||||
fontSize: ThemeConfig.fontSizeHuge,
|
||||
fontWeight: ThemeConfig.fontWeightBold,
|
||||
letterSpacing: ThemeConfig.letterSpacingHeader,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: ThemeConfig.menuButtonBg,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: ThemeConfig.paddingBtnHorizontal,
|
||||
vertical: ThemeConfig.paddingBtnVertical,
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushAndRemoveUntil(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const MainMenuScreen(),
|
||||
),
|
||||
(route) => false,
|
||||
);
|
||||
},
|
||||
child: const Text(
|
||||
AppStrings.returnToMenu,
|
||||
style: TextStyle(
|
||||
color: ThemeConfig.textColorWhite,
|
||||
fontSize: ThemeConfig.fontSizeHeader,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -129,7 +129,28 @@ class CharacterStatusCard extends StatelessWidget {
|
||||
// assetPath: 'assets/images/character/Soldier.png',
|
||||
assetPath: overrideImage ?? character.image!,
|
||||
scale: 5.0, // Zoomed in (300x300 in 200x200 box)
|
||||
frameCount: 6,
|
||||
frameCount: (overrideImage != null &&
|
||||
(overrideImage!.contains("Knight-Hurt") ||
|
||||
overrideImage!.contains("Knight-Death") ||
|
||||
overrideImage!.contains("Knight-Block")))
|
||||
? 4
|
||||
: (overrideImage != null &&
|
||||
overrideImage!.contains("Knight-Attack01"))
|
||||
? 7
|
||||
: (overrideImage != null &&
|
||||
overrideImage!
|
||||
.contains("Knight-Attack02"))
|
||||
? 10
|
||||
: (overrideImage != null &&
|
||||
overrideImage!
|
||||
.contains("Knight-Attack03"))
|
||||
? 11
|
||||
: 6,
|
||||
loop: !(overrideImage != null &&
|
||||
(overrideImage!.contains("Knight-Hurt") ||
|
||||
overrideImage!.contains("Knight-Death") ||
|
||||
overrideImage!.contains("Knight-Block") ||
|
||||
overrideImage!.contains("Knight-Attack"))),
|
||||
flip: !isPlayer,
|
||||
fallbackAssetPath: !isPlayer
|
||||
? 'assets/images/enemies/Orc.png'
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export 'inventory/character_stats_widget.dart';
|
||||
export 'inventory/inventory_grid_widget.dart';
|
||||
export 'inventory/equipped_items_widget.dart';
|
||||
export 'inventory/item_stat_widget.dart';
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../game/models.dart';
|
||||
import '../../game/config.dart';
|
||||
|
||||
class ItemStatWidget extends StatelessWidget {
|
||||
final Item item;
|
||||
final double fontSize;
|
||||
final Color? color;
|
||||
|
||||
const ItemStatWidget({
|
||||
super.key,
|
||||
required this.item,
|
||||
this.fontSize = ThemeConfig.fontSizeMedium,
|
||||
this.color,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
List<String> stats = [];
|
||||
if (item.atkBonus > 0) stats.add("+${item.atkBonus} ${AppStrings.atk}");
|
||||
if (item.hpBonus > 0) stats.add("+${item.hpBonus} ${AppStrings.hp}");
|
||||
if (item.armorBonus > 0) {
|
||||
stats.add("+${item.armorBonus} ${AppStrings.def}");
|
||||
}
|
||||
if (item.luck > 0) stats.add("+${item.luck} ${AppStrings.luck}");
|
||||
if (item.dodge > 0) stats.add("+${item.dodge}% Dodge");
|
||||
|
||||
List<String> effectTexts = item.effects.map((e) => e.description).toList();
|
||||
|
||||
if (stats.isEmpty && effectTexts.isEmpty) return const SizedBox.shrink();
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (stats.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4.0, bottom: 4.0),
|
||||
child: Text(
|
||||
stats.join(", "),
|
||||
style: TextStyle(
|
||||
fontSize: fontSize,
|
||||
color: color ?? ThemeConfig.statAtkColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (effectTexts.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4.0),
|
||||
child: Text(
|
||||
effectTexts.join(", "),
|
||||
style: const TextStyle(
|
||||
fontSize: 11,
|
||||
color: ThemeConfig.rarityLegendary,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ class SpriteAnimationWidget extends StatefulWidget {
|
||||
final int frameCount;
|
||||
final double scale;
|
||||
final bool flip;
|
||||
final bool loop;
|
||||
final Duration? customDuration;
|
||||
final String? fallbackAssetPath;
|
||||
|
||||
const SpriteAnimationWidget({
|
||||
@@ -17,10 +19,11 @@ class SpriteAnimationWidget extends StatefulWidget {
|
||||
required this.assetPath,
|
||||
this.tileWidth = 100.0,
|
||||
this.tileHeight = 100.0,
|
||||
this.frameCount =
|
||||
6, // Default guess, will adjust logic to use actual image width if possible
|
||||
this.frameCount = 6,
|
||||
this.scale = 1.0,
|
||||
this.flip = false,
|
||||
this.loop = true,
|
||||
this.customDuration,
|
||||
this.fallbackAssetPath,
|
||||
});
|
||||
|
||||
@@ -40,11 +43,22 @@ class _SpriteAnimationWidgetState extends State<SpriteAnimationWidget>
|
||||
super.initState();
|
||||
_controller = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 600), // 100ms per frame approx
|
||||
duration: widget.customDuration ?? const Duration(milliseconds: 600),
|
||||
);
|
||||
_loadImage();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant SpriteAnimationWidget oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.assetPath != widget.assetPath) {
|
||||
// Don't set _isLoading = true to avoid flickering.
|
||||
// Keep showing the old image until the new one is loaded.
|
||||
_controller.reset();
|
||||
_loadImage();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadImage() async {
|
||||
try {
|
||||
await _loadAsset(widget.assetPath);
|
||||
@@ -82,11 +96,18 @@ class _SpriteAnimationWidgetState extends State<SpriteAnimationWidget>
|
||||
? maxFrames
|
||||
: widget.frameCount;
|
||||
|
||||
// Adjust duration based on frame count
|
||||
_controller.duration = Duration(
|
||||
milliseconds: _calculatedFrameCount * 100,
|
||||
);
|
||||
_controller.repeat();
|
||||
// Adjust duration based on frame count if not custom
|
||||
if (widget.customDuration == null) {
|
||||
_controller.duration = Duration(
|
||||
milliseconds: _calculatedFrameCount * 100,
|
||||
);
|
||||
}
|
||||
|
||||
if (widget.loop) {
|
||||
_controller.repeat();
|
||||
} else {
|
||||
_controller.forward();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -99,17 +120,26 @@ class _SpriteAnimationWidgetState extends State<SpriteAnimationWidget>
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_isLoading || _image == null) {
|
||||
if (_image == null) {
|
||||
return SizedBox(
|
||||
width: widget.tileWidth * widget.scale,
|
||||
height: widget.tileHeight * widget.scale,
|
||||
child: const Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
return AnimatedBuilder(
|
||||
animation: _controller,
|
||||
builder: (context, child) {
|
||||
int frame;
|
||||
if (!widget.loop) {
|
||||
frame = (_controller.value * _calculatedFrameCount)
|
||||
.floor()
|
||||
.clamp(0, _calculatedFrameCount - 1);
|
||||
} else {
|
||||
frame = (_controller.value * _calculatedFrameCount).floor() %
|
||||
_calculatedFrameCount;
|
||||
}
|
||||
|
||||
return Transform.scale(
|
||||
scaleX: widget.flip ? -1.0 : 1.0,
|
||||
alignment: Alignment.center,
|
||||
@@ -120,9 +150,7 @@ class _SpriteAnimationWidgetState extends State<SpriteAnimationWidget>
|
||||
),
|
||||
painter: SpriteSheetPainter(
|
||||
image: _image!,
|
||||
currentFrame:
|
||||
(_controller.value * _calculatedFrameCount).floor() %
|
||||
_calculatedFrameCount,
|
||||
currentFrame: frame,
|
||||
tileWidth: widget.tileWidth,
|
||||
tileHeight: widget.tileHeight,
|
||||
scale: widget.scale,
|
||||
|
||||
Reference in New Issue
Block a user