Add support for entity instant animations
This commit is contained in:
@@ -39,7 +39,7 @@ public abstract class AnimatedSprite extends Sprite implements Animated {
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
time += dt * 1000;
|
||||
time = shouldAnimate() ? time + ((int) (dt * 1000)) : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
package com.bartlomiejpluta.base.engine.world.entity.model;
|
||||
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.EntityMovement;
|
||||
import com.bartlomiejpluta.base.api.move.Movement;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.world.entity.manager.EntitySetManager;
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -43,6 +46,8 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
|
||||
private boolean animationEnabled = true;
|
||||
|
||||
private EntityInstantAnimation instantAnimation;
|
||||
|
||||
public DefaultEntity(Mesh mesh, EntitySetManager entitySetManager, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows, String entitySetUid) {
|
||||
super(mesh, entitySetManager.loadObject(requireNonNull(entitySetUid)));
|
||||
this.entitySetManager = entitySetManager;
|
||||
@@ -88,7 +93,7 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
|
||||
@Override
|
||||
protected boolean shouldAnimate() {
|
||||
return animationEnabled && isMoving();
|
||||
return animationEnabled && (isMoving() || instantAnimation != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,6 +114,11 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
material.setSpritePosition(spriteDefaultRows.get(faceDirection));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish) {
|
||||
instantAnimation = new EntityInstantAnimation(this, targetFaceDirection, onFinish);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return new EntityMovement(this, direction);
|
||||
@@ -190,4 +200,17 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
public float getScaleY() {
|
||||
return entityScale.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
super.render(screen, camera, shaderManager);
|
||||
|
||||
if (instantAnimation != null && instantAnimation.updateFrame()) {
|
||||
instantAnimation = null;
|
||||
}
|
||||
}
|
||||
|
||||
int currentFrame() {
|
||||
return currentAnimationFrame;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.bartlomiejpluta.base.engine.world.entity.model;
|
||||
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
|
||||
public class EntityInstantAnimation {
|
||||
private static final int FIRST_FRAME = 0;
|
||||
|
||||
private final DefaultEntity entity;
|
||||
private final int lastFrame;
|
||||
private final Direction faceDirectionOnFinish;
|
||||
private final Runnable onFinish;
|
||||
private boolean finished = false;
|
||||
|
||||
EntityInstantAnimation(DefaultEntity entity, Direction faceDirectionOnFinish, Runnable onFinish) {
|
||||
this.entity = entity;
|
||||
this.lastFrame = entity.getMaterial().getTexture().getColumns() - 1;
|
||||
this.faceDirectionOnFinish = faceDirectionOnFinish;
|
||||
this.onFinish = onFinish;
|
||||
}
|
||||
|
||||
public boolean updateFrame() {
|
||||
if (!finished && entity.currentFrame() == lastFrame) {
|
||||
finished = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (finished && entity.currentFrame() == FIRST_FRAME) {
|
||||
if (faceDirectionOnFinish != null) {
|
||||
entity.setFaceDirection(faceDirectionOnFinish);
|
||||
}
|
||||
|
||||
if (onFinish != null) {
|
||||
onFinish.run();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user