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4 4 4 4 4 4 4 4 4 4 \ No newline at end of file diff --git a/src/de/miaurizius/jgame2d/core/GamePanel.java b/src/de/miaurizius/jgame2d/core/GamePanel.java index b72d3c4..7647abe 100644 --- a/src/de/miaurizius/jgame2d/core/GamePanel.java +++ b/src/de/miaurizius/jgame2d/core/GamePanel.java @@ -16,8 +16,14 @@ public class GamePanel extends JPanel implements Runnable { // 4:3 ratio public final int maxScreenCol = 16; public final int maxScreenRow = 12; - final int screenWidth = tileSize * maxScreenCol; // 768 pixels - final int screenHeight = tileSize * maxScreenRow; // 576 pixels + 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; @@ -25,7 +31,7 @@ public class GamePanel extends JPanel implements Runnable { TileManager tileM = new TileManager(this); KeyHandler keyH = new KeyHandler(); Thread gameThread; - Player player = new Player(this, keyH); + public Player player = new Player(this, keyH); public GamePanel() { this.setPreferredSize(new Dimension(screenWidth, screenHeight)); diff --git a/src/de/miaurizius/jgame2d/entity/Entity.java b/src/de/miaurizius/jgame2d/entity/Entity.java index f70c876..51142ba 100644 --- a/src/de/miaurizius/jgame2d/entity/Entity.java +++ b/src/de/miaurizius/jgame2d/entity/Entity.java @@ -6,7 +6,7 @@ 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; diff --git a/src/de/miaurizius/jgame2d/entity/Player.java b/src/de/miaurizius/jgame2d/entity/Player.java index 58a5b4b..ac32b93 100644 --- a/src/de/miaurizius/jgame2d/entity/Player.java +++ b/src/de/miaurizius/jgame2d/entity/Player.java @@ -14,16 +14,21 @@ 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; setDefaultValues(); getPlayerImage(); } public void setDefaultValues() { - x = 100; - y = 100; + worldX = panel.tileSize * 23; + worldY = panel.tileSize * 21; speed = 4; direction = Direction.DOWN; } @@ -48,16 +53,16 @@ public class Player extends Entity { if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) { if(keyH.upPressed) { direction = Direction.UP; - y -= speed; + worldY -= speed; } else if(keyH.downPressed) { direction = Direction.DOWN; - y += speed; + worldY += speed; } else if(keyH.leftPressed) { direction = Direction.LEFT; - x -= speed; + worldX -= speed; } else if(keyH.rightPressed) { direction = Direction.RIGHT; - x += speed; + worldX += speed; } spriteCounter++; @@ -85,7 +90,7 @@ public class Player extends Entity { image = (spriteNum == 1) ? right1 : right2; break; } - graphics2d.drawImage(image, x, y, panel.tileSize, panel.tileSize, null); + graphics2d.drawImage(image, screenX, screenY, panel.tileSize, panel.tileSize, null); } } diff --git a/src/de/miaurizius/jgame2d/tile/TileManager.java b/src/de/miaurizius/jgame2d/tile/TileManager.java index be9d455..1d15917 100644 --- a/src/de/miaurizius/jgame2d/tile/TileManager.java +++ b/src/de/miaurizius/jgame2d/tile/TileManager.java @@ -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++; } } }