Implement Euclidean distance for coordinates
This commit is contained in:
@@ -26,7 +26,11 @@ public interface Locationable extends Placeable {
|
|||||||
|
|
||||||
int manhattanDistance(Vector2ic coordinates);
|
int manhattanDistance(Vector2ic coordinates);
|
||||||
|
|
||||||
|
double euclideanDistance(Vector2ic coordinates);
|
||||||
|
|
||||||
int chebyshevDistance(Locationable other);
|
int chebyshevDistance(Locationable other);
|
||||||
|
|
||||||
int manhattanDistance(Locationable other);
|
int manhattanDistance(Locationable other);
|
||||||
|
|
||||||
|
double euclideanDistance(Locationable other);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import com.bartlomiejpluta.base.api.ai.AI;
|
|||||||
import com.bartlomiejpluta.base.api.ai.NPC;
|
import com.bartlomiejpluta.base.api.ai.NPC;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
|
import com.bartlomiejpluta.base.util.math.Distance;
|
||||||
|
import org.joml.Vector2ic;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@@ -12,20 +14,37 @@ public class RandomMovementAI<N extends NPC> implements AI {
|
|||||||
private final N npc;
|
private final N npc;
|
||||||
|
|
||||||
private final float intervalSeconds;
|
private final float intervalSeconds;
|
||||||
|
private final int radius;
|
||||||
|
private final Vector2ic origin;
|
||||||
private float accumulator = 0.0f;
|
private float accumulator = 0.0f;
|
||||||
private float threshold = 0.0f;
|
private float threshold = 0.0f;
|
||||||
|
|
||||||
public RandomMovementAI(N npc, float intervalSeconds) {
|
public RandomMovementAI(N npc, float intervalSeconds) {
|
||||||
this.npc = npc;
|
this.npc = npc;
|
||||||
this.intervalSeconds = intervalSeconds;
|
this.intervalSeconds = intervalSeconds;
|
||||||
|
this.radius = 0;
|
||||||
|
origin = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RandomMovementAI(N npc, float intervalSeconds, Vector2ic origin, int radius) {
|
||||||
|
this.npc = npc;
|
||||||
|
this.intervalSeconds = intervalSeconds;
|
||||||
|
this.radius = radius;
|
||||||
|
this.origin = origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void nextActivity(ObjectLayer layer, float dt) {
|
public void nextActivity(ObjectLayer layer, float dt) {
|
||||||
if (!npc.isMoving()) {
|
if (!npc.isMoving()) {
|
||||||
if (accumulator > threshold) {
|
if (accumulator > threshold) {
|
||||||
Direction direction = Direction.values()[random.nextInt(4)];
|
var direction = Direction.values()[random.nextInt(4)];
|
||||||
npc.move(direction);
|
var movement = npc.prepareMovement(direction);
|
||||||
|
|
||||||
|
if (origin != null && Distance.euclidean(origin, movement.getTo()) > radius) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
npc.getLayer().pushMovement(movement);
|
||||||
accumulator = 0.0f;
|
accumulator = 0.0f;
|
||||||
threshold = random.nextFloat() * intervalSeconds;
|
threshold = random.nextFloat() * intervalSeconds;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -179,6 +179,11 @@ public abstract class AnimationDelegate implements Animation {
|
|||||||
return animation.euclideanDistance(other);
|
return animation.euclideanDistance(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Vector2ic coordinates) {
|
||||||
|
return animation.euclideanDistance(coordinates);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Matrix4fc getModelMatrix() {
|
public Matrix4fc getModelMatrix() {
|
||||||
return animation.getModelMatrix();
|
return animation.getModelMatrix();
|
||||||
@@ -224,6 +229,11 @@ public abstract class AnimationDelegate implements Animation {
|
|||||||
return animation.manhattanDistance(other);
|
return animation.manhattanDistance(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Locationable other) {
|
||||||
|
return animation.euclideanDistance(other);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vector2fc getPositionOffset() {
|
public Vector2fc getPositionOffset() {
|
||||||
return animation.getPositionOffset();
|
return animation.getPositionOffset();
|
||||||
|
|||||||
@@ -148,6 +148,11 @@ public abstract class CharacterDelegate implements Character {
|
|||||||
return character.manhattanDistance(other);
|
return character.manhattanDistance(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Locationable other) {
|
||||||
|
return character.euclideanDistance(other);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Direction getDirectionTowards(Locationable target) {
|
public Direction getDirectionTowards(Locationable target) {
|
||||||
return character.getDirectionTowards(target);
|
return character.getDirectionTowards(target);
|
||||||
@@ -228,6 +233,11 @@ public abstract class CharacterDelegate implements Character {
|
|||||||
return character.euclideanDistance(other);
|
return character.euclideanDistance(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Vector2ic coordinates) {
|
||||||
|
return character.euclideanDistance(coordinates);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int chebyshevDistance(Vector2ic coordinates) {
|
public int chebyshevDistance(Vector2ic coordinates) {
|
||||||
return character.chebyshevDistance(coordinates);
|
return character.chebyshevDistance(coordinates);
|
||||||
|
|||||||
@@ -68,6 +68,11 @@ public abstract class EntityDelegate implements Entity {
|
|||||||
return entity.manhattanDistance(other);
|
return entity.manhattanDistance(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Locationable other) {
|
||||||
|
return entity.euclideanDistance(other);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Direction getDirectionTowards(Locationable target) {
|
public Direction getDirectionTowards(Locationable target) {
|
||||||
return entity.getDirectionTowards(target);
|
return entity.getDirectionTowards(target);
|
||||||
@@ -148,6 +153,11 @@ public abstract class EntityDelegate implements Entity {
|
|||||||
return entity.euclideanDistance(other);
|
return entity.euclideanDistance(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Vector2ic coordinates) {
|
||||||
|
return entity.euclideanDistance(coordinates);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int chebyshevDistance(Vector2ic coordinates) {
|
public int chebyshevDistance(Vector2ic coordinates) {
|
||||||
return entity.chebyshevDistance(coordinates);
|
return entity.chebyshevDistance(coordinates);
|
||||||
|
|||||||
@@ -94,6 +94,16 @@ public abstract class IconDelegate implements Icon {
|
|||||||
return icon.manhattanDistance(other);
|
return icon.manhattanDistance(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Vector2ic coordinates) {
|
||||||
|
return icon.euclideanDistance(coordinates);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Locationable other) {
|
||||||
|
return icon.euclideanDistance(other);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Direction getDirectionTowards(Locationable target) {
|
public Direction getDirectionTowards(Locationable target) {
|
||||||
return icon.getDirectionTowards(target);
|
return icon.getDirectionTowards(target);
|
||||||
|
|||||||
@@ -71,4 +71,8 @@ public class Distance {
|
|||||||
public static double euclidean(Vector2dc a, Vector2dc b) {
|
public static double euclidean(Vector2dc a, Vector2dc b) {
|
||||||
return sqrt((a.x() - b.x()) * (a.x() - b.x()) + (a.y() - b.y()) * (a.y() - b.y()));
|
return sqrt((a.x() - b.x()) * (a.x() - b.x()) + (a.y() - b.y()) * (a.y() - b.y()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static double euclidean(Vector2ic a, Vector2ic b) {
|
||||||
|
return sqrt((a.x() - b.x()) * (a.x() - b.x()) + (a.y() - b.y()) * (a.y() - b.y()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,5 +108,15 @@ public abstract class LocationableModel extends Model implements Locationable {
|
|||||||
return Distance.manhattan(this.coordinates, other.getCoordinates());
|
return Distance.manhattan(this.coordinates, other.getCoordinates());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Vector2ic coordinates) {
|
||||||
|
return Distance.euclidean(this.coordinates.x, this.coordinates.y, coordinates.x(), coordinates.y());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double euclideanDistance(Locationable other) {
|
||||||
|
return Distance.euclidean(this.coordinates.x, this.coordinates.y, other.getCoordinates().x(), other.getCoordinates().y());
|
||||||
|
}
|
||||||
|
|
||||||
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user