Skip to content
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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

steaksauce-
Copy link

@steaksauce- steaksauce- commented Nov 12, 2018

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:

  • Enable ability to create an item more than once. For example, a player should be able to at least carry 2 or more swords, not just one.
  • Clean up tests to get expected results.
  • Tests should probably be included in a tests dir
  • Any other suggestions that come about.

This is a rudimentary example of an inventory system. I got the idea from https://codereview.stackexchange.com/questions/57438/game-inventory-system
@zupd
Copy link

zupd commented Nov 14, 2018

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
 };

@steaksauce-
Copy link
Author

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.
@zupd
Copy link

zupd commented Nov 14, 2018

@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

I am starting to understand ESLint warnings a little bit better. This will be my last commit before I get to a PC where I can run ESLint locally within the project :)
@steaksauce-
Copy link
Author

@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.
@idliboy
Copy link

idliboy commented Nov 16, 2018

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.

@steaksauce-
Copy link
Author

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 healing potion from the item class, then add some custom functions like potion.consume(). On the other end, I could create a potion from the item class that has a custom function like poison.throw() -- which would deal damage.

@steaksauce-
Copy link
Author

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 healing potion from the item class, then add some custom functions like potion.consume(). On the other end, I could create a potion from the item class that has a custom function like poison.throw() -- which would deal damage.

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 item and inventory out to 2 separate files since they provide different things.

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.

@idliboy
Copy link

idliboy commented Nov 19, 2018

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 healing potion from the item class, then add some custom functions like potion.consume(). On the other end, I could create a potion from the item class that has a custom function like poison.throw() -- which would deal damage.

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 item and inventory out to 2 separate files since they provide different things.

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.

@steaksauce-
Copy link
Author

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.
I am starting to understand ESLint warnings a little bit better. This will be my last commit before I get to a PC where I can run ESLint locally within the project :)
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.
@steaksauce-
Copy link
Author

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...
An item can belong to an inventory (collection of items). ie -- an inventory can have multiple items.
An inventory can belong to a container. A container can be the player's base inventory, a backpack, or a chest. Whatever you want it to be. Essentially, a container is just a fancy way of saying what kind of "object" would own the inventory. I believe a container would set how many items can be held.
A player can have one or more containers. ie, a playerinventory, a backpack, and a pouch with varying items.

I'm working on getting my environment back up and will submit a PR for the prototype that someone can iterate on.

@Jared-Sprague
Copy link
Member

@steaksauce- Sounds good! I think it's cool you want to work on this during Hacktoberfest!

@Jared-Sprague
Copy link
Member

@steaksauce- Let me know when you're ready for a review of this PR.

@steaksauce-
Copy link
Author

@Jared-Sprague -- ready!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants