Add support for setting animation frame
This commit is contained in:
@@ -16,4 +16,6 @@ public interface Animated extends Updatable {
|
||||
float getAnimationSpeed();
|
||||
|
||||
void setAnimationSpeed(float speed);
|
||||
|
||||
void setAnimationFrame(int frame);
|
||||
}
|
||||
|
||||
@@ -66,6 +66,11 @@ public abstract class AnimationDelegate implements Animation {
|
||||
animation.toggleAnimationEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationFrame(int frame) {
|
||||
animation.setAnimationFrame(frame);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getAnimationSpeed() {
|
||||
return animation.getAnimationSpeed();
|
||||
|
||||
@@ -95,6 +95,11 @@ public abstract class EntityDelegate implements Entity {
|
||||
entity.toggleAnimationEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationFrame(int frame) {
|
||||
entity.setAnimationFrame(frame);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getAnimationSpeed() {
|
||||
return entity.getAnimationSpeed();
|
||||
|
||||
@@ -37,6 +37,11 @@ public class EntityPath<T extends Entity> implements Path<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> turn(Direction direction, int newAnimationFrame) {
|
||||
path.add(new TurnSegment<>(direction, newAnimationFrame));
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> wait(float seconds) {
|
||||
path.add(new WaitSegment<>(seconds));
|
||||
return this;
|
||||
|
||||
@@ -3,19 +3,30 @@ package com.bartlomiejpluta.base.util.path;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import lombok.NonNull;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public class TurnSegment<T extends Entity> implements PathSegment<T> {
|
||||
private final Direction direction;
|
||||
private final Integer newAnimationFrame;
|
||||
|
||||
public TurnSegment(Direction direction) {
|
||||
this.direction = requireNonNull(direction);
|
||||
public TurnSegment(@NonNull Direction direction, int newAnimationFrame) {
|
||||
this.direction = direction;
|
||||
this.newAnimationFrame = newAnimationFrame;
|
||||
}
|
||||
|
||||
public TurnSegment(@NonNull Direction direction) {
|
||||
this.direction = direction;
|
||||
this.newAnimationFrame = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PathProgress perform(T entity, ObjectLayer layer, float dt) {
|
||||
entity.setFaceDirection(direction);
|
||||
if (newAnimationFrame != null) {
|
||||
entity.setAnimationFrame(newAnimationFrame);
|
||||
}
|
||||
return PathProgress.SEGMENT_DONE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,14 @@ public abstract class AnimatedSprite extends Sprite implements Animated {
|
||||
return 1 / (float) intervalInMilliseconds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationFrame(int frame) {
|
||||
var positions = getSpriteAnimationFramesPositions();
|
||||
currentAnimationFrame = frame % positions.length;
|
||||
var current = positions[currentAnimationFrame];
|
||||
material.setSpritePosition(current);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
if (shouldAnimate()) {
|
||||
|
||||
Reference in New Issue
Block a user