damongant

Untitled

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