/// <summary> /// Reads the asset type information to gather information /// such as sprite color or other information. /// </summary> private void LoadAssetTypeMeta(AssetSpriteType type, FileInfo file) { // Open up the file using (StreamReader sr = file.OpenText()) { // Wrap it in an XML reader XmlTextReader xml = new XmlTextReader(sr); // Loop through it while (xml.Read()) { // Check for open elements if (xml.NodeType == XmlNodeType.Element) { switch (xml.LocalName) { case "color": // Assign a color to this block type.Color = Color.FromArgb( Int32.Parse(xml["r"]), Int32.Parse(xml["g"]), Int32.Parse(xml["b"])); break; } } } } }
/// <summary> /// Imports the block into the system, assigning any values /// that needs to be assigned. /// </summary> public void OnImportBlock(Block block) { // Get the asset key if (block == null || block.Sprite == null || block.Sprite.ID == null) { return; } string key = block.Sprite.ID; if (!sprites.Contains(key)) { return; } AssetSpriteType ast = sprites[key]; // Assign the colors block.Color = ast.Color; // Figure out heights if (key.Contains("Tall")) { block.Height = 2; } // Figure out shadows block.CastsShadows = key.Contains("Block") || key.Contains("Wall") || key.Contains("Door") || key.Contains("Window"); }
/// <summary> /// Constructs a sprite from the given key, caching the /// drawable data if needed. /// </summary> public ISprite CreateSprite(string key) { // See if we have the sprite type if (!sprites.Contains(key)) { throw new Exception("No such sprite key: " + key); } // Grab the asset type AssetSpriteType ast = sprites[key]; return(ast.CreateRandomSprite()); }
/// <summary> /// Function to queue up all the assets required for the /// functioning of the system. /// </summary> public void Queue() { // Scan the Assets/Images directory for drawables DirectoryInfo images = new DirectoryInfo( Path.Combine("Assets", "Images")); DirectoryInfo sounds = new DirectoryInfo( Path.Combine("Assets", "Sounds")); // Load the individual images into memory foreach (FileInfo fi in images.GetFiles("*.png")) { // Load these images QueueDrawable("Assets/Images/" + fi.Name); } // Load the individual directories Regex regex = new Regex(@"(\s+\d+)?\.png"); foreach (DirectoryInfo di in images.GetDirectories()) { // Ignore some common ones if (di.Name.StartsWith(".")) { continue; } // Get the asset sprite type for this directory string key = di.Name; if (!sprites.Contains(key)) { sprites[key] = new AssetSpriteType(key); } AssetSpriteType spriteType = sprites[key]; // Load the type information, if we have one if (File.Exists(Path.Combine(di.FullName, "block.xml"))) { // Load the block information, this controls the // color of the blocks. LoadAssetTypeMeta(spriteType, new FileInfo(Path.Combine(di.FullName, "block.xml"))); } // Go through each file in this directory foreach (FileInfo fi in di.GetFiles("*.png")) { // Figure out the components string path = String.Format("Assets/Images/{0}/{1}", key, fi.Name); string key2 = regex.Replace(fi.Name, ""); // Get the asset sprite AssetSprite sprite = spriteType.GetAssetSprite(key2); // Queue loading the drawable Queue(new AssetLoaderSprite(sprite, path)); } } // Look for sounds and music foreach (DirectoryInfo di in sounds.GetDirectories()) { // Go through each file in this directory. The // directory name is the category of music while the // individual files are randomly selected from the // category. // "Vorbis is the new mp3" foreach (FileInfo fi in di.GetFiles("*.ogg")) { // We want to register this file with the sound // manager. Since the SoundManager doesn't load // things, we are just registering directory with // it. Game.Sound.Register(di.Name, fi); } } }
/// <summary> /// Function to queue up all the assets required for the /// functioning of the system. /// </summary> public void Queue() { // Scan the Assets/Images directory for drawables DirectoryInfo images = new DirectoryInfo( Path.Combine("Assets", "Images")); DirectoryInfo sounds = new DirectoryInfo( Path.Combine("Assets", "Sounds")); // Load the individual images into memory foreach (FileInfo fi in images.GetFiles("*.png")) { // Load these images QueueDrawable("Assets/Images/" + fi.Name); } // Load the individual directories Regex regex = new Regex(@"(\s+\d+)?\.png"); foreach (DirectoryInfo di in images.GetDirectories()) { // Ignore some common ones if (di.Name.StartsWith(".")) continue; // Get the asset sprite type for this directory string key = di.Name; if (!sprites.Contains(key)) sprites[key] = new AssetSpriteType(key); AssetSpriteType spriteType = sprites[key]; // Load the type information, if we have one if (File.Exists(Path.Combine(di.FullName, "block.xml"))) { // Load the block information, this controls the // color of the blocks. LoadAssetTypeMeta(spriteType, new FileInfo(Path.Combine(di.FullName, "block.xml"))); } // Go through each file in this directory foreach (FileInfo fi in di.GetFiles("*.png")) { // Figure out the components string path = String.Format("Assets/Images/{0}/{1}", key, fi.Name); string key2 = regex.Replace(fi.Name, ""); // Get the asset sprite AssetSprite sprite = spriteType.GetAssetSprite(key2); // Queue loading the drawable Queue(new AssetLoaderSprite(sprite, path)); } } // Look for sounds and music foreach (DirectoryInfo di in sounds.GetDirectories()) { // Go through each file in this directory. The // directory name is the category of music while the // individual files are randomly selected from the // category. // "Vorbis is the new mp3" foreach (FileInfo fi in di.GetFiles("*.ogg")) { // We want to register this file with the sound // manager. Since the SoundManager doesn't load // things, we are just registering directory with // it. Game.Sound.Register(di.Name, fi); } } }