Create basic Animator
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.bartlomiejpluta.samplegame.game.animation;
|
||||
|
||||
import com.bartlomiejpluta.samplegame.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.samplegame.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.samplegame.core.world.object.RenderableObject;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
public abstract class AnimationableObject extends RenderableObject {
|
||||
public AnimationableObject(Mesh mesh, Material material) {
|
||||
super(mesh);
|
||||
setMaterial(material);
|
||||
var dimensions = getSpriteSheetDimensions();
|
||||
material.setSpriteSize(1/(float) dimensions.x, 1/(float) dimensions.y);
|
||||
}
|
||||
|
||||
// Returns time in ms between frames
|
||||
public abstract int getAnimationSpeed();
|
||||
|
||||
public abstract Vector2i getSpriteSheetDimensions();
|
||||
|
||||
public abstract boolean shouldAnimate();
|
||||
|
||||
public abstract Vector2f[] getSpriteAnimationFramesPositions();
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.bartlomiejpluta.samplegame.game.animation;
|
||||
|
||||
public interface Animator {
|
||||
void animate(AnimationableObject[] objects);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.bartlomiejpluta.samplegame.game.animation;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DefaultAnimator implements Animator {
|
||||
|
||||
@Override
|
||||
public void animate(AnimationableObject[] objects) {
|
||||
for (var object : objects) {
|
||||
animate(object);
|
||||
}
|
||||
}
|
||||
|
||||
private void animate(AnimationableObject object) {
|
||||
if(object.shouldAnimate()) {
|
||||
var positions = object.getSpriteAnimationFramesPositions();
|
||||
var delay = object.getAnimationSpeed();
|
||||
var currentPosition = (int) (System.currentTimeMillis() % (positions.length * delay)) / delay;
|
||||
var spriteSize = object.getMaterial().getSpriteSize();
|
||||
var current = positions[currentPosition];
|
||||
object.getMaterial().setSpritePosition(spriteSize.x * current.x, spriteSize.y * current.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user