damongant

Untitled

Apr 22nd, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.IO;
  7. using System.Net;
  8. using Microsoft.Xna.Framework;
  9.  
  10. namespace PGN
  11. {
  12. public class Tileset
  13. {
  14. string hash;
  15. int width;
  16. int height;
  17. int tileSize;
  18. Tile[,] tiles;
  19.  
  20. public bool IsCompatibleTileset()
  21. {
  22. return (tileSize == Utils.TILESIZE);
  23. }
  24.  
  25. public string GetFilePath()
  26. {
  27. if (!File.Exists(FilePath))
  28. throw new Exception("File is not ready!");
  29. return FilePath;
  30. }
  31.  
  32. public string FilePath
  33. {
  34. get { return "UContent\\Tilesets\\" + hash + ".png"; }
  35. }
  36.  
  37. public void DownloadAsset()
  38. {
  39. if (IsReady)
  40. return; //We have this asset already
  41. WebClient client = new WebClient();
  42. var remotePath = string.Format("{0}{1}.png", Utils.CDN, hash);
  43. client.DownloadFile(remotePath, FilePath);
  44. }
  45.  
  46. public bool IsReady
  47. {
  48. get { return File.Exists("UContent\\Tilesets\\" + hash + ".png"); }
  49. }
  50.  
  51. public static Tileset FromHash(string hash)
  52. {
  53. var path = "UContent\\Tilesets\\" + hash + ".png";
  54. if (File.Exists(path))
  55. return FromManifest(path);
  56. WebClient client = new WebClient();
  57. var remotePath = string.Format("{0}{1}.xml", Utils.CDN, hash);
  58. client.DownloadFile(remotePath, path);
  59. return FromManifest(path);
  60. }
  61.  
  62. public static Tileset FromManifest(string filename)
  63. {
  64. var doc = XDocument.Load(filename);
  65. Tileset set = new Tileset();
  66. set.width = Convert.ToInt32(doc.Element("tileset").Attribute("width").Value);
  67. set.height = Convert.ToInt32(doc.Element("tileset").Attribute("height").Value);
  68. set.hash = doc.Element("tileset").Attribute("hash").Value;
  69. set.tileSize = Convert.ToInt32(doc.Element("tileset").Attribute("tilesize").Value);
  70. if (!set.IsCompatibleTileset())
  71. throw new Exception("tileSize mismatch - not valid for this game");
  72.  
  73. set.tiles = new Tile[set.width, set.height];
  74.  
  75. foreach (var tile in doc.Element("tileset").Elements("tile"))
  76. {
  77. var offsetX = Convert.ToInt32(tile.Attribute("offsetX").Value);
  78. var offsetY = Convert.ToInt32(tile.Attribute("offsetY").Value);
  79. var playerPass = (PlayerPass)Convert.ToByte(tile.Element("playerPass").Value);
  80. set.tiles[offsetX-1, offsetY-1] = new Tile()
  81. {
  82. Offset = new Vector2(offsetX, offsetY),
  83. PlayerPass = playerPass,
  84. TilesetHash = set.hash
  85. };
  86. }
  87.  
  88. return set;
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment