Using constants
This commit is contained in:
@ -21,9 +21,6 @@ public class Screen extends JPanel {
|
||||
public static final int SCREEN_WIDTH = MAX_SCREEN_COL * TILE_SIZE;
|
||||
public static final int SCREEN_HEIGHT = MAX_SCREEN_ROW * TILE_SIZE;
|
||||
|
||||
//WORLD
|
||||
public final int WORLD_MAX_COL = 50;
|
||||
public final int WORLD_MAX_ROW = 50;
|
||||
|
||||
World world = new World(this);
|
||||
KeyHandler keyHandler = new KeyHandler();
|
||||
|
||||
@ -1,14 +1,18 @@
|
||||
package se.urmo.my2dgame.tile;
|
||||
|
||||
import se.urmo.my2dgame.entity.Player;
|
||||
import se.urmo.my2dgame.main.Screen;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class World {
|
||||
public static final int WORLD_MAX_COL = 50;
|
||||
public static final int WORLD_MAX_ROW = 50;
|
||||
Screen screen;
|
||||
public static Tile[] tiles = TileManager.getTiles();
|
||||
public int[][] mapTileNum;
|
||||
@ -20,19 +24,19 @@ public class World {
|
||||
}
|
||||
|
||||
private int[][] loadMap(String path) {
|
||||
int[][] map = new int[screen.WORLD_MAX_ROW][screen.WORLD_MAX_COL];
|
||||
int[][] map = new int[WORLD_MAX_ROW][WORLD_MAX_COL];
|
||||
try {
|
||||
InputStream stream = Screen.class.getResourceAsStream(path);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
||||
int row = 0;
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null && row < screen.WORLD_MAX_ROW) {
|
||||
while ((line = reader.readLine()) != null && row < WORLD_MAX_ROW) {
|
||||
map[row++] = Arrays.stream(line.split(" "))
|
||||
.mapToInt(Integer::parseInt)
|
||||
.toArray();
|
||||
}
|
||||
reader.close();
|
||||
} catch (Exception e) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return map;
|
||||
@ -42,22 +46,22 @@ public class World {
|
||||
int worldCol = 0;
|
||||
int worldRow = 0;
|
||||
|
||||
while (worldCol < screen.WORLD_MAX_COL && worldRow < screen.WORLD_MAX_ROW) {
|
||||
while (worldCol < WORLD_MAX_COL && worldRow < WORLD_MAX_ROW) {
|
||||
int tileNum = mapTileNum[worldRow][worldCol];
|
||||
|
||||
int worldX = worldCol * screen.TILE_SIZE;
|
||||
int worldY = worldRow * screen.TILE_SIZE;
|
||||
int worldX = worldCol * Screen.TILE_SIZE;
|
||||
int worldY = worldRow * Screen.TILE_SIZE;
|
||||
|
||||
// Only drawImage for tiles within camera
|
||||
if (screen.isWithinScreen(worldX, worldY)) {
|
||||
int screenX = worldX - screen.player.worldX + screen.player.SCREEN_X;
|
||||
int screenY = worldY - screen.player.worldY + screen.player.SCREEN_Y;
|
||||
g2d.drawImage(tiles[tileNum].getImage(), screenX, screenY, screen.TILE_SIZE, screen.TILE_SIZE, null);
|
||||
int screenX = worldX - screen.player.worldX + Player.SCREEN_X;
|
||||
int screenY = worldY - screen.player.worldY + Player.SCREEN_Y;
|
||||
g2d.drawImage(tiles[tileNum].getImage(), screenX, screenY, Screen.TILE_SIZE, Screen.TILE_SIZE, null);
|
||||
}
|
||||
|
||||
worldCol++;
|
||||
|
||||
if (worldCol == screen.WORLD_MAX_COL) {
|
||||
if (worldCol == WORLD_MAX_COL) {
|
||||
worldCol = 0;
|
||||
worldRow++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user