Changeset 8edd04e in lost-haven for main/Map.java
- Timestamp:
- Jun 7, 2020, 3:04:32 PM (5 years ago)
- Branches:
- master
- Children:
- a49176d
- Parents:
- 155577b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/Map.java
r155577b r8edd04e 1 1 package main; 2 2 3 import java.awt.Color; 4 import java.awt.image.BufferedImage; 3 5 import java.io.*; 4 import java.util.*; 6 import java.util.HashMap; 7 8 import javax.imageio.ImageIO; 5 9 6 10 public class Map { 7 private Location[][] grid; 8 9 public Map(int x, int y) { 10 grid = new Location[x][y]; 11 } 12 13 public Map(String landFile, String structFile, HashMap<LandType, Land> landMap, HashMap<StructureType, Structure> structMap) { 14 try { 15 int length, height, x, y; 16 String str, loc; 17 BufferedReader in = new BufferedReader(new FileReader(landFile)); 18 19 str = in.readLine(); 20 length = Integer.parseInt(str.substring(0, str.indexOf("x"))); 21 height = Integer.parseInt(str.substring(str.indexOf("x")+1)); 22 grid = new Location[length][height]; 23 24 for(x=0; x<height; x++) { 25 str = in.readLine(); 26 for(y=0; y<length; y++) { 27 if(str.indexOf(",") == -1) 28 loc = str; 29 else { 30 loc = str.substring(0, str.indexOf(",")); 31 str = str.substring(str.indexOf(",")+1); 32 } 33 34 if(loc.equals("o")) { 35 loc = "Ocean"; 36 }else if(loc.equals("1")) { 37 loc = "Grass"; 38 } 39 40 grid[y][x] = new Location(landMap.get(LandType.valueOf(loc)), structMap.get(StructureType.None)); 41 } 42 } 43 44 in.close(); 45 46 in = new BufferedReader(new FileReader(structFile)); 47 48 while((loc = in.readLine()) != null) { 49 str = in.readLine(); 50 x = Integer.valueOf(str.substring(0, str.indexOf(","))); 51 y = Integer.valueOf(str.substring(str.indexOf(",")+1)); 52 53 grid[x-1][y-1].setStruct(structMap.get(StructureType.valueOf(loc))); 54 } 55 } catch(IOException ioe) { 56 ioe.printStackTrace(); 57 } 58 } 59 60 public Location getLoc(int x, int y) { 61 return grid[x][y]; 62 } 63 64 public void setLoc(Location loc, int x, int y) { 65 grid[x][y] = loc; 66 } 67 68 public int getLength() { 69 return grid.length; 70 } 71 72 public int getHeight() { 73 if(grid.length>0) 74 return grid[0].length; 75 else 76 return 0; 77 } 11 12 private Location[][] grid; 13 14 public Map(int x, int y) { 15 this.grid = new Location[x][y]; 16 } 17 18 public Map(String mapFile, String structFile, HashMap<LandType, Land> landMap, HashMap<StructureType, Structure> structMap, boolean readMapFromImage) { 19 if (readMapFromImage) { 20 try { 21 BufferedImage img = ImageIO.read(getClass().getResource("../images/" + mapFile)); 22 int length = img.getHeight(); 23 int height = img.getWidth(); 24 this.grid = new Location[height][length]; 25 int x; 26 for (x = 0; x < height; x++) { 27 for (int y = 0; y < length; y++) { 28 String loc; 29 Color clr = new Color(img.getRGB(x, y)); 30 if (clr.getRed() == 243 && clr.getGreen() == 119 && clr.getBlue() == 0) { 31 loc = "Lava"; 32 } else if (clr.getRed() == 128 && clr.getGreen() == 0 && clr.getBlue() == 0) { 33 loc = "Metal"; 34 } else if (clr.getRed() == 255 && clr.getGreen() == 0 && clr.getBlue() == 0) { 35 loc = "Charred"; 36 } else if (clr.getRed() == 95 && clr.getGreen() == 155 && clr.getBlue() == 0) { 37 loc = "Swamp"; 38 } else if (clr.getRed() == 0 && clr.getGreen() == 67 && clr.getBlue() == 0) { 39 loc = "Vines"; 40 } else if (clr.getRed() == 255 && clr.getGreen() == 0 && clr.getBlue() == 255) { 41 loc = "Crystal"; 42 } else if (clr.getRed() == 128 && clr.getGreen() == 0 && clr.getBlue() == 128) { 43 loc = "CrystalFormation"; 44 } else if (clr.getRed() == 0 && clr.getGreen() == 0 && clr.getBlue() == 255) { 45 loc = "Water"; 46 } else if (clr.getRed() == 0 && clr.getGreen() == 128 && clr.getBlue() == 0) { 47 loc = "Forest"; 48 } else if (clr.getRed() == 139 && clr.getGreen() == 63 && clr.getBlue() == 43) { 49 loc = "Tree"; 50 } else if (clr.getRed() == 179 && clr.getGreen() == 247 && clr.getBlue() == 207) { 51 loc = "Plains"; 52 } else if (clr.getRed() == 255 && clr.getGreen() == 255 && clr.getBlue() == 0) { 53 loc = "Desert"; 54 } else if (clr.getRed() == 83 && clr.getGreen() == 83 && clr.getBlue() == 83) { 55 loc = "Mountains"; 56 } else if (clr.getRed() == 8 && clr.getGreen() == 0 && clr.getBlue() == 0) { 57 loc = "Cave"; 58 } else if (clr.getRed() == 0 && clr.getGreen() == 0 && clr.getBlue() == 128) { 59 loc = "Ocean"; 60 } else if (clr.getRed() == 0 && clr.getGreen() == 255 && clr.getBlue() == 255) { 61 loc = "Snow"; 62 } else if (clr.getRed() == 160 && clr.getGreen() == 160 && clr.getBlue() == 164) { 63 loc = "Steam"; 64 } else { 65 loc = "Mountains"; 66 } 67 this.grid[x][y] = new Location(landMap.get(LandType.valueOf(loc)), structMap.get(StructureType.None)); 68 } 69 } 70 BufferedReader in = new BufferedReader(new FileReader(structFile)); 71 String str; 72 while ((str = in.readLine()) != null) { 73 String loc = in.readLine(); 74 x = Integer.valueOf(loc.substring(0, loc.indexOf(","))).intValue(); 75 int y = Integer.valueOf(loc.substring(loc.indexOf(",") + 1)).intValue(); 76 Point loc1 = new Point((x - 1) * 100 + 50, (y - 1) * 100 + 50); 77 if (str.equals("ArtifactPoint") || str.equals("BossPoint")) { 78 String strTarg = in.readLine(); 79 int x2 = Integer.valueOf(strTarg.substring(0, strTarg.indexOf(","))).intValue(); 80 int y2 = Integer.valueOf(strTarg.substring(strTarg.indexOf(",") + 1)).intValue(); 81 Point loc2 = new Point((x2 - 1) * 100 + 50, (y2 - 1) * 100 + 50); 82 ArtifactPoint struct = new ArtifactPoint((ArtifactPoint)structMap.get(StructureType.valueOf(str)), loc1); 83 struct.setTarget(loc2); 84 this.grid[x - 1][y - 1].setStruct(struct); 85 ArtifactPoint struct2 = new ArtifactPoint((ArtifactPoint)structMap.get(StructureType.valueOf(str)), loc2); 86 struct2.setTarget(loc1); 87 this.grid[x2 - 1][y2 - 1].setStruct(struct2); 88 continue; 89 } 90 if (str.equals("RespawnPoint")) { 91 this.grid[x - 1][y - 1].setStruct(new RespawnPoint((RespawnPoint)structMap.get(StructureType.valueOf(str)), loc1)); 92 LostHavenRPG.respawnPoints.add((RespawnPoint)this.grid[x - 1][y - 1].getStruct()); 93 continue; 94 } 95 this.grid[x - 1][y - 1].setStruct(new Structure(structMap.get(StructureType.valueOf(str)), loc1)); 96 } 97 in.close(); 98 } catch (IOException ioe) { 99 ioe.printStackTrace(); 100 } 101 } else { 102 try { 103 BufferedReader in = new BufferedReader(new FileReader("../" + mapFile)); 104 String str = in.readLine(); 105 int length = Integer.parseInt(str.substring(0, str.indexOf("x"))); 106 int height = Integer.parseInt(str.substring(str.indexOf("x") + 1)); 107 this.grid = new Location[length][height]; 108 int x; 109 for (x = 0; x < height; x++) { 110 str = in.readLine(); 111 for (int y = 0; y < length; y++) { 112 String loc; 113 if (str.indexOf(",") == -1) { 114 loc = str; 115 } else { 116 loc = str.substring(0, str.indexOf(",")); 117 str = str.substring(str.indexOf(",") + 1); 118 } 119 if (loc.equals("o")) { 120 loc = "OceanOld"; 121 } else if (loc.equals("1")) { 122 loc = "GrassOld"; 123 } 124 this.grid[y][x] = new Location(landMap.get(LandType.valueOf(loc)), structMap.get(StructureType.None)); 125 } 126 } 127 in.close(); 128 in = new BufferedReader(new FileReader(structFile)); 129 while ((str = in.readLine()) != null) { 130 String loc = in.readLine(); 131 x = Integer.valueOf(loc.substring(0, loc.indexOf(","))).intValue(); 132 int y = Integer.valueOf(loc.substring(loc.indexOf(",") + 1)).intValue(); 133 this.grid[x - 1][y - 1].setStruct(structMap.get(StructureType.valueOf(str))); 134 } 135 in.close(); 136 } catch (IOException ioe) { 137 ioe.printStackTrace(); 138 } 139 } 140 } 141 142 public void clearCreatures() { 143 for (int x = 0; x < getLength(); x++) { 144 for (int y = 0; y < getHeight(); y++) { 145 getLoc(x, y).getCreatures().clear(); 146 } 147 } 148 } 149 150 public Location getLoc(int x, int y) { 151 return this.grid[x][y]; 152 } 153 154 public void setLoc(Location loc, int x, int y) { 155 this.grid[x][y] = loc; 156 } 157 158 public int getLength() { 159 return this.grid.length; 160 } 161 162 public int getHeight() { 163 if (this.grid.length > 0) { 164 return (this.grid[0]).length; 165 } 166 return 0; 167 } 78 168 }
Note:
See TracChangeset
for help on using the changeset viewer.