2022-08-26 16:48:17 +08:00

48 lines
1.6 KiB
TypeScript

import { ITableJson } from "./Core/ITableJson";
import { ITableRow } from "./Core/ITableRow";
export class TableManager {
private static _tableJsons: { [key: string]: ITableJson } = {};
public static AddJsonAssets(jsonAssets: cc.JsonAsset[]) {
if (!jsonAssets) return;
let newAssets: cc.JsonAsset[] = jsonAssets.concat();
for (let jsonAsset of newAssets) {
this.AddJsonAsset(jsonAsset);
}
}
public static AddJsonAsset(jsonAsset: cc.JsonAsset) {
if (!jsonAsset) {
return;
}
for (let tableName in jsonAsset.json) {
console.log(`TableV3 [${tableName}] json loaded`);
this._tableJsons[tableName] = jsonAsset.json[tableName];
}
}
public static GetTable(name: string): ITableJson {
return this._tableJsons[name];
}
public static InitTable<T extends Array<ITableRow>>(name: string, tableType: { new(): T }, rowType: { new(): ITableRow }): T {
let json = this._tableJsons[name];
if (!json) {
throw new Error(`TableV3 [${name}] 尚未載入json檔`);
}
let table = new tableType();
let cols = json.cols;
let colLength = cols.length;
let rows = json.rows;
for (let r of rows) {
let trow = new rowType();
for (let i = 0; i < colLength; i++) {
trow[cols[i]] = r[i];
}
table[trow.Id] = trow;
}
//cc.log(`TableV3 [${name}] init done`);
return table;
}
}