Commit 081dc821 authored by Matthew Odle's avatar Matthew Odle

update multiplier to use tool flag on villager object; create updateVillagers...

update multiplier to use tool flag on villager object; create updateVillagers functions for setting villager state; add more parameters to the knobsAndLevers object
parent d9fa57c3
......@@ -105,16 +105,20 @@ export class Village extends React.Component {
return flag;
};
updateTheVillageStore = (villageStore) => {
this.setState({
villageStore: villageStore,
});
}
useMaterials = (building) => {
const theItems = this.state.villageStore.slice();
const villageStore = this.state.villageStore.slice();
building.materials.map(
item => theItems.map(
theItem => theItem.name === item && theItem.count > 0 ? theItem.count -= 1 : null,
material => villageStore.map(
villageStoreMaterial => villageStoreMaterial.name === material && villageStoreMaterial.count > 0 ? villageStoreMaterial.count -= 1 : null,
)
);
this.setState({
villageStore: theItems,
});
this.updateTheVillageStore(villageStore);
building.materialsInUse = true;
};
......@@ -125,14 +129,11 @@ export class Village extends React.Component {
storeItem => storeItem.name === aGood ? storeItem.count += 1 : null,
)
);
this.setState({
villageStore: villageStore,
});
this.updateTheVillageStore(villageStore);
};
completeProductionCycle = (building) => {
building.progress -= 100;
building.progress -= this.props.knobsAndLevers.productionComplete;
if (!this.canProduce(building)) {
building.producing = false;
building.progress = 0;
......@@ -148,12 +149,18 @@ export class Village extends React.Component {
};
processProductionCycle = (building) => {
const multiplier = building.assignedVillagers.length;
// if multiplier is 0, we're not producing
let multiplier = 0;
building.assignedVillagers.map(villager =>
multiplier += villager.hasTool
? this.props.knobsAndLevers.toolMultiplier
: 1
);
// if multiplier is 0 or less, we're not producing
if (multiplier <= 0) {
building.producing = false;
return;
}
if (building.materials && !building.materialsInUse) {
this.useMaterials(building);
};
......@@ -167,16 +174,15 @@ export class Village extends React.Component {
if (this.canProduce(building) || building.materialsInUse) {
this.processProductionCycle(building);
};
if (building.progress >= 100) {
if (building.progress >= this.props.knobsAndLevers.productionComplete) {
this.completeProductionCycle(building);
this.startNextProductionCycle(building);
};
});
};
componentDidMount = () => {
this.timer = setInterval(() => this.tick(), 1000);
this.timer = setInterval(() => this.tick(), this.props.knobsAndLevers.interval);
};
componentWillUnmount = () => {
clearInterval(this.timer);
......
......@@ -5,8 +5,16 @@ import './index.css';
import { Village } from './components/village';
const defaultCapacity = 50;
const knobsAndLevers = {unassignedVillagers: 30}
const knobsAndLevers = {
unassignedVillagers: 30,
villagerHasTool: true,
toolMultiplier: 4,
toolDurability: 100,
interval: 1000,
productionComplete: 100,
defaultCapacity: 50,
defaultProductionRate: 1,
}
const villageStore = [
{name: 'wood', count: 0, source: 'FORESTER_HUT'},
{name: 'tool', count: 0, source: 'SMITHY'},
......@@ -16,13 +24,12 @@ const villageStore = [
const villagers = []
const addVillager = () => {
let nextId = villagers.length > 0 ? Math.max(...villagers.map(v => v.id)) + 1 : 0;
const villager = {"id": nextId, "hasTool": false, "toolDurability": 0, "assignment": "none"};
const villager = {"id": nextId, "hasTool": knobsAndLevers.villagerHasTool, "toolDurability": knobsAndLevers.toolDurability, "assignment": "none"};
villagers.push(villager);
if (villagers.length < knobsAndLevers['unassignedVillagers']) {
addVillager();
}
}
const init = () => {
addVillager();
}
......@@ -37,9 +44,9 @@ buildings.push({type: 'production',
materials: ['wood', 'iron'],
goods: ['tool'],
assignedVillagers: [],
capacity: defaultCapacity,
capacity: knobsAndLevers.defaultCapacity,
progress: 0,
baseProductionRate: 5,
baseProductionRate: knobsAndLevers.defaultProductionRate,
producing: false
});
buildings.push({type: 'extraction',
......@@ -50,9 +57,9 @@ buildings.push({type: 'extraction',
priority: 0,
goods: ['wood'],
assignedVillagers: [],
capacity: defaultCapacity,
capacity: knobsAndLevers.defaultCapacity,
progress: 0,
baseProductionRate: 5,
baseProductionRate: knobsAndLevers.defaultProductionRate,
producing: false
});
buildings.push({type: 'extraction',
......@@ -63,9 +70,9 @@ buildings.push({type: 'extraction',
priority: 0,
goods: ['iron'],
assignedVillagers: [],
capacity: defaultCapacity,
capacity: knobsAndLevers.defaultCapacity,
progress: 0,
baseProductionRate: 5,
baseProductionRate: knobsAndLevers.defaultProductionRate,
producing: false
});
buildings.push({type: 'production',
......@@ -77,9 +84,9 @@ buildings.push({type: 'production',
materials: ['wood'],
goods: ['firewood'],
assignedVillagers: [],
capacity: defaultCapacity,
capacity: knobsAndLevers.defaultCapacity,
progress: 0,
baseProductionRate: 5,
baseProductionRate: knobsAndLevers.defaultProductionRate,
producing: false
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment