O
O
Omniverse2016-05-06 19:01:56
Python
Omniverse, 2016-05-06 19:01:56

How to properly design a Character's inventory structure?

Hello!
I'm trying to make a main character inventory system for an RPG game.
Interested in the question of how to properly design the inventory of the main character, how to store things (weapons, armor, etc.), how to choose things from the inventory. How it is reasonable to organize all this in an object-oriented approach?
So far I've made this sketch:

class Hero:    
    def __init__(self, name, hp = 100):
        self.name = name
        self.hp = hp
        self.weapons = [] #  инвентарь с оружием
        self.armor = [] # инвентарь с броней
    
    # добавить предмет в инвентарь
    def take(self, item):
        if isinstance(item, Weapon):
            self.weapons.append(item.__dict__)
        elif isinstance(item, Armor):
            self.armor.append(item.__dict__)

class Weapon:
    pass

class Sword(Weapon):
    def __init__(self, name = 'Меч', damage = 30):
        self.name = name
        self.damage = damage

class Bow(Weapon):
    def __init__(self, name = 'Лук', damage = 20):
        self.name = name
        self.damage = damage

class Armor:
    pass

I would be glad to see your examples and advice (preferably in Python, but other languages ​​will do, at least pseudocode).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2016-05-06
@Omniverse

Inventory is inventory. It stores any item based on its size and/or weight. Slots are already specialized cells in which you can place only those types of items for which these slots are intended, for example, weapons or armor.
Thus, it is logical to make the base class "item" (item), describing items in terms of storage in the inventory and general manipulations (buying, selling, etc.) and child classes "armor" (armor), "weapon" (weapon) etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question