diff --git a/src/initPlugin.ts b/src/initPlugin.ts index 7236ef6..e5d3c91 100644 --- a/src/initPlugin.ts +++ b/src/initPlugin.ts @@ -18,6 +18,7 @@ import completion, { floors } from './plugin/completion'; import path from './plugin/fx/path'; import gameCanvas from './plugin/fx/gameCanvas'; import noise from './plugin/fx/noise'; +import smooth from './plugin/fx/smoothView'; function forward() { const toForward: any[] = [ @@ -40,7 +41,8 @@ function forward() { completion(), path(), gameCanvas(), - noise() + noise(), + smooth() ]; // 初始化所有插件,并转发到core上 diff --git a/src/plugin/fx/smoothView.ts b/src/plugin/fx/smoothView.ts new file mode 100644 index 0000000..4999c21 --- /dev/null +++ b/src/plugin/fx/smoothView.ts @@ -0,0 +1,63 @@ +import { debounce } from 'lodash-es'; +import { Transition, hyper } from 'mutate-animate'; + +const tran = new Transition(); +tran.value.x = 0; +tran.value.y = 0; + +let needSmooth = false; + +export default function init() { + tran.ticker.add(() => { + if (core.isPlaying() && needSmooth) { + core.setViewport(tran.value.x, tran.value.y); + } + }); + + const func = debounce(() => { + needSmooth = false; + }, 700); + + control.prototype._drawHero_updateViewport = function ( + x: number, + y: number, + offset: Loc + ) { + const ox = core.clamp( + (x - core._HALF_WIDTH_) * 32 + offset.x, + 0, + Math.max(32 * core.bigmap.width - core._PX_, 0) + ); + const oy = core.clamp( + (y - core._HALF_HEIGHT_) * 32 + offset.y, + 0, + Math.max(32 * core.bigmap.height - core._PY_, 0) + ); + + tran.transition('x', ox).transition('y', oy); + + needSmooth = true; + func(); + }; + + let time2 = Date.now(); + const origin1 = control.prototype._moveAction_moving; + control.prototype._moveAction_moving = function (...params: any[]) { + if (Date.now() - time2 > 20) + tran.mode(hyper('sin', 'out')).time(200).absolute(); + origin1.call(this, ...params); + }; + + const origin2 = control.prototype.moveDirectly; + control.prototype.moveDirectly = function (...params: any[]) { + time2 = Date.now(); + tran.mode(hyper('sin', 'out')).time(600).absolute(); + origin2.call(this, ...params); + }; + + const origin3 = events.prototype._changeFloor_beforeChange; + events.prototype._changeFloor_beforeChange = function (...params: any[]) { + tran.time(1).absolute(); + origin3.call(this, ...params); + }; +}