0

I've been working on a small game off and on related to words for Windows using XNA 4.0 and C#. Words can be edited through a separate Windows Form editor (NOT XNA).

The Word class (which is serializable) contains the following objects:

    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

However, in the game, the pictures are loaded into Texture2D and the audio is loaded into SoundEffect objects.

I'm trying to design nice modular code by making sure that the Word class does not contain anything that does not intrinsically tie itself to XNA or how the graphics are displayed. That being said, I'm wondering how to go about creating the XNA specific portion.

Do you think something like this would be a good idea?

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) { ... }

NOTE: Do you think this is strange, having SpriteEntity and SpriteEntity_2D? I only did this so I could also have SpriteEntity_3D for 3d models.

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 = ...

In the WordSprite constructor it could check the Word.PictureFilenames_Set and use Texture2D.FromStream() to load them all in, but I feel like this is bad for two reasons:

  1. Loading exactly at the time of use could result in lag??
  2. Could end up loading more than one time.

I really want WordSprite to go check a custom ResourceManager, which contains a Dictionary class of the resource file names, would that be better?

But ideally, all of the possible resources should be loaded at the beginning.

Maybe I should have a method called LoadResourcesFromWordList( List<Word> wordlist ) which looks at the list of Words available in the game, and at that time, it should run through the files and call:

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

which loads everything into a Dictionary<object> Resources.

Than I can overload the [ ] index operator so that the user can just call:

ResourceManager[ name_of_resource ]

Then when you create a new WordSprite it will just do something like this in the ctor:

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

Sorry about the HUGE LENGTH of this post, it was kind of a running train of thought. I'm really poor at creating nice modular designed code, and I always second guess myself. Does this seem okay from a OO design standpoint or am I overcomplicating things?

Thanks very much for your assistance!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

-- zoombini

P.S. Can anyone recommend a good book which addresses things like modularity, design, OO from a game designer's perspective? It doesn't necessarily have to be related to C# or XNA.

zoombini
  • 124
  • 1
  • 11

1 Answers1

0

I wouldn't worry about having SpriteEntity_2D and SpriteEntity_3d as separate classes. I think your overall design is pretty good but it is hard to say without knowing the full extent of the project. If you haven't already, you should read about component based systems for games. There is some good stuff here: https://stackoverflow.com/questions/1901251/component-based-game-engine-design

Also, a good book on design(not specifically related to gaming) is 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

Community
  • 1
  • 1
theDazzler
  • 1,039
  • 2
  • 11
  • 27