Extract coordinates-related code from Movable to Locationable interface
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.bartlomiejpluta.base.api.location;
|
||||
|
||||
import com.bartlomiejpluta.base.internal.object.Placeable;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public interface Locationable extends Placeable {
|
||||
void setStepSize(float x, float y);
|
||||
|
||||
Vector2ic getCoordinates();
|
||||
|
||||
void setCoordinates(Vector2ic coordinates);
|
||||
|
||||
void setCoordinates(int x, int y);
|
||||
|
||||
Vector2fc getPositionOffset();
|
||||
|
||||
void setPositionOffset(Vector2fc offset);
|
||||
|
||||
void setPositionOffset(float offsetX, float offsetY);
|
||||
|
||||
int chebyshevDistance(Vector2ic coordinates);
|
||||
|
||||
int manhattanDistance(Vector2ic coordinates);
|
||||
}
|
||||
@@ -1,24 +1,8 @@
|
||||
package com.bartlomiejpluta.base.api.move;
|
||||
|
||||
import com.bartlomiejpluta.base.internal.object.Placeable;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public interface Movable extends Placeable {
|
||||
void setStepSize(float x, float y);
|
||||
|
||||
Vector2ic getCoordinates();
|
||||
|
||||
void setCoordinates(Vector2ic coordinates);
|
||||
|
||||
void setCoordinates(int x, int y);
|
||||
|
||||
Vector2fc getPositionOffset();
|
||||
|
||||
void setPositionOffset(Vector2fc offset);
|
||||
|
||||
void setPositionOffset(float offsetX, float offsetY);
|
||||
import com.bartlomiejpluta.base.api.location.Locationable;
|
||||
|
||||
public interface Movable extends Locationable {
|
||||
void setSpeed(float speed);
|
||||
|
||||
Movement prepareMovement(Direction direction);
|
||||
@@ -27,10 +11,6 @@ public interface Movable extends Placeable {
|
||||
|
||||
boolean isMoving();
|
||||
|
||||
int chebyshevDistance(Vector2ic coordinates);
|
||||
|
||||
int manhattanDistance(Vector2ic coordinates);
|
||||
|
||||
boolean move(Movement movement);
|
||||
|
||||
void abortMove();
|
||||
|
||||
@@ -3,13 +3,14 @@ package com.bartlomiejpluta.base.engine.world.animation.model;
|
||||
import com.bartlomiejpluta.base.api.animation.Animated;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.world.location.LocationableSprite;
|
||||
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
||||
import com.bartlomiejpluta.base.util.math.MathUtil;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class AnimatedSprite extends Sprite implements Animated {
|
||||
public abstract class AnimatedSprite extends LocationableSprite implements Animated {
|
||||
private int time;
|
||||
|
||||
// The time in ms between frames
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.bartlomiejpluta.base.engine.world.location;
|
||||
|
||||
import com.bartlomiejpluta.base.api.location.Locationable;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
||||
import com.bartlomiejpluta.base.util.math.Distance;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class LocationableSprite extends Sprite implements Locationable {
|
||||
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
||||
|
||||
private PlacingMode placingMode;
|
||||
protected final Vector2f coordinateStepSize = new Vector2f(0, 0);
|
||||
protected final Vector2i coordinates = new Vector2i(0, 0);
|
||||
protected final Vector2f positionOffset = new Vector2f(0, 0);
|
||||
|
||||
public LocationableSprite(Mesh mesh, Material material) {
|
||||
super(mesh, material);
|
||||
setCoordinates(0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2ic getCoordinates() {
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCoordinates(int x, int y) {
|
||||
coordinates.x = x;
|
||||
coordinates.y = y;
|
||||
super.setPosition((x + 0.5f) * coordinateStepSize.x + positionOffset.x, (y + 0.5f) * coordinateStepSize.y + positionOffset.y);
|
||||
placingMode = PlacingMode.BY_COORDINATES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(float x, float y) {
|
||||
super.setPosition(x + positionOffset.x, y + positionOffset.y);
|
||||
coordinates.x = (int) (x / coordinateStepSize.x);
|
||||
coordinates.y = (int) (y / coordinateStepSize.y);
|
||||
placingMode = PlacingMode.BY_POSITION;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector2fc position) {
|
||||
setPosition(position.x(), position.y());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCoordinates(Vector2ic coordinates) {
|
||||
setCoordinates(coordinates.x(), coordinates.y());
|
||||
}
|
||||
|
||||
public void setStepSize(float x, float y) {
|
||||
coordinateStepSize.x = x;
|
||||
coordinateStepSize.y = y;
|
||||
adjustPosition();
|
||||
}
|
||||
|
||||
private void adjustPosition() {
|
||||
switch (placingMode) {
|
||||
case BY_POSITION -> setPosition(position);
|
||||
case BY_COORDINATES -> setCoordinates(coordinates);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2fc getPositionOffset() {
|
||||
return positionOffset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(Vector2fc offset) {
|
||||
setPositionOffset(offset.x(), offset.y());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(float offsetX, float offsetY) {
|
||||
this.positionOffset.x = offsetX;
|
||||
this.positionOffset.y = offsetY;
|
||||
adjustPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Vector2ic coordinates) {
|
||||
return Distance.chebyshev(this.coordinates, coordinates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int manhattanDistance(Vector2ic coordinates) {
|
||||
return Distance.manhattan(this.coordinates, coordinates);
|
||||
}
|
||||
}
|
||||
@@ -20,29 +20,15 @@ import static java.lang.Math.max;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class MovableSprite extends AnimatedSprite implements Movable, Updatable {
|
||||
private final Vector2f coordinateStepSize = new Vector2f(0, 0);
|
||||
|
||||
private final Vector2i coordinates = new Vector2i(0, 0);
|
||||
private final Vector2f positionOffset = new Vector2f(0, 0);
|
||||
private int moveTime = 0;
|
||||
private Vector2f movementVector;
|
||||
private int framesToCrossOneTile = 1;
|
||||
|
||||
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
||||
|
||||
private PlacingMode placingMode;
|
||||
|
||||
@Getter
|
||||
private Movement movement;
|
||||
|
||||
public MovableSprite(Mesh mesh, Material material) {
|
||||
super(mesh, material);
|
||||
setCoordinates(0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2ic getCoordinates() {
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,24 +41,6 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
framesToCrossOneTile = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
super.update(dt);
|
||||
|
||||
if (movement != null) {
|
||||
if (moveTime > 0) {
|
||||
--moveTime;
|
||||
movePosition(movementVector);
|
||||
} else {
|
||||
adjustCoordinates();
|
||||
setDefaultAnimationFrame();
|
||||
movement.onFinish();
|
||||
movementVector = null;
|
||||
movement = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void setDefaultAnimationFrame();
|
||||
|
||||
private void adjustCoordinates() {
|
||||
@@ -108,68 +76,20 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCoordinates(int x, int y) {
|
||||
coordinates.x = x;
|
||||
coordinates.y = y;
|
||||
super.setPosition((x + 0.5f) * coordinateStepSize.x + positionOffset.x, (y + 0.5f) * coordinateStepSize.y + positionOffset.y);
|
||||
placingMode = PlacingMode.BY_COORDINATES;
|
||||
}
|
||||
public void update(float dt) {
|
||||
super.update(dt);
|
||||
|
||||
@Override
|
||||
public void setPosition(float x, float y) {
|
||||
super.setPosition(x + positionOffset.x, y + positionOffset.y);
|
||||
coordinates.x = (int) (x / coordinateStepSize.x);
|
||||
coordinates.y = (int) (y / coordinateStepSize.y);
|
||||
placingMode = PlacingMode.BY_POSITION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector2fc position) {
|
||||
setPosition(position.x(), position.y());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCoordinates(Vector2ic coordinates) {
|
||||
setCoordinates(coordinates.x(), coordinates.y());
|
||||
}
|
||||
|
||||
public void setStepSize(float x, float y) {
|
||||
coordinateStepSize.x = x;
|
||||
coordinateStepSize.y = y;
|
||||
adjustPosition();
|
||||
}
|
||||
|
||||
private void adjustPosition() {
|
||||
switch (placingMode) {
|
||||
case BY_POSITION -> setPosition(position);
|
||||
case BY_COORDINATES -> setCoordinates(coordinates);
|
||||
if (movement != null) {
|
||||
if (moveTime > 0) {
|
||||
--moveTime;
|
||||
movePosition(movementVector);
|
||||
} else {
|
||||
adjustCoordinates();
|
||||
setDefaultAnimationFrame();
|
||||
movement.onFinish();
|
||||
movementVector = null;
|
||||
movement = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2fc getPositionOffset() {
|
||||
return positionOffset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(Vector2fc offset) {
|
||||
setPositionOffset(offset.x(), offset.y());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(float offsetX, float offsetY) {
|
||||
this.positionOffset.x = offsetX;
|
||||
this.positionOffset.y = offsetY;
|
||||
adjustPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Vector2ic coordinates) {
|
||||
return Distance.chebyshev(this.coordinates, coordinates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int manhattanDistance(Vector2ic coordinates) {
|
||||
return Distance.manhattan(this.coordinates, coordinates);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user