source: lost-perception/utils/Utils.java

Last change on this file was c3b2f42, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Make the loading screen show correctly on OSX, remove some .class files that were accidentally committed, and change the name of the jar file generated by make from LostHavenRPG.jar to LostPerception.jar.

  • Property mode set to 100644
File size: 10.7 KB
Line 
1package utils;
2
3import java.awt.Rectangle;
4import java.awt.Shape;
5import collision.Bound;
6import java.awt.geom.Ellipse2D;
7import main.Enemy;
8import java.util.ArrayList;
9import main.Creature;
10import java.util.HashMap;
11import java.awt.Graphics2D;
12import java.awt.image.BufferedImageOp;
13import java.awt.Composite;
14import java.awt.AlphaComposite;
15import java.awt.Color;
16import java.awt.image.ImageObserver;
17import java.awt.Image;
18import javax.imageio.ImageIO;
19import java.awt.image.BufferedImage;
20import java.awt.FontFormatException;
21import java.io.File;
22import java.awt.Font;
23import java.io.IOException;
24import java.io.FileReader;
25import java.io.Reader;
26import java.io.InputStreamReader;
27import java.io.BufferedReader;
28import java.awt.DisplayMode;
29import java.awt.GraphicsDevice;
30import java.util.Date;
31import java.text.SimpleDateFormat;
32import main.Direction;
33import java.awt.GraphicsConfiguration;
34
35public class Utils {
36 private static boolean RUNNING_FROM_JAR;
37 private static Utils classLoaderReference;
38 public static GraphicsConfiguration gc;
39
40 static {
41 Utils.classLoaderReference = null;
42 }
43
44 public static void init(final GraphicsConfiguration gc, final boolean jar) {
45 Utils.gc = gc;
46 Utils.RUNNING_FROM_JAR = jar;
47 }
48
49 public static String shortString(final Direction dir) {
50 switch (dir) {
51 case North: {
52 return "n";
53 }
54 case NorthEast: {
55 return "ne";
56 }
57 case East: {
58 return "e";
59 }
60 case SouthEast: {
61 return "se";
62 }
63 case South: {
64 return "s";
65 }
66 case SouthWest: {
67 return "sw";
68 }
69 case West: {
70 return "w";
71 }
72 case NorthWest: {
73 return "nw";
74 }
75 default: {
76 return "";
77 }
78 }
79 }
80
81 public static String dateString() {
82 return new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date());
83 }
84
85 private static DisplayMode getBestDisplayMode(final GraphicsDevice device, final int xRes, final int yRes) {
86 final DisplayMode[] BEST_DISPLAY_MODES = { new DisplayMode(xRes, yRes, 32, 0), new DisplayMode(xRes, yRes, 16, 0), new DisplayMode(xRes, yRes, 8, 0) };
87 for (int x = 0; x < BEST_DISPLAY_MODES.length; ++x) {
88 final DisplayMode[] modes = device.getDisplayModes();
89 for (int i = 0; i < modes.length; ++i) {
90 if (modes[i].getWidth() == BEST_DISPLAY_MODES[x].getWidth() && modes[i].getHeight() == BEST_DISPLAY_MODES[x].getHeight() && modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth()) {
91 return BEST_DISPLAY_MODES[x];
92 }
93 }
94 }
95 return null;
96 }
97
98 public static void chooseBestDisplayMode(final GraphicsDevice device, final int xRes, final int yRes) {
99 final DisplayMode best = getBestDisplayMode(device, xRes, yRes);
100 if (best != null) {
101 device.setDisplayMode(best);
102 }
103 }
104
105 public static BufferedReader loadTextFile(final String fileName) throws IOException {
106 if (Utils.RUNNING_FROM_JAR) {
107 if (Utils.classLoaderReference == null) {
108 Utils.classLoaderReference = new Utils();
109 }
110 return new BufferedReader(new InputStreamReader(Utils.classLoaderReference.getClass().getResourceAsStream("/" + fileName)));
111 }
112 return new BufferedReader(new FileReader(fileName));
113 }
114
115 public static Font loadFont(final String fileName) {
116 try {
117 if (Utils.RUNNING_FROM_JAR) {
118 if (Utils.classLoaderReference == null) {
119 Utils.classLoaderReference = new Utils();
120 }
121 return Font.createFont(0, Utils.classLoaderReference.getClass().getResourceAsStream("/" + fileName));
122 }
123 return Font.createFont(0, new File(fileName));
124 }
125 catch (FontFormatException ffe) {
126 ffe.printStackTrace();
127 }
128 catch (IOException ioe) {
129 ioe.printStackTrace();
130 }
131 return null;
132 }
133
134 public static BufferedImage loadImg(final String fileName) {
135 try {
136 BufferedImage init;
137 if (Utils.RUNNING_FROM_JAR) {
138 if (Utils.classLoaderReference == null) {
139 Utils.classLoaderReference = new Utils();
140 }
141 init = ImageIO.read(Utils.classLoaderReference.getClass().getResource("/images/" + fileName));
142 } else {
143 init = ImageIO.read(new File("images/" + fileName));
144 }
145 final BufferedImage accelerated = Utils.gc.createCompatibleImage(init.getWidth(), init.getHeight(), 3);
146 accelerated.getGraphics().drawImage(init, 0, 0, null);
147 return accelerated;
148 } catch (IOException ioe) {
149 ioe.printStackTrace();
150 return null;
151 }
152 }
153
154 public static BufferedImage loadTransImg(final String fileName, final Color bgColor, final Color shadowColor) {
155 try {
156 BufferedImage init;
157 if (Utils.RUNNING_FROM_JAR) {
158 init = ImageIO.read(Utils.classLoaderReference.getClass().getResource("/images/" + fileName));
159 }
160 else {
161 init = ImageIO.read(new File("images/" + fileName));
162 }
163 init = makeColorTransparent(init, bgColor, 0);
164 init = makeColorTransparent(init, shadowColor, -1593835520);
165 final BufferedImage accelerated = Utils.gc.createCompatibleImage(init.getWidth(), init.getHeight(), 3);
166 accelerated.getGraphics().drawImage(init, 0, 0, null);
167 return accelerated;
168 }
169 catch (IOException ioe) {
170 ioe.printStackTrace();
171 return null;
172 }
173 }
174
175 public static BufferedImage makeColorTransparent(final BufferedImage image, final Color color, final int newColor) {
176 final BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), 2);
177 final Graphics2D g = dimg.createGraphics();
178 g.setComposite(AlphaComposite.Src);
179 g.drawImage(image, null, 0, 0);
180 g.dispose();
181 for (int i = 0; i < dimg.getHeight(); ++i) {
182 for (int j = 0; j < dimg.getWidth(); ++j) {
183 if (dimg.getRGB(j, i) == color.getRGB()) {
184 dimg.setRGB(j, i, newColor);
185 }
186 }
187 }
188 return dimg;
189 }
190
191 public static void loadCreatures(final HashMap<Integer, Creature> crMap, final ArrayList<Creature> crSet) {
192 final int[][] numFramesArr = { { 8, 1, 13, 9, 9 }, { 9, 1, 9, 7, 9 }, { 8, 1, 13, 9, 11 }, { 9, 1, 13, 7, 9 }, { 8, 1, 9, 7, 11 }, { 8, 1, 13, 9, 13 }, { 8, 1, 13, 9, 13 } };
193 Enemy cr = new Enemy("Black Guard", 0, 22, 400);
194 cr.loadModel("black-knight", new Color(111, 79, 51), new Color(41, 29, 18), numFramesArr[0]);
195 cr.setBound(new Bound(new Ellipse2D.Double(-16.0, -16.0, 32.0, 32.0)));
196 cr.setSelectionBound(new Bound(new Rectangle(-20, -53, 40, 63)));
197 cr.strikeFrame = 9;
198 cr.spotRadius = 240;
199 cr.patrolRadius = 400;
200 cr.setAttack(10, 0, 32, 60);
201 cr.setStats(0, 0, 10, 10);
202 if (crSet != null) {
203 crSet.add(cr);
204 }
205 crMap.put(cr.id, cr);
206 cr = new Enemy("Rogue", 0, 30, 300);
207 cr.loadModel("burra", new Color(106, 76, 48), new Color(39, 27, 17), numFramesArr[1]);
208 cr.setBound(new Bound(new Ellipse2D.Double(-13.0, -13.0, 26.0, 26.0)));
209 cr.setSelectionBound(new Bound(new Rectangle(-16, -50, 32, 60)));
210 cr.strikeFrame = 6;
211 cr.spotRadius = 300;
212 cr.patrolRadius = 500;
213 cr.setAttack(5, 0, 32, 60);
214 cr.setStats(0, 0, 7, 7);
215 if (crSet != null) {
216 crSet.add(cr);
217 }
218 crMap.put(cr.id, cr);
219 cr = new Enemy("Ninja", 0, 22, 500);
220 cr.loadModel("black-ninja", new Color(106, 76, 48), new Color(39, 27, 17), numFramesArr[2]);
221 cr.setBound(new Bound(new Ellipse2D.Double(-13.0, -13.0, 26.0, 26.0)));
222 cr.setSelectionBound(new Bound(new Rectangle(-16, -54, 32, 60)));
223 cr.strikeFrame = 9;
224 cr.spotRadius = 300;
225 cr.patrolRadius = 500;
226 cr.setAttack(20, 0, 32, 60);
227 cr.setStats(0, 0, 15, 15);
228 if (crSet != null) {
229 crSet.add(cr);
230 }
231 crMap.put(cr.id, cr);
232 cr = new Enemy("Ranger", 0, 30, 350);
233 cr.loadModel("swordstan", new Color(106, 76, 48), new Color(39, 27, 17), numFramesArr[3]);
234 cr.setBound(new Bound(new Ellipse2D.Double(-13.0, -13.0, 26.0, 26.0)));
235 cr.setSelectionBound(new Bound(new Rectangle(-16, -47, 32, 57)));
236 cr.strikeFrame = 9;
237 cr.spotRadius = 450;
238 cr.patrolRadius = 700;
239 cr.setAttack(6, 0, 32, 60);
240 cr.setStats(0, 0, 8, 8);
241 if (crSet != null) {
242 crSet.add(cr);
243 }
244 crMap.put(cr.id, cr);
245 cr = new Enemy("White Dragon", 0, 52, 1000);
246 cr.loadModel("airdragon", new Color(106, 76, 48), new Color(39, 27, 17), numFramesArr[4]);
247 cr.setBound(new Bound(new Ellipse2D.Double(-20.0, -20.0, 40.0, 40.0)));
248 cr.setSelectionBound(new Bound(new Rectangle(-24, -53, 48, 63)));
249 cr.strikeFrame = 5;
250 cr.spotRadius = 240;
251 cr.patrolRadius = 400;
252 cr.setAttack(50, 0, 45, 70);
253 cr.setStats(0, 0, 120, 120);
254 if (crSet != null) {
255 crSet.add(cr);
256 }
257 crMap.put(cr.id, cr);
258 cr = new Enemy("White Mage", 0, 30, 400);
259 cr.loadModel("white-mage", new Color(105, 74, 46), new Color(33, 23, 14), numFramesArr[6]);
260 cr.setBound(new Bound(new Ellipse2D.Double(-16.0, -16.0, 32.0, 32.0)));
261 cr.setSelectionBound(new Bound(new Rectangle(-16, -47, 32, 57)));
262 cr.strikeFrame = 9;
263 cr.spotRadius = 240;
264 cr.patrolRadius = 400;
265 cr.setAttack(10, 0, 45, 70);
266 cr.setStats(0, 0, 5, 5);
267 if (crSet != null) {
268 crSet.add(cr);
269 }
270 crMap.put(cr.id, cr);
271 cr = new Enemy("Orc", 0, 22, 250);
272 cr.loadModel("orc", new Color(105, 74, 46), new Color(33, 23, 14), numFramesArr[5]);
273 cr.setBound(new Bound(new Ellipse2D.Double(-13.0, -13.0, 26.0, 26.0)));
274 cr.setSelectionBound(new Bound(new Rectangle(-18, -57, 36, 67)));
275 cr.strikeFrame = 9;
276 cr.spotRadius = 240;
277 cr.patrolRadius = 400;
278 cr.setAttack(30, 0, 45, 70);
279 cr.setStats(0, 0, 40, 40);
280 if (crSet != null) {
281 crSet.add(cr);
282 }
283 crMap.put(cr.id, cr);
284 }
285}
Note: See TracBrowser for help on using the repository browser.