mirror of
https://github.com/motajs/template.git
synced 2026-11-05 08:44:30 +08:00
refactor: 将旧样板怪物移植单独提取出来
This commit is contained in:
1 parent
6c1acb990e
commit
39e91b241f
6 files changed
+72
-14
No files matched your search
@@ -1,13 +1,18 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import { Enemy as EnemyImpl } from './enemy';
|
||||
import { IEnemy, IEnemyManager, SpecialCreation } from './types';
|
||||
import {
|
||||
IEnemy,
|
||||
IEnemyManager,
|
||||
IEnemyLegacyBridge,
|
||||
SpecialCreation
|
||||
} from './types';
|
||||
|
||||
export class EnemyManager<TAttr> implements IEnemyManager<TAttr> {
|
||||
/** 特殊属性注册表,code -> 创建函数 */
|
||||
private readonly specialRegistry: Map<number, SpecialCreation<any, TAttr>> =
|
||||
new Map();
|
||||
/** 自定义怪物属性注册表,name -> 默认值 */
|
||||
private readonly attributeRegistry: Map<string, any> = new Map();
|
||||
private readonly attributeRegistry: Map<keyof TAttr, any> = new Map();
|
||||
/** 怪物模板表,code -> IEnemy */
|
||||
private readonly prefabByCode: Map<number, IEnemy<TAttr>> = new Map();
|
||||
/** 怪物模板表,id -> IEnemy */
|
||||
@@ -15,11 +20,16 @@ export class EnemyManager<TAttr> implements IEnemyManager<TAttr> {
|
||||
/** 旧样板怪物 id 到 code 的映射,用于 fromLegacyEnemy 快速查找已有模板 */
|
||||
private readonly legacyIdToCode: Map<string, number> = new Map();
|
||||
|
||||
constructor(readonly bridge: IEnemyLegacyBridge<TAttr>) {}
|
||||
|
||||
registerSpecial(code: number, cons: SpecialCreation<any, TAttr>): void {
|
||||
this.specialRegistry.set(code, cons);
|
||||
}
|
||||
|
||||
registerAttribute(name: string, defaultValue: any): void {
|
||||
setAttributeDefaults<K extends keyof TAttr>(
|
||||
name: K,
|
||||
defaultValue: TAttr[K]
|
||||
): void {
|
||||
if (
|
||||
typeof defaultValue === 'function' ||
|
||||
typeof defaultValue === 'symbol' ||
|
||||
@@ -50,16 +60,13 @@ export class EnemyManager<TAttr> implements IEnemyManager<TAttr> {
|
||||
* @param enemy 旧样板怪物对象
|
||||
*/
|
||||
private createAttributes(enemy: Enemy): TAttr {
|
||||
const attrs: Record<string, any> = {};
|
||||
const attrs: Partial<TAttr> = {};
|
||||
for (const [name, defaultValue] of this.attributeRegistry) {
|
||||
attrs[name] = structuredClone(defaultValue);
|
||||
}
|
||||
attrs.hp = enemy.hp;
|
||||
attrs.atk = enemy.atk;
|
||||
attrs.def = enemy.def;
|
||||
attrs.money = enemy.money;
|
||||
attrs.exp = enemy.exp;
|
||||
attrs.point = enemy.point;
|
||||
|
||||
Object.assign(attrs, this.bridge.fromLegacyEnemy(enemy, attrs));
|
||||
|
||||
return attrs as TAttr;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { IRange, ITileLocator } from '@motajs/common';
|
||||
|
||||
//#region 怪物基础
|
||||
|
||||
export interface ISpecial<T = void> {
|
||||
/** 特殊属性代码 */
|
||||
readonly code: number;
|
||||
@@ -121,8 +123,20 @@ export interface IEnemy<TAttr> extends IReadonlyEnemy<TAttr> {
|
||||
copyFrom(enemy: IReadonlyEnemy<TAttr>): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 怪物管理器
|
||||
|
||||
export type SpecialCreation<T, TAttr> = (enemy: IEnemy<TAttr>) => ISpecial<T>;
|
||||
|
||||
export interface IEnemyLegacyBridge<TAttr> {
|
||||
/**
|
||||
* 从旧样板的怪物对象中获取其属性
|
||||
* @param enemy 旧样板怪物对象
|
||||
*/
|
||||
fromLegacyEnemy(enemy: Enemy, defaultValue: Partial<TAttr>): TAttr;
|
||||
}
|
||||
|
||||
export interface IEnemyManager<TAttr> {
|
||||
/**
|
||||
* 注册一个特殊属性
|
||||
@@ -136,7 +150,10 @@ export interface IEnemyManager<TAttr> {
|
||||
* @param name 属性名称
|
||||
* @param defaultValue 属性默认值
|
||||
*/
|
||||
registerAttribute(name: string, defaultValue: any): void;
|
||||
setAttributeDefaults<K extends keyof TAttr>(
|
||||
name: K,
|
||||
defaultValue: TAttr[K]
|
||||
): void;
|
||||
|
||||
/**
|
||||
* 根据旧样板怪物对象生成一个新的怪物对象
|
||||
@@ -196,6 +213,8 @@ export interface IEnemyManager<TAttr> {
|
||||
changePrefab(code: number | string, enemy: IEnemy<TAttr>): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 辅助接口
|
||||
|
||||
export interface IMapLocHelper {
|
||||
|
||||
Reference in new issue
Block a user