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 type EffectTable = Record<string, { data: EffectData; stacks: number }>;
|
||||||
export function getItemEffects(
|
export function getItemEffects(inv: GridInventory<GameItemMeta>) {
|
||||||
inv: GridInventory<GameItemMeta>,
|
|
||||||
lookup: EffectData[],
|
|
||||||
) {
|
|
||||||
const effects = {} as Record<
|
const effects = {} as Record<
|
||||||
string,
|
string,
|
||||||
Record<string, { data: EffectData; stacks: number }>
|
Record<string, { data: EffectData; stacks: number }>
|
||||||
|
|
@ -273,21 +270,7 @@ export function getItemEffects(
|
||||||
const { startEffects } = item.meta;
|
const { startEffects } = item.meta;
|
||||||
if (!startEffects) continue;
|
if (!startEffects) continue;
|
||||||
|
|
||||||
const table = (effects[item.id] = {} as Record<
|
effects[item.id] = startEffects;
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return effects;
|
return effects;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { ItemData } from "../types";
|
import { EffectData, ItemData } from "../types";
|
||||||
import type { ParsedShape } from "../utils/parse-shape";
|
import type { ParsedShape } from "../utils/parse-shape";
|
||||||
import type { Transform2D } from "../utils/shape-collision";
|
import type { Transform2D } from "../utils/shape-collision";
|
||||||
|
|
||||||
|
|
@ -64,7 +64,7 @@ export interface GameItemMeta {
|
||||||
itemData: ItemData;
|
itemData: ItemData;
|
||||||
shape: ParsedShape;
|
shape: ParsedShape;
|
||||||
consumedUses?: number;
|
consumedUses?: number;
|
||||||
startEffects?: Record<string, number>;
|
startEffects?: Record<string, { data: EffectData; stacks: number }>;
|
||||||
tradePrice?: number;
|
tradePrice?: number;
|
||||||
}
|
}
|
||||||
export type GameItem = InventoryItem<GameItemMeta>;
|
export type GameItem = InventoryItem<GameItemMeta>;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue