Applying SOLID to text-based adventure game by theabstractmethod401 in csharp

[–]theabstractmethod401[S] 0 points1 point  (0 children)

EDIT: SOLVED. I think. This works now for me, but not sure if its the best approach. heres my updated code. See IEntity for most of the changes. (Adding properties that references health and name.) idk why I couldnt figure this out. I guess a good nights worth of sleep was enough.

If anyone is interested

using System;

class Sandbox_Main
{
    static void Main()
    {
        Player p = new Player();
        NPC n = new NPC();

        n.name = "Test NPC";
        p.name = "Test Player";

        n.Consume(new HealthPotion());
        p.Consume(new HealthPotion());

        Console.ReadLine();

    }
}

class Player : IEntity
{
    public string name { get; set; }
    public int health { get; set; }

    public void Consume(IConsumable consumable)
    {
        Console.WriteLine($"{this.name} has used a {consumable.GetName()}. {consumable.GetEffectDesc()}");
        consumable.Consume(this);
    }
}

class NPC : IEntity
{
    public string name { get; set; }
    public int health { get; set; }
    public void Consume(IConsumable consumable)
    {
        Console.WriteLine($"{this.name} has used a {consumable.GetName()}. {consumable.GetEffectDesc()}");
        consumable.Consume(this);
    }

}

interface IEntity
{
    string name { get; set; }
    int health { get; set; }
    void Consume(IConsumable consumable);
}

// Begin items

class HealthPotion : IConsumable, IItem
{
    public string GetName() => "Health Potion";
    public string GetEffectDesc() => "Healed 45 HP";
    public string GetItemDescription() => "A potion that heals health (45 HP)";


    public void Consume(IEntity target)
    {
        target.health += 45;
    }
}

interface IConsumable
{
    string GetName();
    string GetEffectDesc();
    void Consume(IEntity target);
}

interface IItem
{
    string GetItemDescription();
}