game/lib/game/data/item_table.dart

109 lines
3.0 KiB
Dart

import 'dart:convert';
import 'package:flutter/services.dart';
import '../model/item.dart';
import '../enums.dart';
class ItemTemplate {
final String name;
final String description;
final int baseAtk;
final int baseHp;
final int baseArmor;
final EquipmentSlot slot;
final List<ItemEffect> effects;
final int price;
final String? image;
const ItemTemplate({
required this.name,
required this.description,
this.baseAtk = 0,
this.baseHp = 0,
this.baseArmor = 0,
required this.slot,
this.effects = const [],
this.price = 0,
this.image,
});
factory ItemTemplate.fromJson(Map<String, dynamic> json) {
return ItemTemplate(
name: json['name'],
description: json['description'],
baseAtk: json['baseAtk'] ?? 0,
baseHp: json['baseHp'] ?? 0,
baseArmor: json['baseArmor'] ?? 0,
slot: EquipmentSlot.values.firstWhere((e) => e.name == json['slot']),
effects:
(json['effects'] as List<dynamic>?)
?.map((e) => ItemEffect.fromJson(e))
.toList() ??
[],
price: json['price'] ?? 0,
image: json['image'],
);
}
// Create an instance of Item based on this template, optionally scaling with stage
Item createItem({int stage = 1}) {
// Simple scaling logic: add stage-1 to relevant stats
// You can make this more complex (multiplier, tiering, etc.)
int scaledAtk = baseAtk > 0 ? baseAtk + (stage - 1) : 0;
int scaledHp = baseHp > 0 ? baseHp + (stage - 1) * 5 : 0;
int scaledArmor = baseArmor > 0 ? baseArmor + (stage - 1) : 0;
// Use fixed price from template
int finalPrice = price;
// Optional: Increase price if stage > 1 (e.g. +10% per stage)
if (stage > 1) {
finalPrice = (price * (1 + (stage - 1) * 0.1)).toInt();
}
return Item(
name: "$name${stage > 1 ? ' +${stage - 1}' : ''}", // Append +1, +2 etc.
description: description,
atkBonus: scaledAtk,
hpBonus: scaledHp,
armorBonus: scaledArmor,
slot: slot,
effects: effects, // Pass the effects to the Item
price: finalPrice,
image: image,
);
}
}
class ItemTable {
static List<ItemTemplate> weapons = [];
static List<ItemTemplate> armors = [];
static List<ItemTemplate> shields = [];
static List<ItemTemplate> accessories = [];
static Future<void> load() async {
final String jsonString = await rootBundle.loadString(
'assets/data/items.json',
);
final Map<String, dynamic> data = jsonDecode(jsonString);
weapons = (data['weapons'] as List)
.map((e) => ItemTemplate.fromJson(e))
.toList();
armors = (data['armors'] as List)
.map((e) => ItemTemplate.fromJson(e))
.toList();
shields = (data['shields'] as List)
.map((e) => ItemTemplate.fromJson(e))
.toList();
accessories = (data['accessories'] as List)
.map((e) => ItemTemplate.fromJson(e))
.toList();
}
static List<ItemTemplate> get allItems => [
...weapons,
...armors,
...shields,
...accessories,
];
}