Skip to content

Commit

Permalink
Added default grid for snapping
Browse files Browse the repository at this point in the history
  • Loading branch information
srcimon committed Aug 23, 2023
1 parent 289f5c6 commit b8e87fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface Physics {

Physics setGrid(Grid grid);

Optional<Grid> grid();
Grid grid();

Bounds snapToGrid(Bounds bounds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.util.Objects.isNull;
Expand All @@ -17,7 +18,7 @@ public class DefaultPhysics implements Physics {

private PathfindingAlgorithm algorithm = new AStarAlgorithm();

private Grid grid;
private Grid grid = new Grid(Bounds.atOrigin(Vector.zero(), 32, 32), 16);

public DefaultPhysics(final Engine engine) {
this.engine = engine;
Expand Down Expand Up @@ -63,21 +64,18 @@ public Optional<Path> findPath(final Grid grid, final Vector start, final Vector

@Override
public Optional<Path> findPath(final Vector start, final Vector end) {
if (isNull(grid)) {
throw new IllegalStateException("no grid for pathfinding present");
}
return findPath(grid, start, end);
}

@Override
public Physics setGrid(final Grid grid) {
this.grid = grid;
this.grid = requireNonNull(grid, "grid must not be null");
return this;
}

@Override
public Optional<Grid> grid() {
return ofNullable(grid);
public Grid grid() {
return grid;
}

@Override
Expand All @@ -89,9 +87,6 @@ public Physics setPathfindingAlgorithm(final PathfindingAlgorithm algorithm) {
@Override
public Bounds snapToGrid(final Bounds bounds) {
requireNonNull(bounds, "bounds must not be null");
if (isNull(grid)) {
throw new IllegalStateException("no grid present");
}
return bounds.moveTo(grid.snap(bounds.position()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ void beforeEach() {
}

@Test
void findPath_noGrid_throwsException() {
Vector start = $(0, 0);
Vector end = $(2, 5);

assertThatThrownBy(() -> physics.findPath(start, end))
.isInstanceOf(IllegalStateException.class)
.hasMessage("no grid for pathfinding present");
}

@Test
void findPath_gridPresent_addsStartEndEndPositions() {
void findPath_pathFound_addsStartEndEndPositions() {
Grid grid = new Grid($$(0, 0, 10, 10), 2, false);
physics.setGrid(grid);

Expand Down Expand Up @@ -82,25 +72,23 @@ void snapToGrid_positionNull_exception() {
}

@Test
void snapToGrid_noGrid_exception() {
Vector position = $(3, 10);
void snapToGrid_defaultGrod_snapsVectorToGrid() {
var result = physics.snapToGrid($(3, 10));

assertThatThrownBy(() -> physics.snapToGrid(position))
.isInstanceOf(IllegalStateException.class)
.hasMessage("no grid present");
assertThat(result).isEqualTo($(8, 8));
}

@Test
void snapToGrid_gridPresent_snapsVectorToGrid() {
physics.setGrid(new Grid($$(0, 0, 16, 16), 16));
void snapToGrid_customGrid_snapsVectorToGrid() {
physics.setGrid(new Grid($$(0, 0, 32, 32), 4));

var result = physics.snapToGrid($(3, 10));

assertThat(result).isEqualTo($(8, 8));
assertThat(result).isEqualTo($(2, 10));
}

@Test
void snapToGrid_gridPresent_snapsBoundsToGrid() {
void snapToGrid_customGrid_snapsBoundsToGrid() {
physics.setGrid(new Grid($$(0, 0, 16, 16), 16));

var result = physics.snapToGrid($$(3, 10, 8, 8));
Expand Down

0 comments on commit b8e87fe

Please sign in to comment.