feat: WebGL2 地图渲染

This commit is contained in:
AncTe committed 2025-11-16 15:48:22 +08:00
1 parent ad469cf1ff
commit 0d9bef70b1
45 files changed
+5851 -691

No files matched your search

@@ -6,6 +6,8 @@ import { createViewport } from './viewport';
import { Icon, Winskin } from './misc';
import { Animate } from './animate';
import { createItemDetail } from './itemDetail';
import { logger } from '@motajs/common';
import { MapRender } from '../map/element';
export function createElements() {
createCache();
@@ -66,6 +68,18 @@ export function createElements() {
return new Animate();
});
tagMap.register('icon', standardElementNoCache(Icon));
tagMap.register('map-render', (_0, _1, props) => {
if (!props) {
logger.error(42);
return new MapRender([]);
}
const { layerList } = props;
if (!layerList) {
logger.error(42);
return new MapRender([]);
}
return new MapRender(layerList);
});
}
export * from './animate';
@@ -11,6 +11,7 @@ import {
import { EAnimateEvent } from './animate';
import { EIconEvent, EWinskinEvent } from './misc';
import { IEnemyCollection } from '@motajs/types';
import { IRenderLayerData } from '../map/element';
export interface AnimateProps extends BaseProps {}
@@ -60,6 +61,10 @@ export interface LayerProps extends BaseProps {
ex?: readonly ILayerRenderExtends[];
}
export interface MapRenderProps extends BaseProps {
layerList: IRenderLayerData;
}
declare module 'vue/jsx-runtime' {
namespace JSX {
export interface IntrinsicElements {
@@ -0,0 +1,31 @@
import { SizedCanvasImageSource } from '@motajs/render-assets';
import {
IMaterialGetter,
IMaterialManager,
materials
} from '@user/client-base';
import { IMapAssetData, IMapAssetManager } from './types';
import { PrivateListDirtyTracker } from '@motajs/common';
export class MapAssetManager implements IMapAssetManager {
materials: IMaterialManager = materials;
generateAsset(): IMapAssetData {
const data = new MapAssetData();
return data;
}
}
class MapAssetData
extends PrivateListDirtyTracker<number>
implements IMapAssetData
{
sourceList: ImageBitmap[] = [];
skipRef: Map<SizedCanvasImageSource, number> = new Map();
materials: IMaterialGetter = materials;
constructor() {
super(0);
}
}
@@ -0,0 +1,347 @@
import { clamp } from 'lodash-es';
import {
IBlockData,
IBlockIndex,
IBlockInfo,
IBlockSplitter,
IBlockSplitterConfig
} from './types';
export class BlockSplitter<T> implements IBlockSplitter<T> {
blockWidth: number = 0;
blockHeight: number = 0;
dataWidth: number = 0;
dataHeight: number = 0;
width: number = 0;
height: number = 0;
/** 分块映射 */
readonly blockMap: Map<number, IBlockData<T>> = new Map();
/** 数据宽度配置 */
private splitDataWidth: number = 0;
/** 数据高度配置 */
private splitDataHeight: number = 0;
/** 单个分块的宽度配置 */
private splitBlockWidth: number = 0;
/** 单个分块的高度配置 */
private splitBlockHeight: number = 0;
/**
* 检查坐标范围
* @param x 分块横坐标
* @param y 分块纵坐标
*/
private checkLocRange(x: number, y: number) {
return x > 0 && y > 0 && x < this.width && y < this.height;
}
getBlockByLoc(x: number, y: number): IBlockData<T> | null {
if (!this.checkLocRange(x, y)) return null;
const index = y * this.width + x;
return this.blockMap.get(index) ?? null;
}
getBlockByIndex(index: number): IBlockData<T> | null {
return this.blockMap.get(index) ?? null;
}
setBlockByLoc(data: T, x: number, y: number): IBlockData<T> | null {
if (!this.checkLocRange(x, y)) return null;
const index = y * this.width + x;
const block = this.blockMap.get(index);
if (!block) return null;
block.data = data;
return block;
}
setBlockByIndex(data: T, index: number): IBlockData<T> | null {
const block = this.blockMap.get(index);
if (!block) return null;
block.data = data;
return block;
}
*iterateBlockByLoc(x: number, y: number): Generator<IBlockIndex, void> {
if (!this.checkLocRange(x, y)) return;
const index = y * this.width + x;
yield* this.iterateBlockByIndex(index);
}
*iterateBlockByIndex(index: number): Generator<IBlockIndex, void> {
const block = this.blockMap.get(index);
if (!block) return;
const startX = block.x * this.blockWidth;
const startY = block.y * this.blockHeight;
const endX = startX + block.width;
const endY = startY + block.height;
for (let ny = startY; ny < endY; ny++) {
for (let nx = startX; nx < endX; nx++) {
const index: IBlockIndex = {
x: nx,
y: ny,
dataX: nx * this.blockWidth,
dataY: ny * this.blockHeight,
index: ny * this.dataWidth + nx
};
yield index;
}
}
}
*iterateBlockByIndices(
indices: Iterable<number>
): Generator<IBlockIndex, void> {
for (const index of indices) {
yield* this.iterateBlockByIndex(index);
}
}
iterateBlocks(): Iterable<IBlockData<T>> {
return this.blockMap.values();
}
*iterateBlocksOfDataArea(
x: number,
y: number,
width: number,
height: number
): Generator<IBlockData<T>> {
const r = this.width - 1;
const b = this.height - 1;
const rx = x + width;
const by = y + height;
const left = clamp(Math.floor(x / this.blockWidth), 0, r);
const top = clamp(Math.floor(y / this.blockHeight), 0, b);
const right = clamp(Math.floor(rx / this.blockWidth), 0, r);
const bottom = clamp(Math.floor(by / this.blockHeight), 0, b);
for (let ny = left; ny <= right; ny++) {
for (let nx = top; nx <= bottom; nx++) {
const index = ny * this.width + nx;
const block = this.blockMap.get(index);
if (!block) continue;
yield block;
}
}
}
getIndexByLoc(x: number, y: number): number {
if (!this.checkLocRange(x, y)) return -1;
return y * this.width + x;
}
getLocByIndex(index: number): Loc | null {
if (index >= this.width * this.height) return null;
return {
x: index % this.width,
y: Math.floor(index / this.width)
};
}
getIndicesByLocList(list: Iterable<Loc>): Iterable<number> {
const res: number[] = [];
for (const { x, y } of list) {
res.push(this.getIndexByLoc(x, y));
}
return res;
}
getLocListByIndices(list: Iterable<number>): Iterable<Loc | null> {
const res: (Loc | null)[] = [];
for (const index of list) {
res.push(this.getLocByIndex(index));
}
return res;
}
getBlockByDataLoc(x: number, y: number): IBlockData<T> | null {
const bx = Math.floor(x / this.blockWidth);
const by = Math.floor(y / this.blockHeight);
if (!this.checkLocRange(bx, by)) return null;
const index = y * this.width + x;
return this.blockMap.get(index) ?? null;
}
getBlockByDataIndex(index: number): IBlockData<T> | null {
const x = index % this.dataWidth;
const y = Math.floor(index / this.dataWidth);
return this.getBlockByDataLoc(x, y);
}
getIndicesByDataLocList(list: Iterable<Loc>): Set<number> {
const res = new Set<number>();
for (const { x, y } of list) {
const bx = Math.floor(x / this.blockWidth);
const by = Math.floor(y / this.blockHeight);
if (!this.checkLocRange(bx, by)) continue;
res.add(bx + by * this.width);
}
return res;
}
getIndicesByDataIndices(list: Iterable<number>): Set<number> {
const res = new Set<number>();
for (const index of list) {
const x = index % this.dataWidth;
const y = Math.floor(index / this.dataWidth);
const bx = Math.floor(x / this.blockWidth);
const by = Math.floor(y / this.blockHeight);
if (!this.checkLocRange(bx, by)) continue;
res.add(bx + by * this.width);
}
return res;
}
getBlocksByDataLocList(list: Iterable<Loc>): Set<IBlockData<T>> {
const res = new Set<IBlockData<T>>();
for (const { x, y } of list) {
const bx = Math.floor(x / this.blockWidth);
const by = Math.floor(y / this.blockHeight);
if (!this.checkLocRange(bx, by)) continue;
const index = bx + by * this.width;
const data = this.blockMap.get(index);
if (data) res.add(data);
}
return res;
}
getBlocksByDataIndices(list: Iterable<number>): Set<IBlockData<T>> {
const res = new Set<IBlockData<T>>();
for (const index of list) {
const x = index % this.dataWidth;
const y = Math.floor(index / this.dataWidth);
const bx = Math.floor(x / this.blockWidth);
const by = Math.floor(y / this.blockHeight);
if (!this.checkLocRange(bx, by)) continue;
const blockIndex = bx + by * this.width;
const data = this.blockMap.get(blockIndex);
if (data) res.add(data);
}
return res;
}
configSplitter(config: IBlockSplitterConfig): void {
this.splitBlockWidth = config.blockWidth;
this.splitBlockHeight = config.blockHeight;
this.splitDataWidth = config.dataWidth;
this.splitBlockHeight = config.dataHeight;
}
private mapBlock(
x: number,
y: number,
realWidth: number,
width: number,
height: number,
fn: (block: IBlockInfo) => T
) {
const index = y * realWidth + x;
const block: IBlockInfo = {
index,
x,
y,
dataX: x * this.blockWidth,
dataY: y * this.blockHeight,
width,
height
};
const data = fn(block);
const blockData = new SplittedBlockData(this, block, data);
this.blockMap.set(index, blockData);
}
splitBlocks(mapFn: (block: IBlockInfo) => T): void {
this.blockMap.clear();
const restX = this.splitDataWidth % this.splitBlockWidth;
const restY = this.splitDataHeight % this.splitBlockHeight;
const width = Math.floor(this.splitDataWidth / this.splitBlockWidth);
const height = Math.floor(this.splitDataHeight / this.splitBlockHeight);
const hasXRest = restX > 0;
const hasYRest = restY > 0;
const realWidth = hasXRest ? width + 1 : width;
const bw = this.blockWidth;
const bh = this.blockHeight;
this.width = realWidth;
this.height = hasYRest ? height + 1 : height;
for (let ny = 0; ny < height; ny++) {
for (let nx = 0; nx < width; nx++) {
this.mapBlock(nx, ny, realWidth, bw, bh, mapFn);
}
}
if (hasXRest) {
for (let ny = 0; ny < height; ny++) {
this.mapBlock(width, ny, realWidth, restX, bh, mapFn);
}
}
if (hasYRest) {
for (let nx = 0; nx < width; nx++) {
this.mapBlock(nx, height, realWidth, bw, restY, mapFn);
}
}
if (hasXRest && hasYRest) {
this.mapBlock(width, height, realWidth, restX, restY, mapFn);
}
}
}
class SplittedBlockData<T> implements IBlockData<T> {
width: number;
height: number;
x: number;
y: number;
dataX: number;
dataY: number;
index: number;
data: T;
constructor(
readonly splitter: BlockSplitter<T>,
info: IBlockInfo,
data: T
) {
this.width = info.width;
this.height = info.height;
this.x = info.x;
this.y = info.y;
this.dataX = info.dataX;
this.dataY = info.dataY;
this.index = info.index;
this.data = data;
}
left(): IBlockData<T> | null {
return this.splitter.getBlockByLoc(this.x - 1, this.y);
}
right(): IBlockData<T> | null {
return this.splitter.getBlockByLoc(this.x + 1, this.y);
}
up(): IBlockData<T> | null {
return this.splitter.getBlockByLoc(this.x, this.y - 1);
}
down(): IBlockData<T> | null {
return this.splitter.getBlockByLoc(this.x, this.y + 1);
}
leftUp(): IBlockData<T> | null {
return this.splitter.getBlockByLoc(this.x - 1, this.y - 1);
}
leftDown(): IBlockData<T> | null {
return this.splitter.getBlockByLoc(this.x - 1, this.y + 1);
}
rightUp(): IBlockData<T> | null {
return this.splitter.getBlockByLoc(this.x + 1, this.y - 1);
}
rightDown(): IBlockData<T> | null {
return this.splitter.getBlockByLoc(this.x + 1, this.y + 1);
}
next(): IBlockData<T> | null {
return this.splitter.getBlockByIndex(this.index + 1);
}
}
@@ -0,0 +1,114 @@
import {
MotaOffscreenCanvas2D,
RenderItem,
Transform
} from '@motajs/render-core';
import { IMapLayer } from '@user/data-state';
import { IMapRenderer } from './types';
import { MapRenderer } from './renderer';
import { materials } from '@user/client-base';
import { ElementNamespace, ComponentInternalInstance } from 'vue';
export interface IRenderLayerData {
/** 图层对象 */
readonly layer: IMapLayer;
/** 图层纵深 */
readonly zIndex: number;
/** 图层别名 */
readonly alias?: string;
}
export class MapRender extends RenderItem {
/** 地图渲染器 */
readonly renderer: IMapRenderer;
/** 地图视角变换矩阵 */
readonly camera: Transform = new Transform();
/** 地图画布 */
readonly canvas: HTMLCanvasElement;
/** 画布上下文 */
readonly gl: WebGL2RenderingContext;
constructor(layerList: Iterable<IRenderLayerData>) {
super('static');
this.canvas = document.createElement('canvas');
const gl = this.canvas.getContext('webgl2')!;
this.gl = gl;
this.renderer = new MapRenderer(materials, this.gl, this.camera);
for (const layer of layerList) {
this.renderer.addLayer(layer.layer, layer.alias);
this.renderer.setZIndex(layer.layer, layer.zIndex);
}
}
/**
* 更新图层列表
* @param layerList 图层列表
*/
updateLayerList(layerList: Iterable<IRenderLayerData>) {
this.renderer
.getSortedLayer()
.forEach(v => this.renderer.removeLayer(v));
for (const layer of layerList) {
this.renderer.addLayer(layer.layer, layer.alias);
this.renderer.setZIndex(layer.layer, layer.zIndex);
}
}
private sizeGL(width: number, height: number) {
const ratio = this.highResolution ? devicePixelRatio : 1;
const scale = ratio * this.scale;
this.canvas.width = width * scale;
this.canvas.height = height * scale;
this.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
}
onResize(scale: number): void {
super.onResize(scale);
this.sizeGL(this.width, this.height);
}
size(width: number, height: number): void {
super.size(width, height);
this.sizeGL(width, height);
}
updateTransform(transform: Transform): void {
super.updateTransform(transform);
if (transform === this.camera) {
this.update();
}
}
protected render(canvas: MotaOffscreenCanvas2D): void {
console.time('map-element-render');
this.renderer.render(this.gl);
canvas.ctx.drawImage(
this.canvas,
0,
0,
this.canvas.width,
this.canvas.height
);
console.timeEnd('map-element-render');
}
patchProp(
key: string,
prevValue: any,
nextValue: any,
namespace?: ElementNamespace,
parentComponent?: ComponentInternalInstance | null
): void {
switch (key) {
case 'layerList': {
this.updateLayerList(nextValue);
break;
}
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
}
@@ -0,0 +1,3 @@
export * from './asset';
export * from './renderer';
export * from './types';
@@ -0,0 +1,229 @@
import { linear, TimingFn } from 'mutate-animate';
import { IMapRenderer, IMapVertexGenerator, IMovingBlock } from './types';
import { IMaterialFramedData, IMaterialManager } from '@user/client-base';
import { logger } from '@motajs/common';
import { IMapLayer } from '@user/data-state';
export interface IMovingRenderer {
/** 素材管理器 */
readonly manager: IMaterialManager;
/** 顶点数组生成器 */
readonly vertex: IMapVertexGenerator;
/**
* 获得当前时间戳
*/
getTimestamp(): number;
/**
* 删除指定的移动图块对象
* @param block 移动图块对象
*/
deleteMoving(block: IMovingBlock): void;
}
export class MovingBlock implements IMovingBlock {
readonly texture: IMaterialFramedData;
readonly tile: number;
readonly renderer: IMovingRenderer;
readonly index: number;
readonly layer: IMapLayer;
x: number = 0;
y: number = 0;
/** 当前动画开始的时刻 */
private startTime: number = 0;
/** 是否是直线动画 */
private line: boolean = false;
/** 是否是相对模式 */
private relative: boolean = false;
/** 目标横坐标 */
private targetX: number = 0;
/** 目标纵坐标 */
private targetY: number = 0;
/** 直线移动的横坐标增量 */
private dx: number = 0;
/** 直线移动的纵坐标增量 */
private dy: number = 0;
/** 动画时长 */
private time: number = 0;
/** 速率曲线 */
private timing: TimingFn = () => 0;
/** 移动轨迹曲线 */
private curve: TimingFn<2> = () => [0, 0];
/** 动画开始时横坐标 */
private startX: number = 0;
/** 动画开始时纵坐标 */
private startY: number = 0;
/** 当前动画是否已经结束 */
private end: boolean = false;
/** 兑现函数 */
private promiseFunc: () => void = () => {};
constructor(
renderer: IMovingRenderer & IMapRenderer,
index: number,
layer: IMapLayer,
block: number | IMaterialFramedData,
x: number,
y: number
) {
this.renderer = renderer;
this.index = index;
this.layer = layer;
this.x = x;
this.y = y;
if (typeof block === 'number') {
this.texture = renderer.manager.getTile(block)!;
this.tile = block;
} else {
if (!renderer.manager.assetContainsTexture(block.texture)) {
logger.error(34);
}
if (renderer.getOffsetIndex(block.offset) === -1) {
logger.error(41);
}
this.texture = block;
this.tile = -1;
}
}
lineTo(
x: number,
y: number,
time: number,
timing?: TimingFn
): Promise<this> {
this.startX = this.x;
this.startY = this.y;
this.targetX = x;
this.targetY = y;
this.dx = x - this.x;
this.dy = y - this.y;
this.time = time;
this.relative = false;
if (time === 0) {
this.x = x;
this.y = y;
this.end = true;
return Promise.resolve(this);
}
this.end = false;
this.timing = timing ?? linear();
this.line = true;
return new Promise(res => {
this.promiseFunc = () => res(this);
});
}
moveAs(curve: TimingFn<2>, time: number, timing?: TimingFn): Promise<this> {
this.time = time;
this.line = false;
this.relative = false;
this.startX = this.x;
this.startY = this.y;
if (time === 0) {
const [tx, ty] = curve(1);
this.x = tx;
this.y = ty;
this.end = true;
return Promise.resolve(this);
}
this.end = false;
this.timing = timing ?? linear();
this.curve = curve;
return new Promise(res => {
this.promiseFunc = () => res(this);
});
}
moveRelative(
curve: TimingFn<2>,
time: number,
timing?: TimingFn
): Promise<this> {
this.time = time;
this.line = false;
this.relative = false;
this.startX = this.x;
this.startY = this.y;
if (time === 0) {
const [tx, ty] = curve(1);
this.x = tx + this.startX;
this.y = ty + this.startY;
this.end = true;
return Promise.resolve(this);
}
this.end = false;
this.timing = timing ?? linear();
this.curve = curve;
return new Promise(res => {
this.promiseFunc = () => res(this);
});
}
stepMoving(timestamp: number): boolean {
if (this.end) return false;
const dt = timestamp - this.startTime;
if (this.line) {
if (dt > this.time) {
this.x = this.targetX;
this.y = this.targetY;
this.end = true;
this.promiseFunc();
return false;
} else {
const timeProgress = dt / this.time;
const progress = this.timing(timeProgress);
this.x = this.startX + progress * this.dx;
this.y = this.startY + progress * this.dy;
}
} else {
if (dt > this.time) {
const [tx, ty] = this.curve(1);
if (this.relative) {
this.x = tx + this.startX;
this.y = ty + this.startY;
} else {
this.x = tx;
this.y = ty;
}
this.end = true;
this.promiseFunc();
return false;
} else {
const timeProgress = dt / this.time;
const progress = this.timing(timeProgress);
const [tx, ty] = this.curve(progress);
if (this.relative) {
this.x = tx + this.startX;
this.y = ty + this.startY;
} else {
this.x = tx;
this.y = ty;
}
}
}
return true;
}
enableFrameAnimate(): void {
this.renderer.vertex.enableDynamicFrameAnimate(this);
}
disableFrameAnimate(): void {
this.renderer.vertex.disableDynamicFrameAnimate(this);
}
setAlpha(alpha: number): void {
this.renderer.vertex.setDynamicAlpha(this, alpha);
}
destroy(): void {
this.renderer.deleteMoving(this);
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,11 @@
#version 300 es
precision highp float;
in vec3 v_texCoord;
out vec4 outColor;
uniform sampler2DArray u_sampler;
void main() {
outColor = texture(u_sampler, v_texCoord);
}
@@ -0,0 +1,18 @@
#version 300 es
precision highp float;
in vec2 a_position;
in vec2 a_texCoord;
out vec3 v_texCoord;
uniform float u_nowFrame;
uniform mat3 u_transform;
void main() {
// 背景图永远是全图都画,因此变换矩阵应该作用于纹理坐标
vec3 texCoord = vec3(a_texCoord, 1.0);
vec3 transformed = u_transform * texCoord;
v_texCoord = vec3(transformed.xy, u_nowFrame);
gl_Position = vec4(a_position, 0.0, 1.0);
}
@@ -0,0 +1,14 @@
#version 300 es
precision highp float;
precision mediump uint;
in vec4 v_texCoord;
out vec4 outColor;
uniform sampler2DArray u_sampler;
void main() {
vec4 texColor = texture(u_sampler, v_texCoord.xyz);
outColor = vec4(texColor.rgb, texColor.a * v_texCoord.a);
}
@@ -0,0 +1,26 @@
#version 300 es
precision highp float;
precision mediump uint;
in vec3 a_position;
in vec3 a_texCoord;
// x: 最大帧数,y: 偏移池索引,实例化绘制传入
in ivec2 a_offsetData;
// 不透明度,用于前景层虚化
in float a_alpha;
// x,y,z: 纹理坐标,w: 不透明度
out vec4 v_texCoord;
uniform vec2 u_offsetPool[$1];
uniform uint u_nowFrame;
uniform mat3 u_transform;
void main() {
// 偏移量
uint offset = mod(u_nowFrame, a_offsetData.x);
float fOffset = float(offset);
// 贴图坐标
v_texCoord = vec4(a_texCoord.xy + u_offsetPool[a_offsetData.y] * fOffset, a_texCoord.z, a_alpha);
gl_Position = vec4(u_transform * a_position, 1.0);
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,105 @@
import { Transform } from '@motajs/render-core';
import {
IBlockData,
IMapRenderArea,
IMapRenderData,
IMapRenderer,
IMapVertexBlock,
IMapVertexGenerator,
IMapViewportController
} from './types';
import { clamp } from 'lodash-es';
export class MapViewport implements IMapViewportController {
transform: Transform = new Transform();
/** 顶点生成器 */
readonly vertex: IMapVertexGenerator;
constructor(readonly renderer: IMapRenderer) {
this.vertex = renderer.vertex;
}
getRenderArea(): IMapRenderData {
const { cellWidth, cellHeight, renderWidth, renderHeight } =
this.renderer;
const { blockWidth, blockHeight, width, height } = this.vertex.block;
// 减一是因为第一个像素是 0,所以最后一个像素就是宽度减一
const r = renderWidth - 1;
const b = renderHeight - 1;
// 其实只需要算左上角和右下角就行了
const [left, top] = this.transform.transformed(0, 0);
const [right, bottom] = this.transform.transformed(r, b);
const cl = left / cellWidth;
const ct = top / cellHeight;
const cr = right / cellWidth;
const cb = bottom / cellHeight;
const blockLeft = clamp(Math.floor(cl / blockWidth), 0, width - 1);
const blockRight = clamp(Math.floor(cr / blockWidth), 0, width - 1);
const blockTop = clamp(Math.floor(ct / blockHeight), 0, height - 1);
const blockBottom = clamp(Math.floor(cb / blockHeight), 0, height - 1);
const renderArea: IMapRenderArea[] = [];
const updateArea: IMapRenderArea[] = [];
const blockList: IBlockData<IMapVertexBlock>[] = [];
// 使用这种方式的话,索引在换行之前都是连续的,方便整合
for (let ny = blockTop; ny <= blockBottom; ny++) {
const first = this.vertex.block.getBlockByLoc(blockLeft, ny)!;
const last = this.vertex.block.getBlockByLoc(blockRight, ny)!;
if (first.data.dirty) {
blockList.push(first);
}
if (last.data.dirty) {
blockList.push(last);
}
renderArea.push({
startIndex: first.data.startIndex,
endIndex: last.data.endIndex,
count: last.data.endIndex - first.data.startIndex
});
for (let nx = blockLeft + 1; nx < blockRight; nx++) {
const block = this.vertex.block.getBlockByLoc(nx, ny)!;
if (block.data.dirty) {
blockList.push(block);
}
}
}
if (blockList.length > 0) {
if (blockList.length === 1) {
const block = blockList[0];
updateArea.push(block.data);
} else {
let continuousStart: IBlockData<IMapVertexBlock> = blockList[0];
let continuousLast: IBlockData<IMapVertexBlock> = blockList[0];
for (let i = 1; i < blockList.length; i++) {
const block = blockList[i];
if (block.index === continuousLast.index + 1) {
// 连续则合并
continuousLast = block;
} else {
const start = continuousStart.data.startIndex;
const end = continuousLast.data.endIndex;
updateArea.push({
startIndex: start,
endIndex: end,
count: end - start
});
continuousStart = block;
continuousLast = block;
}
}
}
}
return {
render: renderArea,
dirty: updateArea,
blockList
};
}
bindTransform(transform: Transform): void {
this.transform = transform;
}
}
@@ -19,6 +19,20 @@ export const MAP_HEIGHT = CELL_SIZE * MAP_BLOCK_HEIGHT;
export const HALF_MAP_WIDTH = MAP_WIDTH / 2;
/** 地图高度的一半 */
export const HALF_MAP_HEIGHT = MAP_HEIGHT / 2;
/**
* 动态内容预留,不明白含义的话不要动。地图上所有正在移动的图块称为动态内容,这些内容的数量无法预测,因此需要预留数组大小,
* 如果不够再临时扩充。如果你的塔中有大量的移动操作,可以适当提高此值,避免频繁的内存扩充行为,可以一定程度上提高性能表现。
*/
export const DYNAMIC_RESERVE = 16;
/**
* 移动图块容忍度,不明白含义的话不要动。如果移动图块的数量长期小于当前预留数量,那么将会降低预留数量,提升性能表现。
* 调整此值可以调整频率,值越大,越不容易因为数量小于预留数量而减小预留。
*/
export const MOVING_TOLERANCE = 60;
/** 每个格子的默认宽度,现阶段用处不大 */
export const CELL_WIDTH = 32;
/** 每个格子的默认高度,现阶段用处不大 */
export const CELL_HEIGHT = 32;
//#region 状态栏
@@ -56,6 +56,7 @@ import {
LayerGroup
} from '../elements';
import { isNil } from 'lodash-es';
import { materials } from '@user/client-base';
const MainScene = defineComponent(() => {
//#region 基本定义
@@ -267,8 +268,25 @@ const MainScene = defineComponent(() => {
core.doRegisteredAction('onmove', bx, by, ev.offsetX, ev.offsetY);
};
const testRender = (canvas: MotaOffscreenCanvas2D) => {
const tileset = materials.getAsset(0);
if (!tileset) return;
canvas.ctx.drawImage(
tileset.data.texture.source,
0,
0,
canvas.width,
canvas.height
);
};
return () => (
<container id="main-scene" width={MAIN_WIDTH} height={MAIN_HEIGHT}>
<sprite
render={testRender}
loc={[180, 0, 480, 480]}
zIndex={1000}
/>
<LeftStatusBar
loc={[0, 0, STATUS_BAR_WIDTH, STATUS_BAR_HEIGHT]}
status={leftStatus}