mirror of
https://github.com/motajs/template.git
synced 2026-10-17 20:00:18 +08:00
refactor: map
This commit is contained in:
parent
b262918082
commit
7c155be5ce
@ -318,19 +318,19 @@ describe('MapState activation and areas', () => {
|
|||||||
new Set([[{ start: 0, end: 0 }], [{ start: 1, end: 1 }]])
|
new Set([[{ start: 0, end: 0 }], [{ start: 1, end: 1 }]])
|
||||||
);
|
);
|
||||||
|
|
||||||
mapState.notifyEnterFloor('A');
|
mapState.autoActivateFloor('A');
|
||||||
expect(mapState.isMapActive('A')).toBe(false);
|
expect(mapState.isMapActive('A')).toBe(false);
|
||||||
|
|
||||||
mapState.useAutoActivitor(true);
|
mapState.useAutoActivitor(true);
|
||||||
mapState.notifyEnterFloor('A');
|
mapState.autoActivateFloor('A');
|
||||||
expect(mapState.isMapActive('A')).toBe(true);
|
expect(mapState.isMapActive('A')).toBe(true);
|
||||||
|
|
||||||
mapState.notifyEnterFloor('B');
|
mapState.autoActivateFloor('B');
|
||||||
expect(mapState.isMapActive('A')).toBe(false);
|
expect(mapState.isMapActive('A')).toBe(false);
|
||||||
expect(mapState.isMapActive('B')).toBe(true);
|
expect(mapState.isMapActive('B')).toBe(true);
|
||||||
|
|
||||||
mapState.useAutoActivitor(false);
|
mapState.useAutoActivitor(false);
|
||||||
mapState.notifyEnterFloor('A');
|
mapState.autoActivateFloor('A');
|
||||||
expect(mapState.isMapActive('A')).toBe(false);
|
expect(mapState.isMapActive('A')).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import {
|
|||||||
SaveCompression,
|
SaveCompression,
|
||||||
shouldReplay
|
shouldReplay
|
||||||
} from '@user/data-common';
|
} from '@user/data-common';
|
||||||
import { ITileStore } from '@user/data-common';
|
|
||||||
import {
|
import {
|
||||||
IGameMap,
|
IGameMap,
|
||||||
IGameMapSave,
|
IGameMapSave,
|
||||||
@ -34,10 +33,7 @@ export class MapState implements IMapState {
|
|||||||
/** 自动分区激活器开关 */
|
/** 自动分区激活器开关 */
|
||||||
private autoActivitorEnabled: boolean = false;
|
private autoActivitorEnabled: boolean = false;
|
||||||
|
|
||||||
constructor(
|
constructor(readonly state: IDataCommon) {}
|
||||||
private readonly tileStore: ITileStore,
|
|
||||||
public readonly state: IDataCommon
|
|
||||||
) {}
|
|
||||||
|
|
||||||
//#region 楼层管理
|
//#region 楼层管理
|
||||||
|
|
||||||
@ -72,11 +68,10 @@ export class MapState implements IMapState {
|
|||||||
const state = this.createMap(raw.floorId, raw.width, height);
|
const state = this.createMap(raw.floorId, raw.width, height);
|
||||||
for (const [zIndex, map] of entries) {
|
for (const [zIndex, map] of entries) {
|
||||||
const z = Number(zIndex);
|
const z = Number(zIndex);
|
||||||
const layer = state.addLayer();
|
|
||||||
const alias = raw.layerAlias[z];
|
const alias = raw.layerAlias[z];
|
||||||
|
const layer = state.addLayer(alias);
|
||||||
layer.setMapRef(new Uint32Array(map));
|
layer.setMapRef(new Uint32Array(map));
|
||||||
layer.setZIndex(z);
|
layer.setZIndex(z);
|
||||||
state.setLayerAlias(layer, alias);
|
|
||||||
|
|
||||||
// 设置坐标点事件
|
// 设置坐标点事件
|
||||||
const events = raw.events[z];
|
const events = raw.events[z];
|
||||||
@ -106,8 +101,7 @@ export class MapState implements IMapState {
|
|||||||
} else {
|
} else {
|
||||||
this.maps.push(id);
|
this.maps.push(id);
|
||||||
}
|
}
|
||||||
const tile = this.tileStore;
|
const state = new GameMap(this.state, id, width, height);
|
||||||
const state = new GameMap(this.state, tile, id, width, height);
|
|
||||||
// 若已设置参考基准,新楼层直接视为全脏
|
// 若已设置参考基准,新楼层直接视为全脏
|
||||||
if (this.compared) {
|
if (this.compared) {
|
||||||
state.markDirty(true);
|
state.markDirty(true);
|
||||||
@ -162,7 +156,7 @@ export class MapState implements IMapState {
|
|||||||
this.autoActivitorEnabled = enable;
|
this.autoActivitorEnabled = enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
notifyEnterFloor(id: string): void {
|
autoActivateFloor(id: string): void {
|
||||||
if (!this.autoActivitorEnabled) return;
|
if (!this.autoActivitorEnabled) return;
|
||||||
const idx = this.maps.indexOf(id);
|
const idx = this.maps.indexOf(id);
|
||||||
if (idx === -1) return;
|
if (idx === -1) return;
|
||||||
|
|||||||
@ -33,65 +33,44 @@ export class DynamicTileMover extends ObjectMover<IDynamicTile> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@shouldReplay('Dynamic tile moving step should be replayed.')
|
@shouldReplay('Dynamic tile moving step should be replayed.')
|
||||||
protected onStepEnd(
|
protected async onStepEnd(
|
||||||
code: number,
|
code: number,
|
||||||
step: ObjectMoveStep,
|
step: ObjectMoveStep,
|
||||||
tile: IDynamicTile
|
tile: IDynamicTile
|
||||||
): Promise<ITileLocator> {
|
): Promise<ITileLocator> {
|
||||||
if (code !== DynamicMoveCode.Success) {
|
if (code !== DynamicMoveCode.Success) {
|
||||||
logger.warn(126, 'DynamicMoveCode.Success (0)', code.toString());
|
logger.warn(126, 'DynamicMoveCode.Success (0)', code.toString());
|
||||||
return Promise.resolve({ x: tile.x, y: tile.y });
|
return { x: tile.x, y: tile.y };
|
||||||
}
|
}
|
||||||
const locator: ITileLocator = {
|
|
||||||
x: tile.x,
|
|
||||||
y: tile.y
|
|
||||||
};
|
|
||||||
const handler = tile.state.faceManager.get(FaceGroup.Dir8);
|
const handler = tile.state.faceManager.get(FaceGroup.Dir8);
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
logger.warn(192);
|
logger.warn(192);
|
||||||
return Promise.resolve({ x: tile.x, y: tile.y });
|
return { x: tile.x, y: tile.y };
|
||||||
}
|
}
|
||||||
switch (step.type) {
|
switch (step.type) {
|
||||||
case ObjectMoveType.Dir: {
|
case ObjectMoveType.Dir:
|
||||||
const { x, y } = handler.movement(step.move);
|
case ObjectMoveType.DirFace:
|
||||||
tile.setFaceDirection(step.move);
|
|
||||||
locator.x += x;
|
|
||||||
locator.y += y;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case ObjectMoveType.DirFace: {
|
|
||||||
const { x, y } = handler.movement(step.move);
|
|
||||||
tile.setFaceDirection(step.face);
|
|
||||||
locator.x += x;
|
|
||||||
locator.y += y;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case ObjectMoveType.Face: {
|
|
||||||
tile.setFaceDirection(step.value);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case ObjectMoveType.Special: {
|
case ObjectMoveType.Special: {
|
||||||
const { x, y } = handler.movement(this.moveDirection);
|
const { x, y } = handler.movement(this.moveDirection);
|
||||||
tile.setFaceDirection(this.faceDirection);
|
tile.setFaceDirection(this.faceDirection);
|
||||||
locator.x += x;
|
return { x: tile.x + x, y: tile.y + y };
|
||||||
locator.y += y;
|
}
|
||||||
break;
|
case ObjectMoveType.Face: {
|
||||||
|
tile.setFaceDirection(step.value);
|
||||||
|
return { x: tile.x, y: tile.y };
|
||||||
}
|
}
|
||||||
case ObjectMoveType.Teleport:
|
case ObjectMoveType.Teleport:
|
||||||
case ObjectMoveType.Jump: {
|
case ObjectMoveType.Jump: {
|
||||||
const { x, y, rel } = step;
|
const { x, y, rel } = step;
|
||||||
tile.setFaceDirection(this.faceDirection);
|
tile.setFaceDirection(this.faceDirection);
|
||||||
if (rel) {
|
const nx = rel ? tile.x + x : x;
|
||||||
locator.x += x;
|
const ny = rel ? tile.y + y : y;
|
||||||
locator.y += y;
|
return { x: nx, y: ny };
|
||||||
} else {
|
}
|
||||||
locator.x = x;
|
default: {
|
||||||
locator.y = y;
|
return { x: tile.x, y: tile.y };
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Promise.resolve(locator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onStepSettled(): Promise<void> {
|
protected onStepSettled(): Promise<void> {
|
||||||
|
|||||||
@ -41,16 +41,14 @@ export class StaticTile
|
|||||||
}
|
}
|
||||||
|
|
||||||
saveState(): Readonly<IStaticBlockSave> {
|
saveState(): Readonly<IStaticBlockSave> {
|
||||||
let save: IStaticBlockSave;
|
|
||||||
const eventView = this.tileEvent();
|
const eventView = this.tileEvent();
|
||||||
if (eventView.dirty()) {
|
if (eventView.dirty()) {
|
||||||
save = {
|
return {
|
||||||
events: new Map(eventView.get())
|
events: new Map(eventView.get())
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
save = {};
|
return {};
|
||||||
}
|
}
|
||||||
return save;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadState(save: Readonly<IStaticBlockSave>): void {
|
loadState(save: Readonly<IStaticBlockSave>): void {
|
||||||
|
|||||||
@ -834,7 +834,7 @@ export interface IMapState
|
|||||||
* 那么会自动取消激活当前分区,然后激活新进入的分区。此函数一般不需要手动调用。
|
* 那么会自动取消激活当前分区,然后激活新进入的分区。此函数一般不需要手动调用。
|
||||||
* @param id 楼层 id
|
* @param id 楼层 id
|
||||||
*/
|
*/
|
||||||
notifyEnterFloor(id: string): void;
|
autoActivateFloor(id: string): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user