Compare commits
5 Commits
faeea23422
...
2d69c01469
| Author | SHA1 | Date | |
|---|---|---|---|
|
2d69c01469
|
|||
|
00f59ec896
|
|||
|
8b694852ac
|
|||
|
e60f04b8b6
|
|||
|
9e6834b304
|
12
assets/maps/testmap.map
Normal file
12
assets/maps/testmap.map
Normal 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
50
assets/maps/testworld.map
Normal 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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BIN
assets/tiles/earth.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 614 B |
BIN
assets/tiles/grass.png
Normal file
BIN
assets/tiles/grass.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 616 B |
BIN
assets/tiles/sand.png
Normal file
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
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
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
BIN
assets/tiles/water.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 619 B |
54
src/de/miaurizius/jgame2d/core/CollisionHandler.java
Normal file
54
src/de/miaurizius/jgame2d/core/CollisionHandler.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.miaurizius.jgame2d.core;
|
package de.miaurizius.jgame2d.core;
|
||||||
|
|
||||||
import de.miaurizius.jgame2d.entity.Player;
|
import de.miaurizius.jgame2d.entity.Player;
|
||||||
|
import de.miaurizius.jgame2d.tile.TileManager;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@@ -13,22 +14,25 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
|
|
||||||
public final int tileSize = originalTileSize * scale; //48x48 tile
|
public final int tileSize = originalTileSize * scale; //48x48 tile
|
||||||
// 4:3 ratio
|
// 4:3 ratio
|
||||||
final int maxScreenCol = 16;
|
public final int maxScreenCol = 16;
|
||||||
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;
|
||||||
|
|
||||||
|
TileManager tileM = new TileManager(this);
|
||||||
KeyHandler keyH = new KeyHandler();
|
KeyHandler keyH = new KeyHandler();
|
||||||
Thread gameThread;
|
Thread gameThread;
|
||||||
Player player = new Player(this, keyH);
|
public CollisionHandler collisionH = new CollisionHandler(this);
|
||||||
|
public Player player = new Player(this, keyH);
|
||||||
//Default position
|
|
||||||
int playerX = 100;
|
|
||||||
int playerY = 100;
|
|
||||||
int playerSpeed = 4;
|
|
||||||
|
|
||||||
public GamePanel() {
|
public GamePanel() {
|
||||||
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
|
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
|
||||||
@@ -45,7 +49,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
double drawInterval = 1000000000 / FPS;
|
double drawInterval = (double) 1000000000 / FPS;
|
||||||
double delta = 0;
|
double delta = 0;
|
||||||
double lastTime = System.nanoTime();
|
double lastTime = System.nanoTime();
|
||||||
long currentTime;
|
long currentTime;
|
||||||
@@ -55,7 +59,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
while(gameThread != null) {
|
while(gameThread != null) {
|
||||||
currentTime = System.nanoTime();
|
currentTime = System.nanoTime();
|
||||||
delta += (currentTime - lastTime) / drawInterval;
|
delta += (currentTime - lastTime) / drawInterval;
|
||||||
timer += currentTime - lastTime;
|
timer += (long) (currentTime - lastTime);
|
||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
|
|
||||||
if(delta >= 1) {
|
if(delta >= 1) {
|
||||||
@@ -65,7 +69,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
drawCount++;
|
drawCount++;
|
||||||
}
|
}
|
||||||
if(timer >= 1000000000) {
|
if(timer >= 1000000000) {
|
||||||
System.out.println("FPS: " + drawCount);
|
//System.out.println("FPS: " + drawCount);
|
||||||
drawCount = 0;
|
drawCount = 0;
|
||||||
timer = 0;
|
timer = 0;
|
||||||
}
|
}
|
||||||
@@ -79,6 +83,9 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
public void paintComponent(Graphics graphics) {
|
public void paintComponent(Graphics graphics) {
|
||||||
super.paintComponent(graphics);
|
super.paintComponent(graphics);
|
||||||
Graphics2D graphics2d = (Graphics2D) graphics;
|
Graphics2D graphics2d = (Graphics2D) graphics;
|
||||||
|
|
||||||
|
//Draw all components
|
||||||
|
tileM.draw(graphics2d);
|
||||||
player.draw(graphics2d);
|
player.draw(graphics2d);
|
||||||
graphics.dispose();
|
graphics.dispose();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ package de.miaurizius.jgame2d.entity;
|
|||||||
|
|
||||||
import de.miaurizius.jgame2d.core.Direction;
|
import de.miaurizius.jgame2d.core.Direction;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
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;
|
||||||
@@ -15,4 +16,7 @@ public class Entity {
|
|||||||
public int spriteCounter = 0;
|
public int spriteCounter = 0;
|
||||||
public int spriteNum = 1;
|
public int spriteNum = 1;
|
||||||
|
|
||||||
|
public Rectangle solidArea;
|
||||||
|
public boolean collisionOn = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,16 +14,29 @@ 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;
|
||||||
|
|
||||||
|
solidArea = new Rectangle();
|
||||||
|
solidArea.x = 9;
|
||||||
|
solidArea.y = 18;
|
||||||
|
solidArea.width = 32;
|
||||||
|
solidArea.height = 32;
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -46,18 +59,22 @@ public class Player extends Entity {
|
|||||||
public void update() {
|
public void update() {
|
||||||
//Move player
|
//Move player
|
||||||
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;
|
else if(keyH.downPressed) direction = Direction.DOWN;
|
||||||
y -= speed;
|
else if(keyH.leftPressed) direction = Direction.LEFT;
|
||||||
} else if(keyH.downPressed) {
|
else if(keyH.rightPressed) direction = Direction.RIGHT;
|
||||||
direction = Direction.DOWN;
|
|
||||||
y += speed;
|
// CHECK TILE COLLISION
|
||||||
} else if(keyH.leftPressed) {
|
collisionOn = false;
|
||||||
direction = Direction.LEFT;
|
panel.collisionH.checkTile(this);
|
||||||
x -= speed;
|
|
||||||
} else if(keyH.rightPressed) {
|
if(!collisionOn) {
|
||||||
direction = Direction.RIGHT;
|
switch (direction) {
|
||||||
x += speed;
|
case UP -> worldY -= speed;
|
||||||
|
case DOWN -> worldY += speed;
|
||||||
|
case LEFT ->worldX -= speed;
|
||||||
|
case RIGHT -> worldX += speed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
spriteCounter++;
|
spriteCounter++;
|
||||||
@@ -70,22 +87,13 @@ public class Player extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics2D graphics2d) {
|
public void draw(Graphics2D graphics2d) {
|
||||||
BufferedImage image = null;
|
BufferedImage image = switch (direction) {
|
||||||
switch (direction) {
|
case UP -> (spriteNum == 1) ? up1 : up2;
|
||||||
case UP:
|
case DOWN -> (spriteNum == 1) ? down1 : down2;
|
||||||
image = (spriteNum == 1) ? up1 : up2;
|
case LEFT -> (spriteNum == 1) ? left1 : left2;
|
||||||
break;
|
case RIGHT -> (spriteNum == 1) ? right1 : right2;
|
||||||
case DOWN:
|
};
|
||||||
image = (spriteNum == 1) ? down1 : down2;
|
graphics2d.drawImage(image, screenX, screenY, panel.tileSize, panel.tileSize, null);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/de/miaurizius/jgame2d/tile/Tile.java
Normal file
10
src/de/miaurizius/jgame2d/tile/Tile.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
100
src/de/miaurizius/jgame2d/tile/TileManager.java
Normal file
100
src/de/miaurizius/jgame2d/tile/TileManager.java
Normal 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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user