Merge Movement object to MovableObject class

This commit is contained in:
2021-01-31 19:00:19 +01:00
parent 14906b7e37
commit 3c7143ad20
2 changed files with 15 additions and 34 deletions

View File

@@ -23,7 +23,10 @@ public abstract class MovableObject extends AnimationableObject implements Updat
); );
private final Vector2f coordinateStepSize; private final Vector2f coordinateStepSize;
private Movement movement;
private int moveTime = 0;
private Vector2f movementVector;
@Getter @Getter
private final Vector2i coordinates = new Vector2i(0, 0); private final Vector2i coordinates = new Vector2i(0, 0);
@@ -46,18 +49,18 @@ public abstract class MovableObject extends AnimationableObject implements Updat
@Override @Override
public boolean shouldAnimate() { public boolean shouldAnimate() {
return movement != null; return movementVector != null;
} }
@Override @Override
public void update(float dt) { public void update(float dt) {
if (movement != null) { if(movementVector != null) {
var dS = movement.getMovementVector(); if(moveTime > 0) {
if (dS != null) { --moveTime;
movePosition(dS); movePosition(movementVector);
} else { } else {
adjustCoordinates(); adjustCoordinates();
movement = null; movementVector = null;
setAnimationFrame(new Vector2f(DEFAULT_SPRITE, SPRITE_ROWS.get(faceDirection))); setAnimationFrame(new Vector2f(DEFAULT_SPRITE, SPRITE_ROWS.get(faceDirection)));
} }
} }
@@ -69,11 +72,14 @@ public abstract class MovableObject extends AnimationableObject implements Updat
} }
public void move(Direction direction) { public void move(Direction direction) {
if (this.movement != null) { if (this.movementVector != null) {
return; return;
} }
setFaceDirection(direction); setFaceDirection(direction);
this.movement = new Movement(direction, coordinateStepSize, framesToCrossOneTile); var speed = new Vector2f(coordinateStepSize).div(framesToCrossOneTile);
movementVector = direction.asFloatVector().mul(speed);
moveTime = framesToCrossOneTile;
} }
public MovableObject setCoordinates(int x, int y) { public MovableObject setCoordinates(int x, int y) {

View File

@@ -1,25 +0,0 @@
package com.bartlomiejpluta.base.game.world.movement;
import org.joml.Vector2f;
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;
}
}