0

Before I get to my question, here is a sample of the structure of my code:

abstract class Entity
{
    #region Declarations
    public string Name;
    public string Description;
    #endregion

    #region Constructor
    public Entity(string Name, string Description)
    {
        this.Name = Name;
        this.Description = Description;
    }
    #endregion
}

abstract class Item : Entity
{
    #region Declarations
    public bool SingleUse;
    #endregion

    #region Constructor
    public Item(string Name, string Description, bool SingleUse = false)
        :base(Name, Description)
    {
        this.SingleUse = SingleUse;
    }
    #endregion

    #region Public Methods
    public void NoUse()
    {
        Program.SetError("There is a time and place for everything, but this is not the place to use that!");
    }
    #endregion
}

class BrassKey : Item
{
    public BrassKey(string Name, string Description, bool SingleUse = false)
        :base(Name, Description, SingleUse)
    {
    }

    public void Use()
    {
        if (Player.Location == 2)
        {
            Program.SetNotification("The key opened the lock!");
            World.Map[2].Exits.Add(3);
        }
        else
        {
            NoUse();
            return;
        }
    }
}

class ShinyStone : Item
{
    public ShinyStone(string Name, string Description, bool SingleUse = false)
        : base(Name, Description, SingleUse)
    {
    }

    public void Use()
    {
        if (Player.Location == 4)
        {
            Player.Health += Math.Min(Player.MaxHealth / 10, Player.MaxHealth - Player.Health);
            Program.SetNotification("The magical stone restored your health by 10%!");
        }
        else
        {
            Program.SetNotification("The shiny orb glowed shiny colors!");
        }
    }
}

class Rock : Item
{
    public Rock(string Name, string Description, bool SingleUse = false)
        : base(Name, Description, SingleUse)
    {
    }

    public void Use()
    {
        Program.SetNotification("You threw the rock at a wall. Nothing happened.");
    }
}

I then construct a list of Item objects in my World class. Each object in the list is of the type of item it is.

public static List<Item> Items = new List<Item>();

private static void GenerateItems()
{
    Items.Add(new BrassKey(
        "Brass Key",
        "Just your generic key thats in almost every game.",
        true));

    Items.Add(new ShinyStone(
        "Shiny Stone",
        "Its a stone, and its shiny, what more could you ask for?"));

    Items.Add(new Rock(
        "Rock",
        "It doesn't do anything, however, it is said that the mystical game designer used this for testing."));
}

How could I then call the use method of each specific item class like this:

World.Items[itemId].Use();

If there is something you don't understand about my issue, please don't hesitate to ask me!

1 Answer 1

4

Define the Use Method in your ItemClass and mark it virtual. Then in your subclasses mark the method as override and you should be able to do what you want

1
  • I'm going to :) StackOverflow requires me to wait several minutes before I can accept it.
    – rshea0
    Jun 9, 2012 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.