implemented camera

This commit is contained in:
2025-11-26 22:05:58 +01:00
parent e60f04b8b6
commit 8b694852ac
5 changed files with 100 additions and 30 deletions

View File

@@ -15,9 +15,9 @@ public class TileManager {
public TileManager(GamePanel panel) {
this.panel = panel;
tile = new Tile[10];
mapTileNum = new int[panel.maxScreenCol][panel.maxScreenRow];
mapTileNum = new int[panel.maxWorldCol][panel.maxWorldRow];
getTileImage();
loadMap("testmap");
loadMap("testworld");
}
public void getTileImage() {
@@ -30,6 +30,15 @@ public class TileManager {
tile[2] = new Tile();
tile[2].image = ImageIO.read(new FileInputStream("assets/tiles/water.png"));
tile[3] = new Tile();
tile[3].image = ImageIO.read(new FileInputStream("assets/tiles/earth.png"));
tile[4] = new Tile();
tile[4].image = ImageIO.read(new FileInputStream("assets/tiles/tree.png"));
tile[5] = new Tile();
tile[5].image = ImageIO.read(new FileInputStream("assets/tiles/sand.png"));
} catch (IOException e) {
e.printStackTrace();
}
@@ -41,15 +50,15 @@ public class TileManager {
BufferedReader bReader = new BufferedReader(new InputStreamReader(stream));
int col = 0;
int row = 0;
while(col < panel.maxScreenCol && row < panel.maxScreenRow) {
while(col < panel.maxWorldCol && row < panel.maxWorldRow) {
String line = bReader.readLine();
while(col < panel.maxScreenCol) {
while(col < panel.maxWorldCol) {
String[] numbers = line.split(" ");
int num = Integer.parseInt(numbers[col]);
mapTileNum[col][row] = num;
col++;
}
if(col == panel.maxScreenCol) {
if(col == panel.maxWorldCol) {
col = 0;
row++;
}
@@ -61,21 +70,21 @@ public class TileManager {
}
public void draw(Graphics2D graphics2D) {
int col = 0;
int row = 0;
int x = 0;
int y = 0;
int worldCol = 0;
int worldRow = 0;
while(col < panel.maxScreenCol && row < panel.maxScreenRow) {
int tileNum = mapTileNum[col][row];
graphics2D.drawImage(tile[tileNum].image, x, y, panel.tileSize, panel.tileSize, null);
col++;
x += panel.tileSize;
if(col == panel.maxScreenCol) {
col = 0;
x = 0;
row++;
y += panel.tileSize;
while(worldCol < panel.maxWorldCol && worldRow < panel.maxWorldRow) {
int tileNum = mapTileNum[worldCol][worldRow];
int worldX = worldCol * panel.tileSize;
int worldY = worldRow * panel.tileSize;
int screenX = worldX - panel.player.worldX + panel.player.screenX;
int screenY = worldY - panel.player.worldY + panel.player.screenY;
graphics2D.drawImage(tile[tileNum].image, screenX, screenY, panel.tileSize, panel.tileSize, null);
worldCol++;
if(worldCol == panel.maxWorldCol) {
worldCol = 0;
worldRow++;
}
}
}