Coderslang_Master Спасибо, сенсей. С мёртвой точки сдвинулся. Сейчас ошибка такая:
Код нынче такой:
functions.js
export const init = (term) => {
term.clear();
term.hideCursor();
term.grabInput();
term('Welcome to the mining game!');
}
export const updateGold = (term, state) => {
term.moveTo(30, 2);
term.eraseLineAfter();
state.gold+=state.productionRate;
term.moveTo(30, 2);
term.bold.yellow(state.gold);
}
export const checkInitCompleted = (term,state) =>{
term.moveTo(25, 1);
term("You can purchase producers by clicking the number button (1, 2, 3, ...)");
term.moveTo(25, 2);
term(`GOLD:${state.gold}`);
term.moveTo(25, 3);
term(`PRODUCTION RATE:${state.productionRate}`);
state.isInitCompleted=true;
}
handlers.js
import { term } from "./constants.js";
import {updateGold, checkInitCompleted} from "./functions.js"
export const handleStateChange = (term, state) =>{
updateGold(term,state);
return (name, matches, data)=> {
};
}
export const handleKeyPress = (term, state) => {
return (name, matches, data)=> {
let minCost=state.producers[0].cost;
for(let i=1;i<state.producers.length;i++){
if(state.producers[i].cost<minCost){
minCost=state.producers[i].cost;
}
}
if((state.isInitCompleted==false)&&(state.gold>=minCost)){
checkInitCompleted(term,state);
}
let curChar=String.fromCharCode(data.code);
if(curChar=="G" || curChar=="g"){
state.gold++;
updateGold(term,state);
}
//press digit
else if(Number(curChar)>=0 && Number(curChar)<=9){
for (let i = 0; i < state.producers.length; i++) {
if ((Number(curChar) == state.producers[i].id)) {
if(state.producers[i].cost<=state.gold){
state.gold -= state.producers[i].cost;
state.producers[i].cost *= state.producers[i].growthRate;
state.producers[i].count++;
state.productionRate+=state.producers[i].baseProduction;
}else{
term.bold.red("Not enough golD");
}
break;
}
}
}else if(curChar=='/'){
process.exit(0);
}else{console.log("Unnkown Char. Press other key");}
};
}
Всё равно не работает setInterval . Не тикает (
gameEngine.js
import {init} from "./functions.js"
import {handleKeyPress, handleStateChange} from './handlers.js'
export const startMiningGame = (term, state) => {
init(term);
setInterval(handleStateChange(term, state), 1000);
term.on('key', handleKeyPress(term, state) );
}