refactor: 数据端用户结构

This commit is contained in:
AncTe committed 2026-10-16 17:29:51 +08:00
1 parent 0a133058ca
commit a0e402728e
41 files changed
+901 -100

No files matched your search

+5 -1
View File
@@ -1,6 +1,7 @@
import { WebLoadStarter } from '@motajs/loader';
import { createClientCore } from './client/entry';
import { IClientCore } from '@user/client-modules';
import { LoadSceneUI } from '../packages-user/client-modules/src/ui/load';
declare global {
interface Window {
@@ -8,5 +9,8 @@ declare global {
}
}
const client = await createClientCore(new WebLoadStarter());
const client = createClientCore(new WebLoadStarter());
// 游戏副作用启动
window.mainClient = client;
client.sceneController.open(LoadSceneUI, {});
+1 -3
View File
@@ -41,9 +41,7 @@ export function createClientCoreConfig(
* 创建渲染端主对象,返回未经过异步初始化的主对象实例,需要手动调用 `state.load` 初始化
* @param starter 加载启动器
*/
export async function createClientCore(
starter: ILoadTaskStarter
): Promise<IClientCore> {
export function createClientCore(starter: ILoadTaskStarter): IClientCore {
const config = createClientCoreConfig(starter);
const state = new ClientCore(config);
return state;
+2 -1
View File
@@ -1,4 +1,5 @@
import { IClientPluginDefine } from '@user/client-modules';
import { ClientShopPlugin } from './shop';
/** 渲染端插件列表 */
export const clientPlugins: IClientPluginDefine[] = [];
export const clientPlugins: IClientPluginDefine[] = [new ClientShopPlugin()];
+2
View File
@@ -0,0 +1,2 @@
export * from './plugin';
export * from './shop';
+23
View File
@@ -0,0 +1,23 @@
import { IClientPluginDefine } from '@user/client-modules';
import { Shop, ShopUI } from './shop';
export interface IClientShopPlugin extends IClientPluginDefine {
/** 商店组件 */
readonly Shop: typeof Shop;
/** 商店 UI 对象 */
readonly ShopUI: typeof ShopUI;
}
export class ClientShopPlugin implements IClientShopPlugin {
id: string = 'client-shop-plugin';
dependencies: readonly string[] = [];
Shop: typeof Shop = Shop;
ShopUI: typeof ShopUI = ShopUI;
awake(): void {}
async initialize(): Promise<void> {}
async afterLoad(): Promise<void> {}
}
+491
View File
@@ -0,0 +1,491 @@
import { GameUI, SetupComponentOptions } from '@motajs/system';
import { computed, defineComponent, onMounted, ref } from 'vue';
import { Font } from '@motajs/render';
import { Scroll, TextContent } from '@user/client-base';
import { IHeroState } from '@user/data-base';
import { IHeroAttr } from '@user/data-common';
import { FULL_LOC, IUIPropsBase, MAIN_WIDTH } from '@user/client-modules';
import { IShopState, IShopSystem } from '@dp/shop';
import { RENDER_WIDTH } from '@/constant';
export interface IShopProps extends IUIPropsBase {
/** 商店状态对象 */
shop: IShopState;
/** 勇士状态对象 */
heroState: IHeroState<IHeroAttr>;
/** 商店系统对象,负责执行购买与售出交易 */
shopSystem: IShopSystem;
}
/** 商店界面组件的 props 声明 */
const shopProps = {
props: [
'controller',
'instance',
'state',
'shop',
'heroState',
'shopSystem'
]
} satisfies SetupComponentOptions<IShopProps>;
interface IShopItemEntry {
/** 道具图块数字 */
readonly num: number;
/** 道具字符串 id */
readonly id: string;
/** 道具名称 */
readonly name: string;
/** 道具描述 */
readonly text: string;
/** 商店库存 */
readonly stock: number;
/** 玩家拥有数量 */
readonly owned: number;
/** 道具买入价 */
readonly buyPrice: number;
/** 道具卖出价 */
readonly sellPrice: number;
}
/** 顶部购买 / 售出切换栏的高度 */
const MODE_BAR_HEIGHT = 36;
/** 左侧商店列表的宽度 */
const LIST_WIDTH = 250;
/** 左侧商店列表的顶部纵坐标 */
const LIST_TOP = MODE_BAR_HEIGHT + 4;
/** 左侧商店列表的高度,底部为退出按钮留出空间 */
const LIST_HEIGHT = RENDER_WIDTH - LIST_TOP - 16;
/** 商店列表中每一行的高度 */
const ROW_HEIGHT = 40;
/** 行内图标的尺寸 */
const ROW_ICON_SIZE = 28;
/** 右侧信息区域的起始横坐标 */
const INFO_LEFT = LIST_WIDTH;
/** 右侧信息区域的宽度 */
const INFO_WIDTH = RENDER_WIDTH - INFO_LEFT;
/** 道具名称的纵坐标 */
const NAME_TOP = MODE_BAR_HEIGHT + 10;
/** 道具描述区域的顶部纵坐标 */
const DESC_TOP = NAME_TOP + 26;
/** 道具描述区域的高度 */
const DESC_HEIGHT = 116;
/** 价格信息区域每行的高度 */
const INFO_ROW_HEIGHT = 20;
/** 价格信息区域的顶部纵坐标 */
const PRICE_TOP = DESC_TOP + DESC_HEIGHT + 6;
/** 购买数量标题的纵坐标 */
const COUNT_LABEL_Y = PRICE_TOP + INFO_ROW_HEIGHT * 5 + 10;
/** 数量按钮所在行的纵坐标 */
const COUNT_ROW_Y = COUNT_LABEL_Y + 24;
/** 确认按钮的纵坐标 */
const CONFIRM_Y = COUNT_ROW_Y + 42;
/** 数量按钮每次大步进的数量 */
const COUNT_STEP = 10;
/** 标题与按钮字体 */
const titleFont = Font.defaults({ size: 14, weight: 700 });
/** 列表与信息字体 */
const textFont = Font.defaults({ size: 12 });
/** 商店界面组件 */
export const Shop = defineComponent<IShopProps>(props => {
/** 商店状态对象 */
const shop = props.shop;
/** 勇士状态对象 */
const hero = props.heroState;
// 打开数据端当前商店,使其成为购买 / 售出交易的作用对象
onMounted(() => props.shopSystem.openShop(shop));
/** 当前的购买 / 售出模式 */
const mode = ref<'buy' | 'sell'>('buy');
/** 当前选中的道具图块数字,-1 表示未选中 */
const selected = ref(-1);
/** 当前购买 / 售出数量 */
const count = ref(0);
/** 按道具图块数字分别记忆的购买数量 */
const buyCounts = new Map<number, number>();
/** 按道具图块数字分别记忆的售出数量 */
const sellCounts = new Map<number, number>();
/** 商店道具列表,按图块数字升序排列 */
const entries = computed<IShopItemEntry[]>(() => {
const list: IShopItemEntry[] = [];
for (const pair of shop.iterateItems()) {
const num = pair[0];
const data = props.state.itemStore.getData(num);
if (!data) continue;
const id = props.state.tileStore.id(num);
const price = shop.getPrice(num);
list.push({
num,
id: id ?? '',
name: data.name,
text: data.text,
stock: shop.getCount(num),
owned: hero.items.itemCount(num),
buyPrice: price[0],
sellPrice: price[1]
});
}
return list.sort((a, b) => a.num - b.num);
});
/** 当前选中的道具条目,未选中时为 null */
const current = computed<IShopItemEntry | null>(() => {
for (const entry of entries.value) {
if (entry.num === selected.value) return entry;
}
return null;
});
/** 当前模式下可调整的数量上限 */
const countLimit = () => {
if (selected.value < 0) return 0;
if (mode.value === 'buy') {
const stock = shop.getCount(selected.value);
return stock < 0 ? 0 : stock;
}
return hero.items.itemCount(selected.value);
};
/** 将当前数量限制在上限与 0 之间 */
const clampCount = () => {
const limit = countLimit();
if (count.value < 0) count.value = 0;
if (count.value > limit) count.value = limit;
};
/** 保存当前选中道具在当前模式下记忆的数量 */
const saveCount = () => {
if (selected.value < 0) return;
const map = mode.value === 'buy' ? buyCounts : sellCounts;
map.set(selected.value, count.value);
};
/** 载入选中道具在当前模式下记忆的数量并限制在合法范围 */
const loadCount = () => {
const map = mode.value === 'buy' ? buyCounts : sellCounts;
const stored = selected.value < 0 ? undefined : map.get(selected.value);
count.value = stored ?? 0;
clampCount();
};
/**
* 选中一个道具,保存原选中道具的数量并恢复新选中道具的数量
* @param num 被选中道具的图块数字
*/
const selectItem = (num: number) => {
saveCount();
selected.value = num;
loadCount();
};
/**
* 切换购买 / 售出模式,两种模式的数量分别记忆
* @param next 切换到的模式
*/
const changeMode = (next: 'buy' | 'sell') => {
saveCount();
mode.value = next;
loadCount();
};
/**
* 调整数量,按步进上下调整后限制在合法范围
* @param delta 数量的变化量
*/
const adjustCount = (delta: number) => {
count.value += delta;
clampCount();
};
/** 执行当前模式下的交易,数量为 0 时不执行 */
const confirm = () => {
if (selected.value < 0 || count.value === 0) return;
if (mode.value === 'buy') {
props.shopSystem.buyItem(selected.value, count.value);
} else {
props.shopSystem.sellItem(selected.value, count.value);
}
count.value = 0;
saveCount();
};
/** 关闭当前商店界面 */
const exit = () => props.controller.close(props.instance);
return () => {
const entry = current.value;
let total = 0;
if (entry) {
const price =
mode.value === 'buy' ? entry.buyPrice : entry.sellPrice;
total = count.value * price;
}
return (
<container loc={FULL_LOC}>
{/* 顶部购买 / 售出切换栏 */}
<container loc={[0, 0, MAIN_WIDTH, MODE_BAR_HEIGHT]}>
<text
text="购买选项"
loc={[MAIN_WIDTH / 2 - 70, MODE_BAR_HEIGHT / 2]}
anc={[0.5, 0.5]}
font={titleFont}
cursor="pointer"
fillStyle={mode.value === 'buy' ? 'gold' : '#888888'}
onClick={() => changeMode('buy')}
/>
<text
text="售出选项"
loc={[MAIN_WIDTH / 2 + 70, MODE_BAR_HEIGHT / 2]}
anc={[0.5, 0.5]}
font={titleFont}
cursor="pointer"
fillStyle={mode.value === 'sell' ? 'gold' : '#888888'}
onClick={() => changeMode('sell')}
/>
</container>
{/* 商店列表 */}
<Scroll loc={[0, LIST_TOP, LIST_WIDTH, LIST_HEIGHT]}>
{entries.value.map((item, i) => {
const isSelected = item.num === selected.value;
return (
<container
key={item.num}
loc={[
0,
i * ROW_HEIGHT,
LIST_WIDTH,
ROW_HEIGHT
]}
cursor="pointer"
onClick={() => selectItem(item.num)}
>
<g-rectr
loc={[4, 2, LIST_WIDTH - 8, ROW_HEIGHT - 4]}
circle={[4]}
fill
fillStyle={
isSelected
? 'rgba(255, 204, 0, 0.28)'
: 'rgba(255, 255, 255, 0.05)'
}
/>
<icon
icon={item.num}
loc={[10, 6, ROW_ICON_SIZE, ROW_ICON_SIZE]}
/>
<text
text={item.name}
loc={[46, ROW_HEIGHT / 2]}
anc={[0, 0.5]}
font={textFont}
fillStyle="#eeeeee"
/>
<text
text={core.formatBigNumber(item.stock)}
loc={[LIST_WIDTH - 10, ROW_HEIGHT / 2]}
anc={[1, 0.5]}
font={textFont}
fillStyle="#cccccc"
/>
</container>
);
})}
</Scroll>
{/* 选中道具名称 */}
<text
text={entry ? entry.name : '未选择道具'}
loc={[INFO_LEFT + 14, NAME_TOP + 13]}
anc={[0, 0.5]}
font={titleFont}
fillStyle="gold"
/>
{/* 选中道具描述 */}
<Scroll
noscroll
loc={[INFO_LEFT, DESC_TOP, INFO_WIDTH, DESC_HEIGHT]}
>
<TextContent
text={entry ? entry.text : ''}
width={INFO_WIDTH - 28}
loc={[14, 4, INFO_WIDTH - 28, DESC_HEIGHT - 8]}
font={textFont}
fillStyle="#dddddd"
/>
</Scroll>
{/* 价格信息 */}
<container
loc={[
INFO_LEFT,
PRICE_TOP,
INFO_WIDTH,
INFO_ROW_HEIGHT * 5
]}
>
<text
text="买价"
loc={[14, 10]}
anc={[0, 0.5]}
font={textFont}
fillStyle="#dddddd"
/>
<text
text={
entry ? core.formatBigNumber(entry.buyPrice) : '-'
}
loc={[INFO_WIDTH - 14, 10]}
anc={[1, 0.5]}
font={textFont}
fillStyle="lightgreen"
/>
<text
text="卖价"
loc={[14, 30]}
anc={[0, 0.5]}
font={textFont}
fillStyle="#dddddd"
/>
<text
text={
entry ? core.formatBigNumber(entry.sellPrice) : '-'
}
loc={[INFO_WIDTH - 14, 30]}
anc={[1, 0.5]}
font={textFont}
fillStyle="lightcoral"
/>
<text
text="存货"
loc={[14, 50]}
anc={[0, 0.5]}
font={textFont}
fillStyle="#dddddd"
/>
<text
text={entry ? core.formatBigNumber(entry.stock) : '-'}
loc={[INFO_WIDTH - 14, 50]}
anc={[1, 0.5]}
font={textFont}
fillStyle="#cccccc"
/>
<text
text="拥有"
loc={[14, 70]}
anc={[0, 0.5]}
font={textFont}
fillStyle="#dddddd"
/>
<text
text={entry ? core.formatBigNumber(entry.owned) : '-'}
loc={[INFO_WIDTH - 14, 70]}
anc={[1, 0.5]}
font={textFont}
fillStyle="#cccccc"
/>
<text
text="总价"
loc={[14, 90]}
anc={[0, 0.5]}
font={textFont}
fillStyle="#dddddd"
/>
<text
text={core.formatBigNumber(total)}
loc={[INFO_WIDTH - 14, 90]}
anc={[1, 0.5]}
font={textFont}
fillStyle="gold"
/>
</container>
{/* 数量选择 */}
<text
text={mode.value === 'buy' ? '购买数量' : '售出数量'}
loc={[INFO_LEFT + INFO_WIDTH / 2, COUNT_LABEL_Y]}
anc={[0.5, 0.5]}
font={textFont}
fillStyle="#cccccc"
/>
<text
text="<<"
loc={[INFO_LEFT + INFO_WIDTH / 2 - 70, COUNT_ROW_Y]}
anc={[0.5, 0.5]}
font={titleFont}
cursor="pointer"
fillStyle="#ffffff"
onClick={() => adjustCount(-COUNT_STEP)}
/>
<text
text="<"
loc={[INFO_LEFT + INFO_WIDTH / 2 - 40, COUNT_ROW_Y]}
anc={[0.5, 0.5]}
font={titleFont}
cursor="pointer"
fillStyle="#ffffff"
onClick={() => adjustCount(-1)}
/>
<text
text={String(count.value)}
loc={[INFO_LEFT + INFO_WIDTH / 2, COUNT_ROW_Y]}
anc={[0.5, 0.5]}
font={titleFont}
fillStyle="gold"
/>
<text
text=">"
loc={[INFO_LEFT + INFO_WIDTH / 2 + 40, COUNT_ROW_Y]}
anc={[0.5, 0.5]}
font={titleFont}
cursor="pointer"
fillStyle="#ffffff"
onClick={() => adjustCount(1)}
/>
<text
text=">>"
loc={[INFO_LEFT + INFO_WIDTH / 2 + 70, COUNT_ROW_Y]}
anc={[0.5, 0.5]}
font={titleFont}
cursor="pointer"
fillStyle="#ffffff"
onClick={() => adjustCount(COUNT_STEP)}
/>
{/* 购买 / 售出确认按钮 */}
<container loc={[INFO_LEFT, CONFIRM_Y, INFO_WIDTH, 30]}>
<g-rectr
loc={[14, 0, INFO_WIDTH - 28, 30]}
circle={[15]}
fill
stroke
fillStyle="rgba(50, 54, 159, 0.7)"
strokeStyle="gold"
lineWidth={2}
/>
<text
text={mode.value === 'buy' ? '确认购买' : '确认售出'}
loc={[INFO_WIDTH / 2, 15]}
anc={[0.5, 0.5]}
font={titleFont}
cursor="pointer"
fillStyle="#ffffff"
onClick={confirm}
/>
</container>
{/* 右下角退出按钮 */}
<text
text="退出"
loc={[MAIN_WIDTH - 12, MAIN_HEIGHT - 10]}
anc={[1, 1]}
font={titleFont}
cursor="pointer"
fillStyle="#ffffff"
onClick={exit}
/>
</container>
);
};
}, shopProps);
/** 商店界面的 UI 控制器 */
export const ShopUI = new GameUI('shop', Shop);
+1 -1
View File
@@ -175,7 +175,7 @@ export class GuardAura implements IEnemyAuraView<
if (locator.x === this.locator.x && locator.y === this.locator.y) {
return;
}
enemy.getAttribute('guard').add(this.locator);
enemy.getAttribute('guard').add(this.sourceView);
}
applySpecial(): IEnemySpecialModifier<IEnemyAttr> | null {
+1 -7
View File
@@ -5,7 +5,6 @@ import {
IReadonlyEnemyHandler
} from '@user/data-system';
import { IEnemyAttr, IHeroAttr } from '@user/data-common';
import { logger } from '@motajs/common';
import { ISpecial9Value } from './special';
export class MainDamageCalculator implements IDamageCalculator<
@@ -83,13 +82,8 @@ export class MainDamageCalculator implements IDamageCalculator<
this.inGuard = true;
// 计算支援怪的伤害,同时把打支援怪花费的回合数加到当前怪物上,因为打支援怪的时候当前怪物也会打你
// 因此回合数需要加上打支援怪的回合数
for (const guard of guards) {
for (const view of guards) {
// 直接把 enemy 传过去,因此支援的 enemy 会吃到其原本所在位置的光环加成
const view = handler.context.getEnemyByLocator(guard);
if (!view) {
logger.warn(137, guard.x.toString(), guard.y.toString());
continue;
}
const extraInfo = this.calculate({
enemy: view.getComputedEnemy(),
context: handler.context,
+222
View File
@@ -0,0 +1,222 @@
import {
IGameMap,
IHeroLocation,
IHeroLocationHooks,
IHeroModifier,
IHeroMover,
PercentageModifier
} from '@user/data-base';
import {
IEnemyAttr,
IHeroAttr,
IObjectMover,
IObjectMoverHooks,
ObjectMoveStep,
ObjectMoveType
} from '@user/data-common';
import { ICoreState } from '@user/data-state';
import {
ICombatFlowHooks,
IEnemyDamageInfo,
IPathfindingResult,
IPathfindingSystem,
IPathfindingSystemHooks
} from '@user/data-system';
import { ICombatWeakHandler } from './types';
import { isNil } from 'lodash-es';
export class EnemyMoveHooks implements IObjectMoverHooks<IHeroLocation> {
constructor(readonly state: ICoreState) {}
/**
* 处理中毒效果
* @param step 移动步对象
*/
private handlePoison(
step: Readonly<ObjectMoveStep>,
mover: IHeroMover<IHeroLocation>
) {
const poison = this.state.flags.getFieldValueDefaults('poison', 0);
// 中毒伤害小于等于 0,直接返回
if (poison <= 0) return;
const config = mover.getConfig();
// 如果忽略地形,那么也不计算中毒伤害
if (config.ignoreTerrain) return;
// 获取勇士的可修改属性对象
const hero = this.state.hero.getModifiableAttribute();
// 判断是否为常规移动步
if (
step.type === ObjectMoveType.Dir ||
step.type === ObjectMoveType.DirFace ||
step.type === ObjectMoveType.Special
) {
// 给勇士加上 -poison 的生命值,就相当于扣 poison 的生命值
hero.add('hp', -poison);
}
}
async onStepEnd(
_code: number,
step: Readonly<ObjectMoveStep>,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 检查状态对象
if (tile.state !== this.state) return;
// 检查移动器是否为勇士移动器
if (mover !== this.state.hero.location.mover) return;
// 处理中毒
this.handlePoison(step, mover as IHeroMover<IHeroLocation>);
}
}
export class EnemyPathfindingHooks implements IPathfindingSystemHooks {
constructor(readonly state: ICoreState) {}
/**
* 处理中毒效果
* @param path 自动寻路路径
*/
private handlePoison(path: IPathfindingResult) {
const poison = this.state.flags.getFieldValueDefaults('poison', 0);
// 中毒伤害小于等于 0,直接返回
if (poison <= 0) return;
const mover = this.state.hero.location.mover;
const config = mover.getConfig();
// 如果忽略地形,那么也不计算中毒伤害
if (config.ignoreTerrain) return;
const hero = this.state.hero.getModifiableAttribute();
// 勇士减少步数乘以单步中毒的伤害。
hero.add('hp', -path.path.length);
}
onAfterTeleportTo(
path: IPathfindingResult,
system: IPathfindingSystem
): void {
// 判断状态对象
if (system.state !== this.state) return;
// 判断当前绑定的是不是勇士移动器
if (system.mover !== this.state.hero.location.mover) return;
// 处理中毒
this.handlePoison(path);
}
}
export class EnemyCombatHooks
implements ICombatFlowHooks<IEnemyAttr, IHeroAttr>, ICombatWeakHandler
{
/** 当前的虚弱值 */
private weakValue: number = 0;
/** 攻击虚弱修饰器 */
private readonly weakAtkModifier: IHeroModifier<number, number, number>;
/** 防御虚弱修饰器 */
private readonly weakDefModifier: IHeroModifier<number, number, number>;
constructor(readonly state: ICoreState) {
const hero = state.hero.getModifiableAttribute();
// 初始化虚弱属性的修饰器,并添加到勇士修饰器中
this.weakAtkModifier = new PercentageModifier(0);
this.weakDefModifier = new PercentageModifier(0);
hero.addModifier('atk', this.weakAtkModifier);
hero.addModifier('def', this.weakDefModifier);
}
setWeakValue(value: number): void {
this.weakValue = value;
// 需要填写负数才能使勇士属性降低
this.weakAtkModifier.setValue(-value);
this.weakDefModifier.setValue(-value);
}
getWeakValue(): number {
return this.weakValue;
}
async onAfterCombat(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>
): Promise<void> {
// 判断状态对象
if (info.handler.state !== this.state) return;
// 判断勇士对象
if (info.handler.hero !== this.state.hero.getAttribute()) return;
// 处理中毒属性
const poison = info.handler.enemy.getSpecial<number>(10);
if (poison) {
// 获取 poison 全局 flag,如果 flag 不存在,则自动插入为 0
const field = this.state.flags.getOrInsert('poison', 0);
const max = Math.max(field.get(), poison.value);
field.set(max);
}
// 处理虚弱属性
const weak = info.handler.enemy.getSpecial<number>(11);
if (weak) {
const max = Math.max(this.weakValue, weak.value);
this.setWeakValue(max);
}
// 处理重生属性
if (info.handler.enemy.hasSpecial(21)) {
const field = this.state.flags.getFieldValueDefaults<
Map<string, [number, number, number][]>
>('rebirth', new Map());
// 如果战斗不发生在任何地图,或者不存在坐标,那么不计入重生
if (isNil(info.handler.context.floor) || !info.handler.locator) {
return;
}
// 获取当前楼层的重生存储,并向其中添加重生标记
const floor = field.getOrInsert(info.handler.context.floor, []);
const { x, y } = info.handler.locator;
floor.push([info.handler.enemy.code, x, y]);
}
}
}
export class EnemyChangeFloorHooks implements IHeroLocationHooks {
constructor(readonly state: ICoreState) {}
/**
* 处理重生属性
* @param floor 目标楼层
*/
private handleRebirth(map: IGameMap) {
// 先获取指定楼层的重生标记
const field = this.state.flags.getFieldValueDefaults<
Map<string, [number, number, number][]>
>('rebirth', new Map());
const list = field.get(map.floorId);
// 如果不存在重生,直接返回
if (!list || list.length === 0) return;
const event = map.eventLayer;
// 如果不存在事件层,直接返回
if (!event) return;
// 把每个重生怪都放到事件层上
for (const [tile, x, y] of list) {
event.setBlock(tile, x, y);
}
// 清空重生怪列表
list.length = 0;
}
onSetFloor(map: IGameMap | null): void {
if (!map) return;
// 判断状态对象
if (map.state !== this.state) return;
// 处理重生
this.handleRebirth(map);
}
}
+23 -1
View File
@@ -10,13 +10,23 @@ import { MainEnemyFinalEffect } from './final';
import { MainDamageCalculator } from './calculator';
import { MainEnemyComparer } from './comparer';
import { MainMapDamageConverter, MainMapDamageReducer } from './mapDamage';
import { IEnemyInitializationReturns } from './types';
import {
EnemyChangeFloorHooks,
EnemyCombatHooks,
EnemyMoveHooks,
EnemyPathfindingHooks
} from './hook';
import { MainCombatScript } from './script';
/**
* 初始化怪物相关内容
* @param state 数据端主对象
* @param config 数据端配置
*/
export function initializeEnemy(state: ICoreState) {
export function initializeEnemy(
state: ICoreState
): IEnemyInitializationReturns {
// 怪物管理器初始化
const comparer = new MainEnemyComparer();
state.enemyManager.attachEnemyComparer(comparer);
@@ -45,4 +55,16 @@ export function initializeEnemy(state: ICoreState) {
// 注册怪物最终效果
ctx.registerFinalEffect(new MainEnemyFinalEffect());
// 添加默认战前战后脚本
state.combat.addCombatScript(new MainCombatScript());
// 注册相关的钩子
state.hero.location.mover.addHook(new EnemyMoveHooks(state));
state.pathfinding.addHook(new EnemyPathfindingHooks(state));
const combatHook = new EnemyCombatHooks(state);
state.combat.addHook(combatHook);
state.hero.location.addHook(new EnemyChangeFloorHooks(state));
return { weak: combatHook };
}
+60
View File
@@ -0,0 +1,60 @@
import { IEnemyAttr, IHeroAttr } from '@user/data-common';
import {
ICombatFlowHandler,
ICombatScript,
IEnemyDamageInfo
} from '@user/data-system';
export class MainCombatScript implements ICombatScript<IEnemyAttr, IHeroAttr> {
priority: number = 0;
async before(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>,
handler: ICombatFlowHandler<IEnemyAttr, IHeroAttr>
): Promise<boolean> {
const hero = handler.hero;
// 如果伤害大于勇士血量,直接不允许战斗
if (!handler.force && hero.getFinalAttribute('hp') < info.damage) {
return false;
}
return true;
}
async after(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>,
handler: ICombatFlowHandler<IEnemyAttr, IHeroAttr>
): Promise<void> {
const hero = handler.hero;
const readonlyEnemy = info.handler.enemy;
// 计算金币和经验,支援怪也算上
const baseExp = readonlyEnemy.getAttribute('exp');
const baseMoney = readonlyEnemy.getAttribute('money');
const [guardExp, guardMoney] = [
...readonlyEnemy.getAttribute('guard')
].reduce(
(prev, curr) => {
const attr = curr.getComputedEnemy();
const exp = attr.getAttribute('exp');
const money = attr.getAttribute('money');
return [prev[0] + exp, prev[1] + money];
},
[0, 0]
);
const totalExp = baseExp + guardExp;
const totalMoney = baseMoney + guardMoney;
// 给勇士添加经验和金币
hero.add('exp', totalExp);
hero.add('money', totalMoney);
// 进行全局提示
handler.state.emitTip(
`打败${readonlyEnemy.getAttribute('name')},金币+${totalMoney},经验+${totalExp}。`,
readonlyEnemy.code
);
}
}
+5 -16
View File
@@ -77,10 +77,10 @@ export interface ISpecial26Value {
* 2. 坚固、模仿等仅影响自身基础属性的属性:./final.ts, MainEnemyFinalEffect.apply 方法
* 3. 地图伤害:./mapDamage.ts,具体写法参考文档
* 4. 光环属性:./aura.ts 具体写法参考文档
* 5. 中毒的每步效果:在插件系统中,目前暂未实装
* 6. 中毒的瞬移效果:在插件系统中,目前暂未实装
* 7. 衰弱效果:在插件系统中,目前尚未实装
* 8. 重生属性:在插件系统中,目前尚未实装
* 5. 中毒的每步效果:./hook.ts EnemyMoveHooks.handlePoison 方法
* 6. 中毒的瞬移效果:./hook.ts EnemyPathfindingHooks.handlePoison 方法
* 7. 衰弱效果:./hook.ts EnemyCombatHooks.onAfterCombat 方法
* 8. 重生属性:./hook.ts EnemyCombatHooks.onAfterCombat 和 EnemyChangeFloorHooks.onSetFloor 方法
*/
export function createSpecials(
state: ICoreState
@@ -233,18 +233,7 @@ export function createSpecials(
name: '衰弱',
desc: special => {
const weak = special.value;
const suffix = ',多次触发时取最高值。';
if (weak < 1) {
return (
`战斗后,角色陷入衰弱状态,攻防暂时下降${Math.floor(weak * 100)}%` +
suffix
);
} else {
return (
`战斗后,角色陷入衰弱状态,攻防暂时下降${weak}点` +
suffix
);
}
return `战斗后,角色陷入衰弱状态,攻防暂时下降${Math.floor(weak * 100)}%,多次触发时取最高值。`;
},
color: '#F0BBCC'
})
+18
View File
@@ -10,3 +10,21 @@ export const enum MapDamageType {
/** 夹击伤害 */
Between
}
export interface IEnemyInitializationReturns {
/** 虚弱处理对象 */
readonly weak: ICombatWeakHandler;
}
export interface ICombatWeakHandler {
/**
* 设置当前的虚弱值
* @param value 虚弱值
*/
setWeakValue(value: number): void;
/**
* 获取当前的虚弱值
*/
getWeakValue(): number;
}
+24
View File
@@ -0,0 +1,24 @@
import { IEnemyAttr, IHeroAttr } from '@user/data-common';
import { ICoreState } from '@user/data-state';
import { ICombatFlowHooks, IEnemyDamageInfo } from '@user/data-system';
export class MainCombatFlowHooks implements ICombatFlowHooks<
IEnemyAttr,
IHeroAttr
> {
constructor(readonly state: ICoreState) {}
async onBeforeCombat(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>
): Promise<void> {
// 删除下面这一行开始编写你自己的战前钩子
void info;
}
async onAfterCombat(
info: IEnemyDamageInfo<IEnemyAttr, IHeroAttr>
): Promise<void> {
// 删除下面这一行开始编写你自己的战后钩子
void info;
}
}
+30
View File
@@ -0,0 +1,30 @@
import { ICoreState } from '@user/data-state';
import {
IBlockEventParam,
IGameEventExecutorHooks,
IGameEventInvocation
} from '@user/data-system';
export class MainGameEventExecutorHooks implements IGameEventExecutorHooks {
constructor(readonly state: ICoreState) {}
onBeforeExecuteEvent(
events: IGameEventInvocation[],
param: IBlockEventParam
): void {
// 删除下面这两行注释开始编写你自己的事件执行前钩子
void events;
void param;
}
onAfterExecuteEvent(
events: IGameEventInvocation[],
param: IBlockEventParam,
returns: unknown
): void {
// 删除下面这三行注释开始编写你自己的事件执行后钩子
void events;
void param;
void returns;
}
}
+141
View File
@@ -0,0 +1,141 @@
import {
IGameMap,
IHeroAttributeHooks,
IHeroChangeFloorInfo,
IHeroLocation,
IHeroLocationHooks,
IHeroStateHooks
} from '@user/data-base';
import {
FaceDirection,
IHeroAttr,
IObjectMover,
IObjectMoverHooks,
ObjectMoveStep
} from '@user/data-common';
import { ICoreState } from '@user/data-state';
export class MainHeroAttributeHooks implements IHeroAttributeHooks<IHeroAttr> {
constructor(readonly state: ICoreState) {}
onUpdateAttribute<K extends keyof IHeroAttr>(
name: K,
value: IHeroAttr[K]
): void {
// 删除下面这两行开始编写你自己的勇士属性更新钩子
void name;
void value;
}
}
export class MainHeroLocationHooks implements IHeroLocationHooks {
constructor(readonly state: ICoreState) {}
onSetFloor(map: IGameMap | null): void {
// 删除下面这两行开始编写你自己的切换楼层钩子
void map;
}
onSetPos(x: number, y: number): void {
// 删除下面这两行开始编写你自己的勇士位置设置钩子
void x;
void y;
}
}
export class MainHeroMoverHooks implements IObjectMoverHooks<IHeroLocation> {
constructor(readonly state: ICoreState) {}
async onMoveStart(
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 删除下面这两行注释开始编写你自己的勇士移动前钩子
void tile;
void mover;
}
async onStepStart(
code: number,
step: Readonly<ObjectMoveStep>,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 删除下面这四行注释开始编写你自己的勇士每步开始时钩子
void code;
void step;
void tile;
void mover;
}
async onStepEnd(
code: number,
step: Readonly<ObjectMoveStep>,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 删除下面这四行注释开始编写你自己的勇士每步结束后钩子
void code;
void step;
void tile;
void mover;
}
async onMoveEnd(
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): Promise<void> {
// 删除下面这两行注释开始编写你自己的勇士移动前钩子
void tile;
void mover;
}
onSetFaceDir(
dir: FaceDirection,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): void {
// 删除下面这三行注释开始编写你自己的设置面朝朝向的钩子
void dir;
void tile;
void mover;
}
onSetMoveDir(
dir: FaceDirection,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): void {
// 删除下面这三行注释开始编写你自己的设置移动朝向的钩子
void dir;
void tile;
void mover;
}
onSetPos(
x: number,
y: number,
tile: IHeroLocation,
mover: IObjectMover<IHeroLocation>
): void {
// 删除下面这三行注释开始编写你自己的设置位置的钩子
void x;
void y;
void tile;
void mover;
}
}
export class MainHeroStateHooks implements IHeroStateHooks {
constructor(readonly state: ICoreState) {}
async onBeforeChangeFloor(info: IHeroChangeFloorInfo): Promise<void> {
// 删除下面这一行注释开始编写你自己的楼层转换前钩子
void info;
}
async onAfterChangeFloor(info: IHeroChangeFloorInfo): Promise<void> {
// 删除下面这一行注释开始编写你自己的楼层转换后钩子
void info;
}
}
+6
View File
@@ -0,0 +1,6 @@
export * from './combat';
export * from './event';
export * from './hero';
export * from './initialize';
export * from './loader';
export * from './path';
+26
View File
@@ -0,0 +1,26 @@
import { ICoreState } from '@user/data-state';
import { MainCombatFlowHooks } from './combat';
import { MainGameEventExecutorHooks } from './event';
import {
MainHeroAttributeHooks,
MainHeroLocationHooks,
MainHeroMoverHooks,
MainHeroStateHooks
} from './hero';
import { MainMotaDataLoaderHooks } from './loader';
import { MainPathfindingSystemHooks } from './path';
/**
* 初始化数据端主要钩子
* @param state 数据端主对象
*/
export function initializeHooks(state: ICoreState) {
state.combat.addHook(new MainCombatFlowHooks(state));
state.eventSystem.executor.addHook(new MainGameEventExecutorHooks(state));
state.hero.addHook(new MainHeroStateHooks(state));
state.hero.attribute.addHook(new MainHeroAttributeHooks(state));
state.hero.location.addHook(new MainHeroLocationHooks(state));
state.hero.location.mover.addHook(new MainHeroMoverHooks(state));
state.loader.addHook(new MainMotaDataLoaderHooks(state));
state.pathfinding.addHook(new MainPathfindingSystemHooks(state));
}
+19
View File
@@ -0,0 +1,19 @@
import {
ICoreState,
IMotaDataLoader,
IMotaDataLoaderHooks
} from '@user/data-state';
export class MainMotaDataLoaderHooks implements IMotaDataLoaderHooks {
constructor(readonly state: ICoreState) {}
async onCoreConfigLoaded(loader: IMotaDataLoader): Promise<void> {
// 删除下面这两行注释开始编写你自己的核心配置加载后钩子
void loader;
}
async onExtraConfigLoaded(loader: IMotaDataLoader): Promise<void> {
// 删除下面这两行注释开始编写你自己的额外配置加载后钩子
void loader;
}
}
+28
View File
@@ -0,0 +1,28 @@
import { ICoreState } from '@user/data-state';
import {
IPathfindingResult,
IPathfindingSystem,
IPathfindingSystemHooks
} from '@user/data-system';
export class MainPathfindingSystemHooks implements IPathfindingSystemHooks {
constructor(readonly state: ICoreState) {}
onBeforeTeleportTo(
path: IPathfindingResult,
system: IPathfindingSystem
): void {
// 删除下面这两行注释开始编写你自己的自动寻路瞬移前钩子,注意非勇士的瞬移也会触发
void path;
void system;
}
onAfterTeleportTo(
path: IPathfindingResult,
system: IPathfindingSystem
): void {
// 删除下面这两行注释开始编写你自己的自动寻路瞬移后钩子,注意非勇士的瞬移也会触发
void path;
void system;
}
}
+11 -8
View File
@@ -5,21 +5,21 @@ import {
FaceGroup,
IFacedTileLocator
} from '@user/data-common';
import {
ICoreState,
ICoreStateCoreConfig,
IDataPluginDefine
} from '@user/data-state';
import { initializeEnemy } from './enemy';
import { ICoreState, ICoreStateCoreConfig } from '@user/data-state';
import { ICombatWeakHandler, initializeEnemy } from './enemy';
import { initializeReplay } from './replay';
import { logger } from '@motajs/common';
import { DefaultHeroMoveTopImpl, DefaultPassPredicateImpl } from './hero';
import { DefaultDataLoaderHook } from './loader';
import { IDataMainPlugin } from './types';
import { initializeHooks } from './hook';
export class MainDataPlugin implements IDataPluginDefine {
export class MainDataPlugin implements IDataMainPlugin {
readonly id: string = 'main-data-plugin';
readonly dependencies: string[] = [];
weak!: ICombatWeakHandler;
constructor(readonly heroFace: FaceGroup) {}
awake(state: ICoreState): void {
@@ -46,8 +46,11 @@ export class MainDataPlugin implements IDataPluginDefine {
state.loader.addHook(new DefaultDataLoaderHook());
// 其他复杂初始化
initializeEnemy(state);
const enemyReturns = initializeEnemy(state);
initializeReplay(state);
initializeHooks(state);
this.weak = enemyReturns.weak;
// 存档初始化
state.addSaveableContent('@system/hero', state.hero);
+2 -2
View File
@@ -1,5 +1,5 @@
import { IDataPluginDefine } from '@user/data-state';
import { ShopPlugin } from './shop';
import { DataShopPlugin } from './shop';
/** 数据端插件列表 */
export const dataPlugins: IDataPluginDefine[] = [new ShopPlugin()];
export const dataPlugins: IDataPluginDefine[] = [new DataShopPlugin()];
+5 -1
View File
@@ -1 +1,5 @@
export * from './shop';
export * from './manager';
export * from './plugin';
export * from './state';
export * from './system';
export * from './types';
@@ -4,8 +4,8 @@ import { ShopManager } from './manager';
import { ShopState } from './state';
import { ShopSystem } from './system';
export class ShopPlugin implements IShopPlugin {
id: string = 'shop-plugin';
export class DataShopPlugin implements IShopPlugin {
id: string = 'data-shop-plugin';
dependencies: string[] = [];
shop!: IShopSystem;
+7
View File
@@ -0,0 +1,7 @@
import { IDataPluginDefine } from '@user/data-state';
import { ICombatWeakHandler } from './enemy';
export interface IDataMainPlugin extends IDataPluginDefine {
/** 虚弱处理对象 */
readonly weak: ICombatWeakHandler;
}