mirror of
https://github.com/motajs/template.git
synced 2026-11-29 18:24:30 +08:00
feat: 装备系统
This commit is contained in:
1 parent
3ac14f8757
commit
376b0fcac5
15 files changed
+1426
-165
No files matched your search
@@ -1,6 +1,11 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import { SaveCompression } from '@user/data-common';
|
||||
import { IHeroAttribute, IHeroModifier } from './types';
|
||||
import {
|
||||
IHeroAttribute,
|
||||
IHeroAttributeCloneOption,
|
||||
IHeroModifier
|
||||
} from './types';
|
||||
import { isNil } from 'lodash-es';
|
||||
|
||||
export abstract class BaseHeroModifier<T, V> implements IHeroModifier<T, V, V> {
|
||||
abstract readonly type: string;
|
||||
@@ -44,10 +49,9 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
/** 当前勇士属性修饰器 */
|
||||
private readonly modifier: Map<keyof THero, IHeroModifier[]> = new Map();
|
||||
/** 当前每个修饰器对应的属性名称 */
|
||||
private readonly modifierName: Map<
|
||||
IHeroModifier<THero[keyof THero]>,
|
||||
keyof THero
|
||||
> = new Map();
|
||||
private readonly modifierName: Map<IHeroModifier, keyof THero> = new Map();
|
||||
/** 当前标记为不存档的修饰器集合 */
|
||||
private readonly modifierNosave: Set<IHeroModifier> = new Set();
|
||||
/** 当前勇士最终属性 */
|
||||
private readonly finalAttribute: THero;
|
||||
|
||||
@@ -58,6 +62,8 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
this.finalAttribute = structuredClone(attribute);
|
||||
}
|
||||
|
||||
//#region 属性计算
|
||||
|
||||
/**
|
||||
* 判定修饰器结果是否同引用
|
||||
* @param curr 当前属性值
|
||||
@@ -81,6 +87,7 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
const nextValue = modifier.modify(value, baseValue, name);
|
||||
// 部署之后就没必要弹这个警告了,额外判断反而可能会有一定的性能损失,直接 tree-shaking 优化掉
|
||||
if (import.meta.env.DEV && this.isSameReference(value, nextValue)) {
|
||||
// 对于对象属性,如果返回值和原始值的引用相同,那么应该抛出警告
|
||||
const modiferName = modifier.constructor.name;
|
||||
logger.warn(109, modiferName, String(name));
|
||||
}
|
||||
@@ -100,6 +107,7 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
const nextValue = modifier.modify(value, baseValue, name);
|
||||
// 部署之后就没必要弹这个警告了,额外判断反而可能会有一定的性能损失,直接 tree-shaking 优化掉
|
||||
if (import.meta.env.DEV && this.isSameReference(value, nextValue)) {
|
||||
// 对于对象属性,如果返回值和原始值的引用相同,那么应该抛出警告
|
||||
const modiferName = modifier.constructor.name;
|
||||
logger.warn(109, modiferName, String(name));
|
||||
}
|
||||
@@ -116,38 +124,59 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
return this.finalAttribute[name];
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 属性操作
|
||||
|
||||
set<K extends keyof THero>(name: K, value: THero[K]): void {
|
||||
this.attribute[name] = value;
|
||||
this.markDirty(name);
|
||||
}
|
||||
|
||||
add<K extends keyof SelectType<THero, number>>(
|
||||
name: K,
|
||||
value: number
|
||||
): void {
|
||||
add(name: SelectKey<THero, number>, value: number): void {
|
||||
(this.attribute[name] as number) += value;
|
||||
this.markDirty(name);
|
||||
}
|
||||
|
||||
mul<K extends keyof SelectType<THero, number>>(
|
||||
name: K,
|
||||
value: number
|
||||
): void {
|
||||
mul(name: SelectKey<THero, number>, value: number): void {
|
||||
(this.attribute[name] as number) *= value;
|
||||
this.markDirty(name);
|
||||
}
|
||||
|
||||
div<K extends keyof SelectType<THero, number>>(
|
||||
name: K,
|
||||
value: number
|
||||
): void {
|
||||
div(name: SelectKey<THero, number>, value: number): void {
|
||||
(this.attribute[name] as number) /= value;
|
||||
this.markDirty(name);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 修饰器处理
|
||||
|
||||
*iterateModifiers(): IterableIterator<[PropertyKey, IHeroModifier]> {
|
||||
for (const [modifier, name] of this.modifierName) {
|
||||
yield [name, modifier];
|
||||
}
|
||||
}
|
||||
|
||||
getModifiers<K extends keyof THero>(
|
||||
name: K
|
||||
): Iterable<IHeroModifier<THero[K]>> {
|
||||
const arr = this.modifier.get(name) as IHeroModifier<THero[K]>[];
|
||||
return arr ?? [];
|
||||
}
|
||||
|
||||
getModifierIndex(modifier: IHeroModifier): number {
|
||||
const name = this.modifierName.get(modifier);
|
||||
if (isNil(name)) return -1;
|
||||
const arr = this.modifier.get(name);
|
||||
if (!arr) return -1;
|
||||
return arr.indexOf(modifier);
|
||||
}
|
||||
|
||||
addModifier<K extends keyof THero>(
|
||||
name: K,
|
||||
modifier: IHeroModifier<THero[K]>
|
||||
modifier: IHeroModifier<THero[K]>,
|
||||
save: boolean = true
|
||||
): void {
|
||||
if (modifier.owner) {
|
||||
const modiferName = modifier.constructor.name;
|
||||
@@ -160,6 +189,9 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
modifierList.sort((left, right) => right.priority - left.priority);
|
||||
|
||||
this.modifierName.set(modifier, name);
|
||||
if (!save) {
|
||||
this.modifierNosave.add(modifier);
|
||||
}
|
||||
modifier.bindAttribute(this);
|
||||
this.markDirty(name);
|
||||
}
|
||||
@@ -176,10 +208,22 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
modifier.bindAttribute(null);
|
||||
modifierList.splice(index, 1);
|
||||
this.modifierName.delete(modifier);
|
||||
this.modifierNosave.delete(modifier);
|
||||
|
||||
this.markDirty(name);
|
||||
}
|
||||
|
||||
deleteModifierByIndex<K extends keyof THero>(
|
||||
name: K,
|
||||
index: number
|
||||
): IHeroModifier<THero[K]> | null {
|
||||
const arr = this.modifier.get(name);
|
||||
if (!arr) return null;
|
||||
const modifier = arr.splice(index, 1);
|
||||
if (modifier.length === 0) return null;
|
||||
else return modifier[0] as IHeroModifier<THero[K]>;
|
||||
}
|
||||
|
||||
markDirty(name: keyof THero): void {
|
||||
this.recalculateAttribute(name);
|
||||
}
|
||||
@@ -190,17 +234,34 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
this.markDirty(name);
|
||||
}
|
||||
|
||||
clone(cloneModifier: boolean = true): IHeroAttribute<THero> {
|
||||
setModifierSaveEnabled(modifier: IHeroModifier, save: boolean): void {
|
||||
if (save) {
|
||||
this.modifierNosave.delete(modifier);
|
||||
} else {
|
||||
this.modifierNosave.add(modifier);
|
||||
}
|
||||
}
|
||||
|
||||
getModifierSaveEnabled(modifier: IHeroModifier): boolean {
|
||||
return !this.modifierNosave.has(modifier);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 属性克隆
|
||||
|
||||
clone(
|
||||
option: Readonly<Partial<IHeroAttributeCloneOption>> = {}
|
||||
): IHeroAttribute<THero> {
|
||||
const { cloneModifier = true } = option;
|
||||
const cloned = new HeroAttribute<THero>(
|
||||
structuredClone(this.attribute)
|
||||
);
|
||||
if (!cloneModifier) return cloned;
|
||||
// 拷贝修饰器
|
||||
for (const [modifier, name] of this.modifierName) {
|
||||
cloned.addModifier(
|
||||
name,
|
||||
modifier.clone() as IHeroModifier<THero[keyof THero]>
|
||||
);
|
||||
for (const [name, modifiers] of this.modifier) {
|
||||
const arr: IHeroModifier[] = modifiers.map(v => v.clone());
|
||||
cloned.modifier.set(name, arr);
|
||||
cloned.recalculateAttribute(name);
|
||||
}
|
||||
return cloned;
|
||||
}
|
||||
@@ -213,16 +274,5 @@ export class HeroAttribute<THero> implements IHeroAttribute<THero> {
|
||||
return structuredClone(this.attribute);
|
||||
}
|
||||
|
||||
*iterateModifiers(): IterableIterator<[PropertyKey, IHeroModifier]> {
|
||||
for (const [modifier, name] of this.modifierName) {
|
||||
yield [name, modifier];
|
||||
}
|
||||
}
|
||||
|
||||
getModifiers<K extends keyof THero>(
|
||||
name: K
|
||||
): Iterable<IHeroModifier<THero[K]>> {
|
||||
const arr = this.modifier.get(name) as IHeroModifier<THero[K]>[];
|
||||
return arr ?? [];
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
import { IItemRawData, SaveCompression, IDataCommon } from '@user/data-common';
|
||||
import { isNil, maxBy } from 'lodash-es';
|
||||
import { ValueModifier, PercentageModifier } from './modifier';
|
||||
import {
|
||||
IEquipmentState,
|
||||
IHeroModifier,
|
||||
IEquipmentStateSave,
|
||||
IHeroEquipsStore,
|
||||
IEquipmentSorter,
|
||||
IEquipmentSortHandler,
|
||||
IHeroEquipsStoreSave
|
||||
} from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
|
||||
export class EquipmentState<THero> implements IEquipmentState<THero> {
|
||||
readonly item: IItemRawData<THero>;
|
||||
|
||||
/** 装备产生的所有修饰器,每个元素为 [属性名, 修饰器] */
|
||||
readonly modifiers: [SelectKey<THero, number>, IHeroModifier<number>][];
|
||||
|
||||
/** 装备的数值属性 */
|
||||
private readonly value: Map<SelectKey<THero, number>, number>;
|
||||
/** 装备的百分比属性 */
|
||||
private readonly percentage: Map<SelectKey<THero, number>, number>;
|
||||
|
||||
constructor(
|
||||
readonly uid: number,
|
||||
item: IItemRawData<THero>,
|
||||
buildModifier: boolean = true
|
||||
) {
|
||||
this.item = item;
|
||||
this.modifiers = [];
|
||||
|
||||
const equip = item.equip;
|
||||
this.value = new Map(equip.value);
|
||||
this.percentage = new Map(equip.percentage);
|
||||
|
||||
if (buildModifier) {
|
||||
this.rebuildModifiers();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从装备属性重新生成装备修饰器
|
||||
*/
|
||||
private rebuildModifiers() {
|
||||
this.modifiers.length = 0;
|
||||
|
||||
for (const [name, baseVal] of this.value) {
|
||||
this.modifiers.push([name, new ValueModifier(baseVal)]);
|
||||
}
|
||||
for (const [name, basePct] of this.percentage) {
|
||||
this.modifiers.push([name, new PercentageModifier(basePct)]);
|
||||
}
|
||||
}
|
||||
|
||||
getModifiers(): Iterable<
|
||||
[keyof SelectType<THero, number>, IHeroModifier<number>]
|
||||
> {
|
||||
return this.modifiers;
|
||||
}
|
||||
|
||||
/**
|
||||
* NoCompression 级别保存:完整存储所有修饰器值
|
||||
* @returns 完整的装备状态存档
|
||||
*/
|
||||
private saveNoCompression(): IEquipmentStateSave<THero> {
|
||||
return {
|
||||
uid: this.uid,
|
||||
num: this.item.num,
|
||||
value: new Map(this.value),
|
||||
percentage: new Map(this.percentage)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Low / High 压缩级别保存:仅存储与原始定义存在差异的条目
|
||||
* @returns 差异化的装备状态存档
|
||||
*/
|
||||
private saveDiff(): IEquipmentStateSave<THero> {
|
||||
const { value, percentage } = this.item.equip;
|
||||
const valueDiff = new Map<SelectKey<THero, number>, number>();
|
||||
for (const [name, equipValue] of this.value) {
|
||||
const base = value.get(name);
|
||||
if (base !== equipValue) {
|
||||
valueDiff.set(name, equipValue);
|
||||
}
|
||||
}
|
||||
const perDiff = new Map<SelectKey<THero, number>, number>();
|
||||
for (const [name, equipPer] of this.percentage) {
|
||||
const base = percentage.get(name);
|
||||
if (base !== equipPer) {
|
||||
perDiff.set(name, equipPer);
|
||||
}
|
||||
}
|
||||
return {
|
||||
uid: this.uid,
|
||||
num: this.item.num,
|
||||
value: valueDiff,
|
||||
percentage: perDiff
|
||||
};
|
||||
}
|
||||
|
||||
saveState(compression: SaveCompression): IEquipmentStateSave<THero> {
|
||||
if (compression === SaveCompression.NoCompression) {
|
||||
return this.saveNoCompression();
|
||||
} else {
|
||||
return this.saveDiff();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* NoCompression 级别读档:使用完整存档值覆盖所有修饰器
|
||||
* @param state 装备状态存档
|
||||
*/
|
||||
private loadNoCompression(state: IEquipmentStateSave<THero>): void {
|
||||
this.value.clear();
|
||||
this.percentage.clear();
|
||||
for (const [name, value] of state.percentage) {
|
||||
this.value.set(name, value);
|
||||
}
|
||||
for (const [name, value] of state.percentage) {
|
||||
this.percentage.set(name, value);
|
||||
}
|
||||
this.rebuildModifiers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Low / High 压缩级别读档:从存档查询值,缺失则回退到原始定义
|
||||
* @param state 装备状态存档
|
||||
*/
|
||||
private loadDiff(state: IEquipmentStateSave<THero>): void {
|
||||
this.value.clear();
|
||||
this.percentage.clear();
|
||||
for (const [name, value] of state.percentage) {
|
||||
this.value.set(name, value);
|
||||
}
|
||||
for (const [name, value] of state.percentage) {
|
||||
this.percentage.set(name, value);
|
||||
}
|
||||
|
||||
// 差异内容
|
||||
for (const [name, value] of state.value) {
|
||||
this.value.set(name, value);
|
||||
}
|
||||
for (const [name, value] of state.percentage) {
|
||||
this.percentage.set(name, value);
|
||||
}
|
||||
|
||||
this.rebuildModifiers();
|
||||
}
|
||||
|
||||
loadState(
|
||||
state: IEquipmentStateSave<THero>,
|
||||
compression: SaveCompression
|
||||
): void {
|
||||
if (compression === SaveCompression.NoCompression) {
|
||||
this.loadNoCompression(state);
|
||||
} else {
|
||||
this.loadDiff(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class HeroEquipsStore<THero> implements IHeroEquipsStore<THero> {
|
||||
/** 装备实例存储表 */
|
||||
private readonly instanceMap: Map<number, EquipmentState<THero>> =
|
||||
new Map();
|
||||
|
||||
/** 自增 uid 计数器 */
|
||||
private nextUid: number = 0;
|
||||
|
||||
/** 排序器 */
|
||||
private sorter: IEquipmentSorter<THero> | null = null;
|
||||
|
||||
readonly state: IDataCommon;
|
||||
|
||||
constructor(state: IDataCommon) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
add(item: number | string): number {
|
||||
const num = this.state.tileStore.num(item);
|
||||
if (isNil(num)) return -1;
|
||||
|
||||
const raw = this.state.itemStore.getData(num!);
|
||||
if (!raw) return -1;
|
||||
|
||||
const uid = this.nextUid++;
|
||||
const state = new EquipmentState<THero>(uid, raw);
|
||||
this.instanceMap.set(uid, state);
|
||||
return uid;
|
||||
}
|
||||
|
||||
delete(uid: number): void {
|
||||
this.instanceMap.delete(uid);
|
||||
}
|
||||
|
||||
get(uid: number): IEquipmentState<THero> | null {
|
||||
return this.instanceMap.get(uid) ?? null;
|
||||
}
|
||||
|
||||
count(item: number | string): number {
|
||||
const num = this.state.tileStore.num(item);
|
||||
if (isNil(num)) return 0;
|
||||
|
||||
let count = 0;
|
||||
for (const state of this.instanceMap.values()) {
|
||||
if (state.item.num === num) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
useSorter(comparer: IEquipmentSorter<THero> | null): void {
|
||||
this.sorter = comparer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按当前排序器对所有装备实例排序,无排序器时按 uid 升序
|
||||
* @param list 装备实例数组
|
||||
* @returns 排序后的数组
|
||||
*/
|
||||
private sortList(list: EquipmentState<THero>[]): EquipmentState<THero>[] {
|
||||
const sorter = this.sorter;
|
||||
if (sorter) {
|
||||
return list.sort((a, b) => {
|
||||
const handler: IEquipmentSortHandler<THero> = {
|
||||
equipA: a,
|
||||
equipB: b,
|
||||
store: this,
|
||||
state: this.state
|
||||
};
|
||||
const cmp = sorter.compare(handler);
|
||||
if (cmp === 0) {
|
||||
return a.uid - b.uid;
|
||||
} else {
|
||||
return cmp;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return list.sort((a, b) => a.uid - b.uid);
|
||||
}
|
||||
}
|
||||
|
||||
instancesOf(equip: number | string): IEquipmentState<THero>[] {
|
||||
const num = this.state.tileStore.num(equip);
|
||||
if (isNil(num)) return [];
|
||||
return this.sortList(
|
||||
[...this.instanceMap.values()].filter(v => v.item.num === num)
|
||||
);
|
||||
}
|
||||
|
||||
instances(): IEquipmentState<THero>[] {
|
||||
return this.sortList([...this.instanceMap.values()]);
|
||||
}
|
||||
|
||||
saveState(compression: SaveCompression): IHeroEquipsStoreSave<THero> {
|
||||
const equipments: IEquipmentStateSave<THero>[] = [];
|
||||
for (const state of this.instanceMap.values()) {
|
||||
equipments.push(state.saveState(compression));
|
||||
}
|
||||
return { equipments };
|
||||
}
|
||||
|
||||
loadState(
|
||||
state: IHeroEquipsStoreSave<THero>,
|
||||
compression: SaveCompression
|
||||
): void {
|
||||
this.instanceMap.clear();
|
||||
for (const save of state.equipments) {
|
||||
const raw = this.state.itemStore.getData(save.num);
|
||||
if (!raw) {
|
||||
logger.error(59, save.num.toString());
|
||||
continue;
|
||||
}
|
||||
const instance = new EquipmentState<THero>(save.uid, raw, false);
|
||||
instance.loadState(save, compression);
|
||||
this.instanceMap.set(save.uid, instance);
|
||||
}
|
||||
const maxUid = maxBy(state.equipments, 'uid');
|
||||
if (!maxUid) {
|
||||
logger.error(58);
|
||||
return;
|
||||
}
|
||||
this.nextUid = maxUid.uid + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
import { IDataCommon } from '@user/data-common';
|
||||
import {
|
||||
EquipStatus,
|
||||
IEquipmentState,
|
||||
IHeroAttribute,
|
||||
IHeroEquipment,
|
||||
IHeroEquipmentSave,
|
||||
IHeroEquipsStore
|
||||
} from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
|
||||
export class HeroEquipment<THero> implements IHeroEquipment<THero> {
|
||||
/** 当前已装备的装备,键表示装备槽索引,值表示装备至这个装备槽的装备 uid */
|
||||
private readonly equips: Map<number, number> = new Map();
|
||||
|
||||
slots: string[] = [];
|
||||
|
||||
readonly state: IDataCommon;
|
||||
|
||||
constructor(
|
||||
private readonly store: IHeroEquipsStore<THero>,
|
||||
private readonly attribute: IHeroAttribute<THero>
|
||||
) {
|
||||
this.state = this.store.state;
|
||||
}
|
||||
|
||||
setSlots(slots: string[]): void {
|
||||
this.slots = slots;
|
||||
}
|
||||
|
||||
canEquipTo(uid: number, slot: number | string): EquipStatus {
|
||||
const state = this.store.get(uid);
|
||||
if (!state) return EquipStatus.CannotEquip;
|
||||
const raw = state.item;
|
||||
const slots = raw.equip.slots;
|
||||
|
||||
// 如果装备压根不支持这个槽位
|
||||
if (!slots.includes(slot)) {
|
||||
return EquipStatus.CannotEquip;
|
||||
}
|
||||
|
||||
if (typeof slot === 'number') {
|
||||
// 如果传入的 slot 是指定索引
|
||||
if (!this.equips.has(slot)) {
|
||||
return EquipStatus.CanEquip;
|
||||
} else {
|
||||
return EquipStatus.NeedReplace;
|
||||
}
|
||||
} else {
|
||||
// 如果传入的 slot 是槽位名称,那么要对每个槽位都判断
|
||||
let hasEmpty = false;
|
||||
let hasSlot = false;
|
||||
this.slots.forEach((name, index) => {
|
||||
if (slots.includes(name) && !this.equips.has(index)) {
|
||||
hasEmpty = true;
|
||||
}
|
||||
if (name === slot) {
|
||||
hasSlot = true;
|
||||
}
|
||||
});
|
||||
if (hasSlot) {
|
||||
if (hasEmpty) {
|
||||
return EquipStatus.CanEquip;
|
||||
} else {
|
||||
return EquipStatus.NeedReplace;
|
||||
}
|
||||
} else {
|
||||
return EquipStatus.CannotEquip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//#region 装备行为
|
||||
|
||||
/**
|
||||
* 向勇士属性施加装备的修饰器
|
||||
* @param state 装备状态实例
|
||||
*/
|
||||
private loadEquipEffect(state: IEquipmentState<THero>) {
|
||||
for (const [name, modifier] of state.getModifiers()) {
|
||||
// @ts-expect-error 泛型无法推导
|
||||
this.attribute.addModifier(name, modifier, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从勇士属性上删除装备的修饰器
|
||||
* @param state 装备状态实例
|
||||
*/
|
||||
private unloadEquipEffect(state: IEquipmentState<THero>) {
|
||||
for (const [name, modifier] of state.getModifiers()) {
|
||||
// @ts-expect-error 泛型无法推导
|
||||
this.attribute.deleteModifier(name, modifier);
|
||||
}
|
||||
}
|
||||
|
||||
equip(
|
||||
uid: number,
|
||||
slot: number | string,
|
||||
autoUnload: boolean = true
|
||||
): number | undefined {
|
||||
if (this.canEquipTo(uid, slot) === EquipStatus.CannotEquip) {
|
||||
return void 0;
|
||||
}
|
||||
|
||||
// 检查有没有同 uid 装备
|
||||
for (const [index, curr] of this.equips) {
|
||||
if (curr !== uid) continue;
|
||||
if (index === slot || this.slots[index] === slot) {
|
||||
// 指定装备已经装备至了指定装备槽,直接忽略
|
||||
return void 0;
|
||||
} else {
|
||||
// 否则看 autoUnload
|
||||
if (autoUnload) {
|
||||
this.unequip(index);
|
||||
break;
|
||||
} else {
|
||||
return void 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const state = this.store.get(uid);
|
||||
if (!state) {
|
||||
logger.warn(146, uid.toString());
|
||||
return void 0;
|
||||
}
|
||||
|
||||
if (typeof slot === 'number') {
|
||||
// 数字槽位,直接进行指定替换
|
||||
const curr = this.equips.get(slot);
|
||||
this.unequip(slot);
|
||||
this.equips.set(slot, uid);
|
||||
this.loadEquipEffect(state);
|
||||
return curr;
|
||||
} else {
|
||||
// 字符串槽位,需要判断是否包含空槽
|
||||
let first = -1;
|
||||
let empty = -1;
|
||||
this.slots.forEach((name, index) => {
|
||||
if (name !== slot) return;
|
||||
if (first === -1) first = index;
|
||||
if (empty !== -1 && !this.equips.has(index)) {
|
||||
empty = index;
|
||||
}
|
||||
});
|
||||
if (empty === -1) {
|
||||
// 无空槽,替换第一个匹配的槽位
|
||||
if (first === -1) {
|
||||
logger.warn(147, uid.toString());
|
||||
return void 0;
|
||||
} else {
|
||||
const curr = this.equips.get(first);
|
||||
this.unequip(first);
|
||||
this.equips.set(first, uid);
|
||||
this.loadEquipEffect(state);
|
||||
return curr;
|
||||
}
|
||||
} else {
|
||||
// 此时有空槽,直接装备上就行
|
||||
this.equips.set(empty, uid);
|
||||
this.loadEquipEffect(state);
|
||||
return void 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unequip(slot: number): number | undefined {
|
||||
const uid = this.equips.get(slot);
|
||||
if (isNil(uid)) return void 0;
|
||||
|
||||
const state = this.store.get(uid);
|
||||
if (!state) {
|
||||
logger.warn(146, uid.toString());
|
||||
return void 0;
|
||||
}
|
||||
|
||||
this.unloadEquipEffect(state);
|
||||
this.equips.delete(slot);
|
||||
|
||||
return uid;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 装备获取
|
||||
|
||||
equipped(uid: number): boolean {
|
||||
return [...this.equips.values()].includes(uid);
|
||||
}
|
||||
|
||||
getEquipped(slot: number): number | undefined {
|
||||
return this.equips.get(slot);
|
||||
}
|
||||
|
||||
getEquips(): (IEquipmentState<THero> | null)[] {
|
||||
const result: (IEquipmentState<THero> | null)[] = [];
|
||||
for (let i = 0; i < this.slots.length; i++) {
|
||||
const uid = this.equips.get(i);
|
||||
if (isNil(uid)) {
|
||||
result.push(null);
|
||||
} else {
|
||||
const state = this.store.get(uid);
|
||||
if (!state) {
|
||||
logger.warn(146, uid.toString());
|
||||
result.push(null);
|
||||
} else {
|
||||
result.push(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 装备对比
|
||||
|
||||
compareEquip(
|
||||
equipA: number,
|
||||
equipB: number,
|
||||
slot: number
|
||||
): Readonly<Partial<THero>> {
|
||||
const stateA = this.store.get(equipA);
|
||||
const stateB = this.store.get(equipB);
|
||||
if (!stateA) {
|
||||
logger.warn(146, equipA.toString());
|
||||
return {} as Partial<THero>;
|
||||
}
|
||||
if (!stateB) {
|
||||
logger.warn(146, equipB.toString());
|
||||
return {} as Partial<THero>;
|
||||
}
|
||||
|
||||
const clone = this.attribute.clone();
|
||||
|
||||
// 获取比对槽位的装备对应的所有修饰器,这些修饰器需要在克隆对象中删除
|
||||
const equipped = this.getEquipped(slot);
|
||||
if (!isNil(equipped)) {
|
||||
const state = this.store.get(equipped);
|
||||
if (!state) {
|
||||
logger.warn(146, equipped.toString());
|
||||
return {} as Partial<THero>;
|
||||
}
|
||||
for (const [name, modifier] of state.getModifiers()) {
|
||||
const index = this.attribute.getModifierIndex(modifier);
|
||||
clone.deleteModifierByIndex(name, index);
|
||||
}
|
||||
}
|
||||
|
||||
// 分别将两个装备的修饰器克隆并加入到克隆属性上对比
|
||||
const attrA: Partial<THero> = {};
|
||||
const attrB: Partial<THero> = {};
|
||||
const keys = new Set<SelectKey<THero, number>>();
|
||||
|
||||
for (const [name, modifier] of stateA.getModifiers()) {
|
||||
// @ts-expect-error 泛型无法推导
|
||||
clone.addModifier(name, modifier);
|
||||
}
|
||||
for (const [name] of stateA.getModifiers()) {
|
||||
attrA[name] = clone.getFinalAttribute(name);
|
||||
keys.add(name);
|
||||
}
|
||||
for (const [name, modifier] of stateA.getModifiers()) {
|
||||
// @ts-expect-error 泛型无法推导
|
||||
clone.deleteModifier(name, modifier);
|
||||
}
|
||||
|
||||
for (const [name, modifier] of stateB.getModifiers()) {
|
||||
// @ts-expect-error 泛型无法推导
|
||||
clone.addModifier(name, modifier);
|
||||
}
|
||||
for (const [name] of stateB.getModifiers()) {
|
||||
attrB[name] = clone.getFinalAttribute(name);
|
||||
keys.add(name);
|
||||
}
|
||||
|
||||
// 第二次没必要再删除了,因为这个 clone 对象不会再被使用到
|
||||
|
||||
// 然后收集键名并对比
|
||||
const diff: Partial<THero> = {};
|
||||
for (const key of keys) {
|
||||
const final = this.attribute.getFinalAttribute(key);
|
||||
const valueA = (attrA[key] as number) ?? final;
|
||||
const valueB = (attrB[key] as number) ?? final;
|
||||
diff[key] = (valueA - valueB) as THero[SelectKey<THero, number>];
|
||||
}
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
saveState(): IHeroEquipmentSave {
|
||||
return {
|
||||
equipped: this.equips,
|
||||
slots: this.slots
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IHeroEquipmentSave): void {
|
||||
this.slots.length = 0;
|
||||
state.slots.forEach(v => {
|
||||
this.slots.push(v);
|
||||
});
|
||||
// 由于装备修饰器不进存档,所以此时的勇士处于没有任何装备修饰器的状态,故可以安全清除
|
||||
this.equips.clear();
|
||||
for (const [index, uid] of state.equipped) {
|
||||
this.equip(uid, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
export * from './attribute';
|
||||
export * from './equipment';
|
||||
export * from './follower';
|
||||
export * from './items';
|
||||
export * from './location';
|
||||
export * from './modifier';
|
||||
export * from './rendering';
|
||||
export * from './mover';
|
||||
export * from './state';
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
import { IDataCommon, ItemCategory } from '@user/data-common';
|
||||
import { IDataCommon, ItemCategory, SaveCompression } from '@user/data-common';
|
||||
import { HeroEquipsStore } from './equipment';
|
||||
import {
|
||||
IHeroItems,
|
||||
IHeroItemSave,
|
||||
@@ -13,10 +14,12 @@ export class HeroItems<THero> implements IHeroItems<THero> {
|
||||
/** 消耗道具 */
|
||||
private readonly consumables: Map<number, IHeroItemState<THero>> =
|
||||
new Map();
|
||||
/** 装备道具 */
|
||||
private readonly equipments: Map<number, IHeroItemState<THero>> = new Map();
|
||||
|
||||
constructor(readonly state: IDataCommon) {}
|
||||
readonly equipment: HeroEquipsStore<THero>;
|
||||
|
||||
constructor(readonly state: IDataCommon) {
|
||||
this.equipment = new HeroEquipsStore<THero>(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 item 参数解析为 num,字符串 id 通过 tileStore 转换为 num
|
||||
@@ -37,8 +40,6 @@ export class HeroItems<THero> implements IHeroItems<THero> {
|
||||
return this.constants;
|
||||
case ItemCategory.Consumable:
|
||||
return this.consumables;
|
||||
case ItemCategory.Equipment:
|
||||
return this.equipments;
|
||||
default:
|
||||
return this.constants;
|
||||
}
|
||||
@@ -83,6 +84,13 @@ export class HeroItems<THero> implements IHeroItems<THero> {
|
||||
return;
|
||||
}
|
||||
|
||||
if (raw.category === ItemCategory.Equipment) {
|
||||
for (let i = 0; i < count; i++) {
|
||||
this.equipment.add(item);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const map = this.getMap(raw.category);
|
||||
const existing = map.get(num);
|
||||
if (existing) {
|
||||
@@ -160,20 +168,22 @@ export class HeroItems<THero> implements IHeroItems<THero> {
|
||||
}
|
||||
}
|
||||
|
||||
saveState(): IHeroItemsSave {
|
||||
saveState(compression: SaveCompression): IHeroItemsSave<THero> {
|
||||
return {
|
||||
constants: this.mapToSave(this.constants),
|
||||
consumables: this.mapToSave(this.consumables),
|
||||
equipments: this.mapToSave(this.equipments)
|
||||
equipStore: this.equipment.saveState(compression)
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IHeroItemsSave): void {
|
||||
loadState(
|
||||
state: IHeroItemsSave<THero>,
|
||||
compression: SaveCompression
|
||||
): void {
|
||||
this.constants.clear();
|
||||
this.consumables.clear();
|
||||
this.equipments.clear();
|
||||
this.loadMap(this.constants, state.constants);
|
||||
this.loadMap(this.consumables, state.consumables);
|
||||
this.loadMap(this.equipments, state.equipments);
|
||||
this.equipment.loadState(state.equipStore, compression);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { BaseHeroModifier } from './attribute';
|
||||
import { IHeroModifier } from './types';
|
||||
|
||||
export class ValueModifier extends BaseHeroModifier<number, number> {
|
||||
readonly type = '@system/value';
|
||||
|
||||
constructor(
|
||||
value: number,
|
||||
readonly priority: number = 0
|
||||
) {
|
||||
super(value);
|
||||
}
|
||||
|
||||
modify(value: number): number {
|
||||
return value + this.value;
|
||||
}
|
||||
|
||||
clone(): IHeroModifier<number, number> {
|
||||
return new ValueModifier(this.value, this.priority);
|
||||
}
|
||||
}
|
||||
|
||||
export class PercentageModifier extends BaseHeroModifier<number, number> {
|
||||
readonly type = '@system/percentage';
|
||||
|
||||
constructor(
|
||||
value: number,
|
||||
readonly priority: number = 10
|
||||
) {
|
||||
super(value);
|
||||
}
|
||||
|
||||
modify(value: number, baseValue: number): number {
|
||||
return value + baseValue * this.value;
|
||||
}
|
||||
|
||||
clone(): IHeroModifier<number, number> {
|
||||
return new PercentageModifier(this.value, this.priority);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { HeroAttribute } from './attribute';
|
||||
import { HeroEquipment } from './equipment';
|
||||
import { HeroFollowersController } from './follower';
|
||||
import { HeroItems } from './items';
|
||||
import { HeroLocation } from './location';
|
||||
@@ -6,6 +7,7 @@ import { HeroRendering } from './rendering';
|
||||
import {
|
||||
IHeroAttribute,
|
||||
IHeroFollowersController,
|
||||
IHeroEquipment,
|
||||
IHeroItems,
|
||||
IHeroLocation,
|
||||
IHeroModifier,
|
||||
@@ -34,6 +36,7 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
readonly rendering: IHeroRendering;
|
||||
readonly followers: IHeroFollowersController;
|
||||
readonly items: IHeroItems<THero>;
|
||||
readonly equip: IHeroEquipment<THero>;
|
||||
|
||||
constructor(
|
||||
state: IDataCommon,
|
||||
@@ -53,6 +56,7 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
faceHandler
|
||||
);
|
||||
this.items = new HeroItems(state);
|
||||
this.equip = new HeroEquipment(this.items.equipment, this.attribute);
|
||||
}
|
||||
|
||||
attachAttribute(attribute: IHeroAttribute<THero>): void {
|
||||
@@ -108,6 +112,7 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
saveState(compression: SaveCompression): IHeroStateSave<THero> {
|
||||
const modifiers: IModifierStateSave<THero>[] = [];
|
||||
for (const [name, modifier] of this.attribute.iterateModifiers()) {
|
||||
if (!this.attribute.getModifierSaveEnabled(modifier)) continue;
|
||||
modifiers.push({
|
||||
name: name as keyof THero,
|
||||
type: modifier.type,
|
||||
@@ -124,7 +129,8 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
rendering: this.rendering.saveState(compression),
|
||||
followers: followerSaves,
|
||||
modifiers,
|
||||
items: this.items.saveState(compression)
|
||||
items: this.items.saveState(compression),
|
||||
equip: this.equip.saveState(compression)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -144,6 +150,7 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
this.location.loadState(state.location, compression);
|
||||
this.rendering.loadState(state.rendering, compression);
|
||||
this.items.loadState(state.items, compression);
|
||||
this.equip.loadState(state.equip, compression);
|
||||
void this.followers.removeAllFollowers();
|
||||
for (const save of state.followers) {
|
||||
const follower = this.followers.addFollower(save.num);
|
||||
|
||||
@@ -70,6 +70,11 @@ export interface IModifierStateSave<THero> {
|
||||
readonly state: unknown;
|
||||
}
|
||||
|
||||
export interface IHeroAttributeCloneOption {
|
||||
/** 是否克隆属性修饰器,默认为 `true` */
|
||||
cloneModifier: boolean;
|
||||
}
|
||||
|
||||
export interface IReadonlyHeroAttribute<THero> {
|
||||
/**
|
||||
* 获取勇士的基础属性,即未经过任何 Buff 或装备等加成的属性
|
||||
@@ -97,12 +102,14 @@ export interface IReadonlyHeroAttribute<THero> {
|
||||
|
||||
/**
|
||||
* 深拷贝此勇士属性对象
|
||||
* @param cloneModifier 是否同时复制修饰器,默认复制
|
||||
* @param option 深拷贝选项
|
||||
*/
|
||||
clone(cloneModifier?: boolean): IReadonlyHeroAttribute<THero>;
|
||||
clone(
|
||||
option?: Readonly<Partial<IHeroAttributeCloneOption>>
|
||||
): IHeroAttribute<THero>;
|
||||
|
||||
/**
|
||||
* 获取此勇士属性对象的可修改副本
|
||||
* 获取此勇士属性对象的可修改副本,与 `clone()` 效果一致
|
||||
*/
|
||||
getModifiableClone(): IHeroAttribute<THero>;
|
||||
|
||||
@@ -123,6 +130,25 @@ export interface IReadonlyHeroAttribute<THero> {
|
||||
getModifiers<K extends keyof THero>(
|
||||
name: K
|
||||
): Iterable<IHeroModifier<THero[K]>>;
|
||||
|
||||
/**
|
||||
* 获取指定修饰器的索引位置
|
||||
* @param modifier 修饰器对象
|
||||
*/
|
||||
getModifierIndex(modifier: IHeroModifier): number;
|
||||
|
||||
/**
|
||||
* 设置指定修饰器的存档状态
|
||||
* @param modifier 属性修饰器
|
||||
* @param save 不进入存档
|
||||
*/
|
||||
setModifierSaveEnabled(modifier: IHeroModifier, save: boolean): void;
|
||||
|
||||
/**
|
||||
* 获取指定修饰器的存档状态
|
||||
* @param modifier 属性修饰器
|
||||
*/
|
||||
getModifierSaveEnabled(modifier: IHeroModifier): boolean;
|
||||
}
|
||||
|
||||
export interface IHeroAttribute<THero> extends IReadonlyHeroAttribute<THero> {
|
||||
@@ -138,30 +164,32 @@ export interface IHeroAttribute<THero> extends IReadonlyHeroAttribute<THero> {
|
||||
* @param name 属性名称
|
||||
* @param value 属性增减值
|
||||
*/
|
||||
add<K extends SelectKey<THero, number>>(name: K, value: number): void;
|
||||
add(name: SelectKey<THero, number>, value: number): void;
|
||||
|
||||
/**
|
||||
* 将勇士属性乘以某一个值
|
||||
* @param name 属性名称
|
||||
* @param value 属性乘数
|
||||
*/
|
||||
mul<K extends SelectKey<THero, number>>(name: K, value: number): void;
|
||||
mul(name: SelectKey<THero, number>, value: number): void;
|
||||
|
||||
/**
|
||||
* 将勇士属性除以某一个值
|
||||
* @param name 属性名称
|
||||
* @param value 属性除数
|
||||
*/
|
||||
div<K extends SelectKey<THero, number>>(name: K, value: number): void;
|
||||
div(name: SelectKey<THero, number>, value: number): void;
|
||||
|
||||
/**
|
||||
* 向一个属性添加属性修饰器
|
||||
* @param name 属性名称
|
||||
* @param modifier 属性修饰器
|
||||
* @param save 是否进入存档,默认 true
|
||||
*/
|
||||
addModifier<K extends keyof THero>(
|
||||
name: K,
|
||||
modifier: IHeroModifier<THero[K]>
|
||||
modifier: IHeroModifier<THero[K]>,
|
||||
save?: boolean
|
||||
): void;
|
||||
|
||||
/**
|
||||
@@ -175,10 +203,15 @@ export interface IHeroAttribute<THero> extends IReadonlyHeroAttribute<THero> {
|
||||
): void;
|
||||
|
||||
/**
|
||||
* 深拷贝此勇士属性对象
|
||||
* @param cloneModifier 是否同时复制修饰器,默认复制
|
||||
* 根据修饰器索引删除指定修饰器
|
||||
* @param name 属性名称
|
||||
* @param index 修饰器索引
|
||||
* @returns 被删除的修饰器
|
||||
*/
|
||||
clone(cloneModifier?: boolean): IHeroAttribute<THero>;
|
||||
deleteModifierByIndex<K extends keyof THero>(
|
||||
name: K,
|
||||
index: number
|
||||
): IHeroModifier<THero[K]> | null;
|
||||
|
||||
/**
|
||||
* 获取勇士指定属性计算过程的可迭代对象,一般用于调试。
|
||||
@@ -451,13 +484,13 @@ export interface IHeroItemSave {
|
||||
readonly count: number;
|
||||
}
|
||||
|
||||
export interface IHeroItemsSave {
|
||||
export interface IHeroItemsSave<THero> {
|
||||
/** 永久道具存档 */
|
||||
readonly constants: readonly IHeroItemSave[];
|
||||
/** 消耗道具存档 */
|
||||
readonly consumables: readonly IHeroItemSave[];
|
||||
/** 装备道具存档 */
|
||||
readonly equipments: readonly IHeroItemSave[];
|
||||
/** 装备实例仓库存档 */
|
||||
readonly equipStore: IHeroEquipsStoreSave<THero>;
|
||||
}
|
||||
|
||||
export interface IHeroItemState<THero> {
|
||||
@@ -472,7 +505,10 @@ export interface IHeroItemState<THero> {
|
||||
}
|
||||
|
||||
export interface IHeroItems<THero>
|
||||
extends ISaveableContent<IHeroItemsSave>, IDataCommonExtended {
|
||||
extends ISaveableContent<IHeroItemsSave<THero>>, IDataCommonExtended {
|
||||
/** 勇士装备实例存储 */
|
||||
readonly equipment: IHeroEquipsStore<THero>;
|
||||
|
||||
/**
|
||||
* 增加道具数量,count 可以填负数。当道具为 `Pick` 类型时会立刻执行其效果。
|
||||
* @param item 道具图块数字或字符串 id
|
||||
@@ -509,6 +545,187 @@ export interface IHeroItems<THero>
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 勇士装备
|
||||
|
||||
export interface IEquipmentStateSave<THero> {
|
||||
/** 装备实例 uid */
|
||||
readonly uid: number;
|
||||
/** 装备道具图块数字 */
|
||||
readonly num: number;
|
||||
/** 当前数值加成 */
|
||||
readonly value: Map<SelectKey<THero, number>, number>;
|
||||
/** 当前百分比加成 */
|
||||
readonly percentage: Map<SelectKey<THero, number>, number>;
|
||||
}
|
||||
|
||||
export interface IHeroEquipsStoreSave<THero> {
|
||||
/** 所有装备实例的存档 */
|
||||
readonly equipments: readonly IEquipmentStateSave<THero>[];
|
||||
}
|
||||
|
||||
export interface IEquipmentState<THero> extends ISaveableContent<
|
||||
IEquipmentStateSave<THero>
|
||||
> {
|
||||
/** 装备实例 uid */
|
||||
readonly uid: number;
|
||||
/** 装备的定义数据引用 */
|
||||
readonly item: IItemRawData<THero>;
|
||||
|
||||
/**
|
||||
* 获取装备产生的所有修饰器,每个元素为 [属性名, 修饰器]
|
||||
*/
|
||||
getModifiers(): Iterable<[SelectKey<THero, number>, IHeroModifier<number>]>;
|
||||
}
|
||||
|
||||
export interface IEquipmentSortHandler<THero> extends IDataCommonExtended {
|
||||
/** 要排序的装备 A */
|
||||
readonly equipA: IEquipmentState<THero>;
|
||||
/** 要排序的装备 B */
|
||||
readonly equipB: IEquipmentState<THero>;
|
||||
/** 装备存储实例 */
|
||||
readonly store: IHeroEquipsStore<THero>;
|
||||
}
|
||||
|
||||
export interface IEquipmentSorter<THero> {
|
||||
/**
|
||||
* 对比两个装备,返回哪个装备要排到更前面,即哪个装备更强。
|
||||
* 返回负值表示 `equipA` 排到前面,返回正值表示 `equipB` 排到前面,返回 0 表示按照装备实例的 `uid` 排序。
|
||||
* @param handler 装备排序信息对象
|
||||
*/
|
||||
compare(handler: IEquipmentSortHandler<THero>): number;
|
||||
}
|
||||
|
||||
export interface IHeroEquipsStore<THero>
|
||||
extends ISaveableContent<IHeroEquipsStoreSave<THero>>, IDataCommonExtended {
|
||||
/**
|
||||
* 获得一件装备道具,创建装备实例
|
||||
* @param item 道具图块数字或字符串 id
|
||||
* @returns 新分配的装备实例 uid
|
||||
*/
|
||||
add(item: number | string): number;
|
||||
|
||||
/**
|
||||
* 移除指定装备实例
|
||||
* @param uid 装备实例 uid
|
||||
*/
|
||||
delete(uid: number): void;
|
||||
|
||||
/**
|
||||
* 获取指定装备实例的状态,与当前是否装备无关
|
||||
* @param uid 装备实例 uid
|
||||
*/
|
||||
get(uid: number): IEquipmentState<THero> | null;
|
||||
|
||||
/**
|
||||
* 设置当前对象的装备排序器
|
||||
* @param comparer 装备排序器
|
||||
*/
|
||||
useSorter(comparer: IEquipmentSorter<THero> | null): void;
|
||||
|
||||
/**
|
||||
* 获取指定装备的所有装备实例,按照排序器的顺序进行排序,如没有排序器则按 `uid` 排序
|
||||
* @param equip 装备的图块数字或 id
|
||||
*/
|
||||
instancesOf(equip: number | string): IEquipmentState<THero>[];
|
||||
|
||||
/**
|
||||
* 获取当前的所有装备实例,按照排序器的顺序输出,如没有排序器则按 `uid` 排序
|
||||
*/
|
||||
instances(): IEquipmentState<THero>[];
|
||||
|
||||
/**
|
||||
* 获取指定类型装备的实例总数
|
||||
* @param item 道具图块数字或字符串 id
|
||||
*/
|
||||
count(item: number | string): number;
|
||||
}
|
||||
|
||||
export interface IHeroEquipmentSave {
|
||||
/** 当前已装备的 uid 映射 */
|
||||
readonly equipped: ReadonlyMap<number, number>;
|
||||
/** 当前装备槽数组 */
|
||||
readonly slots: readonly string[];
|
||||
}
|
||||
|
||||
export const enum EquipStatus {
|
||||
/** 可以直接装备到目标装备栏 */
|
||||
CanEquip,
|
||||
/** 不能装备到目标装备栏 */
|
||||
CannotEquip,
|
||||
/** 需要将目标装备栏上的装备进行替换 */
|
||||
NeedReplace
|
||||
}
|
||||
|
||||
export interface IHeroEquipment<THero>
|
||||
extends ISaveableContent<IHeroEquipmentSave>, IDataCommonExtended {
|
||||
/** 装备槽位数组,每一项字符串表示这一栏装备的名称,用于显示及装备槽匹配 */
|
||||
readonly slots: readonly string[];
|
||||
|
||||
/**
|
||||
* 设置装备槽
|
||||
* @param slots 装备槽位数组
|
||||
*/
|
||||
setSlots(slots: string[]): void;
|
||||
|
||||
/**
|
||||
* 判断指定装备是否可以装备到指定装备槽
|
||||
* @param uid 装备实例的 uid
|
||||
* @param slot 要装备至的装备槽,可以是装备槽的索引,也可以是装备槽的名称
|
||||
*/
|
||||
canEquipTo(uid: number, slot: number | string): EquipStatus;
|
||||
|
||||
/**
|
||||
* 将指定装备穿上
|
||||
* @param uid 装备实例 uid
|
||||
* @param slot 要装备至的装备槽,可以是装备槽的索引,也可以是装备槽的名称
|
||||
* @param autoUnload 当要装备的装备已经处于某个装备槽,是否自动将其卸下,默认为 `true`
|
||||
* @returns 被卸下的装备实例 uid,如果没有装备被卸下,那么为 `undefined`
|
||||
*/
|
||||
equip(
|
||||
uid: number,
|
||||
slot: number | string,
|
||||
autoUnload?: boolean
|
||||
): number | undefined;
|
||||
|
||||
/**
|
||||
* 将指定装备卸下
|
||||
* @param slot 要卸下的装备槽索引
|
||||
* @returns 被卸下的装备实例 uid,如果没有装备被卸下,那么为 `undefined`
|
||||
*/
|
||||
unequip(slot: number): number | undefined;
|
||||
|
||||
/**
|
||||
* 查询指定装备是否处于装备状态
|
||||
* @param uid 装备实例 uid
|
||||
*/
|
||||
equipped(uid: number): boolean;
|
||||
|
||||
/**
|
||||
* 获取指定装备槽索引位置的装备 uid
|
||||
* @param slot 要获取的装备槽索引
|
||||
*/
|
||||
getEquipped(slot: number): number | undefined;
|
||||
|
||||
/**
|
||||
* 获取所有已装备的实例,按照装备槽排序
|
||||
*/
|
||||
getEquips(): (IEquipmentState<THero> | null)[];
|
||||
|
||||
/**
|
||||
* 比较两个装备在指定槽位的表现,输出装备 A 时的属性减装备 B 时的属性
|
||||
* @param equipA 要比较的装备 A 的 uid
|
||||
* @param equipB 要比较的装备 B 的 uid
|
||||
* @param slot 要比较的装备槽索引
|
||||
*/
|
||||
compareEquip(
|
||||
equipA: number,
|
||||
equipB: number,
|
||||
slot: number
|
||||
): Readonly<Partial<THero>>;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 勇士状态
|
||||
|
||||
export interface IHeroStateSave<THero> {
|
||||
@@ -523,7 +740,9 @@ export interface IHeroStateSave<THero> {
|
||||
/** 勇士属性修饰器状态 */
|
||||
readonly modifiers: readonly IModifierStateSave<THero>[];
|
||||
/** 勇士道具背包状态 */
|
||||
readonly items: IHeroItemsSave;
|
||||
readonly items: IHeroItemsSave<THero>;
|
||||
/** 勇士装备状态 */
|
||||
readonly equip: IHeroEquipmentSave;
|
||||
}
|
||||
|
||||
export interface IHeroState<THero> extends ISaveableContent<
|
||||
@@ -539,6 +758,8 @@ export interface IHeroState<THero> extends ISaveableContent<
|
||||
readonly rendering: IHeroRendering;
|
||||
/** 勇士道具背包 */
|
||||
readonly items: IHeroItems<THero>;
|
||||
/** 勇士装备系统 */
|
||||
readonly equip: IHeroEquipment<THero>;
|
||||
|
||||
/**
|
||||
* 获取勇士当前的位置
|
||||
|
||||
Reference in new issue
Block a user