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