tetris/js/animationManager.js

52 lines
2.1 KiB
JavaScript

class AnimationManager {
constructor(eventBus) {
this.eventBus = eventBus;
this.eventBus.on("animate", ({count, time, indexes, matrix})=>{
return this.start(count, time, indexes, matrix);
})
}
start(count, time, indexes, matrix){
let cnt = count;
this.matrixForAnimation = matrix;
this.blinkedLineIndexes = indexes;
return new Promise((resolve) => {
//Отпишусь от событий клавиатуры.
/* this.eventBus.emit("set-is-animating",{state:true});
this.eventBus.emit("stop-gameplay", {})
this.eventBus.emit("pause-main-render",{});
this.eventBus.emit("resume-animation-render",{});*/
this.eventBus.emit("set-render-mode", {mode:"animation"})
// Здесь я отпишусь от основного рендера
//Подпишусь на рендер анимации.
const recursion = ()=>{
this.animation(cnt);
cnt--;
if(cnt === 0){
//Подпишусь обратно на события клавиатуры.
// Здесь я отпишусь от анимационного рендера
//Подпишусь на основной рендер.
/*this.eventBus.emit("set-is-animating",{state:false});
this.eventBus.emit("start-gameplay", {});
this.eventBus.emit("pause-animation-render",{});
this.eventBus.emit("resume-main-render",{});*/
this.eventBus.emit("set-render-mode", {mode:"game"})
return resolve();
}
setTimeout(recursion, time)
}
setTimeout(recursion, time);
})
}
animation(cnt){
console.log(cnt);
this.eventBus.emit("animation-render", {
matrix: this.matrixForAnimation,
indexes: this.blinkedLineIndexes,
count: cnt
});
}
}