Home > Net >  How do I Make Reusable Class for Inventory/Item System? C#
How do I Make Reusable Class for Inventory/Item System? C#

Time:02-03

I want to make a class that I can use to make endless items and put them in a list, for my inventory system. I don't know a lot of C# so if there is an entirely different solution, I would be open to hearing it.

Here is what I tried.

using System;
using System.Collections.Generic;

namespace Game
{
    public class Item
    {
        bool isChestArmor, isLegArmor, isOneHanded, isItem, isWeapon, hpItem, manaItem, onlyOffHand;
        double damage, resistance, cost;
        string name;
        public string Name
        {
            get {return name;}
            set {name = value;}
        }
        public double Damage
        {
            get {return damage;}
            set {damage = value;}
        }
        public double Resistance
        {
            get {return resistance;}
            set {resistance = value;}
        }
        public double Cost
        {
            get {return cost;}
            set {cost = value;}
        }
        public bool IsChestArmor 
        {
            get {return isChestArmor;}
            set {isChestArmor = value;}
        }
        public bool IsLegArmor 
        {
            get {return isLegArmor;}
            set {isLegArmor = value;}
        }
        public bool IsItem 
        {
            get {return isItem;}
            set {isItem = value;}
        }
        public bool IsWeapon 
        {
            get {return isWeapon;}
            set {isWeapon = value;}
        }
        public bool IsOneHanded
        {
            get {return isOneHanded;}
            set {isOneHanded = value;}
        }
        public bool OnlyOffHand 
        {
            get {return onlyOffHand;}
            set {onlyOffHand = value;}
        }
        public bool HpItem
        {
            get {return hpItem;}
            set {hpItem = value;}
        }
        public bool ManaItem
        {
            get {return manaItem;}
            set {manaItem = value;}
        }
    }
    class MainClass
    {
        public static void Main()
        {
            Item I = new Item();
            List<Item> Inventory = new List<Item>();
            I.ManaItem = false;
            I.HpItem = false;
            I.OnlyOffHand = false;
            I.IsChestArmor = false;
            I.IsLegArmor = false;
            I.IsWeapon = true;
            I.Damage = 10;
            I.Cost = 75;
            I.Resistance = 0;
            I.Name = "Short Sword";
            Inventory.Add(I);
            Console.WriteLine(Inventory[0].Name);

            Console.ReadLine();
        }

    }

}

This doesn't work because I would need a new class for every iteration of an item. It would be nice if there was a way to make duplicates of the Item class, and I'm sure there is a way to do this, I just simply don't know how. Thank you.

CodePudding user response:

Based on your comments, I think this is the core learning you're looking for in the question.

This problem calls for an understanding of polymorphism, or the representation of related concepts in an inheritance hierarchy. Here's one way you can approach that in C#.

(Update to original answer: Added DancingSword that can act like a weapon and like armor).

namespace Game
{
    public enum ArmorLocation
    {
        All, Head, Chest, Leg, Foot
    }
    public interface IItem
    {
        string Name { get; set; }
    }
    public interface IWeapon : IItem
    {
        double Damage { get; set; }
    }
    public interface IArmor : IItem
    {
        double Resistance { get; set; }
        ArmorLocation Location { get; set; }
    }

    public class Weapon : IWeapon
    {
        public double Damage { get; set; }
        public string Name { get; set; }
    }

    public class DancingSword : IWeapon, IArmor
    {
        public double Damage { get; set; }
        public string Name { get; set; }
        public double Resistance { get; set; }
        public ArmorLocation Location { get; set; }
    }

    public class Armor : IArmor
    {
        public double Resistance { get; set; }
        public ArmorLocation Location { get; set; }
        public string Name { get; set; }
    }

    class MainClass
    {
        public static void Main()
        {
            var Inventory = new List<IItem>();
            Inventory.Add(new Weapon() { Name = "Shortsword", Damage = 10.0 });
            Inventory.Add(new Armor() { Name = "Steel Helm", Location = ArmorLocation.Head, Resistance = 3.0 });
            Inventory.Add(new Armor() { Name = "Skeeverhide Boots", Location = ArmorLocation.Foot, Resistance = 1.0 });
            Inventory.Add(new DancingSword() { Name = "Dancing Sword  1", Damage = 8.0, Location = ArmorLocation.All, Resistance = 1.0 });

            Console.WriteLine("Weapon items:");
            foreach (var item in Inventory.OfType<IWeapon>())
                Console.WriteLine(item.Name);

            Console.WriteLine("Armor items:");
            foreach (var item in Inventory.OfType<IArmor>())
                Console.WriteLine(item.Name);

            Console.ReadLine();
        }

    }
}
  • Related