Handler.js
import { 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 = state.gold + 1
handleKeyPress(term, state)
}
for (let i = 0; i < state.producers.length; i++) {
if (String.fromCharCode(data.code) ===
${state.producers.id}`) {
state.gold = state.gold - state.producers.cost
state.producers.cost = state.producers.cost * state.producers.growthRate
state.producers.count = state.producers.count + 1
state.productionRate += state.producers.baseProduction;
}
}
}
}
export const handleStateChange = (term, state) => () => {
return updateGold(term, state)
}
import { 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 = state.gold + 1
handleKeyPress(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.producers.count + 1
state.productionRate += state.producers.baseProduction;
}
}
}
}
export const handleStateChange = (term, state) => () => {
return updateGold(term, state)
}
**Function.js**
export const init = (term) => {
term('Welcome to the mining game!')
term.hideCursor()
term.grabInput()
term.clear()
}
export const updateGold = (term,state) => {
term.moveTo(25,2)
term.eraseLineAfter()
term.bold.yellow(state.gold + " ")
state.gold += state.productionRAte
}
export const handleStateChange = (term, state) => {
return updateGold(term,state)
}
**gameEngine**
import {init, updateGold} from './functions.js'
import { handleKeyPress, handleStateChange } from './handlers.js'
export const startMiningGame = (term,state) => {
init(term)
setInterval(() => handleStateChange(term,state), 1000)
term.on("key", handler)
const handler = handleKeyPress(term,state)
}
`
**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,
}
**