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