Skip to content

Commit de2414f

Browse files
committed
Encapsulated all variables
1 parent 103e425 commit de2414f

File tree

6 files changed

+167
-71
lines changed

6 files changed

+167
-71
lines changed

src/com/redomar/game/Game.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ public class Game extends Canvas implements Runnable {
3030
private static final long serialVersionUID = 1L;
3131

3232
// Setting the size and name of the frame/canvas
33-
public static final int WIDTH = 160;
34-
public static final int HEIGHT = (WIDTH / 3 * 2);
35-
public static final int SCALE = 3;
36-
public static final String NAME = "Game";
37-
public static Game game;
33+
private static final int WIDTH = 160;
34+
private static final int HEIGHT = (WIDTH / 3 * 2);
35+
private static final int SCALE = 3;
36+
private static final String NAME = "Game";
37+
private static Game game;
3838
private static int Jdata_Host;
3939
private static String Jdata_UserName = "";
4040
private static String Jdata_IP = "127.0.0.1";
4141

4242
private JFrame frame;
4343

44-
public boolean running = false;
45-
public int tickCount = 0;
44+
private boolean running = false;
45+
private int tickCount = 0;
4646

4747
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
4848
BufferedImage.TYPE_INT_RGB);
@@ -57,10 +57,10 @@ public class Game extends Canvas implements Runnable {
5757
private LevelHandler level;
5858
private Player player;
5959
private Music music = new Music();
60-
public Thread musicThread = new Thread(music);
61-
public String nowPlaying = "Playing Music";
60+
private Thread musicThread = new Thread(music);
61+
private String nowPlaying = "Playing Music";
6262

63-
public boolean notActive = true;
63+
private boolean notActive = true;
6464

6565
private GameClient socketClient;
6666
private GameServer socketServer;
@@ -82,7 +82,7 @@ public Game() {
8282
}
8383

8484
public void init() {
85-
game = this;
85+
setGame(this);
8686
int index = 0;
8787
for (int r = 0; r < 6; r++) {
8888
for (int g = 0; g < 6; g++) {
@@ -178,7 +178,7 @@ public void run() {
178178
}
179179

180180
public void tick() {
181-
tickCount++;
181+
setTickCount(getTickCount() + 1);
182182
getLevel().tick();
183183
}
184184

@@ -189,8 +189,8 @@ public void render() {
189189
return;
190190
}
191191

192-
int xOffset = getPlayer().x - (screen.width / 2);
193-
int yOffset = getPlayer().y - (screen.height / 2);
192+
int xOffset = getPlayer().x - (screen.getWidth() / 2);
193+
int yOffset = getPlayer().y - (screen.getHeight() / 2);
194194

195195
getLevel().renderTiles(screen, xOffset, yOffset);
196196

@@ -203,9 +203,9 @@ public void render() {
203203

204204
getLevel().renderEntities(screen);
205205

206-
for (int y = 0; y < screen.height; y++) {
207-
for (int x = 0; x < screen.width; x++) {
208-
int colourCode = screen.pixels[x + y * screen.width];
206+
for (int y = 0; y < screen.getHeight(); y++) {
207+
for (int x = 0; x < screen.getWidth(); x++) {
208+
int colourCode = screen.getPixels()[x + y * screen.getWidth()];
209209
if (colourCode < 255) {
210210
pixels[x + y * WIDTH] = colours[colourCode];
211211
}
@@ -264,15 +264,15 @@ public static void main(String[] args) {
264264
Thread.sleep(750);
265265
splash.setProgress(92, "Aquring data: Multiplayer");
266266
Thread.sleep(200);
267-
Jdata_Host = JOptionPane.showConfirmDialog(game, "Do you want to be the HOST?");
267+
Jdata_Host = JOptionPane.showConfirmDialog(getGame(), "Do you want to be the HOST?");
268268
if (Jdata_Host == 1){
269-
Jdata_IP = JOptionPane.showInputDialog(game, "Enter the name \nleave blank for local");
269+
Jdata_IP = JOptionPane.showInputDialog(getGame(), "Enter the name \nleave blank for local");
270270
}
271271
Thread.sleep(200);
272272
splash.setProgress(95, "Aquring data: Username");
273273
Thread.sleep(200);
274274
splash.setProgress(96, "Initalizing as Server:Host");
275-
Jdata_UserName = JOptionPane.showInputDialog(game, "Enter a name");
275+
Jdata_UserName = JOptionPane.showInputDialog(getGame(), "Enter a name");
276276
splash.setProgress(97, "Connecting as" + Jdata_UserName);
277277
Thread.sleep(500);
278278
splash.splashOff();
@@ -331,4 +331,20 @@ public void setNowPlaying(String nowPlaying) {
331331
this.nowPlaying = nowPlaying;
332332
}
333333

334+
public int getTickCount() {
335+
return tickCount;
336+
}
337+
338+
public void setTickCount(int tickCount) {
339+
this.tickCount = tickCount;
340+
}
341+
342+
public static Game getGame() {
343+
return game;
344+
}
345+
346+
public static void setGame(Game game) {
347+
Game.game = game;
348+
}
349+
334350
}

src/com/redomar/game/entities/Player.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void tick() {
4646
isMoving = true;
4747

4848
Packet02Move packet = new Packet02Move(this.getUsername(), this.x, this.y, this.numSteps, this.isMoving, this.movingDir);
49-
packet.writeData(Game.game.getSocketClient());
49+
packet.writeData(Game.getGame().getSocketClient());
5050

5151
} else {
5252
isMoving = false;

src/com/redomar/game/gfx/Screen.java

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22

33
public class Screen {
44

5-
public static final int MAP_WIDTH = 64;
6-
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
5+
private static final int MAP_WIDTH = 64;
6+
private static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
77

8-
public static final byte BIT_MIRROR_X = 0x01;
9-
public static final byte BIT_MIRROR_Y = 0x02;
8+
private static final byte BIT_MIRROR_X = 0x01;
9+
private static final byte BIT_MIRROR_Y = 0x02;
1010

11-
public int[] pixels;
11+
private int[] pixels;
1212

13-
public int xOffset = 0;
14-
public int yOffset = 0;
13+
private int xOffset = 0;
14+
private int yOffset = 0;
1515

16-
public int width;
17-
public int height;
16+
private int width;
17+
private int height;
1818

19-
public SpriteSheet sheet;
19+
private SpriteSheet sheet;
2020

2121
public Screen(int width, int height, SpriteSheet sheet) {
2222

23-
this.width = width;
24-
this.height = height;
23+
this.setWidth(width);
24+
this.setHeight(height);
2525
this.sheet = sheet;
2626

27-
pixels = new int[width * height];
27+
setPixels(new int[width * height]);
2828
}
2929

3030
public void render(int xPos, int yPos, int tile, int colour, int mirrorDir,
@@ -38,7 +38,7 @@ public void render(int xPos, int yPos, int tile, int colour, int mirrorDir,
3838
int scaleMap = scale - 1;
3939
int xTile = tile % 32;
4040
int yTile = tile / 32;
41-
int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
41+
int tileOffset = (xTile << 3) + (yTile << 3) * sheet.getWidth();
4242

4343
for (int y = 0; y < 8; y++) {
4444
int ySheet = y;
@@ -59,23 +59,23 @@ public void render(int xPos, int yPos, int tile, int colour, int mirrorDir,
5959
int xPixel = x + xPos + (x * scaleMap) - ((scaleMap << 3) / 2);
6060

6161
int col = (colour >> (sheet.pixels[xSheet + ySheet
62-
* sheet.width + tileOffset] * 8)) & 255;
62+
* sheet.getWidth() + tileOffset] * 8)) & 255;
6363
if (col < 255) {
6464

6565
for (int yScale = 0; yScale < scale; yScale++) {
6666

67-
if (yPixel + yScale < 0 | yPixel + yScale >= height) {
67+
if (yPixel + yScale < 0 | yPixel + yScale >= getHeight()) {
6868
continue;
6969
}
7070

7171
for (int xScale = 0; xScale < scale; xScale++) {
7272

73-
if (xPixel + xScale < 0 | xPixel + xScale >= width) {
73+
if (xPixel + xScale < 0 | xPixel + xScale >= getWidth()) {
7474
continue;
7575
}
7676

77-
pixels[(xPixel + xScale) + (yPixel + yScale)
78-
* width] = col;
77+
getPixels()[(xPixel + xScale) + (yPixel + yScale)
78+
* getWidth()] = col;
7979
}
8080
}
8181

@@ -89,4 +89,32 @@ public void setOffset(int xOffset, int yOffset) {
8989
this.xOffset = xOffset;
9090
this.yOffset = yOffset;
9191
}
92+
93+
public static int getMapWidthMask() {
94+
return MAP_WIDTH_MASK;
95+
}
96+
97+
public int getWidth() {
98+
return width;
99+
}
100+
101+
public void setWidth(int width) {
102+
this.width = width;
103+
}
104+
105+
public int[] getPixels() {
106+
return pixels;
107+
}
108+
109+
public void setPixels(int[] pixels) {
110+
this.pixels = pixels;
111+
}
112+
113+
public int getHeight() {
114+
return height;
115+
}
116+
117+
public void setHeight(int height) {
118+
this.height = height;
119+
}
92120
}

src/com/redomar/game/gfx/SpriteSheet.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
public class SpriteSheet {
99

10-
public String path;
11-
public int width;
12-
public int height;
10+
private String path;
11+
private int width;
12+
private int height;
1313

1414
public int[] pixels;
1515

@@ -26,11 +26,11 @@ public SpriteSheet(String path) {
2626
return;
2727
}
2828

29-
this.path = path;
30-
this.width = image.getWidth();
29+
this.setPath(path);
30+
this.setWidth(image.getWidth());
3131
this.height = image.getHeight();
3232

33-
pixels = image.getRGB(0, 0, width, height, null, 0, width);
33+
pixels = image.getRGB(0, 0, getWidth(), height, null, 0, getWidth());
3434

3535
for (int i = 0; i < pixels.length; i++) {
3636
pixels[i] = (pixels[i] & 0xff) / 64; // removes alpha (transparency)
@@ -40,4 +40,20 @@ public SpriteSheet(String path) {
4040
// System.out.println(pixels[i]);
4141
}
4242
}
43+
44+
public String getPath() {
45+
return path;
46+
}
47+
48+
public void setPath(String path) {
49+
this.path = path;
50+
}
51+
52+
public int getWidth() {
53+
return width;
54+
}
55+
56+
public void setWidth(int width) {
57+
this.width = width;
58+
}
4359
}

src/com/redomar/game/level/LevelHandler.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
public class LevelHandler {
1818

1919
private byte[] tiles;
20-
public int width;
21-
public int height;
20+
private int width;
21+
private int height;
2222
private List<Entity> entities = new ArrayList<Entity>();
2323
private String imagePath;
2424
private BufferedImage image;
@@ -53,7 +53,7 @@ private void loadTiles() {
5353
width);
5454
for (int y = 0; y < height; y++) {
5555
for (int x = 0; x < width; x++) {
56-
tileCheck: for (Tile t : Tile.tiles) {
56+
tileCheck: for (Tile t : Tile.getTiles()) {
5757
if (t != null
5858
&& t.getLevelColour() == tileColours[x + y * width]) {
5959
this.tiles[x + y * width] = t.getId();
@@ -84,9 +84,9 @@ private void generateLevel() {
8484
for (int y = 0; y < height; y++) {
8585
for (int x = 0; x < width; x++) {
8686
if (x * y % 10 < 7) {
87-
tiles[x + y * width] = Tile.GRASS.getId();
87+
tiles[x + y * width] = Tile.getGrass().getId();
8888
} else {
89-
tiles[x + y * width] = Tile.STONE.getId();
89+
tiles[x + y * width] = Tile.getStone().getId();
9090
}
9191
}
9292
}
@@ -101,7 +101,7 @@ public void tick() {
101101
e.tick();
102102
}
103103

104-
for (Tile t : Tile.tiles) {
104+
for (Tile t : Tile.getTiles()) {
105105
if (t == null) {
106106
break;
107107
}
@@ -114,20 +114,20 @@ public void renderTiles(Screen screen, int xOffset, int yOffset) {
114114
if (xOffset < 0) {
115115
xOffset = 0;
116116
}
117-
if (xOffset > ((width << 3) - screen.width)) {
118-
xOffset = ((width << 3) - screen.width);
117+
if (xOffset > ((width << 3) - screen.getWidth())) {
118+
xOffset = ((width << 3) - screen.getWidth());
119119
}
120120
if (yOffset < 0) {
121121
yOffset = 0;
122122
}
123-
if (yOffset > ((height << 3) - screen.height)) {
124-
yOffset = ((height << 3) - screen.height);
123+
if (yOffset > ((height << 3) - screen.getHeight())) {
124+
yOffset = ((height << 3) - screen.getHeight());
125125
}
126126

127127
screen.setOffset(xOffset, yOffset);
128128

129-
for (int y = (yOffset >> 3); y < (yOffset + screen.height >> 3) + 1; y++) {
130-
for (int x = (xOffset >> 3); x < (xOffset + screen.width >> 3) + 1; x++) {
129+
for (int y = (yOffset >> 3); y < (yOffset + screen.getHeight() >> 3) + 1; y++) {
130+
for (int x = (xOffset >> 3); x < (xOffset + screen.getWidth() >> 3) + 1; x++) {
131131
getTile(x, y).render(screen, this, x << 3, y << 3);
132132
}
133133
}
@@ -141,9 +141,9 @@ public void renderEntities(Screen screen) {
141141

142142
public Tile getTile(int x, int y) {
143143
if (0 > x || x >= width || 0 > y || y >= height) {
144-
return Tile.VOID;
144+
return Tile.getVoid();
145145
}
146-
return Tile.tiles[tiles[x + y * width]];
146+
return Tile.getTiles()[tiles[x + y * width]];
147147
}
148148

149149
public void addEntity(Entity entity) {

0 commit comments

Comments
 (0)