feat: 勇士移动

This commit is contained in:
AncTe committed 2024-08-21 21:29:47 +08:00
1 parent fdd4563f45
commit b4159159a7
14 files changed
+355 -45

No files matched your search

+2 -1
View File
@@ -58,6 +58,7 @@ export class RenderAdapter<T> {
*/
all<R = any>(fn: string, ...params: any[]): Promise<R[]> {
const execute = this.execute.get(fn);
if (!execute) {
return Promise.reject();
} else {
@@ -86,7 +87,7 @@ export class RenderAdapter<T> {
* 对所有元素执行同步函数
* @returns 包含每个元素运行结果的数组
*/
syncAll<R = any>(fn: string, ...params: any[]): R[] {
sync<R = any>(fn: string, ...params: any[]): R[] {
const execute = this.syncExecutes.get(fn);
if (!execute) {
return [];
+5 -3
View File
@@ -196,7 +196,8 @@ export class BlockCacher<T> extends EventEmitter<BlockCacherEvent> {
deep: number = 2 ** 31 - 1
) {
const [bx, by] = this.getBlockXY(x, y);
const [ex, ey] = this.getBlockXY(x + width, y + height);
const [ex, ey] = this.getBlockXY(x + width - 1, y + height - 1);
return this.updateArea(bx, by, ex - bx, ey - by, deep);
}
@@ -213,6 +214,7 @@ export class BlockCacher<T> extends EventEmitter<BlockCacherEvent> {
deep: number = 2 ** 31 - 1
) {
const blocks = this.getIndexOf(x, y, width, height);
blocks.forEach(v => {
this.clearCache(v, deep);
});
@@ -229,8 +231,8 @@ export class BlockCacher<T> extends EventEmitter<BlockCacherEvent> {
const ex = Math.min(x + width, this.blockData.width);
const ey = Math.min(y + height, this.blockData.height);
for (let nx = sx; nx < ex; nx++) {
for (let ny = sy; ny < ey; ny++) {
for (let nx = sx; nx <= ex; nx++) {
for (let ny = sy; ny <= ey; ny++) {
const index = this.getIndex(nx, ny);
res.add(index);
}
+16 -4
View File
@@ -28,6 +28,8 @@ export class FloorDamageExtends implements ILayerGroupRenderExtends {
group!: LayerGroup;
sprite!: Damage;
static listenedDamage: Set<FloorDamageExtends> = new Set();
/**
* 立刻刷新伤害渲染
*/
@@ -52,12 +54,16 @@ export class FloorDamageExtends implements ILayerGroupRenderExtends {
}
private onUpdate = (floor: FloorIds) => {
this.update(floor);
this.sprite.requestBeforeFrame(() => {
this.update(floor);
});
};
private onSetBlock = (x: number, y: number, floor: FloorIds) => {
if (floor !== this.sprite.enemy?.floorId) return;
this.sprite.updateEnemyOn(x, y);
this.sprite.enemy?.once('extract', () => {
if (floor !== this.sprite.enemy?.floorId) return;
this.sprite.updateEnemyOn(x, y);
});
};
/**
@@ -83,12 +89,14 @@ export class FloorDamageExtends implements ILayerGroupRenderExtends {
);
group.removeExtends('floor-damage');
}
FloorDamageExtends.listenedDamage.add(this);
});
}
onDestroy(group: LayerGroup): void {
this.floorBinder.off('update', this.onUpdate);
this.floorBinder.off('setBlock', this.onSetBlock);
FloorDamageExtends.listenedDamage.delete(this);
}
}
@@ -242,15 +250,17 @@ export class Damage extends Sprite {
} else {
data.set(index, enemy);
}
this.update(this);
// 渲染懒更新,优化性能表现
if (!this.needUpdateBlock) {
this.needUpdateBlocks.add(block);
this.requestBeforeFrame(() => {
this.needUpdateBlock = false;
this.needUpdateBlocks.forEach(v => this.updateBlock(v, false));
// todo: 阻击夹域等地图伤害检测是否必要更新,例如不包含阻击夹域的怪就不必要更新这个怪物信息
this.extractAllMapDamage();
// this.extractAllMapDamage();
});
this.needUpdateBlock = true;
}
@@ -264,8 +274,10 @@ export class Damage extends Sprite {
private updateBlock(block: number, map: boolean = true) {
const data = this.blockData.get(block);
if (!data) return;
this.block.clearCache(block, 1);
const renderable = this.renderable.get(block)!;
renderable.clear();
data.forEach(v => this.extract(v, renderable));
if (map) this.extractMapDamage(block, renderable);
}
+2
View File
@@ -117,6 +117,8 @@ export class LayerGroupFloorBinder
if (!ex) return;
ex.setBlock(block, x, y);
console.log(block, x, y);
const floor = this.bindThisFloor ? core.status.floorId : this.floor!;
this.emit('setBlock', x, y, floor, block);
}
+52 -11
View File
@@ -6,6 +6,7 @@ import EventEmitter from 'eventemitter3';
import { texture } from '../cache';
import { TimingFn } from 'mutate-animate';
import { backDir } from '@/plugin/game/utils';
import { isNil } from 'lodash-es';
type HeroMovingStatus = 'stop' | 'moving' | 'moving-as';
@@ -48,8 +49,10 @@ export class HeroRenderer
private moveDir: Move2 = 'down';
/** 上一步走到格子上的时间 */
private lastStepTime: number = 0;
/** 是否已经执行了当前步移动 */
/** 执行当前步移动的Promise */
private moveDetached?: Promise<void>;
/** endMove的Promise */
private moveEnding?: Promise<void>;
/**
* 这一步的移动方向,与{@link moveDir}不同的是,在这一步走完之前,它都不会变,
* 当这一步走完之后,才会将其设置为{@link moveDir}的值
@@ -157,8 +160,6 @@ export class HeroRenderer
if (progress >= 1) {
this.renderable.x = x + dx;
this.renderable.y = y + dy;
console.log(x, y, dx, dy, this.renderable.x, this.renderable.y);
this.emit('stepEnd');
} else {
const rx = dx * progress + x;
@@ -212,15 +213,21 @@ export class HeroRenderer
*/
endMove(): Promise<void> {
if (this.status !== 'moving') return Promise.reject();
return new Promise<void>(resolve => {
this.once('stepEnd', () => {
this.status = 'stop';
this.movingFrame = 0;
this.layer.removeTicker(this.moveId);
this.render();
resolve();
if (this.moveEnding) return this.moveEnding;
else {
const promise = new Promise<void>(resolve => {
this.once('stepEnd', () => {
this.moveEnding = void 0;
this.status = 'stop';
this.movingFrame = 0;
const { x, y } = core.status.hero.loc;
this.setHeroLoc(x, y);
this.render();
resolve();
});
});
});
return (this.moveEnding = promise);
}
}
/**
@@ -242,11 +249,29 @@ export class HeroRenderer
}
}
this.moveDir = dir;
this.stepDir = dir;
if (!this.renderable) return;
this.renderable.render = this.getRenderFromDir(this.moveDir);
this.layer.update(this.layer);
}
/**
* 设置勇士的坐标
* @param x 横坐标
* @param y 纵坐标
*/
setHeroLoc(x?: number, y?: number) {
if (!this.renderable) return;
if (!isNil(x)) {
this.renderable.x = x;
}
if (!isNil(y)) {
this.renderable.y = y;
}
this.layer.update(this.layer);
}
/**
* 按照指定函数移动勇士
* @param x 目标横坐标
@@ -313,6 +338,7 @@ export class HeroRenderer
this.moveId = layer.delegateTicker(() => {
this.moveTick(Date.now());
});
console.log(this);
}
onDestroy(layer: Layer): void {
@@ -346,3 +372,18 @@ adapter.recieve('setMoveSpeed', (item, speed: number) => {
item.setMoveSpeed(speed);
return Promise.resolve();
});
adapter.recieve('setHeroLoc', (item, x?: number, y?: number) => {
item.setHeroLoc(x, y);
return Promise.resolve();
});
adapter.recieve('turn', (item, dir: Dir2) => {
item.turn(dir);
return Promise.resolve();
});
// 同步fallback,用于适配现在的样板,之后会删除
adapter.recieveSync('setHeroLoc', (item, x?: number, y?: number) => {
item.setHeroLoc(x, y);
});
adapter.recieveSync('turn', (item, dir: Dir2) => {
item.turn(dir);
});
+7 -5
View File
@@ -906,7 +906,7 @@ export class Layer extends Container {
this.renderData.push(...data);
} else if (data.length === 1) {
// 特判单个图块的情况
const index = x + y + this.mapWidth;
const index = x + y * this.mapWidth;
this.renderData[index] = data[0];
} else {
// 限定更新区域
@@ -929,7 +929,8 @@ export class Layer extends Container {
if (calAutotile) this.calAutotiles(x, y, width, height);
this.updateBlocks(x, y, width, height);
this.updateBigImages(x, y, width, height);
this.update(this);
// this.update(this);
for (const ex of this.extend.values()) {
ex.onDataPut?.(this, data, width, x, y, calAutotile);
@@ -1099,6 +1100,7 @@ export class Layer extends Container {
height,
Layer.FRAME_ALL
);
this.update(this);
for (const ex of this.extend.values()) {
@@ -1195,7 +1197,7 @@ export class Layer extends Container {
*/
protected renderStatic(camera: Camera, need: NeedRenderData) {
const cell = this.cellSize;
const frame = (RenderItem.animatedFrame % 4) + 1;
const frame = RenderItem.animatedFrame % 4;
const { width } = this.block.blockData;
const blockSize = this.block.blockSize;
const { ctx } = this.staticMap;
@@ -1210,7 +1212,7 @@ export class Layer extends Container {
const y = Math.floor(v / width);
const sx = x * blockSize;
const sy = y * blockSize;
const index = v * 4 + frame - 1;
const index = v * 4 + frame;
const cache = this.block.cache.get(index);
if (cache) {
@@ -1241,7 +1243,7 @@ export class Layer extends Container {
if (num === 0 || num === 17) continue;
const data = texture.getRenderable(num);
if (!data || data.bigImage) continue;
const f = (frame - 1) % data.frame;
const f = frame % data.frame;
const i = data.animate === -1 ? f : data.animate;
const [isx, isy, w, h] = data.render[i];
const px = (nx - sx) * cell;