source: lost-haven/main/Map.java

Last change on this file was 4d8825f, checked in by Dmitry Portnoy <dmp1488@…>, 4 years ago

Add logic to make text files readable from inside a JAR

  • Property mode set to 100644
File size: 6.8 KB
Line 
1package main;
2
3import java.awt.Color;
4import java.awt.image.BufferedImage;
5import java.io.*;
6import java.util.HashMap;
7
8import javax.imageio.ImageIO;
9
10public class Map {
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 = Utils.loadTextFile(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 }
168}
Note: See TracBrowser for help on using the repository browser.