77 lines
3.7 KiB
Java
77 lines
3.7 KiB
Java
package se.urmo.my2dgame.util;
|
|
|
|
import se.urmo.my2dgame.entity.Entity;
|
|
import se.urmo.my2dgame.main.Screen;
|
|
import se.urmo.my2dgame.tile.World;
|
|
|
|
public class CollisionChecker {
|
|
|
|
private final World world;
|
|
|
|
public CollisionChecker(World world) {
|
|
this.world = world;
|
|
}
|
|
|
|
public boolean checkTile(Entity entity) {
|
|
int entityLeftCol = entity.boxLeft() / Screen.TILE_SIZE;
|
|
int entityRightCol = entity.boxRight() / Screen.TILE_SIZE;
|
|
int entityTopRow = entity.boxTop() / Screen.TILE_SIZE;
|
|
int entityBottomRow = entity.boxBottom() / Screen.TILE_SIZE;
|
|
|
|
switch (entity.direction){
|
|
case N -> {
|
|
entityTopRow = (entity.boxTop() - entity.speed)/ Screen.TILE_SIZE;
|
|
return isNonePassable(entityTopRow, entityLeftCol, entityTopRow, entityRightCol);
|
|
}
|
|
case S -> {
|
|
entityBottomRow = (entity.boxBottom() + entity.speed)/ Screen.TILE_SIZE;
|
|
return isNonePassable(entityBottomRow,entityLeftCol,entityBottomRow,entityRightCol);
|
|
}
|
|
case W -> {
|
|
entityLeftCol = (entity.boxLeft() - entity.speed)/ Screen.TILE_SIZE;
|
|
return isNonePassable(entityTopRow, entityLeftCol, entityBottomRow, entityLeftCol);
|
|
}
|
|
case E -> {
|
|
entityRightCol = (entity.boxRight() + entity.speed)/ Screen.TILE_SIZE;
|
|
return isNonePassable(entityTopRow, entityRightCol, entityBottomRow, entityRightCol);
|
|
}
|
|
case NE -> {
|
|
entityTopRow = (entity.boxTop() - entity.speed)/ Screen.TILE_SIZE;
|
|
entityRightCol = (entity.boxRight() + entity.speed)/ Screen.TILE_SIZE;
|
|
entityLeftCol = (entity.boxLeft() + entity.speed)/ Screen.TILE_SIZE;
|
|
entityBottomRow = (entity.boxBottom() - entity.speed)/ Screen.TILE_SIZE;
|
|
return isNonePassable(entityTopRow, entityLeftCol, entityBottomRow, entityRightCol);
|
|
}
|
|
case NW -> {
|
|
entityTopRow = (entity.boxTop() - entity.speed)/ Screen.TILE_SIZE;
|
|
entityRightCol = (entity.boxRight() - entity.speed)/ Screen.TILE_SIZE;
|
|
entityLeftCol = (entity.boxLeft() - entity.speed)/ Screen.TILE_SIZE;
|
|
entityBottomRow = (entity.boxBottom() - entity.speed)/ Screen.TILE_SIZE;
|
|
return isNonePassable(entityBottomRow, entityLeftCol, entityTopRow, entityRightCol);
|
|
}
|
|
case SW -> {
|
|
entityTopRow = (entity.boxTop() + entity.speed)/ Screen.TILE_SIZE;
|
|
entityRightCol = (entity.boxRight() - entity.speed)/ Screen.TILE_SIZE;
|
|
entityLeftCol = (entity.boxLeft() - entity.speed)/ Screen.TILE_SIZE;
|
|
entityBottomRow = (entity.boxBottom() + entity.speed)/ Screen.TILE_SIZE;
|
|
return isNonePassable(entityTopRow, entityLeftCol, entityBottomRow, entityRightCol);
|
|
}
|
|
case SE -> {
|
|
entityTopRow = (entity.boxTop() + entity.speed)/ Screen.TILE_SIZE;
|
|
entityRightCol = (entity.boxRight() + entity.speed)/ Screen.TILE_SIZE;
|
|
entityLeftCol = (entity.boxLeft() + entity.speed)/ Screen.TILE_SIZE;
|
|
entityBottomRow = (entity.boxBottom() + entity.speed)/ Screen.TILE_SIZE;
|
|
return isNonePassable(entityBottomRow, entityLeftCol, entityTopRow, entityRightCol);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private boolean isNonePassable(int row1, int col1, int row2, int col2) {
|
|
int tileNum2 = world.mapTileNum[row1][col1];
|
|
int tileNum1 = world.mapTileNum[row2][col2];
|
|
|
|
return world.tiles[tileNum1].isCollision() || world.tiles[tileNum2].isCollision();
|
|
}
|
|
}
|