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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
|
||||
@@ -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));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user