Guest User

Untitled

a guest
Sep 10th, 2018
229
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Name: Isometric Tiled
  2. // Submenu: Stylize
  3. // Author: Anthony "Mallaboro"
  4. // Title: Isometric Builder
  5. // Version: 1.0
  6. // Desc: Turns orthogonal images into an array of isometric tiles
  7. // Keywords: Orthogonal, Isometric
  8. // URL: https://litematrix.blogspot.com/
  9. // Help: anthonyeboothroyd@gmail.com
  10.  
  11. #region UICode
  12. bool Amount1 = true;//Render background (black)
  13. bool Amount2 = true;//Arrange tiles by orthogonal position
  14. #endregion
  15.  
  16. class Tile
  17. {
  18. //tile width and height
  19. public static int Width = 128;
  20. public static int Height = 64;
  21. //these indexes are used when arranging tiles by orthogonal position
  22. public int x = 0;
  23. public int y = 0;
  24.  
  25. //the array of pixel data
  26. public ColorBgra[,] pixels;
  27.  
  28. public Tile(int x, int y, bool checkers)
  29. {
  30. pixels = new ColorBgra[ Tile.Width, Tile.Height ];
  31. this.x = x;
  32. this.y = y;
  33.  
  34. if(checkers)
  35. {
  36. //outside
  37. for( int _y = 0; _y < Tile.Height; _y++ )
  38. for( int _x = 0; _x < Tile.Width; _x++ )
  39. {
  40. if( _y < Tile.Height / 2 )
  41. if( _x <= ( Tile.Width / 2 ) - ( 2 * _y ) ||
  42. _x >= Tile.Width / 2 + ( 2 * _y ) )
  43. {
  44. pixels[ _x, _y ].R = 0;
  45. pixels[ _x, _y ].G = 0;
  46. pixels[ _x, _y ].B = 0;
  47. pixels[ _x, _y ].A = 255;
  48.  
  49. }
  50.  
  51. if( _y >= Tile.Height / 2 )
  52. if( _x <= ( Tile.Width / 2 ) - ( Tile.Width / 2 ) + ( _y - ( Tile.Height / 2 ) ) * 2 ||
  53. _x >= ( Tile.Width / 2 ) + ( Tile.Width / 2 ) - ( _y - ( Tile.Height / 2 ) ) * 2 )
  54. {
  55. pixels[ _x, _y ].R = 0;
  56. pixels[ _x, _y ].G = 0;
  57. pixels[ _x, _y ].B = 0;
  58. pixels[ _x, _y ].A = 255;
  59. }
  60. }
  61. }
  62. }
  63.  
  64. //"Input" the incoming pixel data
  65. //"x", "y", the pixel index of this tiles pixel array
  66. public void Set( ColorBgra input, int x, int y )
  67. {
  68. //temporary restriction, we do not accept black pixels
  69. //if( pixels[ x, y ] == ColorBgra.Black )
  70. // return;
  71.  
  72. //if the incoming pixel being set is within these parameters,
  73. //it is within this tiles shape and will be stored.
  74.  
  75. bool left = x <= Tile.Width / 2;
  76. bool right = x >= Tile.Width / 2;
  77.  
  78. bool top = y <= Tile.Height / 2;
  79. bool topRight = x < ( Tile.Width / 2 ) + ( y * 2 );
  80. bool topLeft = x > ( Tile.Width / 2 ) - ( y * 2 );
  81.  
  82. bool bot = y > Tile.Height / 2;
  83. bool botLeft = x > ( 2 * ( y - Tile.Height / 2 ) );
  84. bool botRight = x < ( Tile.Width ) - ( 2 * ( y - Tile.Height / 2 ) );
  85.  
  86. //tiles are seporated into 4 quadrants. If the incoming pixel overlaps one of these 4 quadrants it is stored.
  87.  
  88. if( top )
  89. if( left
  90. && topLeft )
  91. {
  92. pixels[ x, y ] = input;
  93. }
  94.  
  95. if( top )
  96. if( right
  97. && topRight )
  98. {
  99. pixels[ x, y ] = input;
  100. }
  101.  
  102. if( bot )
  103. if( left
  104. && botLeft )
  105. {
  106. pixels[ x, y ] = input;
  107. }
  108.  
  109. if( bot )
  110. if( right
  111. && botRight
  112. )
  113. {
  114. pixels[ x, y ] = input;
  115. }
  116.  
  117. }
  118.  
  119. //returns whether or not this tile contains any pixel of color (excluding pure black and pure white)
  120. public bool IsOccupied()
  121. {
  122. for( int y = 0; y < Height; y++ )
  123. for( int x = 0; x < Width; x++ )
  124. {
  125. if( pixels[ x, y ].R > 0 && pixels[ x, y ].R < 255 ||
  126. pixels[ x, y ].G > 0 && pixels[ x, y ].G < 255 ||
  127. pixels[ x, y ].B > 0 && pixels[ x, y ].B < 255 )
  128. return true;
  129. }
  130.  
  131. return false;
  132. }
  133.  
  134. //returns the specified pixel at the x, y index.
  135. public ColorBgra Get( int x, int y )
  136. {
  137. return pixels[ x, y ];
  138. }
  139. }
  140.  
  141. void Render( Surface dst, Surface src, Rectangle rect )
  142. {
  143. //clear the canvas
  144. for (int y = rect.Top; y < rect.Bottom; y++) {
  145. if (IsCancelRequested) return;
  146. for (int x = rect.Left; x < rect.Right; x++)
  147. dst[x,y] = ColorBgra.Transparent;
  148. }
  149.  
  150. //NOTE TO SELF: top-left is 0, 0
  151.  
  152. //a list of occupied tiles containing data
  153. List<Tile> tiles = new List<Tile>( );
  154.  
  155. //sample tile to check whether it has any data
  156. Tile sample;
  157.  
  158. //toggle changes every Y iteration. It keeps the x-axis in the correct position for isometric images
  159. //(toggle == true == pixel_x = 0), (else pixel_x = -( Tile.Width / 2 );)
  160. bool toggle = false;
  161.  
  162. //pixelX and Y are the top-left corners of the tile being analyzed. These values change.
  163. int pixel_x = -( Tile.Width / 2 );
  164. int pixel_y = -( Tile.Height / 2 );
  165.  
  166. //easy-access
  167. int width = src.Bounds.Width;
  168. int height = src.Bounds.Height;
  169.  
  170. //tiles wide and tiles tall are incrimented by 1 to capture the image along the edges
  171. //to elaborate: tiles[0,0] position is -(Tile.Width / 2, -(Tile.Height / 2
  172. //tiles tall is doubled to account for Tile.Height being half of Tile.Width
  173. int tilesWide = (width / Tile.Width) + 1;
  174. int tilesTall = (height / Tile.Height) * 2 + 1;
  175.  
  176. //assign each tile their location
  177. for (int y = 0; y < tilesTall; y++)
  178. {
  179. for (int x = 0; x < tilesWide; x++)
  180. {
  181. sample = new Tile(x, y, Amount1);
  182.  
  183. for (int Y = 0; Y < Tile.Height; Y++)
  184. {
  185. for (int X = 0; X < Tile.Width; X++)
  186. {
  187. if ((pixel_x + X < 1) || (pixel_y + Y < 1))
  188. continue;
  189. if ((pixel_x + X > width-2) || (pixel_y + Y > height-2))
  190. continue;
  191.  
  192. sample.Set(src[pixel_x + X, pixel_y + Y], X, Y);
  193. }
  194. }
  195.  
  196. if(sample.IsOccupied())
  197. tiles.Add(sample);
  198.  
  199. pixel_x += Tile.Width;
  200. }
  201.  
  202. toggle = !toggle;
  203. pixel_x = toggle == true ? 0 : -( Tile.Width / 2);
  204. pixel_y += Tile.Height / 2;
  205. }
  206.  
  207. int _x = 0;
  208. int _y = 0;
  209.  
  210. if(Amount2 == false)
  211. {
  212. for(int i = 0; i < tiles.Count; i++)
  213. {
  214. for (pixel_y = 0; pixel_y < Tile.Height; pixel_y++)
  215. for (pixel_x = 0; pixel_x < Tile.Width; pixel_x++)
  216. {
  217. //prevent crashes if screen width or height isn't great enough
  218. //to fit the tiles
  219. if((_x * Tile.Width) + pixel_x >= width)
  220. break;
  221. if((_y * Tile.Height) + pixel_y >= height)
  222. break;
  223.  
  224. dst[ (_x * Tile.Width) + pixel_x
  225. , ( _y * Tile.Height) + pixel_y] = tiles[i].pixels[pixel_x, pixel_y];
  226. }
  227.  
  228. _x++;
  229.  
  230. if(_x >= tilesWide-1)
  231. {
  232. _x = 0;
  233. _y ++;
  234. }
  235. }
  236. }
  237. else
  238. {
  239. for(int i = 0; i < tiles.Count; i++)
  240. {
  241. for (pixel_y = 0; pixel_y < Tile.Height; pixel_y++)
  242. for (pixel_x = 0; pixel_x < Tile.Width; pixel_x++)
  243. {
  244. //prevent crashes if screen width or height isn't great enough
  245. //to fit the tiles
  246. if((tiles[i].x * Tile.Width) + pixel_x >= width)
  247. break;
  248. if((tiles[i].y * Tile.Height) + pixel_y >= height)
  249. break;
  250.  
  251. dst[ (tiles[i].x * Tile.Width) + pixel_x
  252. , ( tiles[i].y * Tile.Height) + pixel_y] = tiles[i].pixels[pixel_x, pixel_y];
  253. }
  254. }
  255. }
  256. }
RAW Paste Data