Make Animations movable

This commit is contained in:
2021-03-21 00:35:58 +01:00
parent 5c2e33eedc
commit b3a5484b03
4 changed files with 80 additions and 2 deletions

View File

@@ -1,7 +1,11 @@
package com.bartlomiejpluta.base.engine.world.animation.model;
import com.bartlomiejpluta.base.api.game.animation.Animation;
import com.bartlomiejpluta.base.api.game.map.layer.base.Layer;
import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
import com.bartlomiejpluta.base.api.util.math.MathUtil;
import com.bartlomiejpluta.base.api.util.path.Path;
import com.bartlomiejpluta.base.api.util.path.PathExecutor;
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.movement.MovableSprite;
@@ -22,6 +26,11 @@ public class DefaultAnimation extends MovableSprite implements Animation {
@Setter
private Integer repeat = 1;
@Getter
private PathExecutor<Animation> pathExecutor;
private Layer layer;
private boolean isObjectLayer = false;
public DefaultAnimation(Mesh mesh, Material material, @NonNull Vector2fc[] frames) {
super(mesh, material);
this.frames = frames;
@@ -53,6 +62,31 @@ public class DefaultAnimation extends MovableSprite implements Animation {
// do nothing
}
@Override
public void followPath(Path<Animation> path, boolean repeat) {
pathExecutor = new PathExecutor<>(this, repeat, path);
}
@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);
if (pathExecutor != null && isObjectLayer) {
pathExecutor.execute((ObjectLayer) layer, dt);
}
}
@Override
public void onAdd(Layer layer) {
this.layer = layer;
this.isObjectLayer = layer instanceof ObjectLayer;
}
@Override
public boolean finished() {
if (repeat == null) {

View File

@@ -21,7 +21,7 @@ public abstract class BaseLayer implements Layer, Updatable {
@NonNull
protected final Vector2fc stepSize;
private final Queue<Animation> animations = new LinkedList<>();
protected final Queue<Animation> animations = new LinkedList<>();
public BaseLayer(@NonNull GameMap map) {
this.map = map;
@@ -32,6 +32,7 @@ public abstract class BaseLayer implements Layer, Updatable {
public void pushAnimation(Animation animation) {
animation.setStepSize(stepSize.x(), stepSize.y());
animations.add(animation);
animation.onAdd(this);
}
@Override