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