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

50
assets/maps/testworld.map Normal file
View File

@@ -0,0 +1,50 @@
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 0 2 2 2 2 2 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 1 1 1 1 1 4 4 4 4 4 4 4 4 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 1 3 3 3 1 4 4 4 4 4 4 4 0 0 0 5 0 0 0 4 4 4 4 4 4 0 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 1 3 3 3 1 4 4 4 4 4 4 4 4 0 0 0 0 0 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 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 +16,14 @@ public class GamePanel extends JPanel implements Runnable {
// 4:3 ratio // 4:3 ratio
public final int maxScreenCol = 16; public final int maxScreenCol = 16;
public final int maxScreenRow = 12; public final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol; // 768 pixels public final int screenWidth = tileSize * maxScreenCol; // 768 pixels
final int screenHeight = tileSize * maxScreenRow; // 576 pixels public final int screenHeight = tileSize * maxScreenRow; // 576 pixels
// WORLD SETTINGS
public final int maxWorldCol = 50;
public final int maxWorldRow = 50;
public final int worldWidth = tileSize * maxWorldCol;
public final int worldHeight = tileSize * maxWorldRow;
//FPS //FPS
int FPS = 60; int FPS = 60;
@@ -25,7 +31,7 @@ public class GamePanel extends JPanel implements Runnable {
TileManager tileM = new TileManager(this); TileManager tileM = new TileManager(this);
KeyHandler keyH = new KeyHandler(); KeyHandler keyH = new KeyHandler();
Thread gameThread; Thread gameThread;
Player player = new Player(this, keyH); public Player player = new Player(this, keyH);
public GamePanel() { public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight)); this.setPreferredSize(new Dimension(screenWidth, screenHeight));

View File

@@ -6,7 +6,7 @@ import java.awt.image.BufferedImage;
public class Entity { public class Entity {
public int x, y; public int worldX, worldY;
public int speed; public int speed;
public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2; public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2;

View File

@@ -14,16 +14,21 @@ public class Player extends Entity {
GamePanel panel; GamePanel panel;
KeyHandler keyH; KeyHandler keyH;
public final int screenX;
public final int screenY;
public Player(GamePanel panel, KeyHandler keyH) { public Player(GamePanel panel, KeyHandler keyH) {
this.panel = panel; this.panel = panel;
this.keyH = keyH; this.keyH = keyH;
screenX = panel.screenWidth/2 - panel.tileSize/2;
screenY = panel.screenHeight/2 - panel.tileSize/2;
setDefaultValues(); setDefaultValues();
getPlayerImage(); getPlayerImage();
} }
public void setDefaultValues() { public void setDefaultValues() {
x = 100; worldX = panel.tileSize * 23;
y = 100; worldY = panel.tileSize * 21;
speed = 4; speed = 4;
direction = Direction.DOWN; direction = Direction.DOWN;
} }
@@ -48,16 +53,16 @@ public class Player extends Entity {
if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) { if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) {
if(keyH.upPressed) { if(keyH.upPressed) {
direction = Direction.UP; direction = Direction.UP;
y -= speed; worldY -= speed;
} else if(keyH.downPressed) { } else if(keyH.downPressed) {
direction = Direction.DOWN; direction = Direction.DOWN;
y += speed; worldY += speed;
} else if(keyH.leftPressed) { } else if(keyH.leftPressed) {
direction = Direction.LEFT; direction = Direction.LEFT;
x -= speed; worldX -= speed;
} else if(keyH.rightPressed) { } else if(keyH.rightPressed) {
direction = Direction.RIGHT; direction = Direction.RIGHT;
x += speed; worldX += speed;
} }
spriteCounter++; spriteCounter++;
@@ -85,7 +90,7 @@ public class Player extends Entity {
image = (spriteNum == 1) ? right1 : right2; image = (spriteNum == 1) ? right1 : right2;
break; break;
} }
graphics2d.drawImage(image, x, y, panel.tileSize, panel.tileSize, null); graphics2d.drawImage(image, screenX, screenY, panel.tileSize, panel.tileSize, null);
} }
} }

View File

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