refactor: 特殊属性定义方式

This commit is contained in:
AncTe committed 2026-10-09 20:09:51 +08:00
1 parent 46bcafcfbb
commit 1bcdcc3677
14 files changed
+439 -360

No files matched your search

@@ -74,8 +74,8 @@ function createEnemy(
*/
function createSpecial(code: number, value: number): ISpecial<number> {
return new modules.CommonSerializableSpecial<number>(code, value, {
getSpecialName: () => `special-${code}`,
getDescription: () => `description-${code}`,
name: () => `special-${code}`,
desc: () => `description-${code}`,
fromLegacyEnemy: () => value
});
}
@@ -78,8 +78,8 @@ function createEnemy(
/** 构造一个可序列化的数值特殊属性 */
function createSpecial(code: number, value: number): ISpecial<number> {
return new modules.CommonSerializableSpecial<number>(code, value, {
getSpecialName: () => `special-${code}`,
getDescription: () => `description-${code}`,
name: () => `special-${code}`,
desc: () => `description-${code}`,
fromLegacyEnemy: () => value
});
}
@@ -46,8 +46,8 @@ afterAll(() => {
*/
function makeCommonConfig(): ICommonSpecialConfig<number> {
return {
getSpecialName: special => `连击${special.getValue()}`,
getDescription: special => `怪物每回合攻击${special.getValue()}次。`,
name: special => `连击${special.getValue()}`,
desc: special => `怪物每回合攻击${special.getValue()}次。`,
fromLegacyEnemy: () => 0
};
}
@@ -57,8 +57,8 @@ function makeCommonConfig(): ICommonSpecialConfig<number> {
*/
function makeNoneConfig(): ICommonSpecialConfig<void> {
return {
getSpecialName: () => '先攻',
getDescription: () => '怪物首先攻击。',
name: () => '先攻',
desc: () => '怪物首先攻击。',
fromLegacyEnemy: () => undefined
};
}
+44 -8
View File
@@ -2,13 +2,13 @@ import { isEqual } from 'lodash-es';
import { SaveCompression } from '@user/data-common';
import { ISpecial, SpecialCreation } from './types';
// TODO: 颜色参数
export interface ICommonSpecialConfig<T> {
/** 获取特殊属性的名称 */
getSpecialName: (special: ISpecial<T>) => string;
name: string | ((special: ISpecial<T>) => string);
/** 获取特殊属性的描述 */
getDescription: (special: ISpecial<T>) => string;
desc: string | ((special: ISpecial<T>) => string);
/** 获取特殊属性的名称显示颜色 */
color: string | ((special: ISpecial<T>) => string);
}
export class CommonSerializableSpecial<T> implements ISpecial<T> {
@@ -26,12 +26,30 @@ export class CommonSerializableSpecial<T> implements ISpecial<T> {
return this.value;
}
/**
* 获取配置数据的值,是字面量则直接返回,是函数则返回其调用结果
* @param config 配置源数据
*/
private getConfigValue(
config: string | ((special: ISpecial<T>) => string)
) {
if (typeof config === 'string') {
return config;
} else {
return config(this);
}
}
getSpecialName(): string {
return this.config.getSpecialName(this);
return this.getConfigValue(this.config.name);
}
getDescription(): string {
return this.config.getDescription(this);
return this.getConfigValue(this.config.desc);
}
getNameColor(): string {
return this.getConfigValue(this.config.color);
}
clone(): ISpecial<T> {
@@ -72,12 +90,30 @@ export class NonePropertySpecial implements ISpecial<void> {
return void 0;
}
/**
* 获取配置数据的值,是字面量则直接返回,是函数则返回其调用结果
* @param config 配置源数据
*/
private getConfigValue(
config: string | ((special: ISpecial<void>) => string)
) {
if (typeof config === 'string') {
return config;
} else {
return config(this);
}
}
getSpecialName(): string {
return this.config.getSpecialName(this);
return this.getConfigValue(this.config.name);
}
getDescription(): string {
return this.config.getDescription(this);
return this.getConfigValue(this.config.desc);
}
getNameColor(): string {
return this.getConfigValue(this.config.color);
}
clone(): ISpecial<void> {
@@ -55,6 +55,13 @@ export interface ISpecial<T = void> extends ISaveableContent<T> {
*/
getDescription(): string;
// TODO: 后续需要改进渲染端的文字渲染,颜色使用内嵌方式写入到字符串里面,而不是单独指定
/**
* 获取此特殊属性的名称颜色
*/
getNameColor(): string;
/**
* 深拷贝此特殊属性
*/