Enable resetting default frame when movement is finished
This commit is contained in:
@@ -22,4 +22,10 @@ public abstract class AnimationableObject extends RenderableObject {
|
||||
public abstract boolean shouldAnimate();
|
||||
|
||||
public abstract Vector2f[] getSpriteAnimationFramesPositions();
|
||||
|
||||
protected void setAnimationFrame(Vector2f framePosition) {
|
||||
var material = this.getMaterial();
|
||||
var spriteSize = material.getSpriteSize();
|
||||
material.setSpritePosition(spriteSize.x * framePosition.x, spriteSize.y * framePosition.y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,8 @@ public class DefaultAnimator implements Animator {
|
||||
var positions = object.getSpriteAnimationFramesPositions();
|
||||
var delay = object.getAnimationSpeed();
|
||||
var currentPosition = (int) (System.currentTimeMillis() % (positions.length * delay)) / delay;
|
||||
var spriteSize = object.getMaterial().getSpriteSize();
|
||||
var current = positions[currentPosition];
|
||||
object.getMaterial().setSpritePosition(spriteSize.x * current.x, spriteSize.y * current.y);
|
||||
object.setAnimationFrame(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,21 @@ import com.bartlomiejpluta.base.core.world.animation.AnimationableObject;
|
||||
import com.bartlomiejpluta.base.game.logic.Updatable;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
@Slf4j
|
||||
public abstract class MoveableObject extends AnimationableObject implements Updatable {
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public abstract class MovableObject extends AnimationableObject implements Updatable {
|
||||
private static final Vector2i SPRITE_DIMENSION = new Vector2i(4, 4);
|
||||
private static final int DEFAULT_SPRITE = 0;
|
||||
private static final Map<Direction, Integer> SPRITE_ROWS = Map.of(
|
||||
Direction.DOWN, 0,
|
||||
Direction.LEFT, 1,
|
||||
Direction.RIGHT, 2,
|
||||
Direction.UP, 3
|
||||
);
|
||||
|
||||
private final Vector2f coordinateStepSize;
|
||||
private Movement movement;
|
||||
@@ -26,10 +34,10 @@ public abstract class MoveableObject extends AnimationableObject implements Upda
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private int slowness;
|
||||
private int framesToCrossOneTile = 1;
|
||||
|
||||
@Setter
|
||||
private int animationSpeed;
|
||||
private int animationSpeed = 100;
|
||||
|
||||
@Override
|
||||
public int getAnimationSpeed() {
|
||||
@@ -43,13 +51,14 @@ public abstract class MoveableObject extends AnimationableObject implements Upda
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
if(movement != null) {
|
||||
if (movement != null) {
|
||||
var dS = movement.getMovementVector();
|
||||
if(dS != null) {
|
||||
if (dS != null) {
|
||||
movePosition(dS);
|
||||
} else {
|
||||
adjustCoordinates();
|
||||
movement = null;
|
||||
setAnimationFrame(new Vector2f(DEFAULT_SPRITE, SPRITE_ROWS.get(faceDirection)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,21 +69,21 @@ public abstract class MoveableObject extends AnimationableObject implements Upda
|
||||
}
|
||||
|
||||
public void move(Direction direction) {
|
||||
if(this.movement != null) {
|
||||
if (this.movement != null) {
|
||||
return;
|
||||
}
|
||||
setFaceDirection(direction);
|
||||
this.movement = new Movement(direction, coordinateStepSize, 50);
|
||||
this.movement = new Movement(direction, coordinateStepSize, framesToCrossOneTile);
|
||||
}
|
||||
|
||||
public MoveableObject setCoordinates(int x, int y) {
|
||||
public MovableObject setCoordinates(int x, int y) {
|
||||
coordinates.x = x;
|
||||
coordinates.y = y;
|
||||
setPosition((x + 0.5f) * coordinateStepSize.x, (y + 0.5f) * coordinateStepSize.y);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MoveableObject setCoordinates(Vector2i coordinates) {
|
||||
public MovableObject setCoordinates(Vector2i coordinates) {
|
||||
return setCoordinates(coordinates.x, coordinates.y);
|
||||
}
|
||||
|
||||
@@ -85,17 +94,11 @@ public abstract class MoveableObject extends AnimationableObject implements Upda
|
||||
|
||||
@Override
|
||||
public Vector2f[] getSpriteAnimationFramesPositions() {
|
||||
var row = switch (faceDirection) {
|
||||
case DOWN -> 0;
|
||||
case LEFT -> 1;
|
||||
case RIGHT -> 2;
|
||||
case UP -> 3;
|
||||
};
|
||||
|
||||
var row = SPRITE_ROWS.get(faceDirection);
|
||||
return new Vector2f[]{new Vector2f(0, row), new Vector2f(1, row), new Vector2f(2, row), new Vector2f(3, row)};
|
||||
}
|
||||
|
||||
public MoveableObject(Material material, Vector2f coordinateStepSize, float scale) {
|
||||
public MovableObject(Material material, Vector2f coordinateStepSize, float scale) {
|
||||
super(buildMesh(material), material);
|
||||
this.coordinateStepSize = coordinateStepSize;
|
||||
this.setScale(scale);
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.bartlomiejpluta.base.game.world.movement;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.joml.Vector2f;
|
||||
|
||||
@Slf4j
|
||||
|
||||
public class Movement {
|
||||
private final Vector2f dS;
|
||||
private int moveTime;
|
||||
@@ -23,59 +22,4 @@ public class Movement {
|
||||
dS = direction.asFloatVector().mul(speed);
|
||||
moveTime = framesToCrossOneTile;
|
||||
}
|
||||
|
||||
// public Movement(Direction direction, float tileWidth) {
|
||||
// var speed = tileWidth / (float) framesToCrossOneTile;
|
||||
//
|
||||
// var offsetCoordinates = switch (direction) {
|
||||
// case UP -> new Vector2f(0, -1);
|
||||
// case DOWN -> new Vector2f(0, 1);
|
||||
// case LEFT -> new Vector2f(-1, 0);
|
||||
// case RIGHT -> new Vector2f(1, 0);
|
||||
// };
|
||||
//
|
||||
// dS = offsetCoordinates.mul(speed);
|
||||
//
|
||||
// moveTime = framesToCrossOneTile;
|
||||
// }
|
||||
|
||||
|
||||
// private static final int framesToCrossOneTile = 28;
|
||||
//
|
||||
// @Getter
|
||||
// private final Vector2i targetCoordinates;
|
||||
//
|
||||
// private final Vector2f targetPosition;
|
||||
// private final Vector2f dS;
|
||||
// private int moveTime;
|
||||
//
|
||||
// public Vector2f getNextFrameOffset(Vector2f currentPosition, float tolerance) {
|
||||
// if(moveTime > 0) {
|
||||
// moveTime--;
|
||||
//// log.info("dS: [{}, {}]", dS.x, dS.y);
|
||||
// return new Vector2f(dS);
|
||||
// }
|
||||
//
|
||||
// return null;
|
||||
//// var distance = new Vector2f(dS).add(currentPosition).distance(targetPosition);
|
||||
//
|
||||
//// return (distance > 0) ? new Vector2f(dS) : null;
|
||||
// }
|
||||
//
|
||||
//// public Movement(Direction direction, float speed, Vector2i currentCoordinates, Vector2f currentPosition, Vector2f coordinateStepSize) {
|
||||
//// var offsetCoordinates = switch (direction) {
|
||||
//// case UP -> new Vector2i(0, -1);
|
||||
//// case DOWN -> new Vector2i(0, 1);
|
||||
//// case LEFT -> new Vector2i(-1, 0);
|
||||
//// case RIGHT -> new Vector2i(1, 0);
|
||||
//// };
|
||||
////
|
||||
//// moveTime = framesToCrossOneTile;
|
||||
//// var _speed = coordinateStepSize.x / moveTime;
|
||||
//// targetCoordinates = new Vector2i(currentCoordinates).add(offsetCoordinates);
|
||||
//// var offsetPosition = new Vector2f(offsetCoordinates).mul(coordinateStepSize);
|
||||
//// targetPosition = new Vector2f(currentPosition).add(offsetPosition);
|
||||
//// dS = new Vector2f(offsetCoordinates).mul(_speed);
|
||||
//// log.info("{}", _speed);
|
||||
//// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user