Improve movement code
This commit is contained in:
@@ -1,5 +0,0 @@
|
|||||||
package com.bartlomiejpluta.base.game.world;
|
|
||||||
|
|
||||||
public enum Direction {
|
|
||||||
UP, DOWN, LEFT, RIGHT
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.world.movement;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.joml.Vector2f;
|
||||||
|
import org.joml.Vector2i;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public enum Direction {
|
||||||
|
UP(new Vector2i(0, -1)),
|
||||||
|
DOWN(new Vector2i(0, 1)),
|
||||||
|
LEFT(new Vector2i(-1, 0)),
|
||||||
|
RIGHT(new Vector2i(1, 0));
|
||||||
|
|
||||||
|
private final Vector2i vector;
|
||||||
|
|
||||||
|
public Vector2i asIntVector() {
|
||||||
|
return new Vector2i(vector);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector2f asFloatVector() {
|
||||||
|
return new Vector2f(vector);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.bartlomiejpluta.base.game.world;
|
package com.bartlomiejpluta.base.game.world.movement;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||||
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
||||||
@@ -13,14 +13,12 @@ import org.joml.Vector2i;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public abstract class MoveableObject extends AnimationableObject implements Updatable {
|
public abstract class MoveableObject extends AnimationableObject implements Updatable {
|
||||||
private static final Vector2i SPRITE_DIMENSION = new Vector2i(4, 4);
|
private static final Vector2i SPRITE_DIMENSION = new Vector2i(4, 4);
|
||||||
private static final float tolerance = 1f;
|
|
||||||
|
|
||||||
private final Vector2i coordinates = new Vector2i(0, 0);
|
|
||||||
private final Vector2f coordinateStepSize;
|
private final Vector2f coordinateStepSize;
|
||||||
private Vector2i targetCoordinates;
|
private Movement movement;
|
||||||
private Vector2f targetPosition;
|
|
||||||
private Vector2f targetOffset;
|
@Getter
|
||||||
private Vector2f dS;
|
private final Vector2i coordinates = new Vector2i(0, 0);
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@@ -28,56 +26,45 @@ public abstract class MoveableObject extends AnimationableObject implements Upda
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private Direction movementDirection = Direction.DOWN;
|
private int slowness;
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
@Setter
|
||||||
private float speed;
|
private int animationSpeed;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAnimationSpeed() {
|
public int getAnimationSpeed() {
|
||||||
return 100;
|
return animationSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldAnimate() {
|
public boolean shouldAnimate() {
|
||||||
return targetCoordinates != null;
|
return movement != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
if(targetCoordinates != null) {
|
if(movement != null) {
|
||||||
|
var dS = movement.getMovementVector();
|
||||||
|
if(dS != null) {
|
||||||
movePosition(dS);
|
movePosition(dS);
|
||||||
var distance = new Vector2f(dS).add(getPosition()).distance(targetPosition);
|
} else {
|
||||||
|
adjustCoordinates();
|
||||||
log.info("Target: [{}, {}], Current: [{}, {}], distance: {}, dS: [{}, {}]", targetPosition.x, targetPosition.y, getPosition().x, getPosition().y, distance, dS.x, dS.y);
|
movement = null;
|
||||||
|
|
||||||
if(distance < tolerance) {
|
|
||||||
setCoordinates(new Vector2i(coordinates).add(targetCoordinates));
|
|
||||||
targetCoordinates = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void adjustCoordinates() {
|
||||||
|
var position = new Vector2f(getPosition());
|
||||||
|
setCoordinates(new Vector2i((int) (position.x / coordinateStepSize.x), (int) (position.y / coordinateStepSize.y)));
|
||||||
|
}
|
||||||
|
|
||||||
public void move(Direction direction) {
|
public void move(Direction direction) {
|
||||||
if(this.targetCoordinates != null) {
|
if(this.movement != null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setFaceDirection(direction);
|
setFaceDirection(direction);
|
||||||
targetCoordinates = switch (direction) {
|
this.movement = new Movement(direction, coordinateStepSize, 50);
|
||||||
case UP -> new Vector2i(0, -1);
|
|
||||||
case DOWN -> new Vector2i(0, 1);
|
|
||||||
case LEFT -> new Vector2i(-1, 0);
|
|
||||||
case RIGHT -> new Vector2i(1, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
targetOffset = new Vector2f(targetCoordinates).mul(coordinateStepSize);
|
|
||||||
targetPosition = new Vector2f(getPosition()).add(targetOffset);
|
|
||||||
dS = new Vector2f(targetOffset).mul(speed);
|
|
||||||
log.info("TargetCoord: [{}, {}], targetOffset: [{}, {}], dS: [{}, {}], targetPos: [{}, {}]", targetCoordinates.x, targetCoordinates.y, targetOffset.x, targetOffset.y, dS.x, dS.y, targetPosition.x, targetPosition.y);
|
|
||||||
|
|
||||||
this.movementDirection = direction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MoveableObject setCoordinates(int x, int y) {
|
public MoveableObject setCoordinates(int x, int y) {
|
||||||
@@ -91,17 +78,6 @@ public abstract class MoveableObject extends AnimationableObject implements Upda
|
|||||||
return setCoordinates(coordinates.x, coordinates.y);
|
return setCoordinates(coordinates.x, coordinates.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MoveableObject moveCoordinates(int x, int y) {
|
|
||||||
coordinates.x += x;
|
|
||||||
coordinates.y += y;
|
|
||||||
movePosition(x * coordinateStepSize.x, y * coordinateStepSize.y);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MoveableObject moveCoordinates(Vector2i coordinates) {
|
|
||||||
return moveCoordinates(coordinates.x, coordinates.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vector2i getSpriteSheetDimensions() {
|
public Vector2i getSpriteSheetDimensions() {
|
||||||
return SPRITE_DIMENSION;
|
return SPRITE_DIMENSION;
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
public Vector2f getMovementVector() {
|
||||||
|
if(moveTime > 0) {
|
||||||
|
--moveTime;
|
||||||
|
|
||||||
|
return new Vector2f(dS);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Movement(Direction direction, Vector2f tileSize, int framesToCrossOneTile) {
|
||||||
|
var speed = new Vector2f(tileSize).div(framesToCrossOneTile);
|
||||||
|
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