Advertisement
Guest User

Untitled

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