2012-01-17 14 views
0

我一直在使用XNA 4.0和C#開發一個關於Windows的單詞的小遊戲。可以通過單獨的Windows窗體編輯器(NOT XNA)編輯單詞。如何分離和管理XNA中的動態資源? (Design)

詞類(這是可序列化的)包含下列對象:

String WordName 
    HashSet<String> PictureFilenames_Set; // this is a list of png file names for what the picture looks like 
    HashSet<String> AudioFilenames_Set; // this is a list of wav file names for pronounciation 

然而,在遊戲中,圖片被裝入的Texture2D和音頻被裝入SoundEffect中的對象。

我試圖通過確保Word類不包含任何內在地將其本身與XNA綁定或如何顯示圖形來設計好的模塊化代碼。話雖如此,我想知道如何去創建XNA特定部分。

你覺得像這樣的東西會是一個好主意嗎?

base class SpriteEntity 
-- Position x, y 
-- Scale 
-- Display(gametime) 
-- Logic(gametime) 

base class SpriteEntity_2D : SpriteEntity 
-- Int32 ActiveFrame 
-- List<Texture2D> Textures; 
-- override Display(gametime) { show the Texture2D } 
-- override Logic(gametime) { ... } 

注意:你認爲這很奇怪,有SpriteEntity和SpriteEntity_2D嗎?我只做了這個,所以我也可以爲3D模型使用SpriteEntity_3D。

class WordSprite : SpriteEntity_2D 
-- ctor WordSprite (Word word) 

So, for example, if there was an instance of a Word called `AppleWord` in which: 
Word.WordName = "apple" 
WordSprite apple = new WordSprite(AppleWord) 

Then I can just set position and display it. 
apple.X = ..., apple.Y = ... 

WordSprite構造它可以檢查Word.PictureFilenames_Set和使用Texture2D.FromStream()在加載他們,但我覺得這是不好的原因有兩個:

  1. 加載正好在使用的時候可能導致滯後?
  2. 最終可能會加載多次。

我真想WordSprite去檢查一個自定義的ResourceManager,其中包含一個Dictionary類資源文件的名字,那會是更好?

但理想情況下,應該在開始時加載所有可能的資源。

也許我應該有一個叫做LoadResourcesFromWordList(List<Word> wordlist)方法,着眼於可在遊戲中的單詞列表,並在那個時候,它應該通過文件並調用:

ResourceManager.LoadTexture2DResource(filename) 
ResourceManager.LoadWaveFileResource(filename) 

它加載一切都變成Dictionary<object> Resources

比我能超載[]索引操作,使用戶只需撥打:

ResourceManager[ name_of_resource ]

然後,當你創建一個新的WordSprite它只會做這樣的事情在構造函數:

foreach(string pictfile in Word.PictureFilenames_Set) 
    { 
     WordTextureList.add(ResourceManager[pictfile]) 
    } 

對不起這個職位的巨大長度,它是一種思想的列車運行的。我在創建漂亮的模塊化代碼方面真的很差,而且我總是第二次猜測自己。從面向對象的設計角度來看,這看起來不錯嗎?還是我過於複雜?

非常感謝您的幫助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

- zoombini

P.S.任何人都可以推薦一本好書,從遊戲設計師的角度來解決模塊化,設計,面向對象等問題嗎?它不一定必須與C#或XNA相關。

回答

0

我不擔心有SpriteEntity_2D和SpriteEntity_3d作爲單獨的類。我認爲你的總體設計非常好,但如果不知道項目的全部範圍,很難說。如果你還沒有,你應該閱讀基於組件的遊戲系統。有一些好的東西在這裏:Component based game engine design

此外,一本好書的設計(沒有具體涉及到遊戲)是http://www.amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/0596007124/ref=sr_1_sc_1?s=books&ie=UTF8&qid=1326855704&sr=1-1-spell