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
c97ed868e7
commit
3ac14f8757
9 files changed
+440
-52
No files matched your search
@@ -1,5 +1,6 @@
|
||||
export * from './attribute';
|
||||
export * from './follower';
|
||||
export * from './items';
|
||||
export * from './location';
|
||||
export * from './rendering';
|
||||
export * from './mover';
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
import { IDataCommon, ItemCategory } from '@user/data-common';
|
||||
import {
|
||||
IHeroItems,
|
||||
IHeroItemSave,
|
||||
IHeroItemsSave,
|
||||
IHeroItemState
|
||||
} from './types';
|
||||
|
||||
export class HeroItems<THero> implements IHeroItems<THero> {
|
||||
/** 永久道具 */
|
||||
private readonly constants: Map<number, IHeroItemState<THero>> = new Map();
|
||||
/** 消耗道具 */
|
||||
private readonly consumables: Map<number, IHeroItemState<THero>> =
|
||||
new Map();
|
||||
/** 装备道具 */
|
||||
private readonly equipments: Map<number, IHeroItemState<THero>> = new Map();
|
||||
|
||||
constructor(readonly state: IDataCommon) {}
|
||||
|
||||
/**
|
||||
* 将 item 参数解析为 num,字符串 id 通过 tileStore 转换为 num
|
||||
* @param item 道具图块数字或 id
|
||||
*/
|
||||
private resolveNum(item: number | string): number | null {
|
||||
if (typeof item === 'number') return item;
|
||||
return this.state.tileStore.idToNumber(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据道具分类获取对应的分表
|
||||
* @param category 道具分类枚举
|
||||
*/
|
||||
private getMap(category: ItemCategory): Map<number, IHeroItemState<THero>> {
|
||||
switch (category) {
|
||||
case ItemCategory.Constant:
|
||||
return this.constants;
|
||||
case ItemCategory.Consumable:
|
||||
return this.consumables;
|
||||
case ItemCategory.Equipment:
|
||||
return this.equipments;
|
||||
default:
|
||||
return this.constants;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定道具的内部状态,返回可修改引用供内部逻辑使用
|
||||
* @param item 道具图块数字或 id
|
||||
*/
|
||||
private internalGetItemState(
|
||||
item: number | string
|
||||
): IHeroItemState<THero> | null {
|
||||
const num = this.resolveNum(item);
|
||||
if (isNil(num)) return null;
|
||||
|
||||
const category = this.state.itemStore.getCategory(num);
|
||||
const map = this.getMap(category);
|
||||
return map.get(num) ?? null;
|
||||
}
|
||||
|
||||
getItemState(
|
||||
item: number | string
|
||||
): Readonly<IHeroItemState<THero>> | null {
|
||||
return this.internalGetItemState(item);
|
||||
}
|
||||
|
||||
itemCount(item: number | string): number {
|
||||
return this.getItemState(item)?.count ?? 0;
|
||||
}
|
||||
|
||||
addItem(item: number | string, count: number = 1): void {
|
||||
const num = this.resolveNum(item);
|
||||
if (isNil(num)) return;
|
||||
|
||||
const raw = this.state.itemStore.getData(num);
|
||||
if (!raw) return;
|
||||
|
||||
if (raw.category === ItemCategory.Pick) {
|
||||
for (let i = 0; i < count; i++) {
|
||||
raw.effect.useEffect(raw);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const map = this.getMap(raw.category);
|
||||
const existing = map.get(num);
|
||||
if (existing) {
|
||||
existing.count += count;
|
||||
if (existing.count <= 0) {
|
||||
map.delete(num);
|
||||
}
|
||||
} else if (count > 0) {
|
||||
map.set(num, { id: raw.id, num: raw.num, raw, count });
|
||||
}
|
||||
}
|
||||
|
||||
getItem(item: number | string): void {
|
||||
this.addItem(item, 1);
|
||||
}
|
||||
|
||||
useItem(item: number | string): boolean {
|
||||
const state = this.internalGetItemState(item);
|
||||
if (!state) return false;
|
||||
|
||||
const { raw } = state;
|
||||
if (
|
||||
raw.category !== ItemCategory.Constant &&
|
||||
raw.category !== ItemCategory.Consumable
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!raw.effect.canUse(raw)) return false;
|
||||
|
||||
raw.effect.useEffect(raw);
|
||||
|
||||
if (raw.category === ItemCategory.Consumable) {
|
||||
state.count--;
|
||||
if (state.count <= 0) {
|
||||
this.consumables.delete(raw.num);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将分表转换为存档数组
|
||||
* @param map 道具分表
|
||||
*/
|
||||
private mapToSave(
|
||||
map: Map<number, IHeroItemState<THero>>
|
||||
): readonly IHeroItemSave[] {
|
||||
const result: IHeroItemSave[] = [];
|
||||
for (const state of map.values()) {
|
||||
result.push({ num: state.num, count: state.count });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从存档数组恢复分表
|
||||
* @param map 道具分表
|
||||
* @param saves 存档数组
|
||||
*/
|
||||
private loadMap(
|
||||
map: Map<number, IHeroItemState<THero>>,
|
||||
saves: readonly IHeroItemSave[]
|
||||
): void {
|
||||
for (const save of saves) {
|
||||
const raw = this.state.itemStore.getData(save.num);
|
||||
if (!raw) continue;
|
||||
map.set(save.num, {
|
||||
id: raw.id,
|
||||
num: raw.num,
|
||||
raw,
|
||||
count: save.count
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
saveState(): IHeroItemsSave {
|
||||
return {
|
||||
constants: this.mapToSave(this.constants),
|
||||
consumables: this.mapToSave(this.consumables),
|
||||
equipments: this.mapToSave(this.equipments)
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IHeroItemsSave): 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);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
import { HeroAttribute } from './attribute';
|
||||
import { HeroFollowersController } from './follower';
|
||||
import { HeroItems } from './items';
|
||||
import { HeroLocation } from './location';
|
||||
import { HeroRendering } from './rendering';
|
||||
import {
|
||||
IHeroAttribute,
|
||||
IHeroFollowersController,
|
||||
IHeroItems,
|
||||
IHeroLocation,
|
||||
IHeroModifier,
|
||||
IHeroRendering,
|
||||
@@ -31,6 +33,7 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
readonly location: IHeroLocation;
|
||||
readonly rendering: IHeroRendering;
|
||||
readonly followers: IHeroFollowersController;
|
||||
readonly items: IHeroItems<THero>;
|
||||
|
||||
constructor(
|
||||
state: IDataCommon,
|
||||
@@ -49,6 +52,7 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
this.location,
|
||||
faceHandler
|
||||
);
|
||||
this.items = new HeroItems(state);
|
||||
}
|
||||
|
||||
attachAttribute(attribute: IHeroAttribute<THero>): void {
|
||||
@@ -119,7 +123,8 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
location: this.location.saveState(compression),
|
||||
rendering: this.rendering.saveState(compression),
|
||||
followers: followerSaves,
|
||||
modifiers
|
||||
modifiers,
|
||||
items: this.items.saveState(compression)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -138,6 +143,7 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
this.attribute = newAttribute;
|
||||
this.location.loadState(state.location, compression);
|
||||
this.rendering.loadState(state.rendering, compression);
|
||||
this.items.loadState(state.items, compression);
|
||||
void this.followers.removeAllFollowers();
|
||||
for (const save of state.followers) {
|
||||
const follower = this.followers.addFollower(save.num);
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
FaceDirection,
|
||||
IDataCommonExtended,
|
||||
IFaceHandler,
|
||||
IItemRawData,
|
||||
IObjectMovable,
|
||||
IObjectMover,
|
||||
ISaveableContent
|
||||
@@ -441,6 +442,73 @@ export interface IHeroFollowersController
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 勇士道具
|
||||
|
||||
export interface IHeroItemSave {
|
||||
/** 道具图块数字 */
|
||||
readonly num: number;
|
||||
/** 道具持有数量 */
|
||||
readonly count: number;
|
||||
}
|
||||
|
||||
export interface IHeroItemsSave {
|
||||
/** 永久道具存档 */
|
||||
readonly constants: readonly IHeroItemSave[];
|
||||
/** 消耗道具存档 */
|
||||
readonly consumables: readonly IHeroItemSave[];
|
||||
/** 装备道具存档 */
|
||||
readonly equipments: readonly IHeroItemSave[];
|
||||
}
|
||||
|
||||
export interface IHeroItemState<THero> {
|
||||
/** 道具字符串 id */
|
||||
readonly id: string;
|
||||
/** 道具图块数字 */
|
||||
readonly num: number;
|
||||
/** 道具原始定义数据引用 */
|
||||
readonly raw: IItemRawData<THero>;
|
||||
/** 道具持有数量 */
|
||||
count: number;
|
||||
}
|
||||
|
||||
export interface IHeroItems<THero>
|
||||
extends ISaveableContent<IHeroItemsSave>, IDataCommonExtended {
|
||||
/**
|
||||
* 增加道具数量,count 可以填负数。当道具为 `Pick` 类型时会立刻执行其效果。
|
||||
* @param item 道具图块数字或字符串 id
|
||||
* @param count 要增加的数量,默认为 1
|
||||
*/
|
||||
addItem(item: number | string, count?: number): void;
|
||||
|
||||
/**
|
||||
* 勇士获取指定道具,当道具为 `Pick` 类型时会立刻执行其效果,否则使背包中的数量加一。
|
||||
* 是 `addItem(item, 1)` 的另一种写法。
|
||||
* @param item 带锯图块数字或 id
|
||||
*/
|
||||
getItem(item: number | string): void;
|
||||
|
||||
/**
|
||||
* 获取指定道具的状态
|
||||
* @param item 道具图块数字或字符串 id
|
||||
*/
|
||||
getItemState(item: number | string): Readonly<IHeroItemState<THero>> | null;
|
||||
|
||||
/**
|
||||
* 使用道具,仅对 Constant 与 Consumable 类型生效。Consumable 类型使用后数量减一。
|
||||
* @param item 道具图块数字或字符串 id
|
||||
* @returns 道具是否使用成功
|
||||
*/
|
||||
useItem(item: number | string): boolean;
|
||||
|
||||
/**
|
||||
* 获取指定道具的持有数量
|
||||
* @param item 道具图块数字或字符串 id
|
||||
*/
|
||||
itemCount(item: number | string): number;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 勇士状态
|
||||
|
||||
export interface IHeroStateSave<THero> {
|
||||
@@ -454,6 +522,8 @@ export interface IHeroStateSave<THero> {
|
||||
readonly followers: readonly IHeroFollowerSave[];
|
||||
/** 勇士属性修饰器状态 */
|
||||
readonly modifiers: readonly IModifierStateSave<THero>[];
|
||||
/** 勇士道具背包状态 */
|
||||
readonly items: IHeroItemsSave;
|
||||
}
|
||||
|
||||
export interface IHeroState<THero> extends ISaveableContent<
|
||||
@@ -467,6 +537,8 @@ export interface IHeroState<THero> extends ISaveableContent<
|
||||
readonly followers: IHeroFollowersController;
|
||||
/** 勇士的渲染对象,包含一些必要渲染信息,存在于数据端,并非渲染端 */
|
||||
readonly rendering: IHeroRendering;
|
||||
/** 勇士道具背包 */
|
||||
readonly items: IHeroItems<THero>;
|
||||
|
||||
/**
|
||||
* 获取勇士当前的位置
|
||||
|
||||
Reference in new issue
Block a user