mirror of
https://github.com/motajs/template.git
synced 2026-11-19 10:44:30 +08:00
refactor: 插件系统 & 数据端顶层移植
This commit is contained in:
1 parent
b0229c627f
commit
2f3d3387a0
74 files changed
+1322
-3321
No files matched your search
@@ -6,6 +6,7 @@ import {
|
||||
} from '@motajs/common';
|
||||
import { SaveCompression, shouldReplay } from '@user/data-common';
|
||||
import {
|
||||
HeroModifierCreator,
|
||||
IHeroAttribute,
|
||||
IHeroAttributeCloneOption,
|
||||
IHeroAttributeHooks,
|
||||
@@ -20,7 +21,9 @@ export class HeroAttribute<THero>
|
||||
implements IHeroAttribute<THero>
|
||||
{
|
||||
/** 当前的勇士基础属性 */
|
||||
private attribute: THero;
|
||||
private attribute!: THero;
|
||||
/** 当前勇士最终属性 */
|
||||
private finalAttribute!: THero;
|
||||
|
||||
/** 当前勇士属性修饰器 */
|
||||
private readonly modifier: Map<keyof THero, IHeroModifier[]> = new Map();
|
||||
@@ -29,16 +32,11 @@ export class HeroAttribute<THero>
|
||||
/** 当前标记为不存档的修饰器集合 */
|
||||
private readonly modifierNosave: Set<IHeroModifier> = new Set();
|
||||
/** 修饰器工厂函数注册表 */
|
||||
private readonly registry: Map<
|
||||
string,
|
||||
<K extends keyof THero>() => IHeroModifier<THero[K]>
|
||||
> = new Map();
|
||||
/** 当前勇士最终属性 */
|
||||
private readonly finalAttribute: THero;
|
||||
private readonly registry: Map<string, HeroModifierCreator<THero>> =
|
||||
new Map();
|
||||
|
||||
constructor(attribute: THero) {
|
||||
super();
|
||||
// 克隆入参,避免调用方传入的共享基础属性对象被本实例改写
|
||||
this.attribute = structuredClone(attribute);
|
||||
this.finalAttribute = structuredClone(attribute);
|
||||
}
|
||||
|
||||
@@ -22,30 +22,22 @@ import {
|
||||
import {
|
||||
Hookable,
|
||||
HookController,
|
||||
IHookController,
|
||||
IFaceHandler
|
||||
IFaceHandler,
|
||||
IHookController
|
||||
} from '@motajs/common';
|
||||
import { HeroAttribute } from './attribute';
|
||||
|
||||
export class HeroState<THero>
|
||||
extends Hookable<IHeroStateHooks>
|
||||
implements IHeroState<THero>
|
||||
{
|
||||
readonly location: IHeroLocation;
|
||||
location!: IHeroLocation;
|
||||
attribute!: IHeroAttribute<THero>;
|
||||
readonly items: IHeroItems<THero>;
|
||||
readonly equip: IHeroEquipment<THero>;
|
||||
|
||||
constructor(
|
||||
state: IDataCommon,
|
||||
faceHandler: IFaceHandler<FaceDirection>,
|
||||
public readonly attribute: IHeroAttribute<THero>
|
||||
) {
|
||||
constructor(readonly state: IDataCommon) {
|
||||
super();
|
||||
const defaultLoc: IFacedTileLocator = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
direction: FaceDirection.Down
|
||||
};
|
||||
this.location = new HeroLocation(state, defaultLoc, faceHandler);
|
||||
this.items = new HeroItems(state);
|
||||
this.equip = new HeroEquipment(this.items.equipment, this.attribute);
|
||||
}
|
||||
@@ -56,6 +48,19 @@ export class HeroState<THero>
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
createLocation(
|
||||
location: IFacedTileLocator,
|
||||
faceHandler: IFaceHandler<FaceDirection>
|
||||
): void {
|
||||
if (this.location) return;
|
||||
this.location = new HeroLocation(this.state, location, faceHandler);
|
||||
}
|
||||
|
||||
createAttribute(attribute: THero): void {
|
||||
if (this.attribute) return;
|
||||
this.attribute = new HeroAttribute(attribute);
|
||||
}
|
||||
|
||||
getLocation(): IFacedTileLocator {
|
||||
return {
|
||||
x: this.location.x,
|
||||
|
||||
@@ -179,6 +179,10 @@ export interface IReadonlyHeroAttribute<THero> extends IHookable<
|
||||
getModifierSaveEnabled(modifier: IHeroModifier): boolean;
|
||||
}
|
||||
|
||||
export type HeroModifierCreator<THero> = <
|
||||
K extends keyof THero
|
||||
>() => IHeroModifier<THero[K]>;
|
||||
|
||||
export interface IHeroAttribute<THero>
|
||||
extends
|
||||
IReadonlyHeroAttribute<THero>,
|
||||
@@ -259,10 +263,7 @@ export interface IHeroAttribute<THero>
|
||||
* @param type 修饰器类型
|
||||
* @param cons 工厂函数
|
||||
*/
|
||||
registerModifier(
|
||||
type: string,
|
||||
cons: <K extends keyof THero>() => IHeroModifier<THero[K]>
|
||||
): void;
|
||||
registerModifier(type: string, cons: HeroModifierCreator<THero>): void;
|
||||
|
||||
/**
|
||||
* 创建指定类型的修饰器实例
|
||||
@@ -926,8 +927,9 @@ export interface IHeroStateSave<THero> {
|
||||
export interface IHeroState<THero>
|
||||
extends
|
||||
ISaveableContent<IHeroStateSave<THero>>,
|
||||
IHookable<IHeroStateHooks> {
|
||||
/** 勇士移动对象 */
|
||||
IHookable<IHeroStateHooks>,
|
||||
IDataCommonExtended {
|
||||
/** 勇士移动位置对象 */
|
||||
readonly location: IHeroLocation;
|
||||
/** 勇士属性对象 */
|
||||
readonly attribute: IReadonlyHeroAttribute<THero>;
|
||||
@@ -936,6 +938,24 @@ export interface IHeroState<THero>
|
||||
/** 勇士装备系统 */
|
||||
readonly equip: IHeroEquipment<THero>;
|
||||
|
||||
/**
|
||||
* 创建勇士移动位置对象,在创建前不可使用 `this.location`,但为避免频繁判空,类型表示为必选。
|
||||
* 仅能调用一次,多次调用无效。
|
||||
* @param location 勇士的初始位置
|
||||
* @param faceHandler 朝向处理器
|
||||
*/
|
||||
createLocation(
|
||||
location: IFacedTileLocator,
|
||||
faceHandler: IFaceHandler<FaceDirection>
|
||||
): void;
|
||||
|
||||
/**
|
||||
* 绑定勇士属性对象,在绑定前不可使用 `this.attribute`,但为避免频繁判空,类型表示为必选。
|
||||
* 仅能调用一次,多次调用无效
|
||||
* @param attribute 勇士属性对象
|
||||
*/
|
||||
createAttribute(attribute: THero): void;
|
||||
|
||||
/**
|
||||
* 获取勇士当前的位置
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,5 @@ export * from './enemy';
|
||||
export * from './flag';
|
||||
export * from './hero';
|
||||
export * from './map';
|
||||
export * from './shop';
|
||||
|
||||
export * from './types';
|
||||
@@ -1 +0,0 @@
|
||||
export * from './types';
|
||||
@@ -1,102 +0,0 @@
|
||||
import { IDataCommon, SaveCompression } from '@user/data-common';
|
||||
import { IShopManager, IShopManagerSave, IShopState } from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
import { ShopState } from './state';
|
||||
import { isNil } from 'lodash-es';
|
||||
|
||||
export class ShopManager implements IShopManager {
|
||||
/** 当前管理器包含的商店列表 */
|
||||
private readonly shops: Map<string, IShopState> = new Map();
|
||||
|
||||
constructor(readonly state: IDataCommon) {}
|
||||
|
||||
/**
|
||||
* 传入商店 id 或实例,输出商店实例
|
||||
* @param shop 商店的 id 或其实例
|
||||
*/
|
||||
private getShopState(shop: string | IShopState): IShopState | null {
|
||||
if (typeof shop === 'string') {
|
||||
return this.shops.get(shop) ?? null;
|
||||
} else {
|
||||
return shop;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入商店 id 或实例,输出商店 id
|
||||
* @param shop 商店的 id 或其实例
|
||||
*/
|
||||
private getShopId(shop: string | IShopState): string | undefined {
|
||||
if (typeof shop === 'string') {
|
||||
return shop;
|
||||
} else {
|
||||
return shop.id;
|
||||
}
|
||||
}
|
||||
|
||||
addShop(shop: IShopState): void {
|
||||
if (this.shops.has(shop.id)) {
|
||||
logger.warn(198, shop.id);
|
||||
return;
|
||||
}
|
||||
this.shops.set(shop.id, shop);
|
||||
}
|
||||
|
||||
removeShop(shop: string | IShopState): void {
|
||||
const id = this.getShopId(shop);
|
||||
if (!isNil(id)) this.shops.delete(id);
|
||||
}
|
||||
|
||||
getShop(id: string): IShopState | null {
|
||||
return this.shops.get(id) ?? null;
|
||||
}
|
||||
|
||||
hasShop(shop: string | IShopState): boolean {
|
||||
const id = this.getShopId(shop);
|
||||
return isNil(id) || this.shops.has(id);
|
||||
}
|
||||
|
||||
iterateShops(): Iterable<[string, IShopState]> {
|
||||
return this.shops;
|
||||
}
|
||||
|
||||
*iterateEnabledShops(): Iterable<[string, IShopState]> {
|
||||
for (const [key, state] of this.shops) {
|
||||
if (state.enabled) yield [key, state];
|
||||
}
|
||||
}
|
||||
|
||||
*iterateDisabledShops(): Iterable<[string, IShopState]> {
|
||||
for (const [key, state] of this.shops) {
|
||||
if (!state.enabled) yield [key, state];
|
||||
}
|
||||
}
|
||||
|
||||
enableShop(shop: string | IShopState): void {
|
||||
const state = this.getShopState(shop);
|
||||
if (state) state.enable();
|
||||
}
|
||||
|
||||
disableShop(shop: string | IShopState): void {
|
||||
const state = this.getShopState(shop);
|
||||
if (state) state.disable();
|
||||
}
|
||||
|
||||
saveState(compression: SaveCompression): IShopManagerSave {
|
||||
const list = [...this.shops.values()].map(v =>
|
||||
v.saveState(compression)
|
||||
);
|
||||
return {
|
||||
list
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IShopManagerSave, compression: SaveCompression): void {
|
||||
this.shops.clear();
|
||||
for (const save of state.list) {
|
||||
const shop: IShopState = new ShopState(save.id, this.state);
|
||||
shop.loadState(save, compression);
|
||||
this.shops.set(save.id, shop);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
import { IDataCommon } from '@user/data-common';
|
||||
import { IShopState, IShopStateSave } from './types';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { logger } from '@motajs/common';
|
||||
|
||||
export class ShopState implements IShopState {
|
||||
enabled: boolean = false;
|
||||
|
||||
/** 此商店拥有的所有道具及其库存 */
|
||||
private readonly items: Map<number, number> = new Map();
|
||||
/** 此商店中,每个道具的买入价格 */
|
||||
private readonly buyPrice: Map<number, number> = new Map();
|
||||
/** 此商店中,每个道具的售出价格 */
|
||||
private readonly sellPrice: Map<number, number> = new Map();
|
||||
|
||||
constructor(
|
||||
readonly id: string,
|
||||
readonly state: IDataCommon
|
||||
) {}
|
||||
|
||||
addItem(token: number | string, count: number): void {
|
||||
const num = this.state.tileStore.num(token);
|
||||
if (isNil(num)) {
|
||||
logger.warn(196, token.toString());
|
||||
return;
|
||||
}
|
||||
// 注意,这里不把库存为 0 的置空是因为置空之后迭代不到,商店将不再显示这个道具
|
||||
// 但是商店理应显示库存为 0 的道具,一是因为玩家可能需要卖出,二是因为玩家可能需要 0 库存这一信息
|
||||
const before = this.items.getOrInsert(num, 0);
|
||||
this.items.set(num, before + count);
|
||||
}
|
||||
|
||||
getCount(token: number | string): number {
|
||||
const num = this.state.tileStore.num(token);
|
||||
if (isNil(num)) {
|
||||
logger.warn(196, token.toString());
|
||||
return -1;
|
||||
}
|
||||
return this.items.get(num) ?? -1;
|
||||
}
|
||||
|
||||
iterateItems(): Iterable<[number, number]> {
|
||||
return this.items;
|
||||
}
|
||||
|
||||
setPrice(item: number | string, buy: number, sell: number): void {
|
||||
const num = this.state.tileStore.num(item);
|
||||
if (isNil(num)) {
|
||||
logger.warn(196, item.toString());
|
||||
return;
|
||||
}
|
||||
this.buyPrice.set(num, buy);
|
||||
this.sellPrice.set(num, sell);
|
||||
}
|
||||
|
||||
getPrice(item: number | string): [buy: number, sell: number] {
|
||||
const num = this.state.tileStore.num(item);
|
||||
if (isNil(num)) {
|
||||
logger.warn(196, item.toString());
|
||||
return [-1, -1];
|
||||
}
|
||||
const buy = this.buyPrice.get(num);
|
||||
const sell = this.sellPrice.get(num);
|
||||
if (isNil(buy) || isNil(sell)) return [-1, -1];
|
||||
else return [buy, sell];
|
||||
}
|
||||
|
||||
enable(): void {
|
||||
this.enabled = true;
|
||||
}
|
||||
|
||||
disable(): void {
|
||||
this.enabled = false;
|
||||
}
|
||||
|
||||
saveState(): IShopStateSave {
|
||||
return {
|
||||
id: this.id,
|
||||
enabled: this.enabled,
|
||||
items: this.items,
|
||||
buyPrice: this.buyPrice,
|
||||
sellPrice: this.sellPrice
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IShopStateSave): void {
|
||||
if (state.id !== this.id) {
|
||||
logger.warn(197, this.id, state.id);
|
||||
return;
|
||||
}
|
||||
this.enabled = state.enabled;
|
||||
this.items.clear();
|
||||
for (const [key, value] of state.items) {
|
||||
this.items.set(key, value);
|
||||
}
|
||||
this.buyPrice.clear();
|
||||
for (const [key, value] of state.buyPrice) {
|
||||
this.buyPrice.set(key, value);
|
||||
}
|
||||
this.sellPrice.clear();
|
||||
for (const [key, value] of state.sellPrice) {
|
||||
this.sellPrice.set(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
import { IDataCommonExtended, ISaveableContent } from '@user/data-common';
|
||||
|
||||
export interface IShopStateSave {
|
||||
/** 商店 id */
|
||||
readonly id: string;
|
||||
/** 当前商店是否已启用 */
|
||||
readonly enabled: boolean;
|
||||
/** 商店拥有的库存 */
|
||||
readonly items: ReadonlyMap<number, number>;
|
||||
/** 商店道具的买入价 */
|
||||
readonly buyPrice: ReadonlyMap<number, number>;
|
||||
/** 商店道具的售出价 */
|
||||
readonly sellPrice: ReadonlyMap<number, number>;
|
||||
}
|
||||
|
||||
export interface IShopState
|
||||
extends IDataCommonExtended, ISaveableContent<IShopStateSave> {
|
||||
/** 当前商店是否已经启用 */
|
||||
readonly enabled: boolean;
|
||||
/** 当前商店的标识符 */
|
||||
readonly id: string;
|
||||
|
||||
/**
|
||||
* 向此商店添加或减少道具库存,会与原本的库存叠加
|
||||
* @param token 道具数字或 id
|
||||
* @param count 添加或减少的道具数量,正数表示添加,负数表示减少
|
||||
*/
|
||||
addItem(token: number | string, count: number): void;
|
||||
|
||||
/**
|
||||
* 获取指定道具的库存数量,不存在道具则返回 -1
|
||||
* @param token 道具数字或 id
|
||||
*/
|
||||
getCount(token: number | string): number;
|
||||
|
||||
/**
|
||||
* 迭代商店拥有的所有道具,键表示道具的图块数字,值表示数量
|
||||
*/
|
||||
iterateItems(): Iterable<[number, number]>;
|
||||
|
||||
/**
|
||||
* 设置道具价格
|
||||
* @param item 道具数字或 id
|
||||
* @param buy 道具买入价格
|
||||
* @param sell 道具卖出价格
|
||||
*/
|
||||
setPrice(item: number | string, buy: number, sell: number): void;
|
||||
|
||||
/**
|
||||
* 获取道具价格,如果道具不存在则返回 -1
|
||||
* @param item 道具数字或 id
|
||||
*/
|
||||
getPrice(item: number | string): [buy: number, sell: number];
|
||||
|
||||
/**
|
||||
* 启用该商店
|
||||
*/
|
||||
enable(): void;
|
||||
|
||||
/**
|
||||
* 禁用该商店
|
||||
*/
|
||||
disable(): void;
|
||||
}
|
||||
|
||||
export interface IShopManagerSave {
|
||||
/** 商店状态列表 */
|
||||
readonly list: IShopStateSave[];
|
||||
}
|
||||
|
||||
export interface IShopManager
|
||||
extends IDataCommonExtended, ISaveableContent<IShopManagerSave> {
|
||||
/**
|
||||
* 添加新的商店
|
||||
* @param shop 商店状态对象
|
||||
*/
|
||||
addShop(shop: IShopState): void;
|
||||
|
||||
/**
|
||||
* 移除已有商店
|
||||
* @param shop 商店 id 或其状态对象
|
||||
*/
|
||||
removeShop(shop: string | IShopState): void;
|
||||
|
||||
/**
|
||||
* 根据商店 id 获取其状态对象
|
||||
* @param id 商店 id
|
||||
*/
|
||||
getShop(id: string): IShopState | null;
|
||||
|
||||
/**
|
||||
* 判断指定商店是否属于当前管理器
|
||||
* @param shop 商店 id 或其状态对象
|
||||
*/
|
||||
hasShop(shop: string | IShopState): boolean;
|
||||
|
||||
/**
|
||||
* 迭代当前的所有商店,包括未启用的
|
||||
*/
|
||||
iterateShops(): Iterable<[string, IShopState]>;
|
||||
|
||||
/**
|
||||
* 迭代当前所有已启用的商店
|
||||
*/
|
||||
iterateEnabledShops(): Iterable<[string, IShopState]>;
|
||||
|
||||
/**
|
||||
* 迭代当前所有未启用的商店
|
||||
*/
|
||||
iterateDisabledShops(): Iterable<[string, IShopState]>;
|
||||
|
||||
/**
|
||||
* 启用指定商店
|
||||
* @param shop 商店 id 或其状态对象
|
||||
*/
|
||||
enableShop(shop: string | IShopState): void;
|
||||
|
||||
/**
|
||||
* 禁用指定商店
|
||||
* @param shop 商店 id 或其状态对象
|
||||
*/
|
||||
disableShop(shop: string | IShopState): void;
|
||||
}
|
||||
Reference in new issue
Block a user