Create some useful geometric methods
This commit is contained in:
@@ -2,6 +2,9 @@ package com.bartlomiejpluta.base.api.game.entity;
|
||||
|
||||
import org.joml.Vector2i;
|
||||
|
||||
import static java.lang.Math.PI;
|
||||
import static org.joml.Math.atan2;
|
||||
|
||||
public enum Direction {
|
||||
UP(0, -1),
|
||||
DOWN(0, 1),
|
||||
@@ -19,4 +22,37 @@ public enum Direction {
|
||||
public Vector2i asVector() {
|
||||
return new Vector2i(x, y);
|
||||
}
|
||||
|
||||
public Direction opposite() {
|
||||
switch (this) {
|
||||
case UP:
|
||||
return DOWN;
|
||||
case RIGHT:
|
||||
return LEFT;
|
||||
case DOWN:
|
||||
return UP;
|
||||
case LEFT:
|
||||
return RIGHT;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public static Direction ofVector(Vector2i vector) {
|
||||
// X Versor = [1, 0]
|
||||
// dot = 1 * vector.x + 0 * vector.y = vector.x
|
||||
// det = 1 * vector.y - 0 * vector.x = vector.y
|
||||
// angle = atan2(det, dot) = atan2(vector.y, vector.x)
|
||||
float angle = atan2(vector.y, vector.x);
|
||||
|
||||
if (-PI / 4 < angle && angle < PI / 4) {
|
||||
return RIGHT;
|
||||
} else if (PI / 4 <= angle && angle <= 3 * PI / 4) {
|
||||
return DOWN;
|
||||
} else if (3 * PI / 4 < angle && angle < 5 * PI / 4) {
|
||||
return LEFT;
|
||||
} else {
|
||||
return UP;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,12 @@ public interface Entity extends Placeable, Renderable, Updatable {
|
||||
void setSpeed(float speed);
|
||||
|
||||
void setAnimationSpeed(float speed);
|
||||
|
||||
boolean isMoving();
|
||||
|
||||
int chebyshevDistance(Entity other);
|
||||
|
||||
int manhattanDistance(Entity other);
|
||||
|
||||
Direction getDirectionTowards(Entity target);
|
||||
}
|
||||
|
||||
@@ -15,13 +15,18 @@ public interface Placeable {
|
||||
void moveRotation(float rotation);
|
||||
|
||||
float getScaleX();
|
||||
|
||||
void setScaleX(float scale);
|
||||
|
||||
float getScaleY();
|
||||
|
||||
void setScaleY(float scale);
|
||||
|
||||
void setScale(float scale);
|
||||
|
||||
void setScale(float scaleX, float scaleY);
|
||||
|
||||
float euclideanDistance(Placeable other);
|
||||
|
||||
Matrix4f getModelMatrix();
|
||||
}
|
||||
|
||||
@@ -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