Refactor: Slim down BattleProvider and extract logic services. Restore original animations and positioning.
This commit is contained in:
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user