Refactor EntityInstantAnimation
This commit is contained in:
@@ -35,7 +35,5 @@ 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,11 +265,6 @@ 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);
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ public class DefaultEntityManager implements EntityManager {
|
|||||||
private final MeshManager meshManager;
|
private final MeshManager meshManager;
|
||||||
private final EntitySetManager entitySetManager;
|
private final EntitySetManager entitySetManager;
|
||||||
|
|
||||||
|
private final int defaultSpriteColumn;
|
||||||
private final Map<Direction, Integer> spriteDirectionRows;
|
private final Map<Direction, Integer> spriteDirectionRows;
|
||||||
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
||||||
|
|
||||||
@@ -35,7 +36,7 @@ public class DefaultEntityManager implements EntityManager {
|
|||||||
|
|
||||||
this.spriteDirectionRows = configuration.getSpriteDirectionRows();
|
this.spriteDirectionRows = configuration.getSpriteDirectionRows();
|
||||||
|
|
||||||
var defaultSpriteColumn = configuration.getDefaultSpriteColumn();
|
defaultSpriteColumn = configuration.getDefaultSpriteColumn();
|
||||||
this.spriteDefaultRows = spriteDirectionRows
|
this.spriteDefaultRows = spriteDirectionRows
|
||||||
.entrySet()
|
.entrySet()
|
||||||
.stream()
|
.stream()
|
||||||
@@ -49,7 +50,7 @@ public class DefaultEntityManager implements EntityManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Entity createEntity(String entitySetUid) {
|
public Entity createEntity(String entitySetUid) {
|
||||||
return new DefaultEntity(mesh, entitySetManager, spriteDirectionRows, spriteDefaultRows, entitySetUid);
|
return new DefaultEntity(mesh, entitySetManager, defaultSpriteColumn, spriteDirectionRows, spriteDefaultRows, entitySetUid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import static java.util.Objects.requireNonNull;
|
|||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class DefaultEntity extends MovableSprite implements Entity {
|
public class DefaultEntity extends MovableSprite implements Entity {
|
||||||
|
private final int defaultSpriteColumn;
|
||||||
private final EntitySetManager entitySetManager;
|
private final EntitySetManager entitySetManager;
|
||||||
private final Map<Direction, Integer> spriteDirectionRows;
|
private final Map<Direction, Integer> spriteDirectionRows;
|
||||||
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
||||||
@@ -50,8 +51,9 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
|
|
||||||
private final Queue<EntityInstantAnimation> instantAnimations = new LinkedList<>();
|
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, int defaultSpriteColumn, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows, String entitySetUid) {
|
||||||
super(mesh, entitySetManager.loadObject(requireNonNull(entitySetUid)));
|
super(mesh, entitySetManager.loadObject(requireNonNull(entitySetUid)));
|
||||||
|
this.defaultSpriteColumn = defaultSpriteColumn;
|
||||||
this.entitySetManager = entitySetManager;
|
this.entitySetManager = entitySetManager;
|
||||||
this.spriteDirectionRows = spriteDirectionRows;
|
this.spriteDirectionRows = spriteDirectionRows;
|
||||||
this.faceDirection = Direction.DOWN;
|
this.faceDirection = Direction.DOWN;
|
||||||
@@ -116,14 +118,9 @@ 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) {
|
||||||
instantAnimations.add(new EntityInstantAnimation(this, targetFaceDirection, onFinish));
|
instantAnimations.add(new EntityInstantAnimation(this, defaultSpriteColumn, targetFaceDirection, onFinish));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,16 +3,17 @@ package com.bartlomiejpluta.base.engine.world.entity.model;
|
|||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
|
|
||||||
public class EntityInstantAnimation {
|
public class EntityInstantAnimation {
|
||||||
private static final int FIRST_FRAME = 0;
|
|
||||||
|
|
||||||
private final DefaultEntity entity;
|
private final DefaultEntity entity;
|
||||||
|
private final int firstFrame;
|
||||||
private final int lastFrame;
|
private final int lastFrame;
|
||||||
private final Direction faceDirectionOnFinish;
|
private final Direction faceDirectionOnFinish;
|
||||||
private final Runnable onFinish;
|
private final Runnable onFinish;
|
||||||
private boolean finished = false;
|
private boolean finished = false;
|
||||||
|
|
||||||
EntityInstantAnimation(DefaultEntity entity, Direction faceDirectionOnFinish, Runnable onFinish) {
|
EntityInstantAnimation(DefaultEntity entity, int firstFrame, Direction faceDirectionOnFinish, Runnable onFinish) {
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
|
this.firstFrame = firstFrame;
|
||||||
this.lastFrame = entity.getMaterial().getTexture().getColumns() - 1;
|
this.lastFrame = entity.getMaterial().getTexture().getColumns() - 1;
|
||||||
this.faceDirectionOnFinish = faceDirectionOnFinish;
|
this.faceDirectionOnFinish = faceDirectionOnFinish;
|
||||||
this.onFinish = onFinish;
|
this.onFinish = onFinish;
|
||||||
@@ -24,7 +25,7 @@ public class EntityInstantAnimation {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (finished && entity.currentFrame() == FIRST_FRAME) {
|
if (finished && entity.currentFrame() == firstFrame) {
|
||||||
if (faceDirectionOnFinish != null) {
|
if (faceDirectionOnFinish != null) {
|
||||||
entity.setFaceDirection(faceDirectionOnFinish);
|
entity.setFaceDirection(faceDirectionOnFinish);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user