mirror of
https://github.com/motajs/template.git
synced 2026-11-28 00:44:35 +08:00
feat: IITemStore
This commit is contained in:
1 parent
e72ad36e8d
commit
6029d038c6
9 files changed
+394
-5
No files matched your search
@@ -1,2 +1,3 @@
|
||||
export * from './itemStore';
|
||||
export * from './tileStore';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,42 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import {
|
||||
IItemLegacyConverter,
|
||||
IItemRawData,
|
||||
IItemStore,
|
||||
ItemCategory
|
||||
} from './types';
|
||||
|
||||
export class ItemStore<TLegacy> implements IItemStore<TLegacy> {
|
||||
/** 以道具图块数字为键的原始道具定义表 */
|
||||
private readonly dataMap: Map<number, IItemRawData> = new Map();
|
||||
|
||||
/** 当前挂载的旧样板道具转换器 */
|
||||
private converter: IItemLegacyConverter<TLegacy> | null = null;
|
||||
|
||||
getData(num: number): IItemRawData | null {
|
||||
return this.dataMap.get(num) ?? null;
|
||||
}
|
||||
|
||||
getCategory(num: number): ItemCategory {
|
||||
return this.dataMap.get(num)?.category ?? ItemCategory.Unknown;
|
||||
}
|
||||
|
||||
addItem(data: IItemRawData): void {
|
||||
this.dataMap.set(data.num, data);
|
||||
}
|
||||
|
||||
attachLegacyConverter(converter: IItemLegacyConverter<TLegacy>): void {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
fromLegacy(num: number, legacy: TLegacy): IItemRawData {
|
||||
const converter = this.converter;
|
||||
if (!converter) {
|
||||
logger.error(57);
|
||||
throw new Error('Expected a item legacy converter.');
|
||||
}
|
||||
const data = converter.fromLegacy(num, legacy);
|
||||
this.addItem(data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,7 @@ export class TileStore<TLegacy = unknown> implements ITileStore<TLegacy> {
|
||||
const converter = this.legacyConverter;
|
||||
if (!converter) {
|
||||
logger.error(56);
|
||||
throw new Error('Expected a tile legacy converter');
|
||||
throw new Error('Expected a tile legacy converter.');
|
||||
}
|
||||
const data = converter.fromLegacy(num, legacy);
|
||||
this.addTile(data);
|
||||
|
||||
@@ -68,7 +68,7 @@ export interface ITileLegacyConverter<TLegacy> {
|
||||
fromLegacy(num: number, legacy: TLegacy): ITileRawData;
|
||||
}
|
||||
|
||||
export interface ITileStore<TLegacy = unknown> {
|
||||
export interface ITileStore<TLegacy> {
|
||||
/**
|
||||
* 获取指定图块数字对应的完整原始定义
|
||||
* @param num 图块数字
|
||||
@@ -120,3 +120,88 @@ export interface ITileStore<TLegacy = unknown> {
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region item
|
||||
|
||||
export const enum ItemCategory {
|
||||
/** 未知或尚未归类的道具 */
|
||||
Unknown,
|
||||
/** 永久道具,使用后不会消耗 */
|
||||
Constant,
|
||||
/** 一次性道具,使用后消耗 */
|
||||
Consumable,
|
||||
/** 即捡即用道具,捡到立刻生效 */
|
||||
Pick,
|
||||
/** 装备类道具,可装备 / 卸下 */
|
||||
Equipment
|
||||
}
|
||||
|
||||
export interface IItemRawData {
|
||||
/** 道具在地图上的图块数字 */
|
||||
readonly num: number;
|
||||
/** 道具的字符串标识符 */
|
||||
readonly id: string;
|
||||
/** 道具分类 */
|
||||
readonly category: ItemCategory;
|
||||
/** 道具显示名称 */
|
||||
readonly name: string;
|
||||
/** 道具描述文本 */
|
||||
readonly text: string;
|
||||
|
||||
/** 即捡即用效果(Pick 类型) */
|
||||
readonly pickEffect: string;
|
||||
/** 即捡即用事件(Pick 类型) */
|
||||
readonly pickEvent: unknown;
|
||||
/** 道具使用效果(Constant 和 Consumable 类型) */
|
||||
readonly useEffect: string;
|
||||
/** 道具使用事件(Constant 和 Consumable 类型) */
|
||||
readonly useEvent: unknown;
|
||||
/** 是否能够使用道具(Constant 和 Consumable 类型) */
|
||||
readonly canUse: string;
|
||||
/** 装备道具属性(Equipment 类型) */
|
||||
readonly equip: unknown;
|
||||
}
|
||||
|
||||
export interface IItemLegacyConverter<TLegacy> {
|
||||
/**
|
||||
* 将旧样板道具定义转换为新的道具原始数据
|
||||
* @param num 道具图块数字
|
||||
* @param legacy 旧样板道具定义
|
||||
*/
|
||||
fromLegacy(num: number, legacy: TLegacy): IItemRawData;
|
||||
}
|
||||
|
||||
export interface IItemStore<TLegacy> {
|
||||
/**
|
||||
* 获取指定图块数字对应的道具原始数据
|
||||
* @param num 道具图块数字
|
||||
*/
|
||||
getData(num: number): IItemRawData | null;
|
||||
|
||||
/**
|
||||
* 获取指定图块数字对应的道具分类
|
||||
* @param num 道具图块数字
|
||||
*/
|
||||
getCategory(num: number): ItemCategory;
|
||||
|
||||
/**
|
||||
* 添加一个道具原始数据定义
|
||||
* @param data 道具原始数据
|
||||
*/
|
||||
addItem(data: IItemRawData): void;
|
||||
|
||||
/**
|
||||
* 挂载一个旧样板转换器
|
||||
* @param converter 旧样板转换器
|
||||
*/
|
||||
attachLegacyConverter(converter: IItemLegacyConverter<TLegacy>): void;
|
||||
|
||||
/**
|
||||
* 使用当前转换器转换并写入一个旧样板道具定义
|
||||
* @param num 道具图块数字
|
||||
* @param legacy 旧样板道具定义
|
||||
*/
|
||||
fromLegacy(num: number, legacy: TLegacy): IItemRawData;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ITileLocator } from '@motajs/common';
|
||||
import { IFaceManager, IRoleFaceBinder } from './common';
|
||||
import { ITileStore } from './store';
|
||||
import { IItemStore, ITileStore } from './store';
|
||||
import { ISaveSystem } from './save';
|
||||
|
||||
export interface IEnemyAttr {
|
||||
@@ -46,6 +46,8 @@ export interface IHeroAttr {
|
||||
export interface IDataCommon {
|
||||
/** 图块定义存储 */
|
||||
readonly tileStore: ITileStore<MapDataOf<keyof NumberToId>>;
|
||||
/** 道具定义存储 */
|
||||
readonly itemStore: IItemStore;
|
||||
/** 朝向绑定 */
|
||||
readonly roleFace: IRoleFaceBinder;
|
||||
/** 朝向管理 */
|
||||
|
||||
Reference in new issue
Block a user