Simplify EntityInstantAnimation
This commit is contained in:
@@ -38,6 +38,4 @@ public interface Entity extends Movable, Animated, Renderable, Updatable {
|
|||||||
void setZIndex(int zIndex);
|
void setZIndex(int zIndex);
|
||||||
|
|
||||||
CompletableFuture<Void> performInstantAnimation();
|
CompletableFuture<Void> performInstantAnimation();
|
||||||
|
|
||||||
CompletableFuture<Void> performInstantAnimation(Direction targetFaceDirection);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -272,11 +272,6 @@ public abstract class EntityDelegate implements Entity {
|
|||||||
return entity.performInstantAnimation();
|
return entity.performInstantAnimation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletableFuture<Void> performInstantAnimation(Direction targetFaceDirection) {
|
|
||||||
return entity.performInstantAnimation(targetFaceDirection);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean move(Movement movement) {
|
public boolean move(Movement movement) {
|
||||||
return entity.move(movement);
|
return entity.move(movement);
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
package com.bartlomiejpluta.base.engine.world.entity.model;
|
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.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
import com.bartlomiejpluta.base.api.move.EntityMovement;
|
import com.bartlomiejpluta.base.api.move.EntityMovement;
|
||||||
import com.bartlomiejpluta.base.api.move.Movement;
|
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.core.gl.object.mesh.Mesh;
|
||||||
import com.bartlomiejpluta.base.engine.world.entity.manager.EntitySetManager;
|
import com.bartlomiejpluta.base.engine.world.entity.manager.EntitySetManager;
|
||||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@@ -127,14 +124,6 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
return animation.getFuture();
|
return animation.getFuture();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletableFuture<Void> performInstantAnimation(Direction targetFaceDirection) {
|
|
||||||
var animation = new EntityInstantAnimation(this, defaultSpriteColumn, targetFaceDirection);
|
|
||||||
instantAnimations.add(animation);
|
|
||||||
|
|
||||||
return animation.getFuture();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Movement prepareMovement(Direction direction) {
|
public Movement prepareMovement(Direction direction) {
|
||||||
return new EntityMovement(this, direction);
|
return new EntityMovement(this, direction);
|
||||||
@@ -218,13 +207,13 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
public void update(float dt) {
|
||||||
|
super.update(dt);
|
||||||
|
|
||||||
var instantAnimation = instantAnimations.peek();
|
var instantAnimation = instantAnimations.peek();
|
||||||
if (instantAnimation != null && instantAnimation.updateFrame()) {
|
if (instantAnimation != null && instantAnimation.update() == EntityInstantAnimation.State.COMPLETED) {
|
||||||
instantAnimations.poll();
|
instantAnimations.poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
super.render(screen, camera, shaderManager);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int currentFrame() {
|
int currentFrame() {
|
||||||
|
|||||||
@@ -1,48 +1,39 @@
|
|||||||
package com.bartlomiejpluta.base.engine.world.entity.model;
|
package com.bartlomiejpluta.base.engine.world.entity.model;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class EntityInstantAnimation {
|
public class EntityInstantAnimation {
|
||||||
|
public enum State {RUNNING, COMPLETED}
|
||||||
|
|
||||||
private final DefaultEntity entity;
|
private final DefaultEntity entity;
|
||||||
private final int firstFrame;
|
private final int firstFrame;
|
||||||
private final int lastFrame;
|
private final int lastFrame;
|
||||||
private final Direction faceDirectionOnFinish;
|
|
||||||
private boolean finished = false;
|
private boolean finished = false;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final CompletableFuture<Void> future = new CompletableFuture<>();
|
private final CompletableFuture<Void> future = new CompletableFuture<>();
|
||||||
|
|
||||||
EntityInstantAnimation(DefaultEntity entity, int firstFrame) {
|
EntityInstantAnimation(DefaultEntity entity, int firstFrame) {
|
||||||
this(entity, firstFrame, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityInstantAnimation(DefaultEntity entity, int firstFrame, Direction faceDirectionOnFinish) {
|
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
this.firstFrame = firstFrame;
|
this.firstFrame = firstFrame;
|
||||||
this.lastFrame = entity.getMaterial().getTexture().getColumns() - 1;
|
this.lastFrame = entity.getMaterial().getTexture().getColumns() - 1;
|
||||||
this.faceDirectionOnFinish = faceDirectionOnFinish;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean updateFrame() {
|
public State update() {
|
||||||
if (!finished && entity.currentFrame() == lastFrame) {
|
if (!finished && entity.currentFrame() == lastFrame) {
|
||||||
finished = true;
|
finished = true;
|
||||||
return false;
|
return State.RUNNING;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (finished && entity.currentFrame() == firstFrame) {
|
if (finished && entity.currentFrame() == firstFrame) {
|
||||||
if (faceDirectionOnFinish != null) {
|
|
||||||
entity.setFaceDirection(faceDirectionOnFinish);
|
|
||||||
}
|
|
||||||
|
|
||||||
future.complete(null);
|
future.complete(null);
|
||||||
|
|
||||||
return true;
|
return State.COMPLETED;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return State.RUNNING;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user