• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

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

42 Warsaw Coding Academy
+1 głos
346 wizyt
pytanie zadane 2 stycznia 2017 w C# przez timrh Mądrala (6,010 p.)

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);
    }
};

 

Zaloguj lub zarejestruj się, aby odpowiedzieć na to pytanie.

Podobne pytania

0 głosów
2 odpowiedzi 666 wizyt
pytanie zadane 14 września 2018 w Systemy operacyjne, programy przez comoto45 Nowicjusz (160 p.)
+2 głosów
2 odpowiedzi 743 wizyt
pytanie zadane 3 listopada 2017 w Nasze projekty przez Insygnia Nowicjusz (150 p.)
0 głosów
2 odpowiedzi 966 wizyt
pytanie zadane 6 grudnia 2015 w C# przez emSon Stary wyjadacz (10,480 p.)

93,389 zapytań

142,386 odpowiedzi

322,547 komentarzy

62,749 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

VMware Cloud PRO - przenieś swoją infrastrukturę IT do chmury
...