feat: 勇士朝前或朝后移动

This commit is contained in:
unanmed 2024-08-20 18:29:55 +08:00
parent 5e54cc9424
commit 433c4580fe
2 changed files with 20 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import { logger } from '@/core/common/logger';
import EventEmitter from 'eventemitter3'; import EventEmitter from 'eventemitter3';
import { texture } from '../cache'; import { texture } from '../cache';
import { TimingFn } from 'mutate-animate'; import { TimingFn } from 'mutate-animate';
import { backDir } from '@/plugin/game/utils';
type HeroMovingStatus = 'stop' | 'moving' | 'moving-as'; type HeroMovingStatus = 'stop' | 'moving' | 'moving-as';
@ -36,13 +37,15 @@ export class HeroRenderer
/** 勇士移动速度 */ /** 勇士移动速度 */
speed: number = 100; speed: number = 100;
/** 当前勇士朝向 */
dir: Dir = 'down';
/** 勇士移动定时器id */ /** 勇士移动定时器id */
private moveId: number = -1; private moveId: number = -1;
/** 上一次帧数切换的时间 */ /** 上一次帧数切换的时间 */
private lastFrameTime: number = 0; private lastFrameTime: number = 0;
/** 当前的移动方向 */ /** 当前的移动方向 */
private moveDir: Dir = 'down'; private moveDir: Move = 'down';
/** 上一步走到格子上的时间 */ /** 上一步走到格子上的时间 */
private lastStepTime: number = 0; private lastStepTime: number = 0;
/** 是否已经执行了当前步移动 */ /** 是否已经执行了当前步移动 */
@ -54,6 +57,8 @@ export class HeroRenderer
private stepDir: Dir = 'down'; private stepDir: Dir = 'down';
/** 每步的格子增量 */ /** 每步的格子增量 */
private stepDelta: Loc = { x: 0, y: 1 }; private stepDelta: Loc = { x: 0, y: 1 };
/** 动画显示的方向,用于适配后退 */
private animateDir: Dir = 'down';
/** /**
* *
@ -100,13 +105,19 @@ export class HeroRenderer
* *
* @param dir * @param dir
*/ */
getRenderFromDir(dir: Dir): [number, number, number, number][] { getRenderFromDir(dir: Move): [number, number, number, number][] {
if (!this.cellWidth || !this.cellHeight) return []; if (!this.cellWidth || !this.cellHeight) return [];
const index = texture.characterDirection[dir]; let resolved: Dir;
if (dir === 'forward') resolved = this.dir;
else if (dir === 'backward') resolved = backDir(this.dir);
else resolved = dir;
const index = texture.characterDirection[resolved];
const y = index * this.cellHeight; const y = index * this.cellHeight;
return [0, 1, 2, 3].map(v => { const res: [number, number, number, number][] = [0, 1, 2, 3].map(v => {
return [v * this.cellWidth!, y, this.cellWidth!, this.cellHeight!]; return [v * this.cellWidth!, y, this.cellWidth!, this.cellHeight!];
}); });
if (dir === 'backward') return res.reverse();
else return res;
} }
/** /**
@ -161,7 +172,8 @@ export class HeroRenderer
* *
*/ */
private step() { private step() {
this.stepDir = this.moveDir; if (this.moveDir === 'backward') this.stepDir = backDir(this.stepDir);
else if (this.moveDir !== 'forward') this.stepDir = this.moveDir;
this.lastStepTime = Date.now(); this.lastStepTime = Date.now();
this.stepDelta = core.utils.scan[this.stepDir]; this.stepDelta = core.utils.scan[this.stepDir];
this.turn(this.stepDir); this.turn(this.stepDir);
@ -170,7 +182,7 @@ export class HeroRenderer
/** /**
* *
*/ */
move(dir: Dir): Promise<void> { move(dir: Move): Promise<void> {
if (this.status !== 'moving') { if (this.status !== 'moving') {
logger.error( logger.error(
12, 12,
@ -215,7 +227,7 @@ export class HeroRenderer
*/ */
turn(dir?: Dir): void { turn(dir?: Dir): void {
if (!dir) { if (!dir) {
const index = texture.characterTurn.indexOf(this.moveDir) + 1; const index = texture.characterTurn.indexOf(this.stepDir) + 1;
const length = texture.characterTurn.length; const length = texture.characterTurn.length;
const next = texture.characterTurn[index % length]; const next = texture.characterTurn[index % length];
return this.turn(next); return this.turn(next);

2
src/types/util.d.ts vendored
View File

@ -602,7 +602,7 @@ type TextPosition = 'up' | 'center' | 'down';
/** /**
* *
*/ */
type Move = 'forward' | Dir; type Move = 'forward' | 'backward' | Dir;
/** /**
* *