Coderslang_Master
functions.js
export const init = (term) => {
term.clear ();
term ('Welcome to the mining game! Press \'G\' to start!');
term.hideCursor();
term.grabInput();
}
export const updateGold = (term, state) => {
state.gold+=state.productionRate;
term.moveTo(25,2);
term.eraseLineAfter();
term.bold.yellow(state.gold + ' ');
}
export const checkInitCompleted = (term, state) => {
term.moveTo(1,1);
term.eraseLineAfter();
term('You can purchase producers by clicking the number button (1, 2, 3, ...)');
term.moveTo(1,2);
term ('GOLD:')
term.moveTo(1,3);
term ('PRODUCTION RATE: ')
}
constants.js
import terminalKit from 'terminal-kit';
export const term = terminalKit.terminal;
export let config = {gold: 0, productionRate: 0, isInitCompleted: false ,isProducerListUpdated: true, 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 } ]
};
gameEngine.js
import { handleKeyPress,handleStateChange } from './handlers.js';
import {init} from './functions.js';
export const startMiningGame = (term,state) => {
init(term);
term.on('key', handleKeyPress (term, state));
handleKeyPress (term, state);
setInterval(handleStateChange(term,state),1000);
}
handlers.js
import { updateGold,checkInitCompleted } from "./functions.js";
export const handleStateChange = (term, state) => () => {
updateGold(term, state);
}
export const handleKeyPress = (term, state) => {
return (name, matches, data) => {
if (String.fromCharCode(data.code) === 'G' || String.fromCharCode(data.code) === 'g') {
state.gold++;
}
for (let i=0;i<state.producers.length;i++){
if (String.fromCharCode(data.code) === `${state.producers[i].id}`) {
state.gold-= state.producers[i].cost;
state.producers[i].cost=state.producers[i].cost*state.producers[i].growthRate;
state.producers[i].count++;
state.productionRate+=state.producers[i].baseProduction;
}
}
if (state.gold>=10 && state.isInitCompleted === false) {
checkInitCompleted (term,state);
state.checkInitCompleted = true;
}
}
}