Files
PacMan/src/main/java/se/urmo/game/state/CollisionChecker.java
2025-08-10 17:01:15 +02:00

40 lines
1.6 KiB
Java

package se.urmo.game.state;
import se.urmo.game.GameMap;
public class CollisionChecker {
private GameMap map;
public CollisionChecker(GameMap map) {
this.map = map;
}
public boolean canMoveTo(int dir, int objectLeft, int objectTop, int width, int height) {
int objectRight = objectLeft + width;
int objectBotton = objectTop + height;
return switch (dir){
case 0 -> isPassibleRight(objectRight, objectTop, objectBotton);// right
case 1 -> isPassibleLeft(objectLeft, objectTop, objectBotton);// right
case 2 -> isPassibleBottom(objectLeft, objectRight, objectBotton);
case 3 -> isPassibleTop(objectLeft, objectRight, objectTop);// right
default -> throw new IllegalArgumentException("Invalid dir: " + dir);
};
}
private boolean isPassibleBottom(int objectLeft, int objectRight, int objectBotton) {
return map.isPassable(objectLeft, objectBotton) && map.isPassable(objectRight, objectBotton);
}
private boolean isPassibleTop(int objectLeft, int objectRight, int objectTop) {
return map.isPassable(objectLeft, objectTop) && map.isPassable(objectRight, objectTop);
}
private boolean isPassibleLeft(int objectLeft, int objectTop, int objectBotton) {
return map.isPassable(objectLeft, objectTop) && map.isPassable(objectLeft, objectBotton);
}
private boolean isPassibleRight(int objectRight, int objectTop, int objectBotton) {
return map.isPassable(objectRight, objectTop) && map.isPassable(objectRight, objectBotton);
}
}