package game;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

import static game.TileSource.*;

public class TilesFactory {

	private static TilesFactory singleton;

	// load tile sets
	private final Image[] TILE_SETS = new Image[] { new Image("/resources/roguelikeMap_transparent.png"),
			new Image("/resources/roguelikeIndoor_transparent.png"),
			new Image("/resources/roguelikeChar_transparent.png") };

	public static TilesFactory getTilesFactory() {
		if (singleton == null) {
			singleton = new TilesFactory();
		}
		return singleton;
	}

	private TilesFactory() {

	}

	public Image getMapTiles() {
		return TILE_SETS[MAP_TILES.ordinal()];
	}

	public Image getIndoorTiles() {
		return TILE_SETS[INDOOR_TILES.ordinal()];
	}

	public Image getCharTiles() {
		return TILE_SETS[CHAR_TILES.ordinal()];
	}

	
	/**
	 * 
	 * @param gc
	 * @param tileSource
	 * @param x , coordinate for drawing
	 * @param y , coordinate for drawing
	 * @param size , scaling size
	 * @param tileX , choose tile with coordinate (starting at 0, 0)
	 * @param tileY , choose tile with coordinate (starting at 0, 0)
	 */
	public void drawTile(GraphicsContext gc, TileSource tileSource, int x, int y, int size, int tileX, int tileY) {
		int cols = (int) ((TILE_SETS[tileSource.ordinal()].getWidth() + 1) / 17);
		
		int tile = tileX + cols * tileY;

		drawTile(gc, tileSource, x, y, size, tile);
	}

	public void drawTile(GraphicsContext gc, TileSource tileSource, int x, int y, int size, int tile) {
		int cols;
		
		cols = (int) ((TILE_SETS[tileSource.ordinal()].getWidth() + 1) / 17);
		gc.drawImage(TILE_SETS[tileSource.ordinal()], (16 + 1) * (tile % cols), (16 + 1) * (tile / cols), 16, 16, x * size, y * size,
				size, size);
	}

}

@@ -85,16 +85,95 @@
 .Image;%0A
+import javafx.scene.image.PixelReader;%0Aimport javafx.scene.image.WritableImage;
 %0Aimport 
@@ -510,16 +510,49 @@
 png%22) %7D;
+%0A%09%0A%09private Image%5B%5D%5B%5D%5B%5D subTiles;
 %0A%0A%09publi
@@ -709,16 +709,472 @@
 ory() %7B%0A
+%09%09%0A%09%09int sourceCount = TILE_SETS.length;%0A%09%09subTiles = new Image%5BsourceCount%5D%5B%5D%5B%5D;%0A%09%09%0A%09%09//1st dim. count of different ressauces%0A%09%09for(int i = 0; i %3C sourceCount; i++)%7B%0A%09%09%09//2. dim number of cols%0A%09%09%09int cols = (int) ((TILE_SETS%5Bi%5D.getWidth() + 1) / 17);%0A%09%09%09subTiles%5Bi%5D = new Image%5Bcols%5D%5B%5D;%0A%09%09%09for(int j = 0; j %3C cols; j++)%7B%0A%09%09%09%09//3. dim number of rows%0A%09%09%09%09int rows = (int) ((TILE_SETS%5Bi%5D.getHeight() + 1) / 17);%0A%09%09%09%09subTiles%5Bi%5D%5Bj%5D = new Image%5Brows%5D;%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A%0A%09pu
@@ -1806,24 +1806,92 @@
  tileY) %7B%0A%09%09
+%0A%09%09if(subTiles%5BtileSource.ordinal()%5D%5BtileX%5D%5BtileY%5D == null)%7B%0A%09%09%09%0A%09%09%09
 int cols = (
@@ -1954,18 +1954,16 @@
  17);%0A%09%09
-%0A%09
 %09int til
@@ -1992,51 +1992,346 @@
 eY;%0A
-%0A%09%09drawTile(gc, tileSource, x, y, 
+%09%09%09%0A%09%09%09PixelReader reader = TILE_SETS%5BtileSource.ordinal()%5D.getPixelReader();%0A%09%09%09WritableImage newImage = new WritableImage(reader, (16 + 1) * (tile %25 cols), (16 + 1) * (tile / cols), 16, 16);%0A%09%09%09%0A%09%09%09subTiles%5BtileSource.ordinal()%5D%5BtileX%5D%5BtileY%5D = newImage;%0A%09%09%7D%0A%09%09%0A%09%09gc.drawImage(subTiles%5BtileSource.ordinal()%5D%5BtileX%5D%5BtileY%5D, x*
 size, 
-til
+y*siz
 e);%0A

optimize (sub)images with tiles