mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-11-19 00:42:58 +08:00
feat: 地图状态管理
This commit is contained in:
parent
0d9bef70b1
commit
639a0d0f68
10
packages-user/data-state/src/core/core.ts
Normal file
10
packages-user/data-state/src/core/core.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { LayerState } from './layerState';
|
||||||
|
import { ICoreState, ILayerState } from './types';
|
||||||
|
|
||||||
|
export class CoreState implements ICoreState {
|
||||||
|
readonly layer: ILayerState;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.layer = new LayerState();
|
||||||
|
}
|
||||||
|
}
|
||||||
22
packages-user/data-state/src/core/index.ts
Normal file
22
packages-user/data-state/src/core/index.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { CoreState } from './core';
|
||||||
|
|
||||||
|
export function createCoreState() {
|
||||||
|
const width = core._WIDTH_;
|
||||||
|
const height = core._HEIGHT_;
|
||||||
|
const bg = state.layer.addLayer(width, height);
|
||||||
|
const bg2 = state.layer.addLayer(width, height);
|
||||||
|
const event = state.layer.addLayer(width, height);
|
||||||
|
const fg = state.layer.addLayer(width, height);
|
||||||
|
const fg2 = state.layer.addLayer(width, height);
|
||||||
|
state.layer.setLayerAlias(bg, 'bg');
|
||||||
|
state.layer.setLayerAlias(bg2, 'bg2');
|
||||||
|
state.layer.setLayerAlias(event, 'event');
|
||||||
|
state.layer.setLayerAlias(fg, 'fg');
|
||||||
|
state.layer.setLayerAlias(fg2, 'fg2');
|
||||||
|
}
|
||||||
|
|
||||||
|
export const state = new CoreState();
|
||||||
|
|
||||||
|
export * from './core';
|
||||||
|
export * from './layerState';
|
||||||
|
export * from './types';
|
||||||
60
packages-user/data-state/src/core/layerState.ts
Normal file
60
packages-user/data-state/src/core/layerState.ts
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import { logger } from '@motajs/common';
|
||||||
|
import { IMapLayer, MapLayer } from '../map';
|
||||||
|
import { ILayerState } from './types';
|
||||||
|
|
||||||
|
export class LayerState implements ILayerState {
|
||||||
|
readonly layerList: WeakSet<IMapLayer> = new WeakSet();
|
||||||
|
/** 图层到图层别名映射 */
|
||||||
|
readonly layerAliasMap: WeakMap<IMapLayer, string> = new WeakMap();
|
||||||
|
/** 图层别名到图层的映射 */
|
||||||
|
readonly aliasLayerMap: WeakMap<symbol, IMapLayer> = new WeakMap();
|
||||||
|
|
||||||
|
addLayer(width: number, height: number): IMapLayer {
|
||||||
|
const array = new Uint32Array(width * height);
|
||||||
|
const layer = new MapLayer(array, width, height);
|
||||||
|
this.layerList.add(layer);
|
||||||
|
return layer;
|
||||||
|
}
|
||||||
|
|
||||||
|
removeLayer(layer: IMapLayer): void {
|
||||||
|
this.layerList.delete(layer);
|
||||||
|
const alias = this.layerAliasMap.get(layer);
|
||||||
|
if (alias) {
|
||||||
|
const symbol = Symbol.for(alias);
|
||||||
|
this.aliasLayerMap.delete(symbol);
|
||||||
|
this.layerAliasMap.delete(layer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setLayerAlias(layer: IMapLayer, alias: string): void {
|
||||||
|
const symbol = Symbol.for(alias);
|
||||||
|
if (this.aliasLayerMap.has(symbol)) {
|
||||||
|
logger.warn(84, alias);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.layerAliasMap.set(layer, alias);
|
||||||
|
this.aliasLayerMap.set(symbol, layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
getLayerByAlias(alias: string): IMapLayer | null {
|
||||||
|
const symbol = Symbol.for(alias);
|
||||||
|
return this.aliasLayerMap.get(symbol) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getLayerAlias(layer: IMapLayer): string | undefined {
|
||||||
|
return this.layerAliasMap.get(layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
resizeLayer(
|
||||||
|
layer: IMapLayer,
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
keepBlock?: boolean
|
||||||
|
): void {
|
||||||
|
if (keepBlock) {
|
||||||
|
layer.resize(width, height);
|
||||||
|
} else {
|
||||||
|
layer.resize2(width, height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
57
packages-user/data-state/src/core/types.ts
Normal file
57
packages-user/data-state/src/core/types.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import { IMapLayer } from '../map';
|
||||||
|
|
||||||
|
export interface ILayerState {
|
||||||
|
/** 地图列表 */
|
||||||
|
readonly layerList: WeakSet<IMapLayer>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加图层
|
||||||
|
* @param width 地图宽度
|
||||||
|
* @param height 地图高度
|
||||||
|
*/
|
||||||
|
addLayer(width: number, height: number): IMapLayer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除指定图层
|
||||||
|
* @param layer 图层对象
|
||||||
|
*/
|
||||||
|
removeLayer(layer: IMapLayer): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置图层别名
|
||||||
|
* @param layer 图层对象
|
||||||
|
* @param alias 图层别名
|
||||||
|
*/
|
||||||
|
setLayerAlias(layer: IMapLayer, alias: string): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据图层别名获取图层对象
|
||||||
|
* @param alias 图层别名
|
||||||
|
*/
|
||||||
|
getLayerByAlias(alias: string): IMapLayer | null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取图层对象的别名
|
||||||
|
* @param layer 图层对象
|
||||||
|
*/
|
||||||
|
getLayerAlias(layer: IMapLayer): string | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重新设置图层的大小
|
||||||
|
* @param layer 图层对象
|
||||||
|
* @param width 新的图层宽度
|
||||||
|
* @param height 新的图层高度
|
||||||
|
* @param keepBlock 是否保留原有图块,默认不保留
|
||||||
|
*/
|
||||||
|
resizeLayer(
|
||||||
|
layer: IMapLayer,
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
keepBlock?: boolean
|
||||||
|
): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICoreState {
|
||||||
|
/** 地图状态 */
|
||||||
|
readonly layer: ILayerState;
|
||||||
|
}
|
||||||
@ -1,9 +1,16 @@
|
|||||||
|
import { loading } from '@user/data-base';
|
||||||
import { createMechanism } from './mechanism';
|
import { createMechanism } from './mechanism';
|
||||||
|
import { createCoreState } from './core';
|
||||||
|
|
||||||
export function create() {
|
export function create() {
|
||||||
createMechanism();
|
createMechanism();
|
||||||
|
loading.once('loaded', () => {
|
||||||
|
// 加载后初始化全局状态
|
||||||
|
createCoreState();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export * from './core';
|
||||||
export * from './enemy';
|
export * from './enemy';
|
||||||
export * from './map';
|
export * from './map';
|
||||||
export * from './mechanism';
|
export * from './mechanism';
|
||||||
|
|||||||
@ -96,7 +96,7 @@ export interface IMapLayer {
|
|||||||
readonly empty: boolean;
|
readonly empty: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调整地图尺寸,如果尺寸变大,那么会补零,如果尺寸变小,那么会将当前数组裁剪
|
* 调整地图尺寸,维持原有图块。如果尺寸变大,那么会补零,如果尺寸变小,那么会将当前数组裁剪
|
||||||
* @param width 地图宽度
|
* @param width 地图宽度
|
||||||
* @param height 地图高度
|
* @param height 地图高度
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -131,6 +131,7 @@
|
|||||||
"81": "Map data to get is partially (or totally) out of range. Overflowed area will be filled with zero.",
|
"81": "Map data to get is partially (or totally) out of range. Overflowed area will be filled with zero.",
|
||||||
"82": "Big image offset size is larger than 64. Considier reduce big image offset bucket.",
|
"82": "Big image offset size is larger than 64. Considier reduce big image offset bucket.",
|
||||||
"83": "It seems that you call 'updateBlock' too frequently. This will extremely affect game's performance, so considering integrate them into one 'updateArea' or 'updateBlockList' call.",
|
"83": "It seems that you call 'updateBlock' too frequently. This will extremely affect game's performance, so considering integrate them into one 'updateArea' or 'updateBlockList' call.",
|
||||||
|
"84": "Cannot set alias '$1' for layer, since '$1' is already an alias for another layer.",
|
||||||
"1001": "Item-detail extension needs 'floor-binder' and 'floor-damage' extension as dependency.",
|
"1001": "Item-detail extension needs 'floor-binder' and 'floor-damage' extension as dependency.",
|
||||||
"1101": "Cannot add new effect to point effect instance, for there's no more reserve space for it. Please increase the max count of the instance."
|
"1101": "Cannot add new effect to point effect instance, for there's no more reserve space for it. Please increase the max count of the instance."
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user