Compare commits

...

5 Commits

Author SHA1 Message Date
2d69c01469 added collision 2025-11-26 22:51:06 +01:00
00f59ec896 improved camera rendering (only render tiles the player can actually see) 2025-11-26 22:10:53 +01:00
8b694852ac implemented camera 2025-11-26 22:05:58 +01:00
e60f04b8b6 added map drawing system 2025-11-26 21:50:46 +01:00
9e6834b304 added map tiles 2025-11-26 21:34:49 +01:00
14 changed files with 289 additions and 44 deletions

12
assets/maps/testmap.map Normal file
View File

@@ -0,0 +1,12 @@
1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 2 2 2 2 0 0 0 0 0 1
1 0 0 0 0 0 2 2 2 2 0 0 0 0 0 1
1 0 0 0 0 0 2 2 2 2 0 0 0 0 0 1
1 0 0 0 0 0 2 2 2 2 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

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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 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 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 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

BIN
assets/tiles/earth.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 614 B

BIN
assets/tiles/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 616 B

BIN
assets/tiles/sand.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
assets/tiles/tree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

BIN
assets/tiles/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

BIN
assets/tiles/water.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 619 B

View File

@@ -0,0 +1,54 @@
package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.entity.Entity;
public class CollisionHandler {
GamePanel panel;
public CollisionHandler(GamePanel panel) {
this.panel = panel;
}
public void checkTile(Entity entity) {
int entityLeftWorldX = entity.worldX + entity.solidArea.x;
int entityRightWorldX = entity.worldX + entity.solidArea.x + entity.solidArea.width;
int entityTopWorldY = entity.worldY + entity.solidArea.y;
int entityBottomWorldY = entity.worldY + entity.solidArea.y + entity.solidArea.height;
int entityLeftCol = entityLeftWorldX/panel.tileSize;
int entityRightCol = entityRightWorldX/panel.tileSize;
int entityTopRow = entityTopWorldY/panel.tileSize;
int entityBottomRow = entityBottomWorldY/panel.tileSize;
int tileNum1, tileNum2;
switch(entity.direction) {
case UP:
entityTopRow = (entityTopWorldY - entity.speed)/panel.tileSize;
tileNum1 = panel.tileM.mapTileNum[entityLeftCol][entityTopRow];
tileNum2 = panel.tileM.mapTileNum[entityRightCol][entityTopRow];
if(panel.tileM.tile[tileNum1].collision || panel.tileM.tile[tileNum2].collision) entity.collisionOn = true;
break;
case DOWN:
entityBottomRow = (entityBottomWorldY + entity.speed)/panel.tileSize;
tileNum1 = panel.tileM.mapTileNum[entityLeftCol][entityBottomRow];
tileNum2 = panel.tileM.mapTileNum[entityRightCol][entityBottomRow];
if(panel.tileM.tile[tileNum1].collision || panel.tileM.tile[tileNum2].collision) entity.collisionOn = true;
break;
case LEFT:
entityLeftCol = (entityLeftWorldX - entity.speed)/panel.tileSize;
tileNum1 = panel.tileM.mapTileNum[entityLeftCol][entityTopRow];
tileNum2 = panel.tileM.mapTileNum[entityLeftCol][entityBottomRow];
if(panel.tileM.tile[tileNum1].collision || panel.tileM.tile[tileNum2].collision) entity.collisionOn = true;
break;
case RIGHT:
entityRightCol = (entityRightWorldX + entity.speed)/panel.tileSize;
tileNum1 = panel.tileM.mapTileNum[entityRightCol][entityTopRow];
tileNum2 = panel.tileM.mapTileNum[entityRightCol][entityBottomRow];
if(panel.tileM.tile[tileNum1].collision || panel.tileM.tile[tileNum2].collision) entity.collisionOn = true;
break;
}
}
}

View File

@@ -1,6 +1,7 @@
package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.entity.Player;
import de.miaurizius.jgame2d.tile.TileManager;
import javax.swing.*;
import java.awt.*;
@@ -13,22 +14,25 @@ public class GamePanel extends JPanel implements Runnable {
public final int tileSize = originalTileSize * scale; //48x48 tile
// 4:3 ratio
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol; // 768 pixels
final int screenHeight = tileSize * maxScreenRow; // 576 pixels
public final int maxScreenCol = 16;
public final int maxScreenRow = 12;
public final int screenWidth = tileSize * maxScreenCol; // 768 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
int FPS = 60;
TileManager tileM = new TileManager(this);
KeyHandler keyH = new KeyHandler();
Thread gameThread;
Player player = new Player(this, keyH);
//Default position
int playerX = 100;
int playerY = 100;
int playerSpeed = 4;
public CollisionHandler collisionH = new CollisionHandler(this);
public Player player = new Player(this, keyH);
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
@@ -45,7 +49,7 @@ public class GamePanel extends JPanel implements Runnable {
@Override
public void run() {
double drawInterval = 1000000000 / FPS;
double drawInterval = (double) 1000000000 / FPS;
double delta = 0;
double lastTime = System.nanoTime();
long currentTime;
@@ -55,7 +59,7 @@ public class GamePanel extends JPanel implements Runnable {
while(gameThread != null) {
currentTime = System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
timer += currentTime - lastTime;
timer += (long) (currentTime - lastTime);
lastTime = currentTime;
if(delta >= 1) {
@@ -65,7 +69,7 @@ public class GamePanel extends JPanel implements Runnable {
drawCount++;
}
if(timer >= 1000000000) {
System.out.println("FPS: " + drawCount);
//System.out.println("FPS: " + drawCount);
drawCount = 0;
timer = 0;
}
@@ -79,6 +83,9 @@ public class GamePanel extends JPanel implements Runnable {
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2d = (Graphics2D) graphics;
//Draw all components
tileM.draw(graphics2d);
player.draw(graphics2d);
graphics.dispose();
}

View File

@@ -2,11 +2,12 @@ package de.miaurizius.jgame2d.entity;
import de.miaurizius.jgame2d.core.Direction;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Entity {
public int x, y;
public int worldX, worldY;
public int speed;
public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2;
@@ -15,4 +16,7 @@ public class Entity {
public int spriteCounter = 0;
public int spriteNum = 1;
public Rectangle solidArea;
public boolean collisionOn = false;
}

View File

@@ -14,16 +14,29 @@ public class Player extends Entity {
GamePanel panel;
KeyHandler keyH;
public final int screenX;
public final int screenY;
public Player(GamePanel panel, KeyHandler keyH) {
this.panel = panel;
this.keyH = keyH;
screenX = panel.screenWidth/2 - panel.tileSize/2;
screenY = panel.screenHeight/2 - panel.tileSize/2;
solidArea = new Rectangle();
solidArea.x = 9;
solidArea.y = 18;
solidArea.width = 32;
solidArea.height = 32;
setDefaultValues();
getPlayerImage();
}
public void setDefaultValues() {
x = 100;
y = 100;
worldX = panel.tileSize * 23;
worldY = panel.tileSize * 21;
speed = 4;
direction = Direction.DOWN;
}
@@ -46,18 +59,22 @@ public class Player extends Entity {
public void update() {
//Move player
if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) {
if(keyH.upPressed) {
direction = Direction.UP;
y -= speed;
} else if(keyH.downPressed) {
direction = Direction.DOWN;
y += speed;
} else if(keyH.leftPressed) {
direction = Direction.LEFT;
x -= speed;
} else if(keyH.rightPressed) {
direction = Direction.RIGHT;
x += speed;
if(keyH.upPressed) direction = Direction.UP;
else if(keyH.downPressed) direction = Direction.DOWN;
else if(keyH.leftPressed) direction = Direction.LEFT;
else if(keyH.rightPressed) direction = Direction.RIGHT;
// CHECK TILE COLLISION
collisionOn = false;
panel.collisionH.checkTile(this);
if(!collisionOn) {
switch (direction) {
case UP -> worldY -= speed;
case DOWN -> worldY += speed;
case LEFT ->worldX -= speed;
case RIGHT -> worldX += speed;
}
}
spriteCounter++;
@@ -70,22 +87,13 @@ public class Player extends Entity {
}
public void draw(Graphics2D graphics2d) {
BufferedImage image = null;
switch (direction) {
case UP:
image = (spriteNum == 1) ? up1 : up2;
break;
case DOWN:
image = (spriteNum == 1) ? down1 : down2;
break;
case LEFT:
image = (spriteNum == 1) ? left1 : left2;
break;
case RIGHT:
image = (spriteNum == 1) ? right1 : right2;
break;
}
graphics2d.drawImage(image, x, y, panel.tileSize, panel.tileSize, null);
BufferedImage image = switch (direction) {
case UP -> (spriteNum == 1) ? up1 : up2;
case DOWN -> (spriteNum == 1) ? down1 : down2;
case LEFT -> (spriteNum == 1) ? left1 : left2;
case RIGHT -> (spriteNum == 1) ? right1 : right2;
};
graphics2d.drawImage(image, screenX, screenY, panel.tileSize, panel.tileSize, null);
}
}

View File

@@ -0,0 +1,10 @@
package de.miaurizius.jgame2d.tile;
import java.awt.image.BufferedImage;
public class Tile {
public BufferedImage image;
public boolean collision = false;
}

View File

@@ -0,0 +1,100 @@
package de.miaurizius.jgame2d.tile;
import de.miaurizius.jgame2d.core.GamePanel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.*;
public class TileManager {
GamePanel panel;
public Tile[] tile;
public int[][] mapTileNum;
public TileManager(GamePanel panel) {
this.panel = panel;
tile = new Tile[10];
mapTileNum = new int[panel.maxWorldCol][panel.maxWorldRow];
getTileImage();
loadMap("testworld");
}
public void getTileImage() {
try {
tile[0] = new Tile();
tile[0].image = ImageIO.read(new FileInputStream("assets/tiles/grass.png"));
tile[1] = new Tile();
tile[1].image = ImageIO.read(new FileInputStream("assets/tiles/wall.png"));
tile[1].collision = true;
tile[2] = new Tile();
tile[2].image = ImageIO.read(new FileInputStream("assets/tiles/water.png"));
tile[2].collision = true;
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[4].collision = true;
tile[5] = new Tile();
tile[5].image = ImageIO.read(new FileInputStream("assets/tiles/sand.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadMap(String map) {
try {
InputStream stream = new FileInputStream("assets/maps/"+map+".map");
BufferedReader bReader = new BufferedReader(new InputStreamReader(stream));
int col = 0;
int row = 0;
while(col < panel.maxWorldCol && row < panel.maxWorldRow) {
String line = bReader.readLine();
while(col < panel.maxWorldCol) {
String[] numbers = line.split(" ");
int num = Integer.parseInt(numbers[col]);
mapTileNum[col][row] = num;
col++;
}
if(col == panel.maxWorldCol) {
col = 0;
row++;
}
}
bReader.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public void draw(Graphics2D graphics2D) {
int worldCol = 0;
int worldRow = 0;
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;
if(worldX + panel.tileSize > panel.player.worldX - panel.player.screenX &&
worldX - panel.tileSize < panel.player.worldX + panel.player.screenX &&
worldY + panel.tileSize > panel.player.worldY - panel.player.screenY &&
worldY - panel.tileSize < 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++;
}
}
}
}