На 5м этапе в задании сказано: " P.S. Помни, что мы импортируем term и config только в solution.js и дальше пробрасываем их как term и state.
Имена для этих внутренних параметров ты можешь выбрать любые в теории, нам важна сама структура."
Вопрос: почему бы просто не оставить название config? Проверочный движок придирчив к имени state в дальнейшем, и лично у меня возникла небольшая путаница...
Этап 13 (завершен).
gameEngine.js
import {init} from './functions.js'
import {handleKeyPress, handleStateChange} from './handlers.js'
export const startMiningGame = (term,state) => {
init(term);
term.on('key', handleKeyPress(term, state));
setInterval(handleStateChange('timer'), 1000);
};
solution.js
// LET'S DIG SOME GOLD!
import {startMiningGame} from './gameEngine.js'
import {term, config} from './constants.js'
startMiningGame(term, config);
constants.js
import terminalKit from 'terminal-kit';
export const term = terminalKit.terminal;
export const config = {
gold:0,
producers:[
{ id: 1, title: 'Miner', cost: 10, growthRate: 1.13, baseProduction: 0.1, count: 0 },
{ id: 2, title: 'Adventurer', cost: 100, growthRate: 1.17, baseProduction: 1, count: 0 },
{ id: 3, title: 'Professional', cost: 1200, growthRate: 1.14, baseProduction: 9, count: 0 }
],
productionRate:0,
isInitCompleted: false,
isProducerListUpdated: true
}
functions.js
import { config } from "./constants.js";
export const init = (term) => {
term('Welcome to the mining game!');
term.clear();
term.hideCursor();
term.grabInput();
};
export const updateGold = (term, state) =>{
term.moveTo(25, 2);
term.eraseLineAfter();
term.bold.yellow(state.gold+" ");
state.gold += state.productionRate;
};
export const checkInitCompleted = (term, state) =>{
term.clear();
term.moveTo(25, 0);
term('You can purchase producers by clicking the number button (1, 2, 3, ...)');
term.moveTo(25, 2);
term('GOLD:');
term.moveTo(25, 3);
term('PRODUCTION RATE:');
state.isInitCompleted = true;
state.isProducerListUpdated = false;
};
export const updateProducerList = (term, state) =>{
term.moveTo(1, 5);
for(let i = 0; i < state.producers.length; i++){
if (state.producers.count >= 0){
term(${state.producers[i].title}: ${state.producers[i].count} | Production per second: ${state.producers[i].baseProduction.toFixed(1)} | Cost: ${state.producers[i].cost.toFixed(1)}
);
term.eraseLineAfter();
}
}
state.isProducerListUpdated = true;
}
handlers.js
import { checkInitCompleted, updateGold, updateProducerList } from './functions.js';
export const handleKeyPress = (term, state) => {
return (name, matches, data) => {
if (String.fromCharCode(data.code) === "g" || String.fromCharCode(data.code) === "G") {
return state.gold++;
}
let minimalProducerCost = state.producers[0].cost;
for (const i in state.producers) {
if ( minimalProducerCost > state.producers.cost ) {
minimalProducerCost = state.producers.cost;
}
}
if ((state.gold >= minimalProducerCost) && (state.isInitCompleted === false)) {
checkInitCompleted(term, state);
}
if (state.isProducerListUpdated === false) {
updateProducerList(term, state);
}
for (let i = 0; i < state.producers.length; i++){
if (String.fromCharCode(data.code) === ${state.producers[i].id}
){
state.gold = state.gold - state.producers.cost;
state.producers.cost = state.producers.cost * state.producers.growthRate;
state.producers.count++;
state.productionRate += state.producers.baseProduction;
}
}}
}
export const handleStateChange = (term, state) => () => {
updateGold(term, state);
};