Extract Animated trait interface
This commit is contained in:
@@ -1,30 +1,41 @@
|
||||
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.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
||||
import com.bartlomiejpluta.base.util.math.MathUtil;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class AnimatedSprite extends Sprite implements Updatable {
|
||||
public abstract class AnimatedSprite extends Sprite implements Animated {
|
||||
private int time;
|
||||
|
||||
// The time in ms between frames
|
||||
private int intervalInMilliseconds = 100;
|
||||
protected int currentAnimationFrame;
|
||||
|
||||
public AnimatedSprite(Mesh mesh, Material material) {
|
||||
super(mesh, material);
|
||||
}
|
||||
|
||||
// Returns time in ms between frames
|
||||
public abstract int getAnimationSpeed();
|
||||
protected abstract boolean shouldAnimate();
|
||||
|
||||
public abstract boolean shouldAnimate();
|
||||
protected abstract Vector2fc[] getSpriteAnimationFramesPositions();
|
||||
|
||||
public abstract Vector2fc[] getSpriteAnimationFramesPositions();
|
||||
@Override
|
||||
public void setAnimationSpeed(float speed) {
|
||||
intervalInMilliseconds = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getAnimationSpeed() {
|
||||
return 1 / (float) intervalInMilliseconds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
@@ -40,8 +51,7 @@ public abstract class AnimatedSprite extends Sprite implements Updatable {
|
||||
private void animate() {
|
||||
if (shouldAnimate()) {
|
||||
var positions = getSpriteAnimationFramesPositions();
|
||||
var delay = getAnimationSpeed();
|
||||
currentAnimationFrame = ((time % (positions.length * delay)) / delay);
|
||||
currentAnimationFrame = ((time % (positions.length * intervalInMilliseconds)) / intervalInMilliseconds);
|
||||
var current = positions[currentAnimationFrame];
|
||||
material.setSpritePosition(current);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import com.bartlomiejpluta.base.api.move.Movement;
|
||||
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.movement.MovableSprite;
|
||||
import com.bartlomiejpluta.base.util.math.MathUtil;
|
||||
import com.bartlomiejpluta.base.util.path.Path;
|
||||
import com.bartlomiejpluta.base.util.path.PathExecutor;
|
||||
import lombok.Getter;
|
||||
@@ -27,7 +26,6 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
private final Vector2f animationScale = new Vector2f(1, 1);
|
||||
private final Vector2fc animationSpriteSize;
|
||||
|
||||
private int animationSpeed = 100;
|
||||
private int iteration = 0;
|
||||
private boolean updated = false;
|
||||
|
||||
@@ -37,6 +35,8 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
|
||||
private boolean forcedFinish = false;
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
@Getter
|
||||
private PathExecutor<Animation> pathExecutor;
|
||||
private boolean finishOnEnd;
|
||||
@@ -54,22 +54,37 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationSpeed(float speed) {
|
||||
animationSpeed = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
public boolean isAnimationEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAnimationSpeed() {
|
||||
return animationSpeed;
|
||||
public void setAnimationEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldAnimate() {
|
||||
return true;
|
||||
public void enableAnimation() {
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2fc[] getSpriteAnimationFramesPositions() {
|
||||
public void disableAnimation() {
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toggleAnimationEnabled() {
|
||||
enabled = !enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldAnimate() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Vector2fc[] getSpriteAnimationFramesPositions() {
|
||||
return frames;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import com.bartlomiejpluta.base.api.move.Movement;
|
||||
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.util.math.MathUtil;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -32,8 +31,6 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
@Setter
|
||||
private int zIndex = 0;
|
||||
|
||||
private int animationSpeed = 100;
|
||||
|
||||
@Getter
|
||||
private Direction faceDirection;
|
||||
|
||||
@@ -44,6 +41,8 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
@Getter
|
||||
private ObjectLayer layer;
|
||||
|
||||
private boolean animationEnabled = true;
|
||||
|
||||
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;
|
||||
@@ -63,17 +62,37 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAnimationSpeed() {
|
||||
return animationSpeed;
|
||||
public boolean isAnimationEnabled() {
|
||||
return animationEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldAnimate() {
|
||||
return isMoving();
|
||||
public void setAnimationEnabled(boolean enabled) {
|
||||
animationEnabled = enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2fc[] getSpriteAnimationFramesPositions() {
|
||||
public void enableAnimation() {
|
||||
animationEnabled = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableAnimation() {
|
||||
animationEnabled = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toggleAnimationEnabled() {
|
||||
animationEnabled = !animationEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldAnimate() {
|
||||
return animationEnabled && isMoving();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Vector2fc[] getSpriteAnimationFramesPositions() {
|
||||
var row = spriteDirectionRows.get(faceDirection);
|
||||
var frames = material.getTexture().getRows();
|
||||
var array = new Vector2f[frames];
|
||||
@@ -111,11 +130,6 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
setDefaultAnimationFrame();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationSpeed(float speed) {
|
||||
animationSpeed = (int) (1 / MathUtil.clamp(speed, Float.MIN_VALUE, 1.0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Entity other) {
|
||||
return chebyshevDistance(other.getCoordinates());
|
||||
|
||||
Reference in New Issue
Block a user