2013-05-20 49 views
0

所以我對編程一般都很陌生。我目前工作的一個地形生成程序,一切都會好起來,除了這個偉大:有沒有辦法讓這個縮短?

public static class Desert 
    { 
     public const int iChance = 15; 
     public static int chance = iChance; 
     public static int chancepoint = 0; 
     public const int octaves = 4; 
     public const int lengthMin = 60; 
     public const int lengthMax = 90; 
     public const float scaleMin = 250; 
     public const float scaleMax = 350; 
     public const float persistenceMin = 0.5f; 
     public const float persistenceMax = 0.9f; 
     public const pType ptype = pType.Lowland; 
     public const bTag[] tags = { bTag.desert }; 
    } 
    public static class Meadow 
    { 
     public const int iChance = 45; 
     public static int chance = iChance; 
     public static int chancepoint = 0; 
     public const int octaves = 4; 
     public const int lengthMin = 45; 
     public const int lengthMax = 70; 
     public const float scaleMin = 200; 
     public const float scaleMax = 470; 
     public const float persistenceMin = 0.35f; 
     public const float persistenceMax = 0.70f; 
     public const pType ptype = pType.noAbs; 
     public const bTag[] tags = { bTag.lush }; 
    } 

這些是每個不同類型的「生物羣落」的性質。

我目前有大約7個這些,他們都是完全一樣的,除了每個字段的值。

有沒有一種方法可以縮短代碼?我研究繼承,但最終出現了錯誤,我有點困惑。 > <

這將是輝煌的,如果我不得不寫的是:提前

public static class Desert 
    { 
     iChance = 15; 
     chance = iChance; 
     chancepoint = 0; 
     octaves = 4; 
     lengthMin = 60; 
     lengthMax = 90; 
     scaleMin = 250; 
     scaleMax = 350; 
     persistenceMin = 0.5f; 
     persistenceMax = 0.9f; 
     ptype = pType.Lowland; 
     strongTags = { bTag.desert }; 
    } 

感謝。

呵呵,對於這個問題的缺點感到抱歉,如果你看到程序的其餘部分,你可能會尖叫我的代碼有多糟糕。 XD

編輯:這可能是明智的告訴你,我永遠不會改變類內的東西,除了'機會'的價值的例外。

+1

刪除「靜態」的關鍵字,並把所有在構造 – MikeSW

+0

@AwesomePerson怎麼樣的試圖在「列表」中使用此類 – Praveen

+0

您正在將類與其實例混淆。不要聲明7個類,聲明1併爲每個生物實例化它。然後出去買一本關於編程概念的書吧:-) –

回答

3

除了使用靜態類外,還可以使用非靜態類。

public class Biome { 
    // Instance fields with default values 
    public int iChance = 15; 
    public int chance = iChance; 
    public int chancepoint = 0; 
    public int octaves = 4; 
    public int lengthMin = 60; 
    public int lengthMax = 90; 
    public float scaleMin = 250; 
    public float scaleMax = 350; 
    public float persistenceMin = 0.5f; 
    public float persistenceMax = 0.9f; 
    public pType ptype = pType.Lowland; 
    public bTag[] tags = { bTag.desert }; 
} 

這裏使用構造函數初始化:

public Biome(int iChance, int chance, int chancepoint, int octaves, public int lengthMin, int lengthMax, float scaleMin, float scaleMax, float persistenceMin, float persistenceMax,pType ptype, bTag[] tags) { 
    // init fields here 
} 

然後調用構造函數:

Biome bimoe = new Biome(15, iChance, 0, 4, 60, 90, 250, 350, 0.5f, 0.9f, pType.Lowland, { bTag.desert }); 

有了這一點,很難看到哪個參數去哪個領域,但它的很多短。

如果這些字段必須是隻讀的,那麼您可以使用公共getset訪問器創建屬性。例如:

public Chance { get { return chance; } } 

在這種情況下做出的領域專用:

private int chance = iChance; 

(就個人而言,對於這樣的場景,我會把所有的數據文件)

0

這是更好地使用只有一個類沒有繼承,甚至沒有結構。沙漠,草地等不是邏輯上的類,它必須是對象(可能是常量)。

-2

首先,你不能在static classes上繼承。所以你將不得不開始使用實例。

其次,如果你想擴展對象,你會使用繼承。例如,如果你想添加一個新的屬性「bool HasScorpions」到沙漠上,但不是在草地上。

由於您使用相同的屬性,但想要使用不同的值我個人使用一個接口。通過這種方式,您可以只讀屬性,同時仍然可以輕鬆設置值。

public interface Terrain 
{ 
    int iChance = {get { return 15; private set; } ..and repeat. 
    int chance = iChance; 
    int chancepoint = 0; 
    int octaves = 4; 
    int lengthMin = 60; 
    int lengthMax = 90; 
    float scaleMin = 250; 
    float scaleMax = 350; 
    float persistenceMin = 0.5f; 
    float persistenceMax = 0.9f; 
    pType ptype = pType.Lowland; 
    bTag[] tags = { bTag.desert }; 
} 
1

以下將更短:

public const int iChance = 15, octaves = 4, lengthMin = 60, lengthMax = 90; 
public const float scaleMin = 250, scaleMax = 350, persistenceMin = 0.5f, 
        persistenceMax = 0.9f; 
public static int chance = iChance, chancepoint = 0; 

但是......這些真的不喜歡看的東西,應該是static領域,或很可能不連const。它們看起來應該是實例屬性。也許是這樣的:

public class Terrain { 
    public int Chance {get;private set;} 
    public int LengthMin {get;private set;} 
    // ... 
    private Terrain(int chance, int lengthMin, ...) { 
     Chance = chance; 
     LengthMin = lengthMin; 
     // ... 
    } 
    private static readonly Terrain 
     desert = new Terrain(45, 45, ...), 
     meadow = new Terrain(15, 60, ...), 
     ...; 
    public static Terrain Desert { get { return desert;}} 
    public static Terrain Meadow { get { return meadow;}} 
} 
+0

好的,這是有道理的,但一旦我創造它們並且表現相當重要,我就永遠不會改變班上的東西。這會影響性能嗎? – AwesomePerson

+0

@AwesomePerson棘手的一個。常量的優點是它們被「燒入」調用者(它絕不是ldfld等 - 它是一個原始的加載值)。這種情況有多重要取決於情況(您需要進行配置)。但是這裏的第一個示例中的語法(每個聲明有多個值)至少不會太冗長! –

+0

好吧,謝謝! – AwesomePerson

0

你可以做的是使用靜態構造函數中使用一個單獨的類名爲地形和初始化這個多次:

public class Terrain 
    { 
      public int IChance { get; private set; } 
    public int Chancepoint { get; private set; } 
    public int Octaves { get; private set; } 
    public int LengthMin { get; private set; } 
    public int LengthMax { get; private set; } 
    public float ScaleMin { get; private set; } 
    public float ScaleMax { get; private set; } 
    public float PersistenceMin { get; private set; } 
    public float PersistenceMax { get; private set; } 
    public pType Ptype { get; private set; } 
    public bTag[] Tags { get; private set; } 

     public static Terrain Desert() 
     { 
      return new Terrain 
       { 
        IChance = 15, 
        Chancepoint = 0, 
        Octaves = 4, 
        LengthMin = 60, 
        LengthMax = 90, 
        ScaleMin = 250, 
        ScaleMax = 350, 
        PersistenceMin = 0.5f, 
        PersistenceMax = 0.9f, 
        Ptype = pType.Lowland, 
        Tags = new bTag[] {bTag.Desert} 
       }; 
     } 
} 
0

喬的回答是好,但構造函數調用已遠太多未命名的參數 - 350是什麼意思?

這是數據驅動設計的理想人選。

與其定義代碼中的所有Biome類型,不如將Biome類型的所有數據放入文件中,並在運行時讀取文件。 C#語言有很多東西可以幫助你做到這一點,關鍵詞搜索是序列化(and here's a link to MSDN about it)。

最大的優點是您可以更改數據值而無需重新編譯代碼。

缺點是需要更多的代碼來定義第一個實例,但在此之後,您可以根據需要輕鬆創建多個實例。

1

我對地形生成程序瞭解不多,但應該將數據存儲在數據庫中。 然後創建類以將該數據映射到您的應用程序。 我建議你查找「數據結構」並查看哪一個最適合你的應用。

0

你可以做這樣的事情,宣佈這樣一個抽象類,然後從它inherting:

public abstract class Terrain 
    { 
     public int iChance; 
     public int chance; 
     public int chancepoint; 
     public int octaves; 
     public int lengthMin; 
     public int lengthMax; 
     public float scaleMin; 
     public float scaleMax; 
     public float persistenceMin; 
     public float persistenceMax; 
     public pType ptype; 
     public Tag[] strongTags; 
    } 

    public class Desert : Terrain 
    { 

    } 

    public enum pType 
    { 
     Desert = 1, 
     LowLand = 2 
    } 

    public enum Tag 
    { 
     desert = 1, 
     lush = 2 
    } 

然後,您可以實例沙漠像:

var desert = new Desert() 
      { 
       iChance = 15 
       ,chance = 15 
       ,chancepoint = 0 
       ,octaves = 4 
       ,lengthMin = 60 
       ,lengthMax = 90 
       ,scaleMin = 250 
       ,scaleMax = 350 
       ,persistenceMin = 0.5f 
       ,persistenceMax = 0.9f 
       ,ptype = pType.Desert 
       ,strongTags = new Tag[]{Tag.desert} 
      }; 
0

不知道你的具體要求,但這不是一個更好的辦法:

public abstract class BiomeBase 
{ 
    public int Chance  { get; set; } 
    public int Chancepoint { get; set; } 
    public int Octaves  { get; set; } 
    // you get the idea ... 
} 

那麼你有DesertMeadow繼承:

public class Desert : BiomeBase 
{ 
    // everything is inherited ... 
    // you can also add your own properties meant for Desert only (if needed) 
} 

public class Meadow : BiomeBase 
{ 
    // everything is inherited ... 
} 

現在Desert擁有一切Biome有,你可以使用它像這樣:

var desert = new Desert 
{ 
    Chance = 5, 
    Octaves = 1, 
    /// etc 
}; 
+0

冬青牛......多少個答案......我什至不:))))對不起,如果重複。 –