arrays - Creating a simple inventory system in JavaScript -
i'm trying make simple text adventure/farming simulator, i'm new javascript , i'm stuck trying plan out how inventory system work. main problem have there 2 main types of items: food/consumables , tools/durable goods. food created , consumed @ regular rate, whereas tools need bought, , lose durability each use.
the simplest solution think of have quantity of each food item property of player (player.meat, player.vegetable, etc.) since quantity thing changing. tools little more complex, figured create tool prototype, give player inventory array store tools. more thought it, more realized food need prototype (it has properties price , nutritional value well).
my head kind of spinning right trying think of effective way of doing this. create 2 separate prototypes, give player array each of them? need create prototype array can keep track of both item type , quantity? , need search each item in array when want modify it? seems needlessly complicated, can't think of simpler. can help?
you keep array of food items , array of tool items. here example of creating food item , using it.
function fooditem(options){ var options = options || {}; this.name = options.name || "defaultfood"; this.amount = options.amount || 100; this.decrease = options.decrease || 1; this.eat = function(){ this.amount -= this.decrease; if (this.amount < 0){ console.log("food gone"); } } } // create new foods. var carrots = new fooditem({ name:"carrots", amount:50, decrease:2 }); carrots.eat();
Comments
Post a Comment