Enable queueing entity instant animations
This commit is contained in:
@@ -35,5 +35,7 @@ public interface Entity extends Movable, Animated, Renderable, Updatable {
|
|||||||
|
|
||||||
void setZIndex(int zIndex);
|
void setZIndex(int zIndex);
|
||||||
|
|
||||||
|
void performInstantAnimation(Direction targetFaceDirection);
|
||||||
|
|
||||||
void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish);
|
void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -265,6 +265,11 @@ public abstract class EntityDelegate implements Entity {
|
|||||||
entity.setZIndex(zIndex);
|
entity.setZIndex(zIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performInstantAnimation(Direction targetFaceDirection) {
|
||||||
|
entity.performInstantAnimation(targetFaceDirection);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish) {
|
public void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish) {
|
||||||
entity.performInstantAnimation(targetFaceDirection, onFinish);
|
entity.performInstantAnimation(targetFaceDirection, onFinish);
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
package com.bartlomiejpluta.base.engine.world.animation.model;
|
package com.bartlomiejpluta.base.engine.world.animation.model;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.animation.Animated;
|
import com.bartlomiejpluta.base.api.animation.Animated;
|
||||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
|
||||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
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.core.gl.object.mesh.Mesh;
|
||||||
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
||||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
|
||||||
import com.bartlomiejpluta.base.util.math.MathUtil;
|
import com.bartlomiejpluta.base.util.math.MathUtil;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import org.joml.Vector2fc;
|
import org.joml.Vector2fc;
|
||||||
@@ -39,21 +36,14 @@ public abstract class AnimatedSprite extends Sprite implements Animated {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
time = shouldAnimate() ? time + ((int) (dt * 1000)) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
|
||||||
animate();
|
|
||||||
super.render(screen, camera, shaderManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void animate() {
|
|
||||||
if (shouldAnimate()) {
|
if (shouldAnimate()) {
|
||||||
|
time += dt * 1000;
|
||||||
var positions = getSpriteAnimationFramesPositions();
|
var positions = getSpriteAnimationFramesPositions();
|
||||||
currentAnimationFrame = ((time % (positions.length * intervalInMilliseconds)) / intervalInMilliseconds);
|
currentAnimationFrame = ((time % (positions.length * intervalInMilliseconds)) / intervalInMilliseconds);
|
||||||
var current = positions[currentAnimationFrame];
|
var current = positions[currentAnimationFrame];
|
||||||
material.setSpritePosition(current);
|
material.setSpritePosition(current);
|
||||||
|
} else {
|
||||||
|
time = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ import org.joml.Vector2f;
|
|||||||
import org.joml.Vector2fc;
|
import org.joml.Vector2fc;
|
||||||
import org.joml.Vector2i;
|
import org.joml.Vector2i;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
@@ -46,7 +48,7 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
|
|
||||||
private boolean animationEnabled = true;
|
private boolean animationEnabled = true;
|
||||||
|
|
||||||
private EntityInstantAnimation instantAnimation;
|
private final Queue<EntityInstantAnimation> instantAnimations = new LinkedList<>();
|
||||||
|
|
||||||
public DefaultEntity(Mesh mesh, EntitySetManager entitySetManager, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows, String entitySetUid) {
|
public DefaultEntity(Mesh mesh, EntitySetManager entitySetManager, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows, String entitySetUid) {
|
||||||
super(mesh, entitySetManager.loadObject(requireNonNull(entitySetUid)));
|
super(mesh, entitySetManager.loadObject(requireNonNull(entitySetUid)));
|
||||||
@@ -93,7 +95,7 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean shouldAnimate() {
|
protected boolean shouldAnimate() {
|
||||||
return animationEnabled && (isMoving() || instantAnimation != null);
|
return animationEnabled && (isMoving() || !instantAnimations.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -114,9 +116,14 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
material.setSpritePosition(spriteDefaultRows.get(faceDirection));
|
material.setSpritePosition(spriteDefaultRows.get(faceDirection));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performInstantAnimation(Direction targetFaceDirection) {
|
||||||
|
instantAnimations.add(new EntityInstantAnimation(this, targetFaceDirection, null));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish) {
|
public void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish) {
|
||||||
instantAnimation = new EntityInstantAnimation(this, targetFaceDirection, onFinish);
|
instantAnimations.add(new EntityInstantAnimation(this, targetFaceDirection, onFinish));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -203,11 +210,12 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||||
super.render(screen, camera, shaderManager);
|
var instantAnimation = instantAnimations.peek();
|
||||||
|
|
||||||
if (instantAnimation != null && instantAnimation.updateFrame()) {
|
if (instantAnimation != null && instantAnimation.updateFrame()) {
|
||||||
instantAnimation = null;
|
instantAnimations.poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
super.render(screen, camera, shaderManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
int currentFrame() {
|
int currentFrame() {
|
||||||
|
|||||||
Reference in New Issue
Block a user