Refactor Movable model and add position offset property to them
This commit is contained in:
@@ -6,18 +6,9 @@ import com.bartlomiejpluta.base.api.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.api.internal.object.Placeable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.Renderable;
|
||||
import com.bartlomiejpluta.base.api.util.path.Path;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public interface Animation extends Placeable, Movable, Renderable, Updatable {
|
||||
|
||||
void setStepSize(float x, float y);
|
||||
|
||||
Vector2ic getCoordinates();
|
||||
|
||||
void setCoordinates(Vector2ic coordinates);
|
||||
|
||||
void setCoordinates(int x, int y);
|
||||
|
||||
void setAnimationSpeed(float speed);
|
||||
|
||||
Integer getRepeat();
|
||||
@@ -26,8 +17,6 @@ public interface Animation extends Placeable, Movable, Renderable, Updatable {
|
||||
|
||||
void followPath(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail);
|
||||
|
||||
void setSpeed(float speed);
|
||||
|
||||
void onAdd(Layer layer);
|
||||
|
||||
void onFinish(Layer layer);
|
||||
|
||||
@@ -159,6 +159,16 @@ public abstract class AnimationDelegate implements Animation {
|
||||
return animation.isMoving();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(Vector2fc offset) {
|
||||
animation.setPositionOffset(offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(float offsetX, float offsetY) {
|
||||
animation.setPositionOffset(offsetX, offsetY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void followPath(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail) {
|
||||
animation.followPath(path, repeat, finishOnEnd, finishOnFail);
|
||||
|
||||
@@ -4,16 +4,8 @@ import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.api.internal.object.Placeable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.Renderable;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public interface Entity extends Placeable, Movable, Renderable, Updatable {
|
||||
void setStepSize(float x, float y);
|
||||
|
||||
Vector2ic getCoordinates();
|
||||
|
||||
void setCoordinates(Vector2ic coordinates);
|
||||
|
||||
void setCoordinates(int x, int y);
|
||||
|
||||
Direction getFaceDirection();
|
||||
|
||||
|
||||
@@ -71,6 +71,16 @@ public abstract class EntityDelegate implements Entity {
|
||||
return entity.isMoving();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(Vector2fc offset) {
|
||||
entity.setPositionOffset(offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(float offsetX, float offsetY) {
|
||||
entity.setPositionOffset(offsetX, offsetY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Entity other) {
|
||||
return entity.chebyshevDistance(other);
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
package com.bartlomiejpluta.base.api.game.entity;
|
||||
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public interface Movable {
|
||||
void setStepSize(float x, float y);
|
||||
|
||||
Vector2ic getCoordinates();
|
||||
|
||||
void setCoordinates(Vector2ic coordinates);
|
||||
|
||||
void setCoordinates(int x, int y);
|
||||
|
||||
void setPositionOffset(Vector2fc offset);
|
||||
|
||||
void setPositionOffset(float offsetX, float offsetY);
|
||||
|
||||
void setSpeed(float speed);
|
||||
|
||||
Movement prepareMovement(Direction direction);
|
||||
|
||||
Movement getMovement();
|
||||
|
||||
@@ -76,11 +76,6 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
this.finishOnFail = finishOnFail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpeed(float speed) {
|
||||
framesToCrossOneTile = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
super.update(dt);
|
||||
|
||||
@@ -82,11 +82,6 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
setDefaultAnimationFrame();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpeed(float speed) {
|
||||
framesToCrossOneTile = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationSpeed(float speed) {
|
||||
animationSpeed = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.bartlomiejpluta.base.engine.world.movement;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.entity.Direction;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Movable;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Movement;
|
||||
import com.bartlomiejpluta.base.api.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.api.util.math.MathUtil;
|
||||
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.animation.model.AnimatedSprite;
|
||||
@@ -17,27 +19,32 @@ import static java.lang.Math.abs;
|
||||
import static java.lang.Math.max;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class MovableSprite extends AnimatedSprite implements Updatable {
|
||||
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 final Vector2i coordinates = new Vector2i(0, 0);
|
||||
private int framesToCrossOneTile = 1;
|
||||
|
||||
@Getter
|
||||
private Movement movement;
|
||||
|
||||
protected int framesToCrossOneTile = 1;
|
||||
|
||||
public Vector2ic getCoordinates() {
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMoving() {
|
||||
return movement != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpeed(float speed) {
|
||||
framesToCrossOneTile = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
super.update(dt);
|
||||
@@ -61,6 +68,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
setCoordinates(movement.getTo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return new DefaultMovement(this, direction);
|
||||
}
|
||||
@@ -87,21 +95,21 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
public void setCoordinates(int x, int y) {
|
||||
coordinates.x = x;
|
||||
coordinates.y = y;
|
||||
super.setPosition((x + 0.5f) * coordinateStepSize.x, (y + 0.5f) * coordinateStepSize.y);
|
||||
super.setPosition((x + 0.5f) * coordinateStepSize.x + positionOffset.x, (y + 0.5f) * coordinateStepSize.y + positionOffset.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(float x, float y) {
|
||||
super.setPosition(x, y);
|
||||
coordinates.x = (int) (x / coordinateStepSize.x);
|
||||
coordinates.y = (int) (y / coordinateStepSize.y);
|
||||
coordinates.x = (int) ((x - positionOffset.x) / coordinateStepSize.x);
|
||||
coordinates.y = (int) ((y - positionOffset.y) / coordinateStepSize.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector2fc position) {
|
||||
super.setPosition(position);
|
||||
coordinates.x = (int) (position.x() / coordinateStepSize.x);
|
||||
coordinates.y = (int) (position.y() / coordinateStepSize.y);
|
||||
coordinates.x = (int) ((position.x() - positionOffset.x) / coordinateStepSize.x);
|
||||
coordinates.y = (int) ((position.y() - positionOffset.y) / coordinateStepSize.y);
|
||||
}
|
||||
|
||||
public void setCoordinates(Vector2ic coordinates) {
|
||||
@@ -119,6 +127,18 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(Vector2fc offset) {
|
||||
this.positionOffset.x = offset.x();
|
||||
this.positionOffset.y = offset.y();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(float offsetX, float offsetY) {
|
||||
this.positionOffset.x = offsetX;
|
||||
this.positionOffset.y = offsetY;
|
||||
}
|
||||
|
||||
public int chebyshevDistance(Vector2ic coordinates) {
|
||||
return max(abs(this.coordinates.x - coordinates.x()), abs(this.coordinates.y - coordinates.y()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user