damongant

Untitled

May 1st, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 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 : GameAsset
  14. {
  15. int width;
  16.  
  17. public int Width
  18. {
  19. get { return width; }
  20. }
  21. int height;
  22.  
  23. public int Height
  24. {
  25. get { return height; }
  26. }
  27. int tileSize;
  28.  
  29. public int TileSize
  30. {
  31. get { return tileSize; }
  32. }
  33. Tile[,] tiles;
  34.  
  35. public Tile GetTile(Vector2 v)
  36. {
  37. return tiles[(int)v.X-1, (int)v.Y-1];
  38. }
  39.  
  40. public Tile GetTile(int x, int y)
  41. {
  42. return tiles[x-1, y-1];
  43. }
  44.  
  45. Texture2D texture;
  46.  
  47. public Texture2D Texture
  48. {
  49. get { return texture; }
  50. }
  51.  
  52. public Texture2D GetTexture(GraphicsDevice g)
  53. {
  54. if (texture != null)
  55. return texture;
  56. if (!IsReady)
  57. DownloadAsset();
  58. texture = Texture2D.FromStream(g, new FileStream(FilePath, FileMode.Open));
  59. return texture;
  60. }
  61.  
  62. public bool IsCompatibleTileset()
  63. {
  64. return (tileSize == Utils.TILESIZE);
  65. }
  66.  
  67. public string GetFilePath()
  68. {
  69. if (!File.Exists(FilePath))
  70. throw new Exception("File is not ready!");
  71. return FilePath;
  72. }
  73.  
  74. public string FilePath
  75. {
  76. get { return "UContent\\Tilesets\\" + hash + ".png"; }
  77. }
  78.  
  79. public void DownloadAsset()
  80. {
  81. if (IsReady)
  82. return; //We have this asset already
  83. WebClient client = new WebClient();
  84. var remotePath = string.Format("{0}{1}.png", Utils.CDN, hash);
  85. client.DownloadFile(remotePath, FilePath);
  86. }
  87.  
  88. public bool IsReady
  89. {
  90. get { return File.Exists("UContent\\Tilesets\\" + hash + ".png"); }
  91. }
  92.  
  93. public static Tileset FromHash(string hash)
  94. {
  95. var path = "UContent\\Tilesets\\" + hash + ".xml";
  96. if (File.Exists(path))
  97. return FromManifest(path);
  98. if (!Directory.Exists("UContent\\Tilesets\\"))
  99. Directory.CreateDirectory("UContent\\Tilesets\\");
  100. WebClient client = new WebClient();
  101. var remotePath = string.Format("{0}{1}.xml", Utils.CDN, hash);
  102. client.DownloadFile(remotePath, path);
  103. return FromManifest(path);
  104. }
  105.  
  106. public static Tileset FromManifest(string filename)
  107. {
  108. var doc = XDocument.Load(filename);
  109. Tileset set = new Tileset();
  110. set.width = Convert.ToInt32(doc.Element("tileset").Attribute("width").Value);
  111. set.height = Convert.ToInt32(doc.Element("tileset").Attribute("height").Value);
  112. set.hash = doc.Element("tileset").Attribute("hash").Value;
  113. set.tileSize = Convert.ToInt32(doc.Element("tileset").Attribute("tilesize").Value);
  114. if (!set.IsCompatibleTileset())
  115. throw new Exception("tileSize mismatch - not valid for this game");
  116.  
  117. set.tiles = new Tile[set.width, set.height];
  118.  
  119. foreach (var tile in doc.Element("tileset").Elements("tile"))
  120. {
  121. var offsetX = Convert.ToInt32(tile.Attribute("offsetX").Value);
  122. var offsetY = Convert.ToInt32(tile.Attribute("offsetY").Value);
  123. var playerPass = (PlayerPass)Convert.ToByte(tile.Element("playerPass").Value);
  124. set.tiles[offsetX-1, offsetY-1] = new Tile()
  125. {
  126. Offset = new Point(offsetX, offsetY),
  127. PlayerPass = playerPass,
  128. TilesetHash = set.hash
  129. };
  130. }
  131.  
  132. return set;
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment