Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania bez odpowiedzi
Viewing all articles
Browse latest Browse all 21942

Co sądzicie o tym kodzie? (Prosty mechanizm tworzenia itemów do gry RPG)

$
0
0

Witam, napisałem kod który ma na celu stworzenie przedmiotu a następnie nadanie mu właściwości poprzez komponenty, co o nim sądzicie? Można to jakoś lepiej napisać?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public interface ItemComponent { };

public abstract class PhysicalItem
{
    public int Slashing { get; set; }
    public int Bashing { get; set; }
    public int Piercing { get; set; }
}

public abstract class ElementalItem
{
    public int Fire { get; set; }
    public int Water { get; set; }
    public int Earth { get; set; }
    public int Wind { get; set; }
}

public class Valuable : ItemComponent
{
    public int Cost { get; set; }

    public override string ToString()
    {
        return 
            string.Format("It is a valuable item, that can be bought for: {0} gold coins.", Cost);
    }
};

public class PhysicalProtection : PhysicalItem, ItemComponent
{
    public override string ToString()
    {
        return
                string.Format("It gives {0}/{1}/{2} physical protection.", Slashing, Bashing, Piercing);
    }
};

public class PhysicalDamage : PhysicalItem, ItemComponent
{
    public override string ToString()
    {
        return
            string.Format("It does {0}/{1}/{2} physical damages.", Slashing, Bashing, Piercing);
    }
};

public class ElementalProtection : ElementalItem, ItemComponent
{
    public override string ToString()
    {
        return
            string.Format("It gives {0}/{1}/{2}/{3} elemental protection.", Fire, Water, Earth, Wind);
    }
};

public class ElementalDamage : ElementalItem, ItemComponent
{
    public override string ToString()
    {
        return 
            string.Format("It does {0}/{1}/{2}/{3} elemental damages.", Fire, Water, Earth, Wind);
    }
};

public class Item
{
    public string Name { get; private set; }
    private List<ItemComponent> components = new List<ItemComponent>();

    public Item(string name)
    {
        Name = name;
    }

    public Item AddComponents(ItemComponent[] components)
    {
        this.components.AddRange(components);
        return this;
    }

    public T GetComponent<T>()
    {
        return (T)components.OfType<T>().FirstOrDefault();
    }

    public override string ToString()
    {
        var sb = new StringBuilder();
        sb.AppendLine(Name);
        components.ForEach(c => sb.AppendLine(c.ToString()));
        return sb.ToString();
    }
};

class Equipment
{
    public static void Test()
    {
        var fireSword = new Item("Fire Claymore")
            .AddComponents(
            new ItemComponent[] { new Valuable { Cost = 1000 },
                                  new PhysicalDamage { Slashing = 100 },
                                  new ElementalDamage { Fire = 80 },
                                  new PhysicalProtection { Bashing = 25 },
                                  new ElementalProtection { Water = -25 }
            });

        var ancientHelmet = new Item("Ancient Helmet")
            .AddComponents(
            new ItemComponent[] { new Valuable { Cost = 700 },
                                  new PhysicalProtection { Slashing = 200 },
                                  new ElementalProtection { Fire = 20 } }
            );

        Console.WriteLine(fireSword);
        Console.WriteLine(ancientHelmet);
    }
};

 


Viewing all articles
Browse latest Browse all 21942

Trending Articles


Iron Lead - punkty pomiarowe nadwozi


AutoMapa 6.27.0 ( 2301 ) Polska / Europe Finał ( PC_WinCE ) Cracked


[159] P1670 P1671 - 1.75 tbi


Kombajn ziemniaczany GRIMME HLS 750


POTANIACZ


Kasowanie inspekcji Hyundai ix35


Konfiguracja - podłączenie modemu do radia I-COM IC7000


Tekken 7 Spolszczenie PC


Auta / Cars (2006) PLDUB.BRRip.480p.XviD.AC3-LTN / DUBBING PL


Alawar Keygen POLECAM