Advertisement
HuskyWolf

Untitled

Jul 31st, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package com.AI.Map;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7.  
  8. import com.AI.main.Main;
  9.  
  10. public class PixelMap {
  11.    
  12.     private int mapWidth, mapHeight;
  13.     private BufferedImage image;
  14.     private int w, h, type;
  15.     public static int Environment = 1, Terrain = 0;
  16.     private boolean firstTimeLoad = true;
  17.     private HashMap<Vector2, Tile> TilesInMap = new HashMap<Vector2, Tile>();
  18.     public List<Vector2> vList = new ArrayList<Vector2>();
  19.    
  20.     public PixelMap(int mapWidth, int mapHeight, int type, BufferedImage image){
  21.         System.out.println("PixelMap Converted, from pixelImage: " + image);
  22.         this.mapWidth = mapWidth;
  23.         this.mapHeight = mapHeight;
  24.         this.type = type;
  25.         this.image = image;
  26.         w = image.getWidth();
  27.         h = image.getHeight();
  28.     }
  29.    
  30.     public void RenderPixelMap(){
  31.         for(int xx = 0; xx < h; xx++){
  32.             for(int yy = 0; yy < w; yy++){
  33.                 int pixel = image.getRGB(xx, yy);
  34.                 int red = (pixel >> 16) & 0xff;
  35.                 int green = (pixel >> 8) & 0xff;
  36.                 int blue = (pixel) & 0xff;
  37.                
  38.                 if(type == 0){
  39.                     if(red == 25 && green == 255 & blue == 29){
  40.                         renderTile(Tiles.grassTile, new Vector2(xx, yy));
  41.                     }
  42.                     if(red == 0 && green == 155 & blue == 255){
  43.                         renderTile(Tiles.waterTile, new Vector2(xx, yy));
  44.                     }
  45.                     if(red == 255 && green == 187 & blue == 127){
  46.                         renderTile(Tiles.sandTile, new Vector2(xx, yy));
  47.                     }
  48.                     if(red == 128 && green == 128 & blue == 128){
  49.                         renderTile(Tiles.rockTile, new Vector2(xx, yy));
  50.                     }
  51.                 }
  52.                 if(type == 1){
  53.                     if(red == 107 && green == 255 & blue == 107){
  54.                         renderTile(Tiles.treeTile, new Vector2(xx, yy));
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.         if(type == 0)
  60.             if(firstTimeLoad)
  61.                 System.out.println("Terrain|| PixelsRendered: " + (TilesInMap.size() * (32*32)) + " TilesRendered: " + TilesInMap.size());
  62.         if(type == 1)
  63.             if(firstTimeLoad)
  64.                 System.out.println("Environment|| PixelsRendered: " + (TilesInMap.size() * (32*32)) + " TilesRendered: " + TilesInMap.size());
  65.  
  66.         firstTimeLoad = false;
  67.     }
  68.    
  69.     public void renderTile(Tile t, Vector2 V2){
  70.         if(V2.getX() <= mapWidth){
  71.             if(V2.getY() <= mapHeight){
  72.                 Main.getGfx().drawImage(t.getImage(), V2.getX() * 32, V2.getY() * 32, 32, 32, null);
  73.                 if(firstTimeLoad){
  74.                     Vector2 ve2 = new Vector2((V2.getX() * 32), (V2.getY() * 32));
  75.                     TilesInMap.put(ve2, t);
  76.                     vList.add(vList.size(), ve2);
  77.                 }
  78.             }else{
  79.                 System.out.println("Map out of Height bounds!!!");
  80.             }
  81.         }else{
  82.             System.out.println("Map out of Width bounds!!!");
  83.         }
  84.     }
  85.    
  86.     public Tile getTileAt(int xPos, int yPos){
  87.         int numberToShift;
  88.         if(xPos == 0)
  89.             numberToShift = 0;
  90.         else
  91.             numberToShift = -1;
  92.         int startingBlocks = (xPos - numberToShift) * mapHeight;
  93.         return TilesInMap.get(vList.get(startingBlocks + yPos));
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement