mirror of
https://github.com/motajs/template.git
synced 2026-11-28 08:54:30 +08:00
feat: 道具效果存储
This commit is contained in:
1 parent
6029d038c6
commit
9b3c3a4e40
8 files changed
+179
-265
No files matched your search
@@ -15,7 +15,9 @@ import {
|
||||
IHeroAttr,
|
||||
IEnemyAttr,
|
||||
ISaveSystem,
|
||||
SaveSystem
|
||||
SaveSystem,
|
||||
IItemStore,
|
||||
ItemStore
|
||||
} from '@user/data-common';
|
||||
import {
|
||||
EnemyManager,
|
||||
@@ -63,7 +65,12 @@ import {
|
||||
TILE_HEIGHT,
|
||||
TILE_WIDTH
|
||||
} from './shared';
|
||||
import { LegacyTileData, TileLegacyBridge } from './legacy';
|
||||
import {
|
||||
ItemLegacyBridge,
|
||||
LegacyItemData,
|
||||
LegacyTileData,
|
||||
TileLegacyBridge
|
||||
} from './legacy';
|
||||
import { ILoadProgressTotal, LoadProgressTotal } from '@motajs/loader';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { logger } from '@motajs/common';
|
||||
@@ -73,8 +80,9 @@ export class CoreState implements ICoreState {
|
||||
// Layer 0 公共层,最底层的接口,不会依赖任何其他内容,一般是工具性接口及不需要存档的数据
|
||||
readonly roleFace: IRoleFaceBinder;
|
||||
readonly faceManager: IFaceManager;
|
||||
readonly tileStore: ITileStore<LegacyTileData>;
|
||||
readonly saveSystem: ISaveSystem;
|
||||
readonly tileStore: ITileStore<LegacyTileData>;
|
||||
readonly itemStore: IItemStore<LegacyItemData>;
|
||||
|
||||
// Layer 1 数据层,所有可存档内容都在这,一般用于数据存储
|
||||
readonly maps: IMapStore;
|
||||
@@ -118,6 +126,10 @@ export class CoreState implements ICoreState {
|
||||
const tileStore = new TileStore<LegacyTileData>();
|
||||
tileStore.attachLegacyConverter(new TileLegacyBridge());
|
||||
this.tileStore = tileStore;
|
||||
// 道具
|
||||
const itemStore = new ItemStore<LegacyItemData>();
|
||||
itemStore.attachLegacyConverter(new ItemLegacyBridge(this));
|
||||
this.itemStore = itemStore;
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -203,6 +215,7 @@ export class CoreState implements ICoreState {
|
||||
// 加载初始化,先使用兼容层实现
|
||||
loading.once('loaded', () => {
|
||||
this.initTileStore(core.maps.blocksInfo);
|
||||
this.initItemStore(core.items.items);
|
||||
this.initEnemyManager(enemys_fcae963b_31c9_42b4_b48c_bb48d09f3f80);
|
||||
this.initMapStore(
|
||||
core.floorIds,
|
||||
@@ -250,6 +263,22 @@ export class CoreState implements ICoreState {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化道具存储对象
|
||||
* @param data 旧样板道具定义对象
|
||||
*/
|
||||
private initItemStore(data: typeof core.items.items) {
|
||||
const entries = Object.entries(data);
|
||||
for (const [id, legacy] of entries) {
|
||||
const num = this.tileStore.idToNumber(id);
|
||||
if (isNil(num)) {
|
||||
logger.warn(145, id);
|
||||
continue;
|
||||
}
|
||||
this.itemStore.fromLegacy(num, legacy);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化怪物管理器对象
|
||||
* @param data 旧样板怪物存储对象
|
||||
|
||||
@@ -1,101 +1,98 @@
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { loading } from '@user/data-base';
|
||||
import {
|
||||
IItemEffect,
|
||||
IItemLegacyConverter,
|
||||
IItemRawData,
|
||||
ItemCategory
|
||||
} from '@user/data-common';
|
||||
import { IStateSystem } from '@user/data-system';
|
||||
|
||||
type EffectFn = () => void;
|
||||
type CanUseEffectFn = () => boolean;
|
||||
export type LegacyItemData = Item<AllIdsOf<'items'>>;
|
||||
|
||||
interface ItemStateEvent {
|
||||
use: [];
|
||||
}
|
||||
type LegacyItemEffectFn = (state: IStateSystem, item: IItemRawData) => void;
|
||||
type LegacyItemCanUseFn = (state: IStateSystem, item: IItemRawData) => boolean;
|
||||
|
||||
export class ItemState<
|
||||
I extends AllIdsOf<'items'> = AllIdsOf<'items'>
|
||||
> extends EventEmitter<ItemStateEvent> {
|
||||
static items: Map<AllIdsOf<'items'>, ItemState> = new Map();
|
||||
class LegacyItemEffect implements IItemEffect {
|
||||
pickEvent: unknown;
|
||||
useEvent: unknown;
|
||||
|
||||
id: I;
|
||||
cls: ItemClsOf<I>;
|
||||
name: string;
|
||||
text?: string;
|
||||
hideInToolBox: boolean;
|
||||
hideInReplay: boolean;
|
||||
private readonly pickFn: LegacyItemEffectFn | null = null;
|
||||
private readonly useFn: LegacyItemEffectFn | null = null;
|
||||
private readonly canUseFn: LegacyItemCanUseFn | null = null;
|
||||
private readonly state: IStateSystem;
|
||||
|
||||
/** 即捡即用效果 */
|
||||
itemEffect?: string;
|
||||
/** 即捡即用道具捡过之后的提示 */
|
||||
itemEffectTip?: string;
|
||||
/** 使用道具时执行的事件 */
|
||||
useItemEvent?: MotaEvent;
|
||||
/** 使用道具时执行的代码 */
|
||||
useItemEffect?: string;
|
||||
/** 能否使用道具 */
|
||||
canUseItemEffect?: string | boolean;
|
||||
constructor(legacy: LegacyItemData, state: IStateSystem) {
|
||||
this.state = state;
|
||||
this.pickEvent = undefined;
|
||||
this.useEvent = legacy.useItemEvent;
|
||||
|
||||
private noRoute: boolean = false;
|
||||
|
||||
itemEffectFn?: EffectFn;
|
||||
useItemEffectFn?: EffectFn;
|
||||
canUseItemEffectFn?: CanUseEffectFn;
|
||||
|
||||
constructor(id: I) {
|
||||
super();
|
||||
const items = items_296f5d02_12fd_4166_a7c1_b5e830c9ee3a;
|
||||
this.id = id;
|
||||
const item = items[id];
|
||||
this.cls = item.cls;
|
||||
this.name = item.name;
|
||||
this.text = item.text;
|
||||
this.hideInToolBox = item.hideInToolBox;
|
||||
this.hideInReplay = item.hideInReplay;
|
||||
this.itemEffect = item.itemEffect;
|
||||
this.itemEffectTip = item.itemEffectTip;
|
||||
this.useItemEvent = item.useItemEvent;
|
||||
this.useItemEffect = item.useItemEffect;
|
||||
this.canUseItemEffect = item.canUseItemEffect;
|
||||
|
||||
this.compileFunction();
|
||||
}
|
||||
|
||||
private compileFunction() {
|
||||
if (this.itemEffect) {
|
||||
this.itemEffectFn = new Function(
|
||||
`state`,
|
||||
this.itemEffect
|
||||
) as EffectFn;
|
||||
if (legacy.itemEffect) {
|
||||
this.pickFn = new Function(
|
||||
'state',
|
||||
'item',
|
||||
legacy.itemEffect
|
||||
) as LegacyItemEffectFn;
|
||||
}
|
||||
if (this.useItemEffect) {
|
||||
this.useItemEffectFn = new Function(
|
||||
`state`,
|
||||
this.useItemEffect
|
||||
) as EffectFn;
|
||||
if (legacy.useItemEffect) {
|
||||
this.useFn = new Function(
|
||||
'state',
|
||||
'item',
|
||||
legacy.useItemEffect
|
||||
) as LegacyItemEffectFn;
|
||||
}
|
||||
if (this.canUseItemEffect) {
|
||||
if (typeof this.canUseItemEffect === 'boolean') {
|
||||
this.canUseItemEffectFn = () =>
|
||||
this.canUseItemEffect as boolean;
|
||||
} else {
|
||||
this.useItemEffectFn = new Function(
|
||||
`state`,
|
||||
this.canUseItemEffect
|
||||
) as CanUseEffectFn;
|
||||
}
|
||||
if (typeof legacy.canUseItemEffect === 'string') {
|
||||
this.canUseFn = new Function(
|
||||
'state',
|
||||
'item',
|
||||
legacy.canUseItemEffect
|
||||
) as LegacyItemCanUseFn;
|
||||
} else {
|
||||
this.canUseFn = () => !!legacy.canUseItemEffect;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个道具的信息
|
||||
* @param id 要获取的道具id
|
||||
*/
|
||||
static item<I extends AllIdsOf<'items'>>(id: I): ItemState<I> | undefined {
|
||||
return this.items.get(id) as ItemState<I>;
|
||||
pickEffect(item: IItemRawData): void {
|
||||
this.pickFn?.(this.state, item);
|
||||
}
|
||||
|
||||
useEffect(item: IItemRawData): void {
|
||||
this.useFn?.(this.state, item);
|
||||
}
|
||||
|
||||
canUse(item: IItemRawData): boolean {
|
||||
if (!this.canUseFn) return true;
|
||||
else return this.canUseFn(this.state, item);
|
||||
}
|
||||
}
|
||||
|
||||
loading.once('coreInit', () => {
|
||||
for (const key of Object.keys(items_296f5d02_12fd_4166_a7c1_b5e830c9ee3a)) {
|
||||
ItemState.items.set(
|
||||
key as AllIdsOf<'items'>,
|
||||
new ItemState(key as AllIdsOf<'items'>)
|
||||
);
|
||||
export class ItemLegacyBridge implements IItemLegacyConverter<LegacyItemData> {
|
||||
constructor(private readonly state: IStateSystem) {}
|
||||
|
||||
fromLegacy(num: number, legacy: LegacyItemData): IItemRawData {
|
||||
const effect = new LegacyItemEffect(legacy, this.state);
|
||||
return {
|
||||
num,
|
||||
id: legacy.id,
|
||||
category: this.mapCategory(legacy.cls),
|
||||
name: legacy.name,
|
||||
text: legacy.text ?? '',
|
||||
hideInToolbox: legacy.hideInToolBox,
|
||||
effect,
|
||||
equip: legacy.equip
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
private mapCategory(cls: ItemCls): ItemCategory {
|
||||
switch (cls) {
|
||||
case 'constants':
|
||||
return ItemCategory.Constant;
|
||||
case 'tools':
|
||||
return ItemCategory.Consumable;
|
||||
case 'equips':
|
||||
return ItemCategory.Equipment;
|
||||
case 'items':
|
||||
return ItemCategory.Pick;
|
||||
default:
|
||||
return ItemCategory.Unknown;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user