functions
export const init = (term) => {
term.clear();
term ('Welcome to the mining game!');
term.hideCursor();
term.grabInput();
}
export const updateGold = (term, state) => {
term.moveTo(25, 2);
term.eraseLineAfter();
state.gold += state.productionRate;
term.bold.yellow(state.gold + ' ');
}
export const checkInitCompleted = (term, state) => {
term.moveTo(1, 1);
term.eraseLineAfter();
term.white('You can purchase producers by clicking the number button (1,2,3, ...)');
term.moveTo(1, 2);
term.yellow('GOLD:');
term.moveTo(1, 3);
term.yellow('PRODUCTION RATE:');
state.isInitCopleted = true;
}
handlers
import { checkInitCompleted, updateGold } from "./functions.js";
export const handleKeyPress = (term, state) =>{
return (name, matches, data) => {
if (String.fromCharCode(data.code)==='g'||String.fromCharCode(data.code)==='G') {
state.gold++;
}
let minimalProducerCost = state.producers[0].cost;
for (const i in state.producers) {
if (minimalProducerCost>state.producers[i].cost) {
minimalProducerCost = state.producers[i].cost;
}
}
if ((state.gold>=minimalProducerCost)&&(state.isInitCompleted===false)) {
checkInitCompleted(term, state);
}
for (const i in state.producers) {
if ((String.fromCharCode(data.code)===String(state.producers[i].id)) && (state.gold>=state.producers[i].cost)) {
state.gold -= state.producers[i].cost;
state.producers[i].cost *= state.producers[i].growthRate;
state.producers[i].count++;
state.productionRate += state.producers[i].baseProduction;
break;
}
}
}
}
export const handleStateChange = (term, state) => {
return () => {
updateGold(term, state);
}
}