mirror of
https://github.com/unanmed/HumanBreak.git
synced 2026-11-28 21:14:30 +08:00
refactor: src/game 全部移入 monorepo
This commit is contained in:
1 parent
f7c6434aba
commit
ef8d3e9e2e
54 files changed
+722
-254
No files matched your search
@@ -0,0 +1,344 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { cloneDeep } from 'lodash-es';
|
||||
import { HeroSkill, NightSpecial } from '../mechanism/misc';
|
||||
|
||||
/**
|
||||
* 获取勇士在某一点的属性
|
||||
* @param name 要获取的勇士属性
|
||||
* @param floorId 勇士所在楼层
|
||||
*/
|
||||
export function getHeroStatusOn(name: 'all', floorId?: FloorIds): HeroStatus;
|
||||
export function getHeroStatusOn(
|
||||
name: (keyof HeroStatus)[],
|
||||
floorId?: FloorIds
|
||||
): Partial<HeroStatus>;
|
||||
export function getHeroStatusOn<K extends keyof HeroStatus>(
|
||||
name: K,
|
||||
floorId?: FloorIds
|
||||
): HeroStatus[K];
|
||||
export function getHeroStatusOn(
|
||||
name: keyof HeroStatus | 'all' | (keyof HeroStatus)[],
|
||||
floorId?: FloorIds
|
||||
) {
|
||||
// @ts-ignore
|
||||
return getHeroStatusOf(core.status.hero, name, floorId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一定状态下的勇士在某一点的属性
|
||||
* @param status 勇士的状态
|
||||
* @param name 要获取的勇士属性
|
||||
* @param floorId 勇士所在楼层
|
||||
*/
|
||||
export function getHeroStatusOf(
|
||||
status: Partial<HeroStatus>,
|
||||
name: 'all',
|
||||
floorId?: FloorIds
|
||||
): HeroStatus;
|
||||
export function getHeroStatusOf(
|
||||
status: Partial<HeroStatus>,
|
||||
name: (keyof HeroStatus)[],
|
||||
floorId?: FloorIds
|
||||
): Partial<HeroStatus>;
|
||||
export function getHeroStatusOf<K extends keyof HeroStatus>(
|
||||
status: Partial<HeroStatus>,
|
||||
name: K,
|
||||
floorId?: FloorIds
|
||||
): HeroStatus[K];
|
||||
export function getHeroStatusOf(
|
||||
status: DeepPartial<HeroStatus>,
|
||||
name: keyof HeroStatus | 'all' | (keyof HeroStatus)[],
|
||||
floorId?: FloorIds
|
||||
) {
|
||||
return getRealStatus(status, name, floorId);
|
||||
}
|
||||
|
||||
function getRealStatus(
|
||||
status: DeepPartial<HeroStatus>,
|
||||
name: keyof HeroStatus | 'all' | (keyof HeroStatus)[],
|
||||
floorId: FloorIds = core.status.floorId
|
||||
): any {
|
||||
const { getSkillLevel } = Mota.Plugin.require('skillTree_g');
|
||||
if (name instanceof Array) {
|
||||
const res: any = {};
|
||||
name.forEach(v => {
|
||||
res[v] = getRealStatus(status, v, floorId);
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
if (name === 'all') {
|
||||
const res: any = {};
|
||||
for (const [key, value] of Object.entries(core.status.hero)) {
|
||||
if (typeof value === 'number') {
|
||||
res[key] = getRealStatus(
|
||||
status,
|
||||
key as keyof HeroStatus,
|
||||
floorId
|
||||
);
|
||||
} else {
|
||||
res[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
let s = (status[name] ?? core.status.hero[name]) as number;
|
||||
if (s === null || s === void 0) {
|
||||
throw new ReferenceError(
|
||||
`Wrong hero status property name is delivered: ${name}`
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof s !== 'number') return s;
|
||||
|
||||
// 永夜、极昼
|
||||
if (name === 'atk' || name === 'def') {
|
||||
s += NightSpecial.getNight(floorId);
|
||||
}
|
||||
|
||||
const enabled = HeroSkill.getEnabled();
|
||||
// 技能
|
||||
if (enabled === HeroSkill.Blade) {
|
||||
const level = getSkillLevel(2);
|
||||
if (name === 'atk') {
|
||||
s *= 1 + 0.1 * level;
|
||||
} else if (name === 'def') {
|
||||
s *= 1 - 0.1 * level;
|
||||
}
|
||||
} else if (enabled === HeroSkill.Shield) {
|
||||
const level = getSkillLevel(10);
|
||||
if (name === 'atk') {
|
||||
s *= 1 - 0.1 * level;
|
||||
} else if (name === 'def') {
|
||||
s *= 1 + 0.1 * level;
|
||||
}
|
||||
}
|
||||
|
||||
// buff
|
||||
s *= core.status.hero.buff[name] ?? 1;
|
||||
s = Math.floor(s);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
export interface IHeroStatusDefault {
|
||||
atk: number;
|
||||
def: number;
|
||||
hp: number;
|
||||
}
|
||||
|
||||
interface HeroStateEvent {
|
||||
set: [key: string | number | symbol, value: any];
|
||||
}
|
||||
|
||||
type HeroStatusCalculate = (
|
||||
hero: HeroState<any>,
|
||||
key: string | number | symbol,
|
||||
value: any
|
||||
) => any;
|
||||
|
||||
export class HeroState<
|
||||
T extends object = IHeroStatusDefault
|
||||
> extends EventEmitter<HeroStateEvent> {
|
||||
readonly status: T;
|
||||
readonly computedStatus: T;
|
||||
|
||||
readonly buffable: Set<keyof T> = new Set();
|
||||
readonly buffMap: Map<keyof T, number> = new Map();
|
||||
|
||||
private static cal: HeroStatusCalculate = (_0, _1, value) => value;
|
||||
|
||||
constructor(init: T) {
|
||||
super();
|
||||
this.status = init;
|
||||
this.computedStatus = cloneDeep(init);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置某个属性的值
|
||||
* @param key 要设置的属性
|
||||
* @param value 属性值
|
||||
* @returns 是否设置成功
|
||||
*/
|
||||
setStatus<K extends keyof T>(key: K, value: T[K]): boolean {
|
||||
this.status[key] = value;
|
||||
this.emit('set', key, value);
|
||||
return this.refreshStatus(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加或减少一个属性的值,只对数字型的属性有效
|
||||
* @param key 要修改的属性
|
||||
* @param value 属性的增量
|
||||
* @returns 是否设置成功
|
||||
*/
|
||||
addStatus<K extends SelectKey<T, number>>(key: K, value: number): boolean {
|
||||
if (typeof this.status[key] !== 'number') {
|
||||
logger.warn(14, String(key));
|
||||
return false;
|
||||
}
|
||||
return this.setStatus<K>(key, (this.status[key] + value) as T[K]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个属性的原始值
|
||||
* @param key 要获取的属性
|
||||
* @returns 属性的值
|
||||
*/
|
||||
getStatus<K extends keyof T>(key: K): T[K] {
|
||||
return this.status[key];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个属性计算后的值,也就是2.x所说的勇士真实属性
|
||||
* @param key 要获取的属性值
|
||||
*/
|
||||
getComputedStatus<K extends keyof T>(key: K): T[K] {
|
||||
return this.computedStatus[key];
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记某个属性为可以被buff加成
|
||||
*/
|
||||
markBuffable(key: SelectKey<T, number>): void {
|
||||
if (typeof this.status[key] !== 'number') {
|
||||
logger.warn(12, String(key));
|
||||
return;
|
||||
}
|
||||
this.buffable.add(key);
|
||||
this.buffMap.set(key, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置某个属性的buff值
|
||||
* @param key 要设置buff的属性
|
||||
* @param value buff值
|
||||
* @returns 是否设置成功
|
||||
*/
|
||||
setBuff(key: SelectKey<T, number>, value: number): boolean {
|
||||
if (!this.buffable.has(key) || typeof this.status[key] !== 'number') {
|
||||
logger.warn(13, String(key));
|
||||
return false;
|
||||
}
|
||||
this.buffMap.set(key, value);
|
||||
return this.refreshStatus(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加或减少一个buff值
|
||||
* @param key 要修改的buff属性
|
||||
* @param value buff增量
|
||||
* @returns 是否修改成功
|
||||
*/
|
||||
addBuff(key: SelectKey<T, number>, value: number): boolean {
|
||||
if (!this.buffable.has(key) || typeof this.status[key] !== 'number') {
|
||||
logger.warn(13, String(key));
|
||||
return false;
|
||||
}
|
||||
return this.setBuff(key, this.buffMap.get(key)! + value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新某个或所有属性,重新进行计算
|
||||
* @param key 要刷新的属性名,不填表示刷新所有属性
|
||||
* @returns 是否计算成功
|
||||
*/
|
||||
refreshStatus(key?: keyof T): boolean {
|
||||
if (key === void 0) {
|
||||
for (const [key, value] of Object.entries(this.status)) {
|
||||
// @ts-ignore
|
||||
this.computedStatus[key] = HeroState.cal(this, key, value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
this.computedStatus[key] = HeroState.cal(this, key, this.status[key]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算所有可以buff加成的属性
|
||||
* @returns 是否计算成功
|
||||
*/
|
||||
refreshBuffable(): boolean {
|
||||
for (const key of this.buffable) {
|
||||
this.computedStatus[key] = HeroState.cal(
|
||||
this,
|
||||
key,
|
||||
this.status[key]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复写属性计算函数,默认函数不进行计算,直接将原属性返回
|
||||
* @param fn 计算函数,传入两个参数,key表示属性名,value表示属性值,返回值表示计算结果
|
||||
*/
|
||||
static overrideCalculate(fn: HeroStatusCalculate) {
|
||||
this.cal = fn;
|
||||
}
|
||||
}
|
||||
|
||||
interface IHeroItem {
|
||||
items: Map<AllIdsOf<'items'>, number>;
|
||||
|
||||
/**
|
||||
* 设置勇士拥有的物品数量
|
||||
* @param item 物品id
|
||||
* @param value 物品数量
|
||||
* @returns 是否设置成功
|
||||
*/
|
||||
setItem(item: AllIdsOf<'items'>, value: number): boolean;
|
||||
|
||||
/**
|
||||
* 增加或减少勇士拥有的物品数量
|
||||
* @param item 物品id
|
||||
* @param value 物品数量增量
|
||||
* @returns 是否设置成功
|
||||
*/
|
||||
addItem(item: AllIdsOf<'items'>, value: number): boolean;
|
||||
|
||||
/**
|
||||
* 使用一个物品
|
||||
* @param item 物品id
|
||||
* @returns 是否使用成功
|
||||
*/
|
||||
useItem(item: AllIdsOf<'items'>, x?: number, y?: number): boolean;
|
||||
|
||||
/**
|
||||
* 获得一个物品
|
||||
* @param item 物品id
|
||||
* @param num 获得的数量
|
||||
*/
|
||||
getItem(item: AllIdsOf<'items'>, num: number): void;
|
||||
/**
|
||||
* 获得一个物品
|
||||
* @param item 物品id
|
||||
* @param x 物品所在x坐标
|
||||
* @param y 物品所在y坐标
|
||||
* @param floorId 物品所在楼层
|
||||
* @param num 获得的数量
|
||||
*/
|
||||
getItem(
|
||||
item: AllIdsOf<'items'>,
|
||||
x: number,
|
||||
y: number,
|
||||
floorId?: FloorIds,
|
||||
num?: number
|
||||
): void;
|
||||
|
||||
/**
|
||||
* 获取某个物品的数量
|
||||
* @param item 物品id
|
||||
*/
|
||||
itemCount(item: AllIdsOf<'items'>): number;
|
||||
|
||||
/**
|
||||
* 判断勇士是否拥有这个物品
|
||||
* @param item 物品id
|
||||
*/
|
||||
hasItem(item: AllIdsOf<'items'>): boolean;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export * from './hero';
|
||||
export * from './interface';
|
||||
export * from './item';
|
||||
export * from './move';
|
||||
export * from './utils';
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface IStateDamageable {
|
||||
/** 生命值 */
|
||||
hp: number;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { loading } from '@user/data-base';
|
||||
|
||||
type EffectFn = () => void;
|
||||
type CanUseEffectFn = () => boolean;
|
||||
|
||||
interface ItemStateEvent {
|
||||
use: [];
|
||||
}
|
||||
|
||||
export class ItemState<
|
||||
I extends AllIdsOf<'items'> = AllIdsOf<'items'>
|
||||
> extends EventEmitter<ItemStateEvent> {
|
||||
static items: Map<AllIdsOf<'items'>, ItemState> = new Map();
|
||||
|
||||
id: I;
|
||||
cls: ItemClsOf<I>;
|
||||
name: string;
|
||||
text?: string;
|
||||
hideInToolBox: boolean;
|
||||
hideInReplay: boolean;
|
||||
|
||||
/** 即捡即用效果 */
|
||||
itemEffect?: string;
|
||||
/** 即捡即用道具捡过之后的提示 */
|
||||
itemEffectTip?: string;
|
||||
/** 使用道具时执行的事件 */
|
||||
useItemEvent?: MotaEvent;
|
||||
/** 使用道具时执行的代码 */
|
||||
useItemEffect?: string;
|
||||
/** 能否使用道具 */
|
||||
canUseItemEffect?: string | boolean;
|
||||
|
||||
private noRoute: boolean = false;
|
||||
|
||||
itemEffectFn?: EffectFn;
|
||||
useItemEffectFn?: EffectFn;
|
||||
canUseItemEffectFn?: CanUseEffectFn;
|
||||
|
||||
constructor(id: I) {
|
||||
super();
|
||||
const items = items_296f5d02_12fd_4166_a7c1_b5e830c9ee3a;
|
||||
this.id = id;
|
||||
const item = items[id];
|
||||
this.cls = item.cls;
|
||||
this.name = item.name;
|
||||
this.text = item.text;
|
||||
this.hideInToolBox = item.hideInToolBox;
|
||||
this.hideInReplay = item.hideInReplay;
|
||||
this.itemEffect = item.itemEffect;
|
||||
this.itemEffectTip = item.itemEffectTip;
|
||||
this.useItemEvent = item.useItemEvent;
|
||||
this.useItemEffect = item.useItemEffect;
|
||||
this.canUseItemEffect = item.canUseItemEffect;
|
||||
|
||||
this.compileFunction();
|
||||
}
|
||||
|
||||
private compileFunction() {
|
||||
if (this.itemEffect) {
|
||||
this.itemEffectFn = new Function(
|
||||
`state`,
|
||||
this.itemEffect
|
||||
) as EffectFn;
|
||||
}
|
||||
if (this.useItemEffect) {
|
||||
this.useItemEffectFn = new Function(
|
||||
`state`,
|
||||
this.useItemEffect
|
||||
) as EffectFn;
|
||||
}
|
||||
if (this.canUseItemEffect) {
|
||||
if (typeof this.canUseItemEffect === 'boolean') {
|
||||
this.canUseItemEffectFn = () =>
|
||||
this.canUseItemEffect as boolean;
|
||||
} else {
|
||||
this.useItemEffectFn = new Function(
|
||||
`state`,
|
||||
this.canUseItemEffect
|
||||
) as CanUseEffectFn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个道具的信息
|
||||
* @param id 要获取的道具id
|
||||
*/
|
||||
static item<I extends AllIdsOf<'items'>>(id: I): ItemState<I> | undefined {
|
||||
return this.items.get(id) as ItemState<I>;
|
||||
}
|
||||
}
|
||||
|
||||
loading.once('coreInit', () => {
|
||||
for (const key of Object.keys(items_296f5d02_12fd_4166_a7c1_b5e830c9ee3a)) {
|
||||
ItemState.items.set(
|
||||
key as AllIdsOf<'items'>,
|
||||
new ItemState(key as AllIdsOf<'items'>)
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,959 @@
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { backDir, checkCanMoveExtended, toDir } from './utils';
|
||||
import { loading } from '@user/data-base';
|
||||
import type {
|
||||
RenderAdapter,
|
||||
HeroRenderer,
|
||||
FloorViewport,
|
||||
FloorLayer,
|
||||
Layer,
|
||||
LayerGroup,
|
||||
LayerMovingRenderable,
|
||||
LayerFloorBinder
|
||||
} from '@motajs/render';
|
||||
import type { HeroKeyMover } from '@/module/action/move';
|
||||
import { BluePalace, MiscData } from '../mechanism/misc';
|
||||
import { sleep } from '@motajs/common';
|
||||
|
||||
interface MoveStepDir {
|
||||
type: 'dir';
|
||||
value: Move2;
|
||||
}
|
||||
|
||||
interface MoveStepSpeed {
|
||||
type: 'speed';
|
||||
value: number;
|
||||
}
|
||||
|
||||
export type MoveStep = MoveStepDir | MoveStepSpeed;
|
||||
|
||||
export interface IMoveController {
|
||||
/** 本次移动是否完成 */
|
||||
done: boolean;
|
||||
/** 当前移动队列 */
|
||||
queue: MoveStep[];
|
||||
/** 当本次移动完成时兑现 */
|
||||
onEnd: Promise<void>;
|
||||
|
||||
/**
|
||||
* 停止本次移动,停止后将不再能继续移动
|
||||
*/
|
||||
stop(): Promise<void>;
|
||||
|
||||
/**
|
||||
* 向队列末尾添加移动信息
|
||||
* @param step 移动信息
|
||||
*/
|
||||
push(...step: MoveStep[]): void;
|
||||
}
|
||||
|
||||
interface EObjectMovingEvent {
|
||||
stepEnd: [step: MoveStep];
|
||||
moveEnd: [];
|
||||
moveStart: [queue: MoveStep[]];
|
||||
}
|
||||
|
||||
export abstract class ObjectMoverBase extends EventEmitter<EObjectMovingEvent> {
|
||||
/** 移动队列 */
|
||||
private moveQueue: MoveStep[] = [];
|
||||
|
||||
/** 当前的移动速度 */
|
||||
moveSpeed: number = 100;
|
||||
/** 当前的移动方向 */
|
||||
moveDir: Dir2 = 'down';
|
||||
/** 当前是否正在移动 */
|
||||
moving: boolean = false;
|
||||
/** 面朝方向 */
|
||||
faceDir: Dir2 = 'down';
|
||||
|
||||
/** 当前的控制对象 */
|
||||
controller?: IMoveController;
|
||||
|
||||
/**
|
||||
* 当本次移动开始时执行的函数
|
||||
* @param controller 本次移动的控制对象
|
||||
*/
|
||||
protected abstract onMoveStart(controller: IMoveController): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当本次移动结束时执行的函数
|
||||
* @param controller 本次移动的控制对象
|
||||
*/
|
||||
protected abstract onMoveEnd(controller: IMoveController): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当这一步开始移动时执行的函数,可以用来播放移动动画等
|
||||
* @param step 这一步的移动信息
|
||||
* @param controller 本次移动的控制对象
|
||||
* @returns 一个Promise,其值表示本次移动的代码,并将其值传入这一步移动后的操作,即{@link onStepEnd}
|
||||
*/
|
||||
protected abstract onStepStart(
|
||||
step: MoveStepDir,
|
||||
controller: IMoveController
|
||||
): Promise<number>;
|
||||
|
||||
/**
|
||||
* 当这一步移动结束后执行的函数(即{@link onStepStart}成功兑现后),可以用于设置物体坐标等
|
||||
* @param step 这一步的移动信息
|
||||
* @param code 本步移动的代码,即onMoveStart的返回值
|
||||
* @param controller 本次移动的控制对象
|
||||
* @returns 一个Promise,当其兑现时,本步移动全部完成,将会开始下一步的移动
|
||||
*/
|
||||
protected abstract onStepEnd(
|
||||
step: MoveStepDir,
|
||||
code: number,
|
||||
controller: IMoveController
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当移动速度被设置时执行的函数
|
||||
* @param speed 设置为的移动速度
|
||||
* @param controller 本次移动的控制对象
|
||||
*/
|
||||
protected abstract onSetMoveSpeed(
|
||||
speed: number,
|
||||
controller: IMoveController
|
||||
): void;
|
||||
|
||||
private getMoveDir(move: Move2): Dir2 {
|
||||
if (move === 'forward') return this.moveDir;
|
||||
if (move === 'backward') return backDir(this.faceDir);
|
||||
return move;
|
||||
}
|
||||
|
||||
private getFaceDir(move: Move2): Dir2 {
|
||||
if (move === 'forward' || move === 'backward') return this.faceDir;
|
||||
return move;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前面朝方向,移动中设置无效
|
||||
* @param dir 面朝方向
|
||||
*/
|
||||
setFaceDir(dir: Dir2) {
|
||||
if (!this.moving) this.faceDir = dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前移动方向,移动中设置无效
|
||||
* @param dir 移动方向
|
||||
*/
|
||||
setMoveDir(dir: Dir2) {
|
||||
if (!this.moving) this.moveDir = dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始移动,这个操作后,本次移动将不能通过{@link insertMove}添加移动步骤,
|
||||
* 只能通过{@link IMoveController.push}添加,而通过前者添加的移动步骤会在下次移动生效
|
||||
*/
|
||||
startMove(): IMoveController | null {
|
||||
if (this.moving) return null;
|
||||
const queue = this.moveQueue.slice();
|
||||
this.clearMoveQueue();
|
||||
|
||||
this.moving = true;
|
||||
let stopMove = false;
|
||||
|
||||
const start = async () => {
|
||||
// 等待宏任务执行完成,不然controller会在首次调用中未定义
|
||||
await new Promise<void>(res => res());
|
||||
await this.onMoveStart(controller);
|
||||
this.emit('moveStart', queue);
|
||||
while (queue.length > 0) {
|
||||
if (stopMove || !this.moving) break;
|
||||
const step = queue.shift();
|
||||
if (!step) break;
|
||||
if (step.type === 'dir') {
|
||||
this.moveDir = this.getMoveDir(step.value);
|
||||
this.faceDir = this.getFaceDir(step.value);
|
||||
const code = await this.onStepStart(step, controller);
|
||||
await this.onStepEnd(step, code, controller);
|
||||
} else {
|
||||
const speed = step.value;
|
||||
this.moveSpeed = speed;
|
||||
this.onSetMoveSpeed(speed, controller);
|
||||
}
|
||||
this.emit('stepEnd', step);
|
||||
}
|
||||
this.moving = false;
|
||||
this.emit('moveEnd');
|
||||
|
||||
await this.onMoveEnd(controller);
|
||||
};
|
||||
|
||||
const onEnd = start().then(() => {
|
||||
this.controller = void 0;
|
||||
controller.done = true;
|
||||
});
|
||||
const controller: IMoveController = {
|
||||
done: false,
|
||||
onEnd,
|
||||
queue,
|
||||
push(...step) {
|
||||
queue.push(...step);
|
||||
},
|
||||
stop() {
|
||||
stopMove = true;
|
||||
return onEnd;
|
||||
}
|
||||
};
|
||||
this.controller = controller;
|
||||
|
||||
return controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向移动队列末尾插入移动实例
|
||||
* @param move 移动实例
|
||||
*/
|
||||
insertMove(...move: MoveStep[]) {
|
||||
this.moveQueue.push(...move);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空移动队列
|
||||
*/
|
||||
clearMoveQueue() {
|
||||
this.moveQueue = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 向移动队列末尾插入一个移动操作
|
||||
* @param step 移动方向
|
||||
*/
|
||||
oneStep(step: Move2) {
|
||||
this.moveQueue.push({ type: 'dir', value: step });
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照传入的数组移动物体,插入至移动队列末尾
|
||||
* @param steps 移动步骤
|
||||
*/
|
||||
moveAs(steps: MoveStep[]) {
|
||||
this.moveQueue.push(...steps);
|
||||
}
|
||||
}
|
||||
|
||||
const enum BlockMoveCode {
|
||||
Step
|
||||
}
|
||||
|
||||
export class BlockMover extends ObjectMoverBase {
|
||||
/** 楼层渲染适配器,用于显示动画 */
|
||||
static adapter?: RenderAdapter<Layer>;
|
||||
|
||||
x: number;
|
||||
y: number;
|
||||
floorId: FloorIds;
|
||||
layer: FloorLayer;
|
||||
|
||||
/** 本次移动中需要进行动画移动的楼层渲染组件 */
|
||||
private layerItems: Layer[] = [];
|
||||
/** 本次移动过程中的移动renderable实例 */
|
||||
private renderable?: LayerMovingRenderable;
|
||||
/** 本次移动的图块id */
|
||||
private blockNum: number = 0;
|
||||
|
||||
constructor(
|
||||
x: number,
|
||||
y: number,
|
||||
floorId: FloorIds,
|
||||
layer: FloorLayer,
|
||||
dir: Dir = 'down'
|
||||
) {
|
||||
super();
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.floorId = floorId;
|
||||
this.moveDir = dir;
|
||||
this.layer = layer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定移动点
|
||||
* @param x 绑定点横坐标
|
||||
* @param y 绑定点纵坐标
|
||||
* @param floorId 绑定点楼层
|
||||
* @returns 是否绑定成功,例如如果当前绑定点正在移动,那么就会绑定失败
|
||||
*/
|
||||
bind(
|
||||
x: number,
|
||||
y: number,
|
||||
floorId: FloorIds,
|
||||
layer: FloorLayer,
|
||||
dir: Dir = 'down'
|
||||
) {
|
||||
if (this.moving) return false;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.floorId = floorId;
|
||||
this.moveDir = dir;
|
||||
this.layer = layer;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected async onMoveStart(_controller: IMoveController): Promise<void> {
|
||||
const adapter = BlockMover.adapter;
|
||||
if (adapter) {
|
||||
const list = adapter.items;
|
||||
const items = [...list].filter(v => {
|
||||
if (v.layer !== this.layer) return false;
|
||||
const ex = v.getExtends('floor-binder') as LayerFloorBinder;
|
||||
if (!ex) return false;
|
||||
return ex.getFloor() === core.status.floorId;
|
||||
});
|
||||
this.layerItems = items;
|
||||
}
|
||||
|
||||
let blockNum: number = 0;
|
||||
if (this.layer === 'event') {
|
||||
blockNum = core.status.maps[this.floorId].map[this.y][this.x];
|
||||
} else {
|
||||
const array = core.maps._getBgFgMapArray(this.layer, this.floorId);
|
||||
blockNum = array[this.y][this.x];
|
||||
}
|
||||
this.blockNum = blockNum;
|
||||
|
||||
Mota.r(() => {
|
||||
const { Layer } = Mota.require('@motajs/render');
|
||||
const r = Layer.getMovingRenderable(blockNum, this.x, this.y);
|
||||
|
||||
if (r) {
|
||||
this.renderable = r;
|
||||
this.layerItems.forEach(v => {
|
||||
v.moving.add(r);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (this.layer === 'event') {
|
||||
core.removeBlock(this.x, this.y, this.floorId);
|
||||
}
|
||||
}
|
||||
|
||||
protected async onMoveEnd(_controller: IMoveController): Promise<void> {
|
||||
if (this.renderable) {
|
||||
this.layerItems.forEach(v => {
|
||||
v.moving.delete(this.renderable!);
|
||||
});
|
||||
}
|
||||
|
||||
this.layerItems = [];
|
||||
this.renderable = void 0;
|
||||
|
||||
if (this.layer === 'event') {
|
||||
core.setBlock(this.blockNum as AllNumbers, this.x, this.y);
|
||||
}
|
||||
}
|
||||
|
||||
protected async onStepStart(
|
||||
step: MoveStepDir,
|
||||
_controller: IMoveController
|
||||
): Promise<BlockMoveCode> {
|
||||
await this.moveAnimate(step);
|
||||
const { x: dx, y: dy } = core.utils.scan2[this.moveDir];
|
||||
this.x += dx;
|
||||
this.y += dy;
|
||||
|
||||
return BlockMoveCode.Step;
|
||||
}
|
||||
|
||||
protected async onStepEnd(
|
||||
_step: MoveStepDir,
|
||||
_code: BlockMoveCode,
|
||||
_controller: IMoveController
|
||||
): Promise<void> {}
|
||||
|
||||
protected onSetMoveSpeed(
|
||||
_speed: number,
|
||||
_controller: IMoveController
|
||||
): void {}
|
||||
|
||||
private moveAnimate(_step: MoveStepDir) {
|
||||
const layer = this.layerItems[0];
|
||||
if (!layer) return;
|
||||
if (!this.renderable) return;
|
||||
const data = this.renderable;
|
||||
const fx = this.x;
|
||||
const fy = this.y;
|
||||
const { x: dx, y: dy } = core.utils.scan2[this.moveDir];
|
||||
const start = Date.now();
|
||||
const replay = core.status.replay.speed ?? 1;
|
||||
const time = replay === 24 ? 1 : this.moveSpeed / replay;
|
||||
|
||||
return new Promise<void>(res => {
|
||||
layer.delegateTicker(
|
||||
() => {
|
||||
const now = Date.now() - start;
|
||||
const progress = now / time;
|
||||
data.x = fx + dx * progress;
|
||||
data.y = fy + dy * progress;
|
||||
this.layerItems.forEach(v => {
|
||||
v.update(v);
|
||||
});
|
||||
},
|
||||
this.moveSpeed,
|
||||
() => {
|
||||
data.x = fx + dx;
|
||||
data.y = fy + dy;
|
||||
data.zIndex = fy + dy;
|
||||
res();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
interface CanMoveStatus {
|
||||
/** 由CannotIn和CannotOut计算出的信息,不可移动时不会触发触发器 */
|
||||
canMove: boolean;
|
||||
/** 由图块的noPass计算出的信息,不可移动时会触发触发器 */
|
||||
noPass: boolean;
|
||||
}
|
||||
|
||||
interface PortalStatus {
|
||||
/** 下一步是否会步入传送门 */
|
||||
portal: boolean;
|
||||
/** 传送门会传到哪 */
|
||||
data?: BluePalace.PortalTo;
|
||||
}
|
||||
|
||||
const enum HeroMoveCode {
|
||||
Step,
|
||||
Stop,
|
||||
/** 不能移动,并撞击前面一格的图块,触发其触发器 */
|
||||
Hit,
|
||||
/** 不能移动,同时当前格有CannotOut,或目标格有CannotIn,不会触发前面一格的触发器 */
|
||||
CannotMove,
|
||||
/** 进入传送门 */
|
||||
Portal,
|
||||
/** 循环式地图 */
|
||||
Loop,
|
||||
/** 循环式地图撞击 */
|
||||
LoopHit
|
||||
}
|
||||
|
||||
export class HeroMover extends ObjectMoverBase {
|
||||
/** 勇士渲染适配器,用于等待动画等操作 */
|
||||
static adapter?: RenderAdapter<HeroRenderer>;
|
||||
/** 视角适配器 */
|
||||
static viewport?: RenderAdapter<FloorViewport>;
|
||||
|
||||
/** 当前移动是否忽略地形 */
|
||||
private ignoreTerrain: boolean = false;
|
||||
/** 当前移动是否不计入录像 */
|
||||
private noRoute: boolean = false;
|
||||
/** 当前移动是否是在lockControl条件下开始的 */
|
||||
private inLockControl: boolean = false;
|
||||
/** 是否会在特殊时刻进行自动存档 */
|
||||
private autoSave: boolean = false;
|
||||
|
||||
/** 本次移动开始时的移动速度 */
|
||||
private beforeMoveSpeed: number = 100;
|
||||
|
||||
/** 这一步的传送门信息 */
|
||||
private portalData?: BluePalace.PortalTo;
|
||||
|
||||
override startMove(
|
||||
ignoreTerrain: boolean = false,
|
||||
noRoute: boolean = false,
|
||||
inLockControl: boolean = false,
|
||||
autoSave: boolean = false
|
||||
): IMoveController | null {
|
||||
if (this.moving) return null;
|
||||
this.ignoreTerrain = ignoreTerrain;
|
||||
this.noRoute = noRoute;
|
||||
this.inLockControl = inLockControl;
|
||||
this.autoSave = autoSave;
|
||||
return super.startMove();
|
||||
}
|
||||
|
||||
private checkAutoSave(x: number, y: number, nx: number, ny: number) {
|
||||
const index = `${x},${y}`;
|
||||
const nIndex = `${nx},${ny}`;
|
||||
const map = core.status.thisMap.enemy.mapDamage;
|
||||
const dam = map[index];
|
||||
const nextDam = map[nIndex];
|
||||
if (nextDam?.mockery || (!dam?.hunt && nextDam?.hunt)) {
|
||||
core.autosave();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
protected async onMoveStart(controller: IMoveController): Promise<void> {
|
||||
this.beforeMoveSpeed = this.moveSpeed;
|
||||
const adapter = HeroMover.adapter;
|
||||
const viewport = HeroMover.viewport;
|
||||
if (!adapter || !viewport) return;
|
||||
if (!core.isReplaying() || core.status.replay.speed <= 3) {
|
||||
adapter.sync('startAnimate');
|
||||
await adapter.all('readyMove');
|
||||
}
|
||||
// 这里要检查前面那一格能不能走,不能走则不触发平滑视角,以避免撞墙上视角卡住
|
||||
if (!this.ignoreTerrain) {
|
||||
const { x, y } = core.status.hero.loc;
|
||||
const firstDir = controller.queue.find(
|
||||
v => v.type === 'dir'
|
||||
)?.value;
|
||||
if (firstDir && firstDir !== 'backward' && firstDir !== 'forward') {
|
||||
const data = this.checkCanMove(x, y, toDir(firstDir as Dir));
|
||||
if (data.canMove && !data.noPass) {
|
||||
viewport.sync('startMove');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
viewport.sync('startMove');
|
||||
}
|
||||
}
|
||||
|
||||
protected async onMoveEnd(controller: IMoveController): Promise<void> {
|
||||
this.moveSpeed = this.beforeMoveSpeed;
|
||||
this.onSetMoveSpeed(this.moveSpeed, controller);
|
||||
const adapter = HeroMover.adapter;
|
||||
const viewport = HeroMover.viewport;
|
||||
if (!adapter || !viewport) return;
|
||||
adapter.sync('endAnimate');
|
||||
await adapter.all('endMove');
|
||||
viewport.sync('endMove');
|
||||
core.clearContinueAutomaticRoute();
|
||||
core.stopAutomaticRoute();
|
||||
}
|
||||
|
||||
protected async onStepStart(
|
||||
_step: MoveStepDir,
|
||||
controller: IMoveController
|
||||
): Promise<HeroMoveCode> {
|
||||
const showDir = toDir(this.faceDir);
|
||||
const dir4Move = toDir(this.moveDir);
|
||||
core.setHeroLoc('direction', showDir);
|
||||
|
||||
const { x, y } = core.status.hero.loc;
|
||||
const { x: nx, y: ny } = this.nextLoc(x, y, this.moveDir);
|
||||
|
||||
if (this.autoSave && !this.ignoreTerrain) {
|
||||
this.checkAutoSave(x, y, nx, ny);
|
||||
}
|
||||
|
||||
if (!this.inLockControl && core.status.lockControl) {
|
||||
controller.stop();
|
||||
return HeroMoveCode.Stop;
|
||||
}
|
||||
|
||||
if (!this.ignoreTerrain || !this.noRoute) {
|
||||
this.moveDir = dir4Move;
|
||||
}
|
||||
|
||||
// 检查传送门
|
||||
if (!this.ignoreTerrain) {
|
||||
const { portal, data } = this.checkPortal(x, y, dir4Move);
|
||||
if (portal && data) {
|
||||
this.portalData = data;
|
||||
await this.renderHeroSwap(data);
|
||||
return HeroMoveCode.Portal;
|
||||
}
|
||||
}
|
||||
|
||||
const dir = this.moveDir;
|
||||
if (!this.ignoreTerrain) {
|
||||
const { noPass, canMove } = this.checkCanMove(x, y, dir4Move);
|
||||
|
||||
if (!canMove) {
|
||||
return HeroMoveCode.CannotMove;
|
||||
}
|
||||
// 循环式地图
|
||||
const floorId = core.status.floorId;
|
||||
if (MiscData.loopMaps.has(core.status.floorId)) {
|
||||
const floor = core.status.maps[floorId];
|
||||
const width = floor.width;
|
||||
if (
|
||||
(x === 0 && dir === 'left') ||
|
||||
(x === width - 1 && dir === 'right')
|
||||
) {
|
||||
if (noPass) {
|
||||
return HeroMoveCode.LoopHit;
|
||||
}
|
||||
await Promise.all([
|
||||
this.renderHeroLoop(),
|
||||
this.moveAnimate(nx, ny, showDir, dir)
|
||||
]);
|
||||
return HeroMoveCode.Loop;
|
||||
}
|
||||
}
|
||||
// 不能移动
|
||||
if (noPass) {
|
||||
return HeroMoveCode.Hit;
|
||||
}
|
||||
}
|
||||
|
||||
// 可以移动,显示移动动画
|
||||
|
||||
await this.moveAnimate(nx, ny, showDir, dir);
|
||||
|
||||
return HeroMoveCode.Step;
|
||||
}
|
||||
|
||||
protected async onStepEnd(
|
||||
_step: MoveStepDir,
|
||||
code: HeroMoveCode,
|
||||
controller: IMoveController
|
||||
): Promise<void> {
|
||||
const { x, y } = core.status.hero.loc;
|
||||
const { x: nx, y: ny } = this.nextLoc(x, y, this.moveDir);
|
||||
const showDir = toDir(this.faceDir);
|
||||
|
||||
// 前方不能移动
|
||||
if (
|
||||
code === HeroMoveCode.CannotMove ||
|
||||
code === HeroMoveCode.Hit ||
|
||||
code === HeroMoveCode.LoopHit
|
||||
) {
|
||||
controller.stop();
|
||||
this.onCannotMove(showDir);
|
||||
if (code === HeroMoveCode.Hit) {
|
||||
core.trigger(nx, ny);
|
||||
} else if (code === HeroMoveCode.LoopHit) {
|
||||
const floor = core.status.thisMap;
|
||||
if (x === 0) core.trigger(floor.width - 1, y);
|
||||
else core.trigger(0, y);
|
||||
}
|
||||
core.checkRouteFolding();
|
||||
return;
|
||||
}
|
||||
|
||||
// 本次移动停止
|
||||
if (code === HeroMoveCode.Stop) {
|
||||
core.clearContinueAutomaticRoute();
|
||||
core.stopAutomaticRoute();
|
||||
controller.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// 本次移动正常完成
|
||||
if (
|
||||
code === HeroMoveCode.Step ||
|
||||
code === HeroMoveCode.Portal ||
|
||||
code === HeroMoveCode.Loop
|
||||
) {
|
||||
if (code === HeroMoveCode.Portal) {
|
||||
const data = this.portalData;
|
||||
if (!data) return;
|
||||
core.setHeroLoc('x', data.x);
|
||||
core.setHeroLoc('y', data.y);
|
||||
core.setHeroLoc('direction', data.dir);
|
||||
} else if (code === HeroMoveCode.Loop) {
|
||||
const map = core.status.thisMap;
|
||||
if (x === 0) core.setHeroLoc('x', map.width - 1);
|
||||
else core.setHeroLoc('x', 0);
|
||||
} else {
|
||||
core.setHeroLoc('x', nx, true);
|
||||
core.setHeroLoc('y', ny, true);
|
||||
}
|
||||
|
||||
if (!this.ignoreTerrain) {
|
||||
const direction = core.getHeroLoc('direction');
|
||||
core.control._moveAction_popAutomaticRoute();
|
||||
if (!this.noRoute) core.status.route.push(direction);
|
||||
|
||||
core.moveOneStep();
|
||||
core.checkRouteFolding();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected onSetMoveSpeed(
|
||||
speed: number,
|
||||
_controller: IMoveController
|
||||
): void {
|
||||
const adapter = HeroMover.adapter;
|
||||
if (!adapter) return;
|
||||
adapter.sync('setMoveSpeed', speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动动画
|
||||
* @param x 目标横坐标
|
||||
* @param y 目标纵坐标
|
||||
* @param showDir 显示方向
|
||||
* @param moveDir 移动方向
|
||||
*/
|
||||
private async moveAnimate(
|
||||
x: number,
|
||||
y: number,
|
||||
showDir: Dir,
|
||||
moveDir: Dir2
|
||||
) {
|
||||
const adapter = HeroMover.adapter;
|
||||
const viewport = HeroMover.viewport;
|
||||
if (!adapter || !viewport) return;
|
||||
const replay = core.status.replay.speed;
|
||||
const speed = replay === 24 ? 1 : this.moveSpeed / replay;
|
||||
viewport.all('moveTo', x, y, speed * 1.6);
|
||||
adapter.sync('setAnimateDir', showDir);
|
||||
if (core.isReplaying() && core.status.replay.speed > 3) {
|
||||
adapter.sync('endAnimate');
|
||||
await sleep(speed);
|
||||
await adapter.all('setHeroLoc', x, y);
|
||||
} else {
|
||||
if (core.isReplaying()) {
|
||||
adapter.sync('startAnimate');
|
||||
}
|
||||
await adapter.all('move', moveDir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前方一格不能走时,执行的函数
|
||||
* @param dir 移动方向
|
||||
*/
|
||||
private onCannotMove(dir: Dir) {
|
||||
if (!this.noRoute) core.status.route.push(dir);
|
||||
core.status.automaticRoute.moveStepBeforeStop = [];
|
||||
core.status.automaticRoute.lastDirection = dir;
|
||||
|
||||
if (core.status.automaticRoute.moveStepBeforeStop.length == 0) {
|
||||
core.clearContinueAutomaticRoute();
|
||||
core.stopAutomaticRoute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查前方是否可以移动
|
||||
* @param x 当前横坐标
|
||||
* @param y 当前纵坐标
|
||||
* @param dir 移动方向
|
||||
*/
|
||||
private checkCanMove(x: number, y: number, dir: Dir): CanMoveStatus {
|
||||
// 如果是循环式地图
|
||||
const floorId = core.status.floorId;
|
||||
if (MiscData.loopMaps.has(floorId)) {
|
||||
const floor = core.status.maps[floorId];
|
||||
const width = floor.width;
|
||||
if (x === 0 && dir === 'left') {
|
||||
const noPass = core.noPass(width - 1, y);
|
||||
const move = checkCanMoveExtended(0, y, width - 1, y, 'left');
|
||||
return { noPass, canMove: move };
|
||||
} else if (x === width - 1 && dir === 'right') {
|
||||
const noPass = core.noPass(0, y);
|
||||
const move = checkCanMoveExtended(width - 1, y, 0, y, 'right');
|
||||
return { noPass, canMove: move };
|
||||
}
|
||||
}
|
||||
const { x: nx, y: ny } = this.nextLoc(x, y, dir);
|
||||
const noPass = core.noPass(nx, ny);
|
||||
const canMove = core.canMoveHero(x, y, dir);
|
||||
return { noPass, canMove };
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前方一格的坐标
|
||||
* @param x 当前横坐标
|
||||
* @param y 当前纵坐标
|
||||
* @param dir 移动方向
|
||||
*/
|
||||
private nextLoc(x: number, y: number, dir: Dir2): Loc {
|
||||
const { x: dx, y: dy } = core.utils.scan2[dir];
|
||||
const nx = x + dx;
|
||||
const ny = y + dy;
|
||||
return { x: nx, y: ny };
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查前方一格是否会步入传送门
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
* @param dir 移动方向
|
||||
*/
|
||||
private checkPortal(x: number, y: number, dir: Dir): PortalStatus {
|
||||
const map = BluePalace.portalMap.get(core.status.floorId);
|
||||
if (!map) {
|
||||
return { portal: false };
|
||||
}
|
||||
const width = core.status.thisMap.width;
|
||||
const index = x + y * width;
|
||||
const data = map?.get(index);
|
||||
if (!data) {
|
||||
return { portal: false };
|
||||
}
|
||||
const to = data[dir];
|
||||
if (to) {
|
||||
return { portal: true, data: to };
|
||||
}
|
||||
return { portal: false };
|
||||
}
|
||||
|
||||
private renderHeroSwap(data: BluePalace.PortalTo) {
|
||||
const adapter = HeroMover.adapter;
|
||||
if (!adapter) return;
|
||||
const list = adapter.items;
|
||||
const { x: tx, y: ty, dir: toDir } = data;
|
||||
const { x, y, direction } = core.status.hero.loc;
|
||||
const { x: dx } = core.utils.scan[direction];
|
||||
const { x: tdx } = core.utils.scan[toDir];
|
||||
|
||||
const promises = [...list].map(v => {
|
||||
if (!v.renderable) return;
|
||||
const renderable = { ...v.renderable };
|
||||
renderable.render = v.getRenderFromDir(toDir);
|
||||
renderable.zIndex = ty;
|
||||
const heroDir = v.moveDir;
|
||||
|
||||
const width = v.renderable.render[0][2];
|
||||
const height = v.renderable.render[0][3];
|
||||
const cell = v.layer.cellSize;
|
||||
const restHeight = height - cell;
|
||||
if (!width || !height) return;
|
||||
|
||||
const originFrom = structuredClone(v.renderable.render);
|
||||
const originTo = structuredClone(renderable.render);
|
||||
v.layer.moving.add(renderable);
|
||||
v.layer.requestUpdateMoving();
|
||||
|
||||
const start = Date.now();
|
||||
return new Promise<void>(res => {
|
||||
const tick = () => {
|
||||
const now = Date.now();
|
||||
const progress = (now - start) / this.moveSpeed;
|
||||
const clipWidth = cell * progress;
|
||||
const clipHeight = cell * progress;
|
||||
const beforeWidth = width - clipWidth;
|
||||
const beforeHeight = height - clipHeight;
|
||||
|
||||
v.renderable!.x = x;
|
||||
v.renderable!.y = y;
|
||||
if (heroDir === 'left' || heroDir === 'right') {
|
||||
v.renderable!.x = x + (clipWidth / 2 / cell) * dx;
|
||||
v.renderable!.render.forEach((v, i) => {
|
||||
v[2] = beforeWidth;
|
||||
if (heroDir === 'left') {
|
||||
v[0] = originFrom[i][0] + clipWidth;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
v.renderable!.render.forEach((v, i) => {
|
||||
v[3] = beforeHeight;
|
||||
if (heroDir === 'up') {
|
||||
v[1] =
|
||||
originFrom[i][1] + clipHeight + restHeight;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
renderable.x = tx;
|
||||
renderable.y = ty;
|
||||
if (toDir === 'left' || toDir === 'right') {
|
||||
renderable.x = tx + (clipWidth / 2 / cell - 0.5) * tdx;
|
||||
renderable.render.forEach((v, i) => {
|
||||
v[2] = clipWidth;
|
||||
if (toDir === 'right') {
|
||||
v[0] = originTo[i][0] + beforeWidth;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (toDir === 'down') renderable.y = ty - 1 + progress;
|
||||
renderable.render.forEach((v, i) => {
|
||||
v[3] = clipHeight + restHeight;
|
||||
if (toDir === 'down') {
|
||||
v[1] = originTo[i][1] + clipHeight + restHeight;
|
||||
v[3] = clipHeight;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
v.layer.delegateTicker(tick, this.moveSpeed, () => {
|
||||
v.renderable!.render = originFrom;
|
||||
v.setAnimateDir(data.dir);
|
||||
v.layer.moving.delete(renderable);
|
||||
v.layer.requestUpdateMoving();
|
||||
res();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
private renderHeroLoop() {
|
||||
const adapter = HeroMover.adapter;
|
||||
const viewport = HeroMover.viewport;
|
||||
if (!adapter || !viewport) return;
|
||||
const MotaRenderer = Mota.require('@motajs/render').MotaRenderer;
|
||||
const render = MotaRenderer.get('render-main');
|
||||
const group = render?.getElementById('layer-loop') as LayerGroup;
|
||||
const layer = group?.getLayer('event');
|
||||
const mainGroup = render?.getElementById('layer-main') as LayerGroup;
|
||||
const mainLayer = mainGroup?.getLayer('event');
|
||||
const hero = mainLayer?.getExtends('floor-hero') as HeroRenderer;
|
||||
const renderable = hero?.renderable;
|
||||
if (!layer || !hero || !renderable) return;
|
||||
const { x, y } = core.status.hero.loc;
|
||||
const width = core.status.thisMap.width;
|
||||
const loopHero = { ...renderable };
|
||||
layer.moving.add(loopHero);
|
||||
|
||||
let target: number;
|
||||
let from: number;
|
||||
if (x === 0) {
|
||||
from = width;
|
||||
target = width - 1;
|
||||
} else {
|
||||
from = -1;
|
||||
target = 0;
|
||||
}
|
||||
const delta = target - from;
|
||||
loopHero.x = from;
|
||||
|
||||
layer.requestUpdateMoving();
|
||||
|
||||
const startTime = Date.now();
|
||||
return new Promise<void>(res => {
|
||||
layer.delegateTicker(
|
||||
() => {
|
||||
const progress = (Date.now() - startTime) / this.moveSpeed;
|
||||
const dx = delta * progress;
|
||||
loopHero.x = dx + from;
|
||||
layer.update(layer);
|
||||
},
|
||||
this.moveSpeed,
|
||||
() => {
|
||||
layer.moving.delete(loopHero);
|
||||
layer.requestUpdateMoving();
|
||||
viewport.all('setPosition', x === 0 ? width - 1 : 0, y);
|
||||
res();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
interface HeroMoveCollection {
|
||||
mover: HeroMover;
|
||||
keyMover?: HeroKeyMover;
|
||||
}
|
||||
|
||||
// 初始化基础勇士移动实例
|
||||
const heroMover = new HeroMover();
|
||||
export const heroMoveCollection: HeroMoveCollection = {
|
||||
mover: heroMover
|
||||
};
|
||||
loading.once('coreInit', () => {
|
||||
// 注册按键操作
|
||||
Mota.r(() => {
|
||||
const { HeroKeyMover } = Mota.require('@motajs/system-action');
|
||||
const { gameKey } = Mota.require('@motajs/system-action');
|
||||
const keyMover = new HeroKeyMover(gameKey, heroMover);
|
||||
heroMoveCollection.keyMover = keyMover;
|
||||
});
|
||||
});
|
||||
|
||||
// Adapter初始化
|
||||
loading.once('coreInit', () => {
|
||||
if (main.replayChecking || main.mode === 'editor') return;
|
||||
const Adapter = Mota.require('@motajs/render').RenderAdapter;
|
||||
const adapter = Adapter.get<HeroRenderer>('hero-adapter');
|
||||
const viewport = Adapter.get<FloorViewport>('viewport');
|
||||
const layerAdapter = Adapter.get<Layer>('layer');
|
||||
HeroMover.adapter = adapter;
|
||||
HeroMover.viewport = viewport;
|
||||
BlockMover.adapter = layerAdapter;
|
||||
});
|
||||
@@ -0,0 +1,107 @@
|
||||
const dirMap: Record<Dir2, Dir> = {
|
||||
down: 'down',
|
||||
left: 'left',
|
||||
leftdown: 'left',
|
||||
leftup: 'left',
|
||||
right: 'right',
|
||||
rightdown: 'right',
|
||||
rightup: 'right',
|
||||
up: 'up'
|
||||
};
|
||||
|
||||
/**
|
||||
* 将八方向转换为四方向,一般用于斜角移动时的方向显示
|
||||
* @param dir 八方向
|
||||
*/
|
||||
export function toDir(dir: Dir2): Dir {
|
||||
return dirMap[dir];
|
||||
}
|
||||
|
||||
const backDirMap: Record<Dir2, Dir2> = {
|
||||
up: 'down',
|
||||
down: 'up',
|
||||
left: 'right',
|
||||
right: 'left',
|
||||
leftup: 'rightdown',
|
||||
rightup: 'leftdown',
|
||||
leftdown: 'rightup',
|
||||
rightdown: 'leftup'
|
||||
};
|
||||
|
||||
export function backDir(dir: Dir): Dir;
|
||||
export function backDir(dir: Dir2): Dir2;
|
||||
export function backDir(dir: Dir2): Dir2 {
|
||||
return backDirMap[dir];
|
||||
}
|
||||
|
||||
export function locInMap(x: number, y: number, floorId: FloorIds) {
|
||||
const { width, height } = core.status.maps[floorId];
|
||||
return x >= 0 && y >= 0 && x < width && y < height;
|
||||
}
|
||||
|
||||
/**
|
||||
* 广义检查能否移动,指定两个点以及行走方向,返回能否执行上述移动。
|
||||
* 可以传入地图之外的点,视为可以随意移动。仅检查 cannotIn 和 cannotOut,不检查 noPass
|
||||
* @param fx 出点横坐标
|
||||
* @param fy 出点纵坐标
|
||||
* @param tx 入点横坐标
|
||||
* @param ty 入点纵坐标
|
||||
* @param dir 行走方向
|
||||
* @param fromFloorId 出点楼层id
|
||||
* @param toFloorId 入点楼层id
|
||||
*/
|
||||
export function checkCanMoveExtended(
|
||||
fx: number,
|
||||
fy: number,
|
||||
tx: number,
|
||||
ty: number,
|
||||
dir: Dir,
|
||||
fromFloorId: FloorIds = core.status.floorId,
|
||||
toFloorId: FloorIds = core.status.floorId
|
||||
) {
|
||||
const fromInMap = locInMap(fx, fy, fromFloorId);
|
||||
const toInMap = locInMap(tx, ty, toFloorId);
|
||||
const map = maps_90f36752_8815_4be8_b32b_d7fad1d0542e;
|
||||
|
||||
if (fromInMap) {
|
||||
const cannotMove = core.floors[fromFloorId].cannotMove;
|
||||
const fromIndex: LocString = `${fx},${fy}`;
|
||||
// 检查当前点是否有不可出
|
||||
if (cannotMove[fromIndex]?.includes(dir)) return false;
|
||||
const blocks = getBlockForLoc(fx, fy, fromFloorId);
|
||||
const can = blocks.some(v => {
|
||||
if (v === 0) return false;
|
||||
const out = map[v as Exclude<AllNumbers, 0>]?.cannotOut;
|
||||
return out?.includes(dir);
|
||||
});
|
||||
if (can) return false;
|
||||
}
|
||||
if (toInMap) {
|
||||
const cannotMoveIn = core.floors[toFloorId].cannotMoveIn;
|
||||
const toIndex: LocString = `${tx},${ty}`;
|
||||
const back = backDir(dir);
|
||||
// 检查目标点是否有不可入
|
||||
if (cannotMoveIn[toIndex]?.includes(back)) return false;
|
||||
const blocks = getBlockForLoc(tx, ty, toFloorId);
|
||||
const can = blocks.some(v => {
|
||||
if (v === 0) return false;
|
||||
const out = map[v as Exclude<AllNumbers, 0>]?.cannotIn;
|
||||
return out?.includes(back);
|
||||
});
|
||||
if (can) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getBlockForLoc(x: number, y: number, floorId: FloorIds) {
|
||||
const map = core.status.maps[floorId];
|
||||
const floor = core.floors[floorId];
|
||||
return [
|
||||
floor.bgmap[y][x],
|
||||
floor.bg2map[y][x],
|
||||
map.map[y][x],
|
||||
floor.bgmap[y][x],
|
||||
floor.bg2map[y][x]
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user