refactor: simplify item effect handling in grid inventory
Update `getItemEffects` to directly use `startEffects` from item meta instead of performing a manual lookup against the effect table. This is made possible by updating `GameItemMeta` to store the full effect object (data and stacks) instead of just the effect ID and stack count.
This commit is contained in:
parent
0547180074
commit
113d240f71
|
|
@ -259,10 +259,7 @@ export function getAdjacentItems<TMeta>(
|
|||
}
|
||||
|
||||
// export type EffectTable = Record<string, { data: EffectData; stacks: number }>;
|
||||
export function getItemEffects(
|
||||
inv: GridInventory<GameItemMeta>,
|
||||
lookup: EffectData[],
|
||||
) {
|
||||
export function getItemEffects(inv: GridInventory<GameItemMeta>) {
|
||||
const effects = {} as Record<
|
||||
string,
|
||||
Record<string, { data: EffectData; stacks: number }>
|
||||
|
|
@ -273,21 +270,7 @@ export function getItemEffects(
|
|||
const { startEffects } = item.meta;
|
||||
if (!startEffects) continue;
|
||||
|
||||
const table = (effects[item.id] = {} as Record<
|
||||
string,
|
||||
{
|
||||
data: EffectData;
|
||||
stacks: number;
|
||||
}
|
||||
>);
|
||||
for (const [id, stacks] of Object.entries(startEffects)) {
|
||||
const data = lookup.find((row) => row.id === id);
|
||||
if (!data) continue;
|
||||
table[id] = {
|
||||
data,
|
||||
stacks,
|
||||
};
|
||||
}
|
||||
effects[item.id] = startEffects;
|
||||
}
|
||||
|
||||
return effects;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { ItemData } from "../types";
|
||||
import { EffectData, ItemData } from "../types";
|
||||
import type { ParsedShape } from "../utils/parse-shape";
|
||||
import type { Transform2D } from "../utils/shape-collision";
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ export interface GameItemMeta {
|
|||
itemData: ItemData;
|
||||
shape: ParsedShape;
|
||||
consumedUses?: number;
|
||||
startEffects?: Record<string, number>;
|
||||
startEffects?: Record<string, { data: EffectData; stacks: number }>;
|
||||
tradePrice?: number;
|
||||
}
|
||||
export type GameItem = InventoryItem<GameItemMeta>;
|
||||
|
|
|
|||
Loading…
Reference in New Issue