Skip to content

Commit

Permalink
chore: replace Rectangle with BlockArea (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend authored Aug 16, 2021
1 parent 0216b20 commit 051b946
Showing 1 changed file with 19 additions and 43 deletions.
62 changes: 19 additions & 43 deletions src/main/java/org/terasology/navgraph/Entrance.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package org.terasology.navgraph;

import com.google.common.collect.Sets;
import org.terasology.engine.world.block.BlockArea;

import java.util.Set;

Expand All @@ -16,76 +17,51 @@ public enum Type {
}

public final Set<Floor> neighborFloors = Sets.newHashSet();
private Rect area;
private final BlockArea area = new BlockArea(BlockArea.INVALID);
private Type type;
private Floor floor;
private final Floor floor;

public Entrance(Floor floor) {
this.floor = floor;
}

public boolean isPartOfEntrance(int x, int y) {
if (area == null) {
if (!area.isValid()) {
return true;
}
if (area.contains(x, y)) {
return true;
}
int x1 = Math.min(area.x, x);
int y1 = Math.min(area.y, y);
int x2 = Math.max(area.x + area.w, x);
int y2 = Math.max(area.y + area.h, y);
int x1 = Math.min(area.minX(), x);
int y1 = Math.min(area.minY(), y);
int x2 = Math.max(area.maxX(), x);
int y2 = Math.max(area.maxY(), y);

if (type == Type.VERTICAL) {
return y2 - y1 == 0 && area.w + 1 == x2 - x1;
return y2 - y1 == 0 && area.getSizeX() == x2 - x1;
} else if (type == Type.HORIZONTAL) {
return x2 - x1 == 0 && area.h + 1 == y2 - y1;
return x2 - x1 == 0 && area.getSizeY() == y2 - y1;
} else {
return x2 - x1 <= 1 && y2 - y1 <= 1;
}
}

public void addToEntrance(int x, int y) {
if (area == null) {
area = new Rect(x, y, 0, 0);
} else {
if (!area.contains(x, y)) {
int x1 = Math.min(area.x, x);
int y1 = Math.min(area.y, y);
int x2 = Math.max(area.x + area.w, x);
int y2 = Math.max(area.y + area.h, y);
area = new Rect(x1, y1, x2 - x1, y2 - y1);
if (area.w > area.h) {
type = Type.VERTICAL;
} else if (area.w < area.h) {
type = Type.HORIZONTAL;
}

if (!area.contains(x, y)) {
area.union(x, y);
if (area.getSizeX() > area.getSizeY()) {
type = Type.VERTICAL;
} else if (area.getSizeX() < area.getSizeY()) {
type = Type.HORIZONTAL;
}
}
}

public WalkableBlock getAbstractBlock() {
int mx = area.x + area.w / 2;
int my = area.y + area.h / 2;
int mx = (area.minX() + area.getSizeX()) / 2;
int my = (area.minY() + area.getSizeY()) / 2;

return floor.getBlock(mx, my);
}

private static final class Rect {
int x;
int y;
int w;
int h;

private Rect(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}

private boolean contains(int px, int py) {
return px >= x && py >= y && px < x + h && py < y + h;
}
}
}

0 comments on commit 051b946

Please sign in to comment.