JSFiddle - React, Tailwind, and code Playground
by Drath
JavaScript
//Inventory set-up
var invitem = [];
var invitems = function(id, decay, weight, i) {
i = i || {};
i.id = id;
i.decay = decay;
i.weight = weight;
return i;
}
//Item Table
var items = {
branch: { name: "A Branch", equip: "lefthand", attack: 1, weight: 1, decay: 10 },
sharprock: { name: "A Sharp Rock", use: "carving", weight: 2, decay: 10 },
string: { name: "A Piece of String" },
pooraxe: { name: "A Poor Axe", equip: "lefthand", use: "gathering", attack: 3, weight: 5, decay: 30 },
tannin: { name: "Tannin" },
treebark: { name: "Tree Bark" },
mortarandpestle: { name: "Mortar and Pestle", weight: 2, decay: 10 }
};
//Crafting Table
var recipes = {
tannin: {
requires: [["mortarandpestle", 1],["treebark", 1]],
consumes: [["treebark", 1]],
creates: ["tannin", 1]
},
pooraxe: {
requires: [["sharprock", 2],["branch", 1],["string", 1]],
consumes: [["sharprock", 2],["branch", 1],["string", 1]],
creates: ["pooraxe", 1]
}
};
function availableRecipes() {
var requirements;
//loop through every recipe:
for(var r in recipes) {
var recipe = recipes[r];
requirements = [];
//loop through recipe requirements:
//Copy into temp array, remove keys as items are met, if it's empty at the end, we know it has been fulfilled
for(var req in recipe.requires) {
var iRequire = recipe.requires[req];
//iRequire[1] is the quantity of an item that is required. We're going to loop as many times as we need that item and push individual keys onto our temp requirements array.
for(var l=0; l<iRequire[1]; l++) {
requirements.push(iRequire[0]);
}
}
//loop through player inventory
for(var item in invitem) {
if(requirements.length < 1)
continue;
for(var left in requirements) {
var reqItem...