mirror of
https://github.com/motajs/template.git
synced 2026-11-29 09:54:30 +08:00
feat: 楼层切换
This commit is contained in:
1 parent
4bf39062a8
commit
f4c77d152f
15 files changed
+449
-23
No files matched your search
@@ -1,9 +1,22 @@
|
||||
import { IFacedTileLocator } from '@motajs/common';
|
||||
import {
|
||||
Hookable,
|
||||
HookController,
|
||||
IFacedTileLocator,
|
||||
IHookController
|
||||
} from '@motajs/common';
|
||||
import { FaceDirection, IDataCommon, IFaceHandler } from '@user/data-common';
|
||||
import { HeroMover } from './mover';
|
||||
import { IHeroLocation, IHeroLocationSave, IHeroMover } from './types';
|
||||
import {
|
||||
IHeroLocation,
|
||||
IHeroLocationHook,
|
||||
IHeroLocationSave,
|
||||
IHeroMover
|
||||
} from './types';
|
||||
|
||||
export class HeroLocation implements IHeroLocation {
|
||||
export class HeroLocation
|
||||
extends Hookable<IHeroLocationHook>
|
||||
implements IHeroLocation
|
||||
{
|
||||
x: number;
|
||||
y: number;
|
||||
floorId: string | undefined = undefined;
|
||||
@@ -16,6 +29,7 @@ export class HeroLocation implements IHeroLocation {
|
||||
loc: IFacedTileLocator,
|
||||
faceHandler: IFaceHandler<FaceDirection>
|
||||
) {
|
||||
super();
|
||||
this.state = state;
|
||||
this.x = loc.x;
|
||||
this.y = loc.y;
|
||||
@@ -24,9 +38,21 @@ export class HeroLocation implements IHeroLocation {
|
||||
this.mover = mover;
|
||||
}
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IHeroLocationHook>
|
||||
): IHookController<IHeroLocationHook> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
setFloor(floorId: string | undefined): void {
|
||||
this.floorId = floorId;
|
||||
this.forEachHook(hook => hook.onSetFloor?.(floorId));
|
||||
}
|
||||
|
||||
setPos(x: number, y: number): void {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.forEachHook(hook => hook.onSetPos?.(x, y));
|
||||
}
|
||||
|
||||
getCurrentFaceDirection(): FaceDirection {
|
||||
|
||||
@@ -40,7 +40,7 @@ export class HeroMover<T extends IHeroLocation>
|
||||
readonly tile: T,
|
||||
faceHandler: IFaceHandler<FaceDirection>
|
||||
) {
|
||||
super(faceHandler);
|
||||
super(faceHandler, tile.getCurrentFaceDirection());
|
||||
this.state = tile.state;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,9 @@ import {
|
||||
IHeroState,
|
||||
IHeroStateSave,
|
||||
IModifierStateSave,
|
||||
IReadonlyHeroAttribute
|
||||
IReadonlyHeroAttribute,
|
||||
IHeroChangeFloorInfo,
|
||||
IHeroStateHook
|
||||
} from './types';
|
||||
import {
|
||||
FaceDirection,
|
||||
@@ -23,9 +25,18 @@ import {
|
||||
IFaceHandler,
|
||||
SaveCompression
|
||||
} from '@user/data-common';
|
||||
import { IFacedTileLocator, logger } from '@motajs/common';
|
||||
import {
|
||||
Hookable,
|
||||
HookController,
|
||||
IFacedTileLocator,
|
||||
IHookController,
|
||||
logger
|
||||
} from '@motajs/common';
|
||||
|
||||
export class HeroState<THero> implements IHeroState<THero> {
|
||||
export class HeroState<THero>
|
||||
extends Hookable<IHeroStateHook>
|
||||
implements IHeroState<THero>
|
||||
{
|
||||
/** 修饰器工厂函数注册表 */
|
||||
private readonly registry: Map<
|
||||
string,
|
||||
@@ -43,6 +54,7 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
faceHandler: IFaceHandler<FaceDirection>,
|
||||
public attribute: IHeroAttribute<THero>
|
||||
) {
|
||||
super();
|
||||
this.rendering = new HeroRendering(state);
|
||||
const defaultLoc: IFacedTileLocator = {
|
||||
x: 0,
|
||||
@@ -59,8 +71,10 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
this.equip = new HeroEquipment(this.items.equipment, this.attribute);
|
||||
}
|
||||
|
||||
attachAttribute(attribute: IHeroAttribute<THero>): void {
|
||||
this.attribute = attribute;
|
||||
protected createController(
|
||||
hook: Partial<IHeroStateHook>
|
||||
): IHookController<IHeroStateHook> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
getLocation(): IFacedTileLocator {
|
||||
@@ -71,6 +85,12 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
};
|
||||
}
|
||||
|
||||
//#region 属性相关
|
||||
|
||||
attachAttribute(attribute: IHeroAttribute<THero>): void {
|
||||
this.attribute = attribute;
|
||||
}
|
||||
|
||||
getModifiableAttribute(): IHeroAttribute<THero> {
|
||||
return this.attribute;
|
||||
}
|
||||
@@ -109,6 +129,22 @@ export class HeroState<THero> implements IHeroState<THero> {
|
||||
return modifier;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
async changeFloor(info: IHeroChangeFloorInfo): Promise<void> {
|
||||
await Promise.all(
|
||||
this.forEachHook(hook => hook.onBeforeChangeFloor?.(info))
|
||||
);
|
||||
const mover = this.location.mover;
|
||||
mover.setFaceDir(info.face);
|
||||
mover.setMoveDir(info.face);
|
||||
mover.setPos(info.x, info.y);
|
||||
this.location.setFloor(info.target);
|
||||
await Promise.all(
|
||||
this.forEachHook(hook => hook.onAfterChangeFloor?.(info))
|
||||
);
|
||||
}
|
||||
|
||||
saveState(compression: SaveCompression): IHeroStateSave<THero> {
|
||||
const modifiers: IModifierStateSave<THero>[] = [];
|
||||
for (const [name, modifier] of this.attribute.iterateModifiers()) {
|
||||
|
||||
@@ -227,6 +227,21 @@ export interface IHeroAttribute<THero> extends IReadonlyHeroAttribute<THero> {
|
||||
|
||||
//#region 勇士位置
|
||||
|
||||
export interface IHeroLocationHook extends IHookBase {
|
||||
/**
|
||||
* 当设置勇士位置时触发
|
||||
* @param x 设置为的横坐标
|
||||
* @param y 设置为的纵坐标
|
||||
*/
|
||||
onSetPos?(x: number, y: number): void;
|
||||
|
||||
/**
|
||||
* 当设置勇士所处楼层时触发
|
||||
* @param floorId 设置为的楼层 id,undefined 表示尚不处于任何楼层
|
||||
*/
|
||||
onSetFloor?(floorId: string | undefined): void;
|
||||
}
|
||||
|
||||
export interface IHeroLocationSave {
|
||||
/** 当前横坐标 */
|
||||
readonly x: number;
|
||||
@@ -242,11 +257,18 @@ export interface IHeroLocation
|
||||
extends
|
||||
ISaveableContent<IHeroLocationSave>,
|
||||
IObjectMovable,
|
||||
IHookable<IHeroLocationHook>,
|
||||
IDataCommonExtended {
|
||||
/** 当前所在楼层 id,undefined 表示尚不处于任何楼层 */
|
||||
readonly floorId: string | undefined;
|
||||
/** 勇士的移动对象 */
|
||||
readonly mover: IHeroMover<this>;
|
||||
|
||||
/**
|
||||
* 设置勇士所在的楼层 id,注意此方法会引起数据变化,但是不会产生楼层切换动画
|
||||
* @param floorId 目标楼层 id
|
||||
*/
|
||||
setFloor(floorId: string | undefined): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -728,6 +750,33 @@ export interface IHeroEquipment<THero>
|
||||
|
||||
//#region 勇士状态
|
||||
|
||||
export interface IHeroChangeFloorInfo {
|
||||
/** 要切换至的目标楼层 */
|
||||
readonly target: string;
|
||||
/** 要切换至的目标位置横坐标 */
|
||||
readonly x: number;
|
||||
/** 要切换至的目标位置纵坐标 */
|
||||
readonly y: number;
|
||||
/** 要切换至的目标朝向 */
|
||||
readonly face: FaceDirection;
|
||||
}
|
||||
|
||||
export interface IHeroStateHook extends IHookBase {
|
||||
/**
|
||||
* 当勇士切换楼层前触发,此钩子执行完毕后会立刻触发 `IHeroLocation` 的 `onSetFloor` 钩子。
|
||||
* 此钩子应该用于切换楼层前的过渡及必要的数据准备,不应该修改勇士位置相关的数据。
|
||||
* @param info 勇士切换楼层信息对象
|
||||
*/
|
||||
onBeforeChangeFloor?(info: IHeroChangeFloorInfo): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当勇士切换楼层后触发,此钩子会在 `IHeroLocation` 的 `onSetFloor` 钩子执行完毕后立刻触发。
|
||||
* 此钩子应该用于切换楼层后的过渡及必要的数据处理,不应该修改勇士位置相关的数据。
|
||||
* @param info 勇士切换楼层信息对象
|
||||
*/
|
||||
onAfterChangeFloor?(info: IHeroChangeFloorInfo): Promise<void>;
|
||||
}
|
||||
|
||||
export interface IHeroStateSave<THero> {
|
||||
/** 勇士属性状态 */
|
||||
readonly attribute: THero;
|
||||
@@ -745,9 +794,8 @@ export interface IHeroStateSave<THero> {
|
||||
readonly equip: IHeroEquipmentSave;
|
||||
}
|
||||
|
||||
export interface IHeroState<THero> extends ISaveableContent<
|
||||
IHeroStateSave<THero>
|
||||
> {
|
||||
export interface IHeroState<THero>
|
||||
extends ISaveableContent<IHeroStateSave<THero>>, IHookable<IHeroStateHook> {
|
||||
/** 勇士移动对象 */
|
||||
readonly location: IHeroLocation;
|
||||
/** 勇士属性对象 */
|
||||
@@ -809,6 +857,12 @@ export interface IHeroState<THero> extends ISaveableContent<
|
||||
type: string,
|
||||
name: K
|
||||
): IHeroModifier<THero[K], V> | null;
|
||||
|
||||
/**
|
||||
* 切换勇士所在楼层
|
||||
* @param info 楼层切换信息对象
|
||||
*/
|
||||
changeFloor(info: IHeroChangeFloorInfo): Promise<void>;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -1,5 +1,5 @@
|
||||
export * from './layerState';
|
||||
export * from './mapLayer';
|
||||
export * from './mapStore';
|
||||
export * from './mapState';
|
||||
export * from './mover';
|
||||
export * from './types';
|
||||
+52
-2
@@ -1,5 +1,5 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import { IDataCommon, SaveCompression } from '@user/data-common';
|
||||
import { IDataCommon, IMapRawData, SaveCompression } from '@user/data-common';
|
||||
import { ITileStore } from '@user/data-common';
|
||||
import {
|
||||
ILayerState,
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
MapArea
|
||||
} from './types';
|
||||
import { LayerState } from './layerState';
|
||||
import { uniq } from 'lodash-es';
|
||||
import { isNil, uniq } from 'lodash-es';
|
||||
|
||||
export class MapState implements IMapState {
|
||||
/** 楼层 id 到状态对象的映射 */
|
||||
@@ -39,6 +39,56 @@ export class MapState implements IMapState {
|
||||
|
||||
//#region 楼层管理
|
||||
|
||||
fromRaw(raw: IMapRawData): ILayerState | null {
|
||||
let length = 0;
|
||||
const entries = Object.entries(raw.map);
|
||||
for (const [_, map] of Object.entries(raw.map)) {
|
||||
if (length > 0 && map.length !== length) {
|
||||
logger.error(60, map.length.toString(), length.toString());
|
||||
return null;
|
||||
} else {
|
||||
length = map.length;
|
||||
}
|
||||
}
|
||||
if (length % raw.width !== 0) {
|
||||
logger.error(61, length.toString(), raw.width.toString());
|
||||
return null;
|
||||
}
|
||||
|
||||
// 设置楼层图块数据
|
||||
const height = length / raw.width;
|
||||
const state = this.createLayerState(raw.floorId, raw.width, height);
|
||||
for (const [zIndex, map] of entries) {
|
||||
const z = Number(zIndex);
|
||||
if (isNaN(z)) {
|
||||
logger.error(62, 'IMapRawData.map', String(zIndex));
|
||||
continue;
|
||||
}
|
||||
const layer = state.addLayer();
|
||||
const alias = raw.layerAlias[z];
|
||||
layer.setMapRef(new Uint32Array(map));
|
||||
layer.setZIndex(z);
|
||||
state.setLayerAlias(layer, alias);
|
||||
|
||||
// 设置图块静态触发器
|
||||
const extra = raw.blockData[z];
|
||||
for (const [index, data] of Object.entries(extra)) {
|
||||
const indexNum = Number(index);
|
||||
if (isNaN(indexNum)) {
|
||||
logger.error(62, 'IMapRawData.blockData', String(index));
|
||||
continue;
|
||||
}
|
||||
if (!isNil(data.trigger)) {
|
||||
const x = indexNum % raw.width;
|
||||
const y = Math.floor(indexNum / raw.width);
|
||||
layer.setTriggerType(data.trigger, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
createLayerState(id: string, width: number, height: number): ILayerState {
|
||||
if (this.mapData.has(id)) {
|
||||
logger.warn(121, id);
|
||||
@@ -18,7 +18,7 @@ const enum DynamicMoveCode {
|
||||
export class DynamicTileMover extends ObjectMover<IDynamicTile> {
|
||||
constructor(public readonly tile: IDynamicTile) {
|
||||
const face = tile.state.faceManager;
|
||||
super(face.get(DYNAMIC_MOVER_FACE)!);
|
||||
super(face.get(DYNAMIC_MOVER_FACE)!, tile.getCurrentFaceDirection());
|
||||
}
|
||||
|
||||
protected onMoveStart(): Promise<void> {
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import { IHookable, IHookBase, IHookController } from '@motajs/common';
|
||||
import {
|
||||
IHookable,
|
||||
IHookBase,
|
||||
IHookController,
|
||||
ITileLocator
|
||||
} from '@motajs/common';
|
||||
import {
|
||||
FaceDirection,
|
||||
IDataCommonExtended,
|
||||
IMapBlockRawData,
|
||||
IMapRawData,
|
||||
IMoverController,
|
||||
IObjectMovable,
|
||||
IObjectMover,
|
||||
@@ -21,6 +28,8 @@ export interface IMapLayerData {
|
||||
}
|
||||
|
||||
export interface ILayerLocation {
|
||||
/** 该点的位置 */
|
||||
readonly locator: ITileLocator;
|
||||
/** 该点的静态图块数字 */
|
||||
readonly tile: number;
|
||||
/** 该点的静态图块信息 */
|
||||
@@ -29,6 +38,8 @@ export interface ILayerLocation {
|
||||
readonly trigger: number;
|
||||
/** 该点包含的所有动态图块 */
|
||||
readonly dynamics: Iterable<IDynamicTile>;
|
||||
/** 该点的静态图块原始数据 */
|
||||
readonly block: IMapBlockRawData;
|
||||
}
|
||||
|
||||
export interface IMapLayerHooks extends IHookBase {
|
||||
@@ -442,6 +453,12 @@ export interface IMapState
|
||||
*/
|
||||
getActiveMap(id: string): ILayerState | null;
|
||||
|
||||
/**
|
||||
* 从楼层原始数据生成楼层状态
|
||||
* @param raw 楼层原始数据
|
||||
*/
|
||||
fromRaw(raw: IMapRawData): ILayerState | null;
|
||||
|
||||
/**
|
||||
* 创建并注册一个空白楼层,若 id 已存在则警告并覆盖,返回楼层状态对象
|
||||
* @param id 楼层 id
|
||||
|
||||
Reference in new issue
Block a user