-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create inventory.js #40
base: master
Are you sure you want to change the base?
Create inventory.js #40
Conversation
This is a rudimentary example of an inventory system. I got the idea from https://codereview.stackexchange.com/questions/57438/game-inventory-system
Cool. I added an amount field btw, and a few tests if we want that. If you want to print this out as a string, check my display_items() method. You dont have an array of objects since you are using a key for each objects. Therefore it becomes an object and you cant join it, but you must iterate over it instead, or fetch its keys. Take a look at this if you want to change something const item = class {
constructor(name, attack, armor, weight, cost, amount = 1) {
this.name = name;
this.attack = attack;
this.armor = armor;
this.weight = weight;
this.amount = amount;
this.cost = cost;
}
};
const inventory = class {
constructor() {
this.items = [];
}
add_item(item) {
if (item !== null) {
if (this.items[item.name]) {
this.items[item.name].amount += 1 ;
} else {
this.items[item.name] = item;
}
}
}
display_items() {
return Object.keys(inv.items).join(", ")
// Or Object.keys(inv.items).map((k) => inv.items[k]) to get and array of objects and not just an object
}; |
Also on the topic of amounts, introduce if the item is stackable, and if so, what is the max size of the stack? Somewhat related is the weight attribute, which could be used as explicit weight (a hero can only carry 1lbs of pens in his front pocket), or used as a way to define how many inventory slots are used by the item (for example, the old Diablo 2 inventory system). I'm very new at js (this being the only thing I've really written), but I can try to see it through. |
This change introduces my attempts at ESLint to resolve some of the warnings/errors in the build. I have also introduced some recommended changes to the inventory system.
@steaksauce- In that case, there can be problems with the keys used for the items in the items object. Since you added the name as a key, you imply that and one kind of item has a unique name. If we introduce stacks, we cant use the same name for other stacks when one stack gets full. So we need to find a way to have more stacks without using the same key |
Ah thanks! I'll look into it as I straighten out my linting woes. |
This commit (finally) fixes my ESLint woes. I've added some tests and expectations, but am not getting the expected results. JS feels weird to me, but I'm getting the hang of it.
Would it be useful to add an optional, custom parameter list for each item? For an item that doesn't really fit into the attack/armor group. |
I thought about that. For example, a healing potion would provide neither attack or armor. I'm not a dev by trade, but I think I would create a |
The more I think about this, I would think that the game dev would create some custom item classes (I'm open for discussion on that though). Also, I'm doing this to create an inventory system, but you can't have inventory without items. I may split I'm going to poke around and see how some community games are handling these problems and draw conclusions from that, but I'm open to discussion on here as well. |
Yeah, that makes sense to just extend the item class to fit their use cases. Having them in 2 separate files is also a good idea I think. |
I've been on a bit of a hiatus (studied for and passed my Certified Kubernetes Admin. exam -- go me!). I plan on picking this back up with my free time soon. |
This is a rudimentary example of an inventory system. I got the idea from https://codereview.stackexchange.com/questions/57438/game-inventory-system
This change introduces my attempts at ESLint to resolve some of the warnings/errors in the build. I have also introduced some recommended changes to the inventory system.
This commit (finally) fixes my ESLint woes. I've added some tests and expectations, but am not getting the expected results. JS feels weird to me, but I'm getting the hang of it.
In the spirit of Hacktoberfest, I am going to try to hash out a rough copy. I've lost interest in JavaScript, but I think I can hobble together the concept. I will create classes for Player, Container, Inventory, and Item. An item can have attributes pertaining to itself -- attack value, defense value, is it stackable, etc... I'm working on getting my environment back up and will submit a PR for the prototype that someone can iterate on. |
…someone else should iterate on.
@steaksauce- Sounds good! I think it's cool you want to work on this during Hacktoberfest! |
@steaksauce- Let me know when you're ready for a review of this PR. |
@Jared-Sprague -- ready! |
This is a rudimentary example of an inventory system. I got the idea from https://codereview.stackexchange.com/questions/57438/game-inventory-system
I'm pretty bad at JavaScript and tried a few different methods. This is what I came up with. This is a good start for #24.
TODO: