Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
srcimon committed Sep 22, 2023
1 parent bae7b96 commit ff95bc5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public List<Vector> calculateArea(final Bounds lightBox, double minAngle, double
private List<Segment> getSegmentsOf(final List<Bounds> allBounds) {
final List<Segment> allSegments = new ArrayList<>();
for (final var bounds : allBounds) {
ListUtil.addAll(allSegments, Borders.ALL.extractSegmentsMethod().apply(bounds));
ListUtil.addAll(allSegments, Borders.ALL.extractSegments(bounds));
}
return allSegments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public enum Borders {
this.extractSegmentsMethod = extractSegmentsMethod;
}

public Function<Bounds, List<Segment>> extractSegmentsMethod() {
return extractSegmentsMethod;
public List<Segment> extractSegments(Bounds bounds) {
return extractSegmentsMethod.apply(bounds);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.srcimon.screwbox.core.physics;

import io.github.srcimon.screwbox.core.Bounds;
import io.github.srcimon.screwbox.core.Segment;
import io.github.srcimon.screwbox.core.Vector;
import io.github.srcimon.screwbox.core.entities.Entity;
Expand All @@ -11,7 +10,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

import static java.util.Objects.nonNull;
Expand All @@ -21,14 +19,14 @@ public class Raycast {
private final Segment ray;
private final List<Entity> entities;
private final List<Predicate<Entity>> filters;
private final Function<Bounds, List<Segment>> method;
private final Borders borders;

Raycast(final Segment ray, final List<Entity> entities, final List<Predicate<Entity>> filters,
final Function<Bounds, List<Segment>> method) {
final Borders borders) {
this.ray = ray;
this.entities = entities;
this.filters = filters;
this.method = method;
this.borders = borders;
}

public Optional<Vector> nearestHit() {
Expand Down Expand Up @@ -80,7 +78,7 @@ public boolean hasHit() {

private List<Vector> getIntersections(final Entity entity) {
List<Vector> intersections = new ArrayList<>();
for (final Segment border : method.apply(entity.get(TransformComponent.class).bounds)) {
for (final Segment border : borders.extractSegments(entity.get(TransformComponent.class).bounds)) {
final Vector intersectionPoint = ray.intersectionPoint(border);
if (nonNull(intersectionPoint)) {
intersections.add(intersectionPoint);
Expand All @@ -90,7 +88,7 @@ private List<Vector> getIntersections(final Entity entity) {
}

private boolean intersectsRay(final Entity entity) {
for (final Segment border : method.apply(entity.get(TransformComponent.class).bounds)) {
for (final Segment border : borders.extractSegments(entity.get(TransformComponent.class).bounds)) {
if (ray.intersects(border)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Raycast castingTo(final double x, final double y) {
public Raycast castingTo(final Vector to) {
final var ray = Segment.between(from, to);
final List<Entity> matchingEntities = entities.fetchAll(archetype);
return new Raycast(ray, matchingEntities, filters, borders.extractSegmentsMethod());
return new Raycast(ray, matchingEntities, filters, borders);
}

public Raycast castingVertical(final double length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,38 @@

import io.github.srcimon.screwbox.core.Bounds;
import io.github.srcimon.screwbox.core.Segment;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class BordersTest {

private static final Bounds BOX = Bounds.atPosition(10, 10, 42, 2);

@Test
void extractSegmentsMethod_ofAll_extractsAllSegments() {
var method = Borders.ALL.extractSegmentsMethod();

Assertions.assertThat(method.apply(BOX)).contains(
Segment.between(BOX.origin(), BOX.topRight()),
Segment.between(BOX.topRight(), BOX.bottomRight()),
Segment.between(BOX.bottomLeft(), BOX.origin()),
Segment.between(BOX.topRight(), BOX.bottomRight()))
.hasSize(4);
void extractSegments_ofAll_extractsAllSegments() {
var segments = Borders.ALL.extractSegments(BOX);

assertThat(segments).containsExactly(
Segment.between(BOX.origin(), BOX.topRight()),
Segment.between(BOX.topRight(), BOX.bottomRight()),
Segment.between(BOX.bottomRight(), BOX.bottomLeft()),
Segment.between(BOX.bottomLeft(), BOX.origin()));
}

@Test
void extractSegmentsMethod_ofTopOnly_extractsTopSegment() {
var method = Borders.TOP_ONLY.extractSegmentsMethod();
void extractSegments_ofTopOnly_extractsTopSegment() {
var segments = Borders.TOP_ONLY.extractSegments(BOX);

Assertions.assertThat(method.apply(BOX))
.contains(Segment.between(BOX.origin(), BOX.topRight()))
.hasSize(1);
assertThat(segments).containsExactly(Segment.between(BOX.origin(), BOX.topRight()));
}

@Test
void extractSegmentsMethod_ofVerticalOnly_extractsVerticalSegments() {
var method = Borders.VERTICAL_ONLY.extractSegmentsMethod();
void extractSegments_ofVerticalOnly_extractsVerticalSegments() {
var segments = Borders.VERTICAL_ONLY.extractSegments(BOX);

Assertions.assertThat(method.apply(BOX)).contains(
Segment.between(BOX.bottomLeft(), BOX.origin()),
Segment.between(BOX.topRight(), BOX.bottomRight()))
.hasSize(2);
assertThat(segments).containsExactly(
Segment.between(BOX.topRight(), BOX.bottomRight()),
Segment.between(BOX.bottomLeft(), BOX.origin()));
}
}

0 comments on commit ff95bc5

Please sign in to comment.