Commit 0782eb90 authored by Matthew Odle's avatar Matthew Odle

update village to use the villager objects

* add assigned-villagers element to buildings
* change assignedVillagers to an array
* update the villager object with the current assignment when assigned
* update the building object with the id of the villager assigned
parent 884e71a8
import React from 'react';
export class AssignedVillagers extends React.Component {
render() {
var building = this.props.building;
return (
<div className='goods'>
<span>Villagers Assigned: </span>
<span>{building.assignedVillagers.length}</span>
</div>
);
}
}
......@@ -2,6 +2,7 @@ import React from 'react';
import { Materials } from './materials';
import { Goods } from './goods';
import { AssignedVillagers } from './assigned-villagers';
export class Building extends React.Component {
......@@ -20,7 +21,7 @@ export class Building extends React.Component {
{ building.materials ? <Materials building={building} /> : null }
<div>Active: {building.producing ? "Yes" : "No" }</div>
<div>Progress: {building.progress}</div>
<div>Villagers Assigned: {building.assignedVillagers}</div>
{ building.assignedVillagers ? <AssignedVillagers building={building} /> : null }
</div>
);
}
......
......@@ -8,42 +8,20 @@ export class Village extends React.Component {
super();
this.state = {
buildings: props.buildings.concat(),
unassignedVillagers: props.knobsAndLevers['unassignedVillagers'],
elapsed: 0,
villageStore: props.villageStore.concat(),
villagers: props.villagers.concat(),
};
}
canAdjust = (building, amount, operation) => {
let unassignedVillagers = this.state.unassignedVillagers;
if (operation === 'assign' && (building.assignedVillagers >= building.capacity || unassignedVillagers <= 0)) {
return false;
}
if (operation === 'unassign' && building.assignedVillagers <= 0) {
building.assignedVillagers = 0;
return false;
}
return true;
}
getValues = (building, operation) => {
let unassignedVillagers = this.state.unassignedVillagers;
let checkValue = 0;
let signMultiplier = 1;
if (operation === 'assign') {
checkValue = unassignedVillagers;
} else if (operation === 'unassign') {
checkValue = building.assignedVillagers;
signMultiplier = -1;
}
return {checkValue: checkValue, signMultiplier: signMultiplier}
getUnassignedVillagers = () => {
return this.props.villagers.slice().filter( villager => villager.assignment === 'none')
}
updateTheVillage = (building, unassignedVillagers) => {
updateTheBuildings = (building) => {
const buildings = this.props.buildings.slice();
const buildingIndex = buildings.findIndex(indexBuilding => indexBuilding.id === building.id);
this.setState({
unassignedVillagers: unassignedVillagers,
buildings: [
...buildings.slice(0, buildingIndex),
building,
......@@ -52,18 +30,69 @@ export class Village extends React.Component {
});
}
manageVillagers = (building, amount, operation) => {
if (!this.canAdjust(building, amount, operation)) {
updateTheVillagers = (villager) => {
const villagers = this.props.villagers.slice();
const villagerIndex = villagers.findIndex(indexVillager => indexVillager.id === villager.id);
this.setState({
villagers: [
...villagers.slice(0, villagerIndex),
villager,
...villagers.slice(villagerIndex + 1),
]
});
}
assignVillagers = (building, amount) => {
if (building.capacity - building.assignedVillagers.length <= 0 || this.getUnassignedVillagers().length === 0) {
return;
}
const allVillagers = this.props.villagers.slice();
const selectedVillager = allVillagers.find( villager => villager.assignment === 'none' && amount > 0)
selectedVillager.assignment = building.name;
building.assignedVillagers.push(selectedVillager.id);
this.updateTheBuildings(building);
this.updateTheVillagers(selectedVillager);
amount -= 1;
if (amount > 0) {
this.assignVillagers(building, amount);
}
}
getTheVillager = (villagerId) => {
const allVillagers = this.props.villagers.slice();
return allVillagers.find( villager => villager.id === villagerId);
}
unassignVillagers = (building, amount) => {
if (building.assignedVillagers.length === 0) {
return;
}
let unassignedVillagers = this.state.unassignedVillagers;
let values = this.getValues(building, operation);
let adjustmentValue = values.checkValue - amount > 0 ? amount : values.checkValue;
building.assignedVillagers += values.signMultiplier * adjustmentValue;
unassignedVillagers -= values.signMultiplier * adjustmentValue;
const selectedVillagerId = building.assignedVillagers.pop();
const villager = this.getTheVillager(selectedVillagerId);
villager.assignment = 'none';
this.updateTheVillage(building, unassignedVillagers);
this.updateTheBuildings(building);
this.updateTheVillagers(villager);
amount -= 1;
if (amount > 0) {
this.unassignVillagers(building, amount);
}
}
manageVillagers = (building, amount, operation) => {
if (operation === 'assign' && this.getUnassignedVillagers().length > 0) {
this.assignVillagers(building, amount);
}
if (operation === 'unassign') {
this.unassignVillagers(building, amount);
}
};
canProduce = (building) => {
......@@ -125,7 +154,7 @@ export class Village extends React.Component {
};
processProductionCycle = (building) => {
const multiplier = building.type === 'storage' ? this.state.unassignedVillagers : building.assignedVillagers;
const multiplier = building.assignedVillagers.length;
// if multiplier is 0, we're not producing
if (multiplier <= 0) {
building.producing = false;
......
......@@ -36,7 +36,7 @@ buildings.push({type: 'production',
priority: 0,
materials: ['wood', 'iron'],
goods: ['tool'],
assignedVillagers: 0,
assignedVillagers: [],
capacity: defaultCapacity,
progress: 0,
baseProductionRate: 5,
......@@ -49,7 +49,7 @@ buildings.push({type: 'extraction',
description: "it's big, it's heavy, it's wood",
priority: 0,
goods: ['wood'],
assignedVillagers: 0,
assignedVillagers: [],
capacity: defaultCapacity,
progress: 0,
baseProductionRate: 5,
......@@ -62,7 +62,7 @@ buildings.push({type: 'extraction',
description: "the strength of the earth to forge the tools",
priority: 0,
goods: ['iron'],
assignedVillagers: 0,
assignedVillagers: [],
capacity: defaultCapacity,
progress: 0,
baseProductionRate: 5,
......@@ -76,7 +76,7 @@ buildings.push({type: 'production',
priority: 0,
materials: ['wood'],
goods: ['firewood'],
assignedVillagers: 0,
assignedVillagers: [],
capacity: defaultCapacity,
progress: 0,
baseProductionRate: 5,
......
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