changed tile array to three-dimensional array (i0=map; i1=col; i2=row)

This commit is contained in:
2025-12-12 12:52:50 +01:00
parent 5dd43b6f9d
commit 67309a6e28
9 changed files with 161 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.core.enums.Map;
import de.miaurizius.jgame2d.core.handlers.*;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.entity.Player;
@@ -37,7 +38,7 @@ public class GamePanel extends JPanel implements Runnable {
// WORLD SETTINGS
public final int maxWorldCol = 50;
public final int maxWorldRow = 50;
public final String currentMap = "testmap2";
public final Map currentMap = Map.OVERWORLD;
//FPS
final int FPS = 60;

View File

@@ -0,0 +1,23 @@
package de.miaurizius.jgame2d.core.enums;
public enum Map {
OVERWORLD("world3", 0),
HUT("hut", 1);
private final String name;
private final int index;
Map(String name, int index) {
this.name = name;
this.index = index;
}
public String getName() {
return name;
}
public int getIndex() {
return index;
}
}

View File

@@ -84,6 +84,18 @@ public class AssetSetter {
panel.iTile[i] = new DryTreeTI(panel,31,12);i++;
panel.iTile[i] = new DryTreeTI(panel,32,12);i++;
panel.iTile[i] = new DryTreeTI(panel,33,12);i++;
panel.iTile[i] = new DryTreeTI(panel,18,40);i++;
panel.iTile[i] = new DryTreeTI(panel,17,40);i++;
panel.iTile[i] = new DryTreeTI(panel,16,40);i++;
panel.iTile[i] = new DryTreeTI(panel,15,40);i++;
panel.iTile[i] = new DryTreeTI(panel,14,40);i++;
panel.iTile[i] = new DryTreeTI(panel,13,40);i++;
panel.iTile[i] = new DryTreeTI(panel,13,41);i++;
panel.iTile[i] = new DryTreeTI(panel,12,41);i++;
panel.iTile[i] = new DryTreeTI(panel,11,41);i++;
panel.iTile[i] = new DryTreeTI(panel,10,41);i++;
panel.iTile[i] = new DryTreeTI(panel,10,40);i++;
}
}

View File

@@ -28,26 +28,26 @@ public class CollisionHandler {
switch(entity.direction) {
case UP:
entityTopRow = (entityTopWorldY - entity.speed)/panel.tileSize;
tileNum1 = panel.tileM.mapTileNum[entityLeftCol][entityTopRow];
tileNum2 = panel.tileM.mapTileNum[entityRightCol][entityTopRow];
tileNum1 = panel.tileM.mapTileNum[panel.currentMap.getIndex()][entityLeftCol][entityTopRow];
tileNum2 = panel.tileM.mapTileNum[panel.currentMap.getIndex()][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];
tileNum1 = panel.tileM.mapTileNum[panel.currentMap.getIndex()][entityLeftCol][entityBottomRow];
tileNum2 = panel.tileM.mapTileNum[panel.currentMap.getIndex()][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];
tileNum1 = panel.tileM.mapTileNum[panel.currentMap.getIndex()][entityLeftCol][entityTopRow];
tileNum2 = panel.tileM.mapTileNum[panel.currentMap.getIndex()][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];
tileNum1 = panel.tileM.mapTileNum[panel.currentMap.getIndex()][entityRightCol][entityTopRow];
tileNum2 = panel.tileM.mapTileNum[panel.currentMap.getIndex()][entityRightCol][entityBottomRow];
if(panel.tileM.tile[tileNum1].collision || panel.tileM.tile[tileNum2].collision) entity.collisionOn = true;
break;
}

View File

@@ -172,6 +172,7 @@ public class KeyHandler implements KeyListener {
case KeyEvent.VK_SPACE:
if(panel.ui.commandNum == 0) {
panel.gameState = GameState.PLAY;
panel.playMusic(0);
panel.retry();
}
if(panel.ui.commandNum == 1) {

View File

@@ -128,6 +128,8 @@ public class Player extends Entity {
if(life <= 0) {
panel.gameState = GameState.GAMEOVER;
panel.ui.commandNum = -1;
panel.stopMusic();
panel.playSE(12);
}
}

View File

@@ -3,6 +3,7 @@ package de.miaurizius.jgame2d.tile;
import de.miaurizius.jgame2d.core.Boot;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.Utility;
import de.miaurizius.jgame2d.core.enums.Map;
import javax.imageio.ImageIO;
import java.awt.*;
@@ -13,14 +14,14 @@ public class TileManager {
GamePanel panel;
public Tile[] tile;
public int[][] mapTileNum;
public int[][][] mapTileNum;
public TileManager(GamePanel panel) {
this.panel = panel;
tile = new Tile[50];
mapTileNum = new int[panel.maxWorldCol][panel.maxWorldRow];
mapTileNum = new int[Map.values().length][panel.maxWorldCol][panel.maxWorldRow];
getTileImage();
loadMap(panel.currentMap);
for(Map m : Map.values()) loadMap(m);
}
public void initializeTile(int i, String name, boolean col) {
@@ -85,15 +86,20 @@ public class TileManager {
initializeTile(40, "wall", true);
initializeTile(41, "tree", true);
//
initializeTile(42, "hut", false);
initializeTile(43, "floor01", false);
initializeTile(44, "table01", true);
for (Tile tile : tile) {
if(tile == null) continue;
tile.image = Utility.scaleImage(tile.image, panel.tileSize, panel.tileSize);
}
}
public void loadMap(String map) {
public void loadMap(Map map) {
try {
InputStream stream = new FileInputStream("assets/maps/"+map+".map");
InputStream stream = new FileInputStream("assets/maps/"+map.getName()+".map");
BufferedReader bReader = new BufferedReader(new InputStreamReader(stream));
int col = 0;
int row = 0;
@@ -102,7 +108,7 @@ public class TileManager {
while(col < panel.maxWorldCol) {
String[] numbers = line.split(" ");
int num = Integer.parseInt(numbers[col]);
mapTileNum[col][row] = num;
mapTileNum[map.getIndex()][col][row] = num;
col++;
}
if(col == panel.maxWorldCol) {
@@ -121,7 +127,7 @@ public class TileManager {
int worldRow = 0;
while(worldCol < panel.maxWorldCol && worldRow < panel.maxWorldRow) {
int tileNum = mapTileNum[worldCol][worldRow];
int tileNum = mapTileNum[panel.currentMap.getIndex()][worldCol][worldRow];
int worldX = worldCol * panel.tileSize;
int worldY = worldRow * panel.tileSize;