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