Skip to content

Commit e09eba7

Browse files
committed
Update resource reading system
1 parent d1f69c7 commit e09eba7

4 files changed

Lines changed: 61 additions & 44 deletions

File tree

.github/workflows/build-jar.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
- name: Upload JAR as release asset
4545
uses: actions/upload-release-asset@v1
4646
env:
47-
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
47+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4848
with:
4949
upload_url: ${{ github.event.release.upload_url }}
5050
asset_path: build/jar/MazeGame.jar

dev/abhay7/MazeGame/MapGenerator.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package dev.abhay7.MazeGame;
22

3-
import java.io.File;
4-
import java.io.FileNotFoundException;
3+
import java.io.InputStream;
54
import java.util.ArrayList;
65
import java.util.Scanner;
76
import java.util.Stack;
87

98
public class MapGenerator {
109

11-
public static Integer[][][] maps;
10+
public Integer[][][] maps;
1211

13-
static {
14-
File f = new File(MazeGame.MAPS_PATH);
15-
try {
16-
Scanner scan = new Scanner(f);
12+
public MapGenerator() {
13+
try (InputStream inputStream = getClass().getResourceAsStream(MazeGame.MAPS_PATH)) {
14+
Scanner scan = new Scanner(inputStream);
1715
ArrayList<Integer[][]> mapsList = new ArrayList<>();
1816

1917
ArrayList<Integer[]> rows = new ArrayList<>();
@@ -48,7 +46,7 @@ public class MapGenerator {
4846
}
4947

5048
scan.close();
51-
} catch(FileNotFoundException fnfe) {
49+
} catch(Exception e) {
5250
System.err.println("Failed to read res/maps.txt! Levels will not be read.\n");
5351
// fnfe.printStackTrace();
5452
maps = new Integer[][][] {
@@ -79,11 +77,11 @@ public class MapGenerator {
7977
}
8078
}
8179

82-
public static Integer[][] getMap(int level) {
80+
public Integer[][] getMap(int level) {
8381
return maps[level];
8482
}
8583

86-
public static Integer[][] generateMaze(int size) {
84+
public Integer[][] generateMaze(int size) {
8785
if(size < 5) throw new IllegalArgumentException("Argument " + size + " is less than 5");
8886
Integer[][] maze = new Integer[size][size];
8987

@@ -112,7 +110,7 @@ public static Integer[][] generateMaze(int size) {
112110
return maze;
113111
}
114112

115-
private static void dfsGeneration(Coord curr, Stack<Coord> coords, Integer[][] maze) {
113+
private void dfsGeneration(Coord curr, Stack<Coord> coords, Integer[][] maze) {
116114
if(coords.size() == 0) return;
117115
ArrayList<Coord> availCoords = getAvailCoords(curr, maze);
118116
if(availCoords.size() > 0) {
@@ -126,7 +124,7 @@ private static void dfsGeneration(Coord curr, Stack<Coord> coords, Integer[][] m
126124
}
127125
}
128126

129-
private static ArrayList<Coord> getAvailCoords(Coord curr, Integer[][] maze) {
127+
private ArrayList<Coord> getAvailCoords(Coord curr, Integer[][] maze) {
130128

131129
ArrayList<Coord> coords = new ArrayList<Coord>();
132130

@@ -169,7 +167,7 @@ private static ArrayList<Coord> getAvailCoords(Coord curr, Integer[][] maze) {
169167
return coords;
170168
}
171169

172-
private static class Coord {
170+
private class Coord {
173171
private int y, x;
174172
Coord(int y, int x) {
175173
this.y = y;

dev/abhay7/MazeGame/MazeGame.java

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import java.awt.event.MouseEvent;
1111
import java.awt.event.MouseListener;
1212
import java.awt.image.BufferStrategy;
13-
1413
import java.util.concurrent.TimeUnit;
1514

1615
import javax.imageio.ImageIO;
1716

18-
import java.io.File;
17+
import java.io.FileNotFoundException;
18+
import java.io.InputStream;
1919

2020
import javax.swing.JLabel;
2121
import javax.swing.JOptionPane;
@@ -46,13 +46,15 @@ public class MazeGame extends Canvas implements Runnable, MouseListener {
4646
// Actual Player/Camera object
4747
public Player player;
4848

49+
private MapGenerator mapGenerator;
50+
4951
// Resources
50-
public static final String MAPS_PATH = "res/maps.txt";
51-
public static final String BACKGROUND_IMAGE_PATH = "res/bgimage.jpg";
52-
public static final String TITLE_FONT_PATH = "res/valorant.ttf";
53-
public static final String BUTTON_FONT_PATH = "res/nintendo.otf";
54-
public static final String WALKING_SFX = "res/footsteps.wav";
55-
public static final String GAME_SOUND = "res/MazeGameSong.wav";
52+
public static final String MAPS_PATH = "/res/maps.txt";
53+
public static final String BACKGROUND_IMAGE_PATH = "/res/bgimage.jpg";
54+
public static final String TITLE_FONT_PATH = "/res/valorant.ttf";
55+
public static final String BUTTON_FONT_PATH = "/res/nintendo.otf";
56+
public static final String WALKING_SFX = "/res/footsteps.wav";
57+
public static final String GAME_SOUND = "/res/MazeGameSong.wav";
5658
private Image backgroundImage;
5759
private Font titleFont;
5860
private Font buttonFont;
@@ -86,37 +88,50 @@ public class MazeGame extends Canvas implements Runnable, MouseListener {
8688
// Constructor for this mazegame, which essentially initializes a Window while passing "this" game instance as the canvas
8789
public MazeGame() {
8890
// Initialize our campaign maps
89-
this.map = MapGenerator.getMap(currentLevel);
90-
levelCount = MapGenerator.maps.length;
91+
mapGenerator = new MapGenerator();
92+
this.map = mapGenerator.getMap(currentLevel);
93+
levelCount = mapGenerator.maps.length;
9194

9295
sp = new SoundPlayer(GAME_SOUND);
9396
soundThread = new Thread(sp);
9497
soundThread.start();
9598

9699
// Initialize Resources
97-
try {
98-
File bgImgFile = new File(BACKGROUND_IMAGE_PATH);
99-
backgroundImage = ImageIO.read(bgImgFile);
100-
} catch(Exception e) {
100+
try (InputStream bgImgStream = getClass().getResourceAsStream(BACKGROUND_IMAGE_PATH)) {
101+
if (bgImgStream != null) {
102+
backgroundImage = ImageIO.read(bgImgStream);
103+
} else {
104+
throw new FileNotFoundException("Background image not found: " + BACKGROUND_IMAGE_PATH);
105+
}
106+
} catch (Exception e) {
101107
backgroundImage = null;
102108
e.printStackTrace();
103109
}
104110

105-
try {
106-
File fontFile = new File(TITLE_FONT_PATH);
107-
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
108-
titleFont = titleFont.deriveFont(titleFont.getSize() * (WIDTH / 20) * 1.0f);
109-
} catch(Exception e) {
111+
try (InputStream fontStream = getClass().getResourceAsStream(TITLE_FONT_PATH)) {
112+
if (fontStream != null) {
113+
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
114+
titleFont = titleFont.deriveFont(titleFont.getSize() * (WIDTH / 20) * 1.0f);
115+
} else {
116+
throw new FileNotFoundException("Title font not found: " + TITLE_FONT_PATH);
117+
}
118+
} catch (Exception e) {
110119
titleFont = null;
120+
e.printStackTrace();
111121
}
112122

113-
try {
114-
File fontFile = new File(BUTTON_FONT_PATH);
115-
buttonFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
116-
buttonFont = buttonFont.deriveFont(buttonFont.getSize() * (WIDTH / 20) * 1.0f);
117-
} catch(Exception e) {
123+
try (InputStream fontStream = getClass().getResourceAsStream(BUTTON_FONT_PATH)) {
124+
if (fontStream != null) {
125+
buttonFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
126+
buttonFont = buttonFont.deriveFont(buttonFont.getSize() * (WIDTH / 20) * 1.0f);
127+
} else {
128+
throw new FileNotFoundException("Button font not found: " + BUTTON_FONT_PATH);
129+
}
130+
} catch (Exception e) {
118131
buttonFont = null;
132+
e.printStackTrace();
119133
}
134+
120135

121136
// Create the window
122137
new Window(WIDTH, HEIGHT, TITLE, ICON_PATH, this);
@@ -206,7 +221,7 @@ private void tickGame() {
206221
player.setX(playerStartX);
207222
player.setY(playerStartY);
208223
player.setDirection(playerStartDirection);
209-
map = MapGenerator.getMap(currentLevel);
224+
map = mapGenerator.getMap(currentLevel);
210225
}
211226
if(player.getQuitIsPressed() == true) {
212227
player.setQuitIsPressed(false);
@@ -216,7 +231,7 @@ private void tickGame() {
216231
player.setX(playerStartX);
217232
player.setY(playerStartY);
218233
player.setDirection(playerStartDirection);
219-
map = MapGenerator.getMap(currentLevel);
234+
map = mapGenerator.getMap(currentLevel);
220235
isGenMap = false;
221236
}
222237
}
@@ -404,7 +419,7 @@ else if(e.getX() > quitButX1 && e.getX() < quitButX2 && e.getY() > quitButY1 &&
404419
else if(e.getX() > easyButX1 && e.getX() < easyButX2 && e.getY() > easyButY1 && e.getY() < easyButY2) isEasyMode = true;
405420
else if(e.getX() > hardButX1 && e.getX() < hardButX2 && e.getY() > hardButY1 && e.getY() < hardButY2) isEasyMode = false;
406421
else if(e.getX() > genButX1 && e.getX() < genButX2 && e.getY() > genButY1 && e.getY() < genButY2) {
407-
map = MapGenerator.generateMaze(35);
422+
map = mapGenerator.generateMaze(35);
408423
isPlaying = true;
409424
isGenMap = true;
410425
}

dev/abhay7/MazeGame/SoundPlayer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.abhay7.MazeGame;
22

3-
import java.io.File;
3+
import java.net.URL;
44

55
import javax.sound.sampled.AudioInputStream;
66
import javax.sound.sampled.AudioSystem;
@@ -13,8 +13,12 @@ public class SoundPlayer implements Runnable {
1313

1414
public SoundPlayer(String audPath) {
1515
try {
16-
File f = new File(audPath);
17-
this.audioStream = AudioSystem.getAudioInputStream(f);
16+
URL audioUrl = getClass().getResource(audPath);
17+
if (audioUrl == null) {
18+
throw new IllegalArgumentException("Audio file not found: " + audPath);
19+
}
20+
21+
this.audioStream = AudioSystem.getAudioInputStream(audioUrl);
1822
audioClip = AudioSystem.getClip();
1923
audioClip.open(this.audioStream);
2024
} catch(Exception e) {

0 commit comments

Comments
 (0)