You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

43 lines
1.1 KiB

import type { BagSlotRecord } from '../types/MergeTypes';
/** 背包棋盘(纯数据,默认 5 槽) */
export class BagBoard {
readonly slotCount: number;
private _slots: (BagSlotRecord | null)[];
constructor(slotCount = 5) {
this.slotCount = slotCount;
this._slots = new Array(slotCount).fill(null);
}
get slots(): readonly (BagSlotRecord | null)[] {
return this._slots;
}
getSlot(index: number): BagSlotRecord | null {
return this._slots[index] ?? null;
}
isEmpty(index: number): boolean {
return this._slots[index] == null;
}
findEmptySlot(): number {
return this._slots.findIndex((s) => s == null);
}
setSlot(index: number, record: BagSlotRecord | null): void {
if (index < 0 || index >= this.slotCount) return;
this._slots[index] = record;
}
clearSlot(index: number): void {
this.setSlot(index, null);
}
fillSlots(records: BagSlotRecord[]): void {
for (let i = 0; i < this.slotCount; i++) {
this._slots[i] = records[i] ?? null;
}
}
}