From 14906b7e379c78188ab5a3740aade93390aa1ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Sun, 31 Jan 2021 18:06:18 +0100 Subject: [PATCH] Enable resetting default frame when movement is finished --- .../world/animation/AnimationableObject.java | 6 ++ .../core/world/animation/DefaultAnimator.java | 3 +- ...MoveableObject.java => MovableObject.java} | 41 +++++++------ .../base/game/world/movement/Movement.java | 58 +------------------ 4 files changed, 30 insertions(+), 78 deletions(-) rename game/src/main/java/com/bartlomiejpluta/base/game/world/movement/{MoveableObject.java => MovableObject.java} (71%) diff --git a/engine/src/main/java/com/bartlomiejpluta/base/core/world/animation/AnimationableObject.java b/engine/src/main/java/com/bartlomiejpluta/base/core/world/animation/AnimationableObject.java index 15977a56..cdde8802 100755 --- a/engine/src/main/java/com/bartlomiejpluta/base/core/world/animation/AnimationableObject.java +++ b/engine/src/main/java/com/bartlomiejpluta/base/core/world/animation/AnimationableObject.java @@ -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); + } } diff --git a/engine/src/main/java/com/bartlomiejpluta/base/core/world/animation/DefaultAnimator.java b/engine/src/main/java/com/bartlomiejpluta/base/core/world/animation/DefaultAnimator.java index 238a9598..84600aaf 100755 --- a/engine/src/main/java/com/bartlomiejpluta/base/core/world/animation/DefaultAnimator.java +++ b/engine/src/main/java/com/bartlomiejpluta/base/core/world/animation/DefaultAnimator.java @@ -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); } } } diff --git a/game/src/main/java/com/bartlomiejpluta/base/game/world/movement/MoveableObject.java b/game/src/main/java/com/bartlomiejpluta/base/game/world/movement/MovableObject.java similarity index 71% rename from game/src/main/java/com/bartlomiejpluta/base/game/world/movement/MoveableObject.java rename to game/src/main/java/com/bartlomiejpluta/base/game/world/movement/MovableObject.java index 0c45865d..a6a40ed2 100755 --- a/game/src/main/java/com/bartlomiejpluta/base/game/world/movement/MoveableObject.java +++ b/game/src/main/java/com/bartlomiejpluta/base/game/world/movement/MovableObject.java @@ -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 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); diff --git a/game/src/main/java/com/bartlomiejpluta/base/game/world/movement/Movement.java b/game/src/main/java/com/bartlomiejpluta/base/game/world/movement/Movement.java index 9b2d416a..b60c0543 100755 --- a/game/src/main/java/com/bartlomiejpluta/base/game/world/movement/Movement.java +++ b/game/src/main/java/com/bartlomiejpluta/base/game/world/movement/Movement.java @@ -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); -//// } }