CommandLineHeroes / hero-engine

Open-source HTML5 adventure game engine
MIT License
219 stars 66 forks source link

Create inventory.js #40

Open steaksauce- opened 5 years ago

steaksauce- commented 5 years ago

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:

zupd commented 5 years ago

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- commented 5 years ago

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.

zupd commented 5 years ago

@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

steaksauce- commented 5 years ago

@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.

idliboy commented 5 years ago

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- commented 5 years ago

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- commented 5 years ago

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 commented 5 years ago

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- commented 5 years ago

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.

steaksauce- commented 4 years ago

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 commented 4 years ago

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

Jared-Sprague commented 4 years ago

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

steaksauce- commented 4 years ago

@Jared-Sprague -- ready!