Make Animations movable
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package com.bartlomiejpluta.base.api.game.animation;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.entity.Movable;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.api.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.api.internal.object.Placeable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.Renderable;
|
||||
import com.bartlomiejpluta.base.api.util.path.Path;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
public interface Animation extends Placeable, Renderable, Updatable {
|
||||
public interface Animation extends Placeable, Movable, Renderable, Updatable {
|
||||
|
||||
void setStepSize(float x, float y);
|
||||
|
||||
@@ -21,5 +24,11 @@ public interface Animation extends Placeable, Renderable, Updatable {
|
||||
|
||||
void setRepeat(Integer repeat);
|
||||
|
||||
void followPath(Path<Animation> path, boolean repeat);
|
||||
|
||||
void setSpeed(float speed);
|
||||
|
||||
void onAdd(Layer layer);
|
||||
|
||||
boolean finished();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.bartlomiejpluta.base.api.game.animation;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Direction;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Movement;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.internal.object.Placeable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import com.bartlomiejpluta.base.api.util.path.Path;
|
||||
import org.joml.Matrix4fc;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
@@ -140,6 +144,36 @@ public abstract class AnimationDelegate implements Animation {
|
||||
return animation.getModelMatrix();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return animation.prepareMovement(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement getMovement() {
|
||||
return animation.getMovement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMoving() {
|
||||
return animation.isMoving();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void followPath(Path<Animation> path, boolean repeat) {
|
||||
animation.followPath(path, repeat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpeed(float speed) {
|
||||
animation.setSpeed(speed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdd(Layer layer) {
|
||||
animation.onAdd(layer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
animation.render(screen, camera, shaderManager);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user