refactor: 移动 & 按键微调

This commit is contained in:
AncTe committed 2024-08-21 13:21:56 +08:00
1 parent 433c4580fe
commit b0f9e9d17d
26 files changed
+444 -130

No files matched your search

+23
View File
@@ -1,4 +1,5 @@
type AdapterFunction<T> = (item: T, ...params: any[]) => Promise<any>;
type SyncAdapterFunction<T> = (item: T, ...params: any[]) => any;
/**
* 渲染适配器,用作渲染层与数据层沟通的桥梁,用于在数据层等待渲染层执行,常用与动画等。
@@ -14,6 +15,7 @@ export class RenderAdapter<T> {
id: string;
private execute: Map<string, AdapterFunction<T>> = new Map();
private syncExecutes: Map<string, SyncAdapterFunction<T>> = new Map();
constructor(id: string) {
this.id = id;
@@ -42,6 +44,14 @@ export class RenderAdapter<T> {
this.execute.set(id, fn);
}
/**
* 设置同步执行函数
* @param fn 对于每个元素执行的函数
*/
recieveSync(id: string, fn: SyncAdapterFunction<T>): void {
this.syncExecutes.set(id, fn);
}
/**
* 对所有元素执行函数,当所有元素都运行完毕后兑现,类似于Promise.all
* @returns 包含每个元素运行结果的数组
@@ -72,6 +82,19 @@ export class RenderAdapter<T> {
}
}
/**
* 对所有元素执行同步函数
* @returns 包含每个元素运行结果的数组
*/
syncAll<R = any>(fn: string, ...params: any[]): R[] {
const execute = this.syncExecutes.get(fn);
if (!execute) {
return [];
} else {
return [...this.items].map(v => execute!(v, ...params));
}
}
/**
* 销毁这个adapter
*/
+7 -2
View File
@@ -77,14 +77,19 @@ class TextureCache extends EventEmitter<TextureCacheEvent> {
/** 自动元件额外连接信息,用于对非自身图块进行连接 */
autoConn: Map<number, Set<number>> = new Map();
/** 行走图朝向绑定 */
characterDirection: Record<Dir, number> = {
characterDirection: Record<Dir2, number> = {
down: 0,
left: 1,
right: 2,
up: 3
up: 3,
leftup: 1,
leftdown: 1,
rightup: 2,
rightdown: 2
};
/** 行走图转向顺序 */
characterTurn: Dir[] = ['up', 'right', 'down', 'left'];
characterTurn2: Dir2[] = ['leftup', 'rightup', 'rightdown', 'leftdown'];
constructor() {
super();
+25 -12
View File
@@ -45,7 +45,7 @@ export class HeroRenderer
/** 上一次帧数切换的时间 */
private lastFrameTime: number = 0;
/** 当前的移动方向 */
private moveDir: Move = 'down';
private moveDir: Move2 = 'down';
/** 上一步走到格子上的时间 */
private lastStepTime: number = 0;
/** 是否已经执行了当前步移动 */
@@ -54,11 +54,11 @@ export class HeroRenderer
* 这一步的移动方向,与{@link moveDir}不同的是,在这一步走完之前,它都不会变,
* 当这一步走完之后,才会将其设置为{@link moveDir}的值
*/
private stepDir: Dir = 'down';
private stepDir: Dir2 = 'down';
/** 每步的格子增量 */
private stepDelta: Loc = { x: 0, y: 1 };
/** 动画显示的方向,用于适配后退 */
private animateDir: Dir = 'down';
// private animateDir: Dir = 'down';
/**
* 设置勇士所用的图片资源
@@ -105,9 +105,9 @@ export class HeroRenderer
* 根据方向获取勇士的裁切信息
* @param dir 方向
*/
getRenderFromDir(dir: Move): [number, number, number, number][] {
getRenderFromDir(dir: Move2): [number, number, number, number][] {
if (!this.cellWidth || !this.cellHeight) return [];
let resolved: Dir;
let resolved: Dir2;
if (dir === 'forward') resolved = this.dir;
else if (dir === 'backward') resolved = backDir(this.dir);
else resolved = dir;
@@ -157,6 +157,8 @@ 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;
@@ -175,14 +177,14 @@ export class HeroRenderer
if (this.moveDir === 'backward') this.stepDir = backDir(this.stepDir);
else if (this.moveDir !== 'forward') this.stepDir = this.moveDir;
this.lastStepTime = Date.now();
this.stepDelta = core.utils.scan[this.stepDir];
this.stepDelta = core.utils.scan2[this.stepDir];
this.turn(this.stepDir);
}
/**
* 移动勇士
*/
move(dir: Move): Promise<void> {
move(dir: Move2): Promise<void> {
if (this.status !== 'moving') {
logger.error(
12,
@@ -225,12 +227,19 @@ export class HeroRenderer
* 勇士转向,不填表示顺时针转一个方向
* @param dir 移动方向
*/
turn(dir?: Dir): void {
turn(dir?: Dir2): void {
if (!dir) {
const index = texture.characterTurn.indexOf(this.stepDir) + 1;
const length = texture.characterTurn.length;
const next = texture.characterTurn[index % length];
return this.turn(next);
const index = texture.characterTurn2.indexOf(this.stepDir);
if (index === -1) {
const length = texture.characterTurn.length;
const index = texture.characterTurn.indexOf(
this.stepDir as Dir
);
const next = texture.characterTurn[index % length];
return this.turn(next);
} else {
return this.turn(texture.characterTurn[index]);
}
}
this.moveDir = dir;
if (!this.renderable) return;
@@ -333,3 +342,7 @@ adapter.recieve(
return item.moveAs(x, y, time, fn);
}
);
adapter.recieve('setMoveSpeed', (item, speed: number) => {
item.setMoveSpeed(speed);
return Promise.resolve();
});
+10 -10
View File
@@ -163,16 +163,16 @@ Mota.require('var', 'hook').once('reset', () => {
layer.requestAfterFrame(() => {
hero.setImage(core.material.images.images['hero2.png']);
});
hero.readyMove();
layer.delegateTicker(
() => {
hero.move('right');
},
10000,
() => {
hero.endMove();
}
);
// hero.readyMove();
// layer.delegateTicker(
// () => {
// hero.move('right');
// },
// 10000,
// () => {
// hero.endMove();
// }
// );
camera.move(240, 240);
render.update();