diff --git a/src/data/gameData.js b/src/data/gameData.js index aa1b0ca..2efb193 100644 --- a/src/data/gameData.js +++ b/src/data/gameData.js @@ -3,20 +3,41 @@ const gameData = { heat: 1, }; +let heatListeners = []; +function attachHeatListener(observer) { + if (heatListeners.includes(observer)) return false; + + heatListeners.push(observer); + return true; +} +function detachHeatListener(observer) { + const index = heatListeners.indexOf(observer); + if (index === -1) return false; + + heatListeners.splice(index, 1); + return true; +} + +function callHeatListeners() { + heatListeners.forEach((observer) => observer(gameData.heat)); +} + +function increaseHeat() { + if (gameData.heat >= defaultState.maxHeat) return; + gameData.heat++; + callHeatListeners(); +} + const activeOrders = []; function makeDelivery() { gameData.essence += 25; currentState.ordersFullfilled++; if (currentState.ordersFullfilled >= currentState.nextHeat) { - // increase heat - gameData.heat++; - currentState.nextHeat += gameData.heat + 1; - currentState.baseTime = defaultState.baseTime / (1 + gameData.heat); - currentState.timeToNextOrder = - defaultState.orderAdditionRatio * currentState.baseTime; + increaseHeat(); } } + function addOrder(recipe) { if (activeOrders.length > 2) return; activeOrders.push({ @@ -29,27 +50,35 @@ function addOrder(recipe) { } const defaultState = Object.freeze({ - baseTime: 8000, // in ms - orderAdditionRatio: 8, - orderDurationRatio: 12, + baseTime: 15000, // in ms + maxHeat: 6, + orderAdditionRatio: 12, + orderDurationRatio: 6, + cookRatio: 3, + stationRatio: 2 / 3, }); +// check reset State for starting values const currentState = { - baseTime: defaultState.baseTime / (1 + gameData.heat), - timeToNextOrder: - (defaultState.orderAdditionRatio * defaultState.baseTime) / 2, + baseTime: 0, + timeToNextOrder: 0, ordersFullfilled: 0, - nextHeat: gameData.heat + 1, + nextHeat: 0, }; function resetState() { gameData.essence = 0; gameData.heat = 1; - - currentState.baseTime = defaultState.baseTime / (1 + gameData.heat); - currentState.timeToNextOrder = - defaultState.orderAdditionRatio * defaultState.baseTime; currentState.ordersFullfilled = 0; + + heatListeners.length = 0; + attachHeatListener((heatLevel) => { + currentState.nextHeat += heatLevel + 1; + currentState.baseTime = defaultState.baseTime / (2 + heatLevel); + currentState.timeToNextOrder = + defaultState.orderAdditionRatio * currentState.baseTime; + }); + callHeatListeners(); } export { @@ -60,6 +89,7 @@ export { defaultState, currentState, resetState, + attachHeatListener, + detachHeatListener, + increaseHeat, }; - -// WE PROBABLY WILL ADD MORE STUFF HERE LATER diff --git a/src/entities/station.js b/src/entities/station.js index 01afb89..34dc532 100644 --- a/src/entities/station.js +++ b/src/entities/station.js @@ -8,6 +8,7 @@ import { } from "../data/items.js"; import { gameData, + defaultState, activeOrders, addOrder, makeDelivery, @@ -40,7 +41,7 @@ class Station { this.startWorking = (baseTime) => { this._work_lock = true; - this.duration = (baseTime * 2) / 3; + this.duration = Math.ceil(baseTime * defaultState.stationRatio); }; this.doWork = (timeStep) => { @@ -127,8 +128,8 @@ class Ingredient { class Glass { constructor(glassType) { this.inventory = glassType; - this.canPlace = (playerInv) => playerInv.ingredient === this.inventory; - this.place = (playerInv) => (playerInv.ingredient = 0); + this.canPlace = (playerInv) => playerInv.glass === this.inventory; + this.place = (playerInv) => (playerInv.glass = 0); this.canTake = (playerInv) => playerInv.empty(); this.take = (playerInv) => { @@ -191,12 +192,11 @@ class Cauldron { this.itemCount = 0; }; - // to be re-written this.canWork = () => this.duration === 0 && this._get_base(this.inventory) !== 0; this.startWorking = (baseTime) => { - this.duration = 4 * baseTime; + this.duration = baseTime * defaultState.cookRatio; this._first_brew = true; }; @@ -237,8 +237,7 @@ class Cauldron { }; this._in_inventory = (ing) => { - if ((this.inventory & ing) === 0) return false; - return true; + return (this.inventory & ing) !== 0; }; } }