Inventory slot ids can be either strings or numbers.
This commit is contained in:
parent
8c7969771d
commit
6b6fd8970d
|
|
@ -1,20 +1,20 @@
|
|||
import type { InventoryOptions, InventorySlotInput } from "./types";
|
||||
import type { InventoryOptions, InventorySlotInput, SlotId } from "./types";
|
||||
|
||||
interface SlotEntry {
|
||||
readonly slotId: string;
|
||||
readonly slotId: SlotId;
|
||||
readonly limit: number | undefined;
|
||||
state: { itemId: string; amount: number } | null;
|
||||
}
|
||||
|
||||
export class Inventory {
|
||||
private readonly slots: Map<string, SlotEntry>;
|
||||
private readonly slots: Map<SlotId, SlotEntry>;
|
||||
private readonly maxAmountPerItem: Record<string, number>;
|
||||
|
||||
constructor(slotDefs: Array<InventorySlotInput>, options?: InventoryOptions) {
|
||||
this.slots = new Map(
|
||||
slotDefs.map(def => {
|
||||
const slotId = typeof def === 'string' ? def : def.slotId;
|
||||
const limit = typeof def === 'string' ? undefined : def.limit;
|
||||
const slotId = typeof def === 'object' ? def.slotId : def;
|
||||
const limit = typeof def === 'object' ? def.limit : undefined;
|
||||
return [slotId, { slotId, limit, state: null }];
|
||||
})
|
||||
);
|
||||
|
|
@ -34,7 +34,7 @@ export class Inventory {
|
|||
return this.slotCapFor(slot, itemId) - (slot.state?.amount ?? 0);
|
||||
}
|
||||
|
||||
addItem(itemId: string, amount: number, slotId?: string): boolean {
|
||||
addItem(itemId: string, amount: number, slotId?: SlotId): boolean {
|
||||
if (amount < 0) return false;
|
||||
if (amount === 0) return true;
|
||||
|
||||
|
|
@ -78,7 +78,7 @@ export class Inventory {
|
|||
return remaining === 0;
|
||||
}
|
||||
|
||||
removeItem(itemId: string, amount: number, slotId?: string): boolean {
|
||||
removeItem(itemId: string, amount: number, slotId?: SlotId): boolean {
|
||||
if (amount < 0) return false;
|
||||
if (amount === 0) return true;
|
||||
|
||||
|
|
@ -107,7 +107,7 @@ export class Inventory {
|
|||
return remaining === 0;
|
||||
}
|
||||
|
||||
getAmount(itemId: string, slotId?: string): number {
|
||||
getAmount(itemId: string, slotId?: SlotId): number {
|
||||
if (slotId !== undefined) {
|
||||
const slot = this.slots.get(slotId);
|
||||
return slot?.state?.itemId === itemId ? slot.state.amount : 0;
|
||||
|
|
|
|||
|
|
@ -97,12 +97,14 @@ function isQuestStage(v: unknown): v is QuestStage {
|
|||
&& Array.isArray(s.actions) && s.actions.every(isRPGAction);
|
||||
}
|
||||
|
||||
export type SlotId = string | number;
|
||||
|
||||
export interface InventorySlotDefinition {
|
||||
slotId: string;
|
||||
slotId: SlotId;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export type InventorySlotInput = string | InventorySlotDefinition;
|
||||
export type InventorySlotInput = SlotId | InventorySlotDefinition;
|
||||
|
||||
export interface InventoryOptions {
|
||||
maxAmountPerItem?: Record<string, number>;
|
||||
|
|
|
|||
Loading…
Reference in New Issue