intersection works
This commit is contained in:
@ -1,8 +1,65 @@
|
||||
package se.urmo.game.collision;
|
||||
|
||||
import se.urmo.game.map.GameMap;
|
||||
import se.urmo.game.util.Direction;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class GhostCollisionChecker {
|
||||
private final GameMap map;
|
||||
|
||||
public GhostCollisionChecker(GameMap map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Point getValidDestination(Direction direction, Point position, int agent_width, int agent_height) {
|
||||
|
||||
List<Point> bounderies = switch (direction) {
|
||||
case RIGHT -> List.of(
|
||||
new Point(position.x + agent_width, position.y), // TOPRIGHT
|
||||
new Point(position.x + agent_width, position.y + agent_height)); // BOTTOMRIGHT
|
||||
case LEFT -> List.of(
|
||||
position, // TOPLEFT
|
||||
new Point(position.x, position.y + agent_height)); // BOTTOMLEFT
|
||||
case UP -> List.of(
|
||||
position, // TOPLEFT
|
||||
new Point(position.x + agent_width, position.y)); // TOPRIGHT
|
||||
case DOWN -> List.of(
|
||||
new Point(position.x, position.y + agent_height), // BOTTOMLEFT
|
||||
new Point(position.x + agent_width, position.y + agent_height)); // BOTTOMRIGHT
|
||||
default -> Collections.EMPTY_LIST;
|
||||
};
|
||||
|
||||
System.out.println( direction + " bounderies for " + position + " are " + bounderies);
|
||||
|
||||
List<Point> normalizedBoundaries = bounderies.stream()
|
||||
.map(p -> normalizePosition(direction, p, agent_width, agent_height))
|
||||
.toList();
|
||||
|
||||
if (map.isSolid(normalizedBoundaries)) {
|
||||
return normalizePosition(direction, position, agent_width, agent_height);
|
||||
}
|
||||
return null; // Blocked
|
||||
}
|
||||
|
||||
public Point normalizePosition(Direction dir, Point pos, int agent_width, int agent_height) {
|
||||
int x = pos.x;
|
||||
int y = pos.y;
|
||||
int width = map.getWidth();
|
||||
int height = map.getHeight();
|
||||
|
||||
// tunnel
|
||||
if (x < GameMap.OFFSET_X) x = width - agent_width - GameMap.OFFSET_X; // right
|
||||
if (x >= (width - GameMap.OFFSET_X)) x = GameMap.OFFSET_X; // left
|
||||
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
public List<Direction> isIntersection(Point position) {
|
||||
List<Direction> intersection = map.isIntersection(position);
|
||||
System.out.println("Possible travel directions: " + intersection);
|
||||
return intersection;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user