This commit is contained in:
2025-12-01 18:38:47 +09:00
parent 514b49f7d9
commit ae1ebdc6bf
27 changed files with 1965 additions and 597 deletions
+313
View File
@@ -0,0 +1,313 @@
import 'package:flutter/material.dart';
import 'package:game_test/game/model/item.dart';
import 'package:provider/provider.dart';
import '../providers/battle_provider.dart';
import '../game/model/entity.dart';
class BattleScreen extends StatefulWidget {
const BattleScreen({super.key});
@override
State<BattleScreen> createState() => _BattleScreenState();
}
class _BattleScreenState extends State<BattleScreen> {
final ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
// Scroll to the bottom of the log when new messages are added
WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
});
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
void _showRiskLevelSelection(BuildContext context, ActionType actionType) {
final player = context.read<BattleProvider>().player;
final baseValue = actionType == ActionType.attack
? player.totalAtk
: player.totalDefense;
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text("Select Risk Level for ${actionType.name}"),
children: RiskLevel.values.map((risk) {
String infoText = "";
Color infoColor = Colors.black;
double efficiency = 0.0;
int expectedValue = 0;
switch (risk) {
case RiskLevel.safe:
efficiency = 0.5;
infoColor = Colors.green;
break;
case RiskLevel.normal:
efficiency = 1.0;
infoColor = Colors.blue;
break;
case RiskLevel.risky:
efficiency = 2.0;
infoColor = Colors.red;
break;
}
expectedValue = (baseValue * efficiency).toInt();
String valueUnit = actionType == ActionType.attack
? "Dmg"
: "Armor";
String successRate = "";
switch (risk) {
case RiskLevel.safe:
successRate = "100%";
break;
case RiskLevel.normal:
successRate = "80%";
break;
case RiskLevel.risky:
successRate = "40%";
break;
}
infoText =
"Success: $successRate, Eff: ${(efficiency * 100).toInt()}% ($expectedValue $valueUnit)";
return SimpleDialogOption(
onPressed: () {
context.read<BattleProvider>().playerAction(actionType, risk);
Navigator.pop(context);
// Ensure the log scrolls to the bottom after action
WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
});
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
risk.name,
style: const TextStyle(fontWeight: FontWeight.bold),
),
Text(
infoText,
style: TextStyle(fontSize: 12, color: infoColor),
),
],
),
);
}).toList(),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Consumer<BattleProvider>(
builder: (context, provider, child) =>
Text("Colosseum's Choice - Stage ${provider.stage}"),
),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () => context.read<BattleProvider>().initializeBattle(),
),
],
),
body: Consumer<BattleProvider>(
builder: (context, battleProvider, child) {
return Stack(
children: [
Column(
children: [
// Top (Status Area)
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildCharacterStatus(
battleProvider.enemy,
isEnemy: true,
),
_buildCharacterStatus(
battleProvider.player,
isEnemy: false,
),
],
),
),
// Middle (Log Area)
Expanded(
child: Container(
color: Colors.black87,
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
controller: _scrollController,
itemCount: battleProvider.battleLogs.length,
itemBuilder: (context, index) {
return Text(
battleProvider.battleLogs[index],
style: const TextStyle(
color: Colors.white,
fontFamily: 'Monospace',
fontSize: 12,
),
);
},
),
),
),
// Bottom (Control Area)
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildActionButton(
context,
"ATTACK",
ActionType.attack,
battleProvider.isPlayerTurn &&
!battleProvider.player.isDead &&
!battleProvider.enemy.isDead &&
!battleProvider.showRewardPopup,
),
_buildActionButton(
context,
"DEFEND",
ActionType.defend,
battleProvider.isPlayerTurn &&
!battleProvider.player.isDead &&
!battleProvider.enemy.isDead &&
!battleProvider.showRewardPopup,
),
],
),
),
],
),
if (battleProvider.showRewardPopup)
Container(
color: Colors.black54,
child: Center(
child: SimpleDialog(
title: const Text("Victory! Choose a Reward"),
children: battleProvider.rewardOptions.map((item) {
return SimpleDialogOption(
onPressed: () {
battleProvider.selectReward(item);
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
item.name,
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
_buildItemStatText(item), // Display stats here
Text(
item.description,
style: const TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
],
),
);
}).toList(),
),
),
),
],
);
},
),
);
}
Widget _buildItemStatText(Item item) {
List<String> stats = [];
if (item.atkBonus > 0) stats.add("+${item.atkBonus} ATK");
if (item.hpBonus > 0) stats.add("+${item.hpBonus} HP");
if (item.armorBonus > 0) stats.add("+${item.armorBonus} DEF");
if (stats.isEmpty) return const SizedBox.shrink(); // Hide if no stats
return Padding(
padding: const EdgeInsets.only(top: 4.0, bottom: 4.0),
child: Text(
stats.join(", "),
style: const TextStyle(fontSize: 12, color: Colors.blueAccent),
),
);
}
Widget _buildCharacterStatus(Character character, {bool isEnemy = false}) {
return Column(
children: [
Text(
"${character.name}: HP ${character.hp}/${character.totalMaxHp}",
style: TextStyle(
color: character.isDead ? Colors.red : Colors.white,
fontWeight: FontWeight.bold,
),
),
SizedBox(
width: 100,
child: LinearProgressIndicator(
value: character.totalMaxHp > 0
? character.hp / character.totalMaxHp
: 0,
color: isEnemy ? Colors.red : Colors.green,
backgroundColor: Colors.grey,
),
),
if (!isEnemy) ...[
Text("Armor: ${character.armor}"),
Text("ATK: ${character.totalAtk}"),
Text("DEF: ${character.totalDefense}"),
],
],
);
}
Widget _buildActionButton(
BuildContext context,
String text,
ActionType actionType,
bool isEnabled,
) {
return ElevatedButton(
onPressed: isEnabled
? () => _showRiskLevelSelection(context, actionType)
: null,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 15),
backgroundColor: Colors.blueGrey,
foregroundColor: Colors.white,
textStyle: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
child: Text(text),
);
}
}
+408
View File
@@ -0,0 +1,408 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/battle_provider.dart';
import '../game/model/item.dart';
import '../game/model/entity.dart';
class InventoryScreen extends StatelessWidget {
const InventoryScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Inventory & Stats")),
body: Consumer<BattleProvider>(
builder: (context, battleProvider, child) {
final player = battleProvider.player;
return Column(
children: [
// Player Stats Header
Card(
margin: const EdgeInsets.all(16.0),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
player.name,
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text("Stage: ${battleProvider.stage}"),
const Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildStatItem(
"HP",
"${player.hp}/${player.totalMaxHp}",
),
_buildStatItem("ATK", "${player.totalAtk}"),
_buildStatItem("DEF", "${player.totalDefense}"),
_buildStatItem("Shield", "${player.armor}"), // Temporary armor points
],
),
],
),
),
),
// Equipped Items Section (Slot based)
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Equipped Items",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: EquipmentSlot.values.map((slot) {
final item = player.equipment[slot];
return Expanded(
child: InkWell(
onTap: item != null
? () => _showUnequipConfirmationDialog(context, battleProvider, item)
: null,
child: Card(
color: item != null
? Colors.blueGrey[600]
: Colors.grey[800],
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Text(
slot.name.toUpperCase(),
style: const TextStyle(
fontSize: 10,
fontWeight: FontWeight.bold,
color: Colors.grey,
),
),
const SizedBox(height: 4),
Icon(
_getIconForSlot(slot),
size: 24,
color: item != null
? Colors.white
: Colors.grey,
),
const SizedBox(height: 4),
Text(
item?.name ?? "Empty",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 12,
color: item != null
? Colors.white
: Colors.grey,
),
overflow: TextOverflow.ellipsis,
),
if (item != null) _buildItemStatText(item),
],
),
),
),
),
);
}).toList(),
),
],
),
),
// Inventory (Bag) Section
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"Bag (${player.inventory.length}/${player.maxInventorySize})",
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
),
Expanded(
child: GridView.builder(
padding: const EdgeInsets.all(16.0),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
crossAxisSpacing: 8.0,
mainAxisSpacing: 8.0,
),
itemCount: player.maxInventorySize,
itemBuilder: (context, index) {
if (index < player.inventory.length) {
final item = player.inventory[index];
return InkWell(
onTap: () {
// Show confirmation dialog before equipping
_showEquipConfirmationDialog(
context,
battleProvider,
item,
);
},
child: Card(
color: Colors.blueGrey[700],
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.backpack, size: 32),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
item.name,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 10),
overflow: TextOverflow.ellipsis,
),
),
_buildItemStatText(item),
],
),
),
);
} else {
// Empty slot
return Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
color: Colors.grey[800],
),
child: const Center(
child: Icon(Icons.add_box, color: Colors.grey),
),
);
}
},
),
),
],
);
},
),
);
}
IconData _getIconForSlot(EquipmentSlot slot) {
switch (slot) {
case EquipmentSlot.weapon:
return Icons.g_mobiledata; // Using a generic 'game' icon for weapon
case EquipmentSlot.armor:
return Icons.checkroom;
case EquipmentSlot.shield:
return Icons.shield;
case EquipmentSlot.accessory:
return Icons.diamond;
}
}
Widget _buildStatItem(String label, String value) {
return Column(
children: [
Text(label, style: const TextStyle(color: Colors.grey, fontSize: 12)),
Text(
value,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
],
);
}
void _showEquipConfirmationDialog(
BuildContext context,
BattleProvider provider,
Item newItem,
) {
final player = provider.player;
final oldItem = player.equipment[newItem.slot];
// Calculate predicted stats
final currentMaxHp = player.totalMaxHp;
final currentAtk = player.totalAtk;
final currentDef = player.totalDefense;
final currentHp = player.hp;
// Predict new stats
int newMaxHp = currentMaxHp - (oldItem?.hpBonus ?? 0) + newItem.hpBonus;
int newAtk = currentAtk - (oldItem?.atkBonus ?? 0) + newItem.atkBonus;
int newDef = currentDef - (oldItem?.armorBonus ?? 0) + newItem.armorBonus;
// Predict HP (Percentage Logic)
double ratio = currentMaxHp > 0 ? currentHp / currentMaxHp : 0.0;
int newHp = (newMaxHp * ratio).toInt();
if (newHp < 0) newHp = 0;
if (newHp > newMaxHp) newHp = newMaxHp;
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Change Equipment"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Equip ${newItem.name}?",
style: const TextStyle(fontWeight: FontWeight.bold),
),
if (oldItem != null)
Text(
"Replaces ${oldItem.name}",
style: const TextStyle(fontSize: 12, color: Colors.grey),
),
const SizedBox(height: 16),
_buildStatChangeRow("Max HP", currentMaxHp, newMaxHp),
_buildStatChangeRow("Current HP", currentHp, newHp),
_buildStatChangeRow("ATK", currentAtk, newAtk),
_buildStatChangeRow("DEF", currentDef, newDef),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text("Cancel"),
),
ElevatedButton(
onPressed: () {
provider.equipItem(newItem);
Navigator.pop(ctx);
},
child: const Text("Confirm"),
),
],
),
);
}
void _showUnequipConfirmationDialog(
BuildContext context,
BattleProvider provider,
Item itemToUnequip,
) {
final player = provider.player;
// Calculate predicted stats
final currentMaxHp = player.totalMaxHp;
final currentAtk = player.totalAtk;
final currentDef = player.totalDefense;
final currentHp = player.hp;
// Predict new stats (Subtract item bonuses)
int newMaxHp = currentMaxHp - itemToUnequip.hpBonus;
int newAtk = currentAtk - itemToUnequip.atkBonus;
int newDef = currentDef - itemToUnequip.armorBonus;
// Predict HP (Percentage Logic)
double ratio = currentMaxHp > 0 ? currentHp / currentMaxHp : 0.0;
int newHp = (newMaxHp * ratio).toInt();
if (newHp < 0) newHp = 0;
if (newHp > newMaxHp) newHp = newMaxHp;
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Unequip Item"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Unequip ${itemToUnequip.name}?",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
_buildStatChangeRow("Max HP", currentMaxHp, newMaxHp),
_buildStatChangeRow("Current HP", currentHp, newHp),
_buildStatChangeRow("ATK", currentAtk, newAtk),
_buildStatChangeRow("DEF", currentDef, newDef),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text("Cancel"),
),
ElevatedButton(
onPressed: () {
provider.unequipItem(itemToUnequip);
Navigator.pop(ctx);
},
child: const Text("Confirm"),
),
],
),
);
}
Widget _buildStatChangeRow(String label, int oldVal, int newVal) {
int diff = newVal - oldVal;
Color color = diff > 0
? Colors.green
: (diff < 0 ? Colors.red : Colors.grey);
String diffText = diff > 0 ? "(+$diff)" : (diff < 0 ? "($diff)" : "");
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(label),
Row(
children: [
Text("$oldVal", style: const TextStyle(color: Colors.grey)),
const Icon(Icons.arrow_right, size: 16, color: Colors.grey),
Text(
"$newVal",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(width: 4),
Text(
diffText,
style: TextStyle(
color: color,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
],
),
],
),
);
}
Widget _buildItemStatText(Item item) {
List<String> stats = [];
if (item.atkBonus > 0) stats.add("+${item.atkBonus} ATK");
if (item.hpBonus > 0) stats.add("+${item.hpBonus} HP");
if (item.armorBonus > 0) stats.add("+${item.armorBonus} DEF");
if (stats.isEmpty) return const SizedBox.shrink(); // Hide if no stats
return Padding(
padding: const EdgeInsets.only(top: 2.0, bottom: 2.0),
child: Text(
stats.join(", "),
style: const TextStyle(fontSize: 10, color: Colors.blueAccent),
),
);
}
}
+47
View File
@@ -0,0 +1,47 @@
import 'package:flutter/material.dart';
import 'battle_screen.dart';
import 'inventory_screen.dart';
class MainWrapper extends StatefulWidget {
const MainWrapper({super.key});
@override
State<MainWrapper> createState() => _MainWrapperState();
}
class _MainWrapperState extends State<MainWrapper> {
int _currentIndex = 0;
final List<Widget> _screens = [
const BattleScreen(),
const InventoryScreen(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: _screens,
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.flash_on),
label: 'Battle',
),
BottomNavigationBarItem(
icon: Icon(Icons.backpack),
label: 'Inventory',
),
],
),
);
}
}