package dev.llan.model.board.pathfinding; import dev.llan.model.board.Grid; import dev.llan.model.board.Point; import java.util.ArrayList; import java.util.List; public class Path implements Cloneable { private final List path; private final List aggregateCost; public Path() { path = new ArrayList<>(); aggregateCost = new ArrayList<>(); } public void removeFirst() { path.removeFirst(); double cost = aggregateCost.removeFirst(); aggregateCost.replaceAll(val -> val - cost); } public Point getFirst() { return path.getFirst(); } public boolean isEmpty() { return path.isEmpty(); } void addFirst(Point point) { path.addFirst(point); } public Point getEndpoint() { return path.getLast(); } void calculateCosts(Point start, Grid grid) { double sum = 0.0; Point current = start; for(Point point : path) { sum += grid.getMovementCost(point, current); aggregateCost.add(sum); current = point; } assert aggregateCost.size() == path.size(); } public List getAggregateCost() { return aggregateCost; } public int getPathSize() { return path.size(); } public List getPath() { return path; } @Override public Path clone() { Path clone = new Path(); for(Point point : this.path) { Point clonedPoint = point.clone(); clone.path.add(clonedPoint); } for(double val : this.aggregateCost) { clone.aggregateCost.add(val); } return clone; } }