feat: cost implementation for card play

This commit is contained in:
2026-04-17 14:10:28 +08:00
parent 8155747cac
commit fb66ec55c4
4 changed files with 174 additions and 10 deletions
@@ -1,7 +1,7 @@
import {CombatEntity, EffectTable} from "./types";
import {EffectData} from "@/samples/slay-the-spire-like/system/types";
import {PlayerEntity} from "@/samples/slay-the-spire-like/system/combat/types";
import {CombatState} from "./types";
import {CombatEntity, CombatState, EffectTable, PlayerEntity} from "./types";
import {CardData, EffectData} from "@/samples/slay-the-spire-like/system/types";
import {GameItemMeta} from "@/samples/slay-the-spire-like/system/progress/types";
import {GridInventory} from "@/samples/slay-the-spire-like/system/grid-inventory/types";
export function addEffect(effects: EffectTable, effect: EffectData, stacks: number){
let current = effects[effect.id];
@@ -82,4 +82,28 @@ export function* getAliveEnemies(state: CombatState) {
export function getCombatEntity(state: CombatState, entityKey: string){
return entityKey === 'player' ? state.player : state.enemies.find(e => e.id === entityKey);
}
}
export function canPlayCard(player: PlayerEntity, costType: CardData['costType'], costCount: number, itemId: string, inventory: GridInventory<GameItemMeta>): boolean {
if (costType === 'energy') {
return player.energy >= costCount;
}
if (costType === 'uses') {
const item = inventory.items.get(itemId);
if (!item || !item.meta) return false;
const depletion = item.meta.depletion ?? 0;
return depletion < costCount;
}
return true;
}
export function payCardCost(player: PlayerEntity, costType: CardData['costType'], costCount: number, itemId: string, inventory: GridInventory<GameItemMeta>): void {
if (costType === 'energy') {
player.energy -= costCount;
} else if (costType === 'uses') {
const item = inventory.items.get(itemId);
if (item && item.meta) {
item.meta.depletion = (item.meta.depletion ?? 0) + costCount;
}
}
}