Create some useful geometric methods
This commit is contained in:
@@ -9,8 +9,8 @@ import com.bartlomiejpluta.base.engine.world.entity.config.EntitySpriteConfigura
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -21,7 +21,6 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
|
||||
private int animationSpeed = 100;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private Direction faceDirection;
|
||||
|
||||
@@ -63,6 +62,12 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFaceDirection(Direction direction) {
|
||||
this.faceDirection = direction;
|
||||
setDefaultAnimationFrame();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpeed(float speed) {
|
||||
framesToCrossOneTile = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
@@ -73,6 +78,21 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
animationSpeed = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Entity other) {
|
||||
return chebyshevDistance(other.getCoordinates());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int manhattanDistance(Entity other) {
|
||||
return manhattanDistance(other.getCoordinates());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Direction getDirectionTowards(Entity target) {
|
||||
return Direction.ofVector(new Vector2i(target.getCoordinates()).sub(getCoordinates()));
|
||||
}
|
||||
|
||||
public DefaultEntity(Mesh mesh, Material material, EntitySpriteConfiguration configuration) {
|
||||
super(mesh, material);
|
||||
this.defaultSpriteColumn = configuration.getDefaultSpriteColumn();
|
||||
|
||||
@@ -11,6 +11,9 @@ import lombok.Getter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
import static java.lang.Math.max;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class MovableSprite extends AnimatedSprite implements Updatable {
|
||||
private final Vector2f coordinateStepSize = new Vector2f(0, 0);
|
||||
@@ -80,6 +83,14 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
setCoordinates(coordinates);
|
||||
}
|
||||
|
||||
public int chebyshevDistance(Vector2i coordinates) {
|
||||
return max(abs(this.coordinates.x - coordinates.x), abs(this.coordinates.y - coordinates.y));
|
||||
}
|
||||
|
||||
public int manhattanDistance(Vector2i coordinates) {
|
||||
return abs(this.coordinates.x - coordinates.x) + abs(this.coordinates.y - coordinates.y);
|
||||
}
|
||||
|
||||
public MovableSprite(Mesh mesh, Material material) {
|
||||
super(mesh, material);
|
||||
setCoordinates(0, 0);
|
||||
|
||||
@@ -69,6 +69,11 @@ public abstract class Model implements Placeable {
|
||||
this.scaleY = scaleY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float euclideanDistance(Placeable other) {
|
||||
return other.getPosition().distance(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matrix4f getModelMatrix() {
|
||||
return modelMatrix
|
||||
|
||||
Reference in New Issue
Block a user