Compare commits
2 Commits
30ab25f5cb
...
f317d28c9c
| Author | SHA1 | Date | |
|---|---|---|---|
| f317d28c9c | |||
| aa21a21fcc |
@ -18,7 +18,6 @@ public abstract class Entity {
|
||||
public int spriteNum = 1;
|
||||
|
||||
public Rectangle solidArea;
|
||||
public boolean collisionOn = false;
|
||||
|
||||
public abstract int boxLeft();
|
||||
public abstract int boxRight();
|
||||
@ -33,7 +32,7 @@ public abstract class Entity {
|
||||
protected final EnumSet<Direction> easts = EnumSet.of(Direction.E, Direction.NE, Direction.SE);
|
||||
protected final EnumSet<Direction> wests = EnumSet.of(Direction.W, Direction.NW, Direction.SW);
|
||||
|
||||
protected static final Map<Point, Direction> directionMap = Map.ofEntries(
|
||||
protected static final Map<Point, Direction> DIRECTION_MAP = Map.ofEntries(
|
||||
Map.entry(new Point(0, -1), Direction.N),
|
||||
Map.entry(new Point(1, -1), Direction.NE),
|
||||
Map.entry(new Point(1, 0), Direction.E),
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package se.urmo.my2dgame.entity;
|
||||
|
||||
import se.urmo.my2dgame.ImageUtil;
|
||||
import se.urmo.my2dgame.main.CollisionChecker;
|
||||
import se.urmo.my2dgame.util.ImageUtil;
|
||||
import se.urmo.my2dgame.util.CollisionChecker;
|
||||
import se.urmo.my2dgame.main.KeyHandler;
|
||||
import se.urmo.my2dgame.main.Screen;
|
||||
import se.urmo.my2dgame.main.UpdateListener;
|
||||
@ -68,7 +68,7 @@ public class Player extends Entity implements UpdateListener {
|
||||
|
||||
// Lookup direction
|
||||
Point movement = new Point(dx, dy);
|
||||
direction = directionMap.getOrDefault(movement, direction);
|
||||
direction = DIRECTION_MAP.getOrDefault(movement, direction);
|
||||
|
||||
if (direction != null && collisionChecker.checkTile(this)) {
|
||||
if (norths.contains(direction)) {
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
package se.urmo.my2dgame.main;
|
||||
|
||||
import se.urmo.my2dgame.object.Key;
|
||||
|
||||
public class AssetSetter {
|
||||
Screen screen;
|
||||
public AssetSetter(Screen screen) {
|
||||
this.screen = screen;
|
||||
}
|
||||
|
||||
public void setObject() {
|
||||
screen.obj[0] = new Key(23 * screen.TILE_SIZE, 7 * screen.TILE_SIZE);
|
||||
screen.obj[1] = new Key(23 * screen.TILE_SIZE, 40 * screen.TILE_SIZE);
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,10 @@
|
||||
package se.urmo.my2dgame.main;
|
||||
|
||||
import se.urmo.my2dgame.entity.Player;
|
||||
import se.urmo.my2dgame.object.AssetSetter;
|
||||
import se.urmo.my2dgame.object.SuperObject;
|
||||
import se.urmo.my2dgame.tile.TileManager;
|
||||
import se.urmo.my2dgame.tile.World;
|
||||
import se.urmo.my2dgame.util.CollisionChecker;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
@ -19,14 +21,11 @@ public class Screen extends JPanel {
|
||||
public static final int SCREEN_WIDTH = MAX_SCREEN_COL * TILE_SIZE;
|
||||
public static final int SCREEN_HEIGHT = MAX_SCREEN_ROW * TILE_SIZE;
|
||||
|
||||
//WORLD
|
||||
public final int WORLD_MAX_COL = 50;
|
||||
public final int WORLD_MAX_ROW = 50;
|
||||
|
||||
TileManager tileManager = new TileManager(this);
|
||||
World world = new World(this);
|
||||
KeyHandler keyHandler = new KeyHandler();
|
||||
|
||||
public CollisionChecker collisionChecker = new CollisionChecker(tileManager);
|
||||
public CollisionChecker collisionChecker = new CollisionChecker(world);
|
||||
public AssetSetter assetSetter = new AssetSetter(this);
|
||||
public Player player = new Player(collisionChecker, keyHandler);
|
||||
public SuperObject[] obj = new SuperObject[10];
|
||||
@ -48,7 +47,7 @@ public class Screen extends JPanel {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
tileManager.draw(g2d);
|
||||
world.draw(g2d);
|
||||
Arrays.stream(obj)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(o -> o.draw(g2d, this));
|
||||
|
||||
15
src/main/java/se/urmo/my2dgame/object/AssetSetter.java
Normal file
15
src/main/java/se/urmo/my2dgame/object/AssetSetter.java
Normal file
@ -0,0 +1,15 @@
|
||||
package se.urmo.my2dgame.object;
|
||||
|
||||
import se.urmo.my2dgame.main.Screen;
|
||||
|
||||
public class AssetSetter {
|
||||
Screen screen;
|
||||
public AssetSetter(Screen screen) {
|
||||
this.screen = screen;
|
||||
}
|
||||
|
||||
public void setObject() {
|
||||
screen.obj[0] = new Key(23 * Screen.TILE_SIZE, 7 * Screen.TILE_SIZE);
|
||||
screen.obj[1] = new Key(23 * Screen.TILE_SIZE, 40 * Screen.TILE_SIZE);
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package se.urmo.my2dgame.object;
|
||||
|
||||
import se.urmo.my2dgame.entity.Player;
|
||||
import se.urmo.my2dgame.main.Screen;
|
||||
|
||||
import java.awt.*;
|
||||
@ -8,19 +9,15 @@ import java.awt.image.BufferedImage;
|
||||
public class SuperObject {
|
||||
public String name;
|
||||
public BufferedImage image;
|
||||
public boolean collision = false;
|
||||
public int worldX;
|
||||
public int worldY;
|
||||
|
||||
public void draw(Graphics2D g2d, Screen screen) {
|
||||
// Only drawImage for tiles within camera
|
||||
if (worldX + screen.TILE_SIZE > screen.player.worldX - screen.player.SCREEN_X &&
|
||||
worldX - screen.TILE_SIZE < screen.player.worldX + screen.player.SCREEN_X &&
|
||||
worldY + screen.TILE_SIZE > screen.player.worldY - screen.player.SCREEN_Y &&
|
||||
worldY - screen.TILE_SIZE < screen.player.worldY + screen.player.SCREEN_Y) {
|
||||
int screenX = worldX - screen.player.worldX + screen.player.SCREEN_X;
|
||||
int screenY = worldY - screen.player.worldY + screen.player.SCREEN_Y;
|
||||
g2d.drawImage(image, screenX, screenY, screen.TILE_SIZE, screen.TILE_SIZE, null);
|
||||
if(screen.isWithinScreen(worldX, worldY)) {
|
||||
int screenX = worldX - screen.player.worldX + Player.SCREEN_X;
|
||||
int screenY = worldY - screen.player.worldY + Player.SCREEN_Y;
|
||||
g2d.drawImage(image, screenX, screenY, Screen.TILE_SIZE, Screen.TILE_SIZE, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,21 @@ package se.urmo.my2dgame.tile;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Tile {
|
||||
public BufferedImage image;
|
||||
public boolean collission = false;
|
||||
private BufferedImage image;
|
||||
private boolean collission = false;
|
||||
|
||||
public Tile(BufferedImage image) {
|
||||
this(image, false);
|
||||
}
|
||||
public Tile(BufferedImage image, boolean collission) {
|
||||
this.image = image;
|
||||
this.collission = collission;
|
||||
}
|
||||
|
||||
public boolean isCollision() {
|
||||
return collission;
|
||||
}
|
||||
public BufferedImage getImage(){
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,98 +1,21 @@
|
||||
package se.urmo.my2dgame.tile;
|
||||
|
||||
import se.urmo.my2dgame.main.Screen;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
import se.urmo.my2dgame.util.ImageUtil;
|
||||
|
||||
public class TileManager {
|
||||
Screen screen;
|
||||
public Tile[] tiles = new Tile[10];
|
||||
public int[][] mapTileNum;
|
||||
|
||||
public TileManager(Screen screen) {
|
||||
this.screen = screen;
|
||||
getTileImage();
|
||||
this.mapTileNum = loadMap("/maps/world01.txt");
|
||||
private static final Tile[] tiles = new Tile[10];
|
||||
|
||||
static {
|
||||
tiles[0] = new Tile(ImageUtil.loadImage("/tiles/grass.png"));
|
||||
tiles[1] = new Tile(ImageUtil.loadImage("/tiles/wall.png"), true);
|
||||
tiles[2] = new Tile(ImageUtil.loadImage("/tiles/water.png"), true);
|
||||
tiles[3] = new Tile(ImageUtil.loadImage("/tiles/earth.png"), true);
|
||||
tiles[4] = new Tile(ImageUtil.loadImage("/tiles/tree.png"), true);
|
||||
tiles[5] = new Tile(ImageUtil.loadImage("/tiles/sand.png"));
|
||||
}
|
||||
|
||||
public int[][] loadMap(String path) {
|
||||
int[][] map = new int[screen.WORLD_MAX_ROW][screen.WORLD_MAX_COL];
|
||||
try {
|
||||
InputStream stream = Screen.class.getResourceAsStream(path);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
||||
int row = 0;
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null && row < screen.WORLD_MAX_ROW) {
|
||||
map[row++] = Arrays.stream(line.split(" "))
|
||||
.mapToInt(Integer::parseInt)
|
||||
.toArray();
|
||||
}
|
||||
reader.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public void getTileImage() {
|
||||
try {
|
||||
tiles[0] = new Tile();
|
||||
tiles[0].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/grass.png"));
|
||||
|
||||
tiles[1] = new Tile();
|
||||
tiles[1].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/wall.png"));
|
||||
tiles[1].collission = true;
|
||||
|
||||
tiles[2] = new Tile();
|
||||
tiles[2].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/water.png"));
|
||||
tiles[2].collission = true;
|
||||
|
||||
tiles[3] = new Tile();
|
||||
tiles[3].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/earth.png"));
|
||||
|
||||
tiles[4] = new Tile();
|
||||
tiles[4].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/tree.png"));
|
||||
tiles[4].collission = true;
|
||||
|
||||
tiles[5] = new Tile();
|
||||
tiles[5].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/sand.png"));
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
public static Tile[] getTiles() {
|
||||
return tiles;
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d) {
|
||||
int worldCol = 0;
|
||||
int worldRow = 0;
|
||||
|
||||
while (worldCol < screen.WORLD_MAX_COL && worldRow < screen.WORLD_MAX_ROW) {
|
||||
int tileNum = mapTileNum[worldRow][worldCol];
|
||||
|
||||
int worldX = worldCol * screen.TILE_SIZE;
|
||||
int worldY = worldRow * screen.TILE_SIZE;
|
||||
|
||||
// Only drawImage for tiles within camera
|
||||
if (screen.isWithinScreen(worldX, worldY)) {
|
||||
int screenX = worldX - screen.player.worldX + screen.player.SCREEN_X;
|
||||
int screenY = worldY - screen.player.worldY + screen.player.SCREEN_Y;
|
||||
g2d.drawImage(tiles[tileNum].image, screenX, screenY, screen.TILE_SIZE, screen.TILE_SIZE, null);
|
||||
}
|
||||
|
||||
worldCol++;
|
||||
|
||||
if (worldCol == screen.WORLD_MAX_COL) {
|
||||
worldCol = 0;
|
||||
worldRow++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
72
src/main/java/se/urmo/my2dgame/tile/World.java
Normal file
72
src/main/java/se/urmo/my2dgame/tile/World.java
Normal file
@ -0,0 +1,72 @@
|
||||
package se.urmo.my2dgame.tile;
|
||||
|
||||
import se.urmo.my2dgame.entity.Player;
|
||||
import se.urmo.my2dgame.main.Screen;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class World {
|
||||
public static final int WORLD_MAX_COL = 50;
|
||||
public static final int WORLD_MAX_ROW = 50;
|
||||
Screen screen;
|
||||
public static Tile[] tiles = TileManager.getTiles();
|
||||
public int[][] mapTileNum;
|
||||
|
||||
|
||||
public World(Screen screen) {
|
||||
this.screen = screen;
|
||||
this.mapTileNum = loadMap("/maps/world01.txt");
|
||||
}
|
||||
|
||||
private int[][] loadMap(String path) {
|
||||
int[][] map = new int[WORLD_MAX_ROW][WORLD_MAX_COL];
|
||||
try {
|
||||
InputStream stream = Screen.class.getResourceAsStream(path);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
||||
int row = 0;
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null && row < WORLD_MAX_ROW) {
|
||||
map[row++] = Arrays.stream(line.split(" "))
|
||||
.mapToInt(Integer::parseInt)
|
||||
.toArray();
|
||||
}
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d) {
|
||||
int worldCol = 0;
|
||||
int worldRow = 0;
|
||||
|
||||
while (worldCol < WORLD_MAX_COL && worldRow < WORLD_MAX_ROW) {
|
||||
int tileNum = mapTileNum[worldRow][worldCol];
|
||||
|
||||
int worldX = worldCol * Screen.TILE_SIZE;
|
||||
int worldY = worldRow * Screen.TILE_SIZE;
|
||||
|
||||
// Only drawImage for tiles within camera
|
||||
if (screen.isWithinScreen(worldX, worldY)) {
|
||||
int screenX = worldX - screen.player.worldX + Player.SCREEN_X;
|
||||
int screenY = worldY - screen.player.worldY + Player.SCREEN_Y;
|
||||
g2d.drawImage(tiles[tileNum].getImage(), screenX, screenY, Screen.TILE_SIZE, Screen.TILE_SIZE, null);
|
||||
}
|
||||
|
||||
worldCol++;
|
||||
|
||||
if (worldCol == WORLD_MAX_COL) {
|
||||
worldCol = 0;
|
||||
worldRow++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,14 +1,15 @@
|
||||
package se.urmo.my2dgame.main;
|
||||
package se.urmo.my2dgame.util;
|
||||
|
||||
import se.urmo.my2dgame.entity.Entity;
|
||||
import se.urmo.my2dgame.tile.TileManager;
|
||||
import se.urmo.my2dgame.main.Screen;
|
||||
import se.urmo.my2dgame.tile.World;
|
||||
|
||||
public class CollisionChecker {
|
||||
|
||||
private final TileManager tileManager;
|
||||
private final World world;
|
||||
|
||||
public CollisionChecker(TileManager tileManager) {
|
||||
this.tileManager = tileManager;
|
||||
public CollisionChecker(World world) {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
public boolean checkTile(Entity entity) {
|
||||
@ -67,9 +68,9 @@ public class CollisionChecker {
|
||||
}
|
||||
|
||||
private boolean isNonePassable(int row1, int col1, int row2, int col2) {
|
||||
int tileNum2 = tileManager.mapTileNum[row1][col1];
|
||||
int tileNum1 = tileManager.mapTileNum[row2][col2];
|
||||
int tileNum2 = world.mapTileNum[row1][col1];
|
||||
int tileNum1 = world.mapTileNum[row2][col2];
|
||||
|
||||
return tileManager.tiles[tileNum1].collission || tileManager.tiles[tileNum2].collission;
|
||||
return world.tiles[tileNum1].isCollision() || world.tiles[tileNum2].isCollision();
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package se.urmo.my2dgame;
|
||||
package se.urmo.my2dgame.util;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
Reference in New Issue
Block a user