Make AnimationManager shares common Mesh 1x1 among all animations
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
package com.bartlomiejpluta.base.engine.world.animation.manager;
|
package com.bartlomiejpluta.base.engine.world.animation.manager;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||||
|
import com.bartlomiejpluta.base.engine.common.init.Initianizable;
|
||||||
import com.bartlomiejpluta.base.engine.common.manager.AssetManager;
|
import com.bartlomiejpluta.base.engine.common.manager.AssetManager;
|
||||||
import com.bartlomiejpluta.base.engine.world.animation.asset.AnimationAsset;
|
import com.bartlomiejpluta.base.engine.world.animation.asset.AnimationAsset;
|
||||||
|
|
||||||
public interface AnimationManager extends AssetManager<AnimationAsset, Animation> {
|
public interface AnimationManager extends Initianizable, AssetManager<AnimationAsset, Animation> {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,12 @@ public class DefaultAnimationManager implements AnimationManager {
|
|||||||
private final Map<String, AnimationAsset> assets = new HashMap<>();
|
private final Map<String, AnimationAsset> assets = new HashMap<>();
|
||||||
private final Map<String, Vector2fc[]> frames = new HashMap<>();
|
private final Map<String, Vector2fc[]> frames = new HashMap<>();
|
||||||
private final ProjectConfiguration configuration;
|
private final ProjectConfiguration configuration;
|
||||||
|
private Mesh mesh;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
mesh = meshManager.createQuad(1, 1, 0.5f, 0.5f);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerAsset(AnimationAsset asset) {
|
public void registerAsset(AnimationAsset asset) {
|
||||||
@@ -51,7 +56,6 @@ public class DefaultAnimationManager implements AnimationManager {
|
|||||||
var source = configuration.projectFile("animations", asset.getSource());
|
var source = configuration.projectFile("animations", asset.getSource());
|
||||||
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||||
var material = Material.textured(texture);
|
var material = Material.textured(texture);
|
||||||
var mesh = buildMesh(material, asset.getRows(), asset.getColumns());
|
|
||||||
|
|
||||||
return new DefaultAnimation(mesh, material, animationFrames);
|
return new DefaultAnimation(mesh, material, animationFrames);
|
||||||
}
|
}
|
||||||
@@ -69,11 +73,4 @@ public class DefaultAnimationManager implements AnimationManager {
|
|||||||
|
|
||||||
return frames;
|
return frames;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mesh buildMesh(Material material, int rows, int columns) {
|
|
||||||
var texture = material.getTexture();
|
|
||||||
var spriteWidth = texture.getWidth() / columns;
|
|
||||||
var spriteHeight = texture.getHeight() / rows;
|
|
||||||
return meshManager.createQuad(spriteWidth, spriteHeight, spriteWidth / 2f, spriteHeight / 2f);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import com.bartlomiejpluta.base.util.path.PathExecutor;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.joml.Vector2f;
|
||||||
import org.joml.Vector2fc;
|
import org.joml.Vector2fc;
|
||||||
|
|
||||||
import static com.bartlomiejpluta.base.util.path.PathProgress.DONE;
|
import static com.bartlomiejpluta.base.util.path.PathProgress.DONE;
|
||||||
@@ -20,6 +21,8 @@ import static com.bartlomiejpluta.base.util.path.PathProgress.SEGMENT_FAILED;
|
|||||||
public class DefaultAnimation extends MovableSprite implements Animation {
|
public class DefaultAnimation extends MovableSprite implements Animation {
|
||||||
private final Vector2fc[] frames;
|
private final Vector2fc[] frames;
|
||||||
private final int lastFrameIndex;
|
private final int lastFrameIndex;
|
||||||
|
private final Vector2f animationScale = new Vector2f(1, 1);
|
||||||
|
private final Vector2fc animationSpriteSize;
|
||||||
|
|
||||||
private int animationSpeed = 100;
|
private int animationSpeed = 100;
|
||||||
private int iteration = 0;
|
private int iteration = 0;
|
||||||
@@ -42,6 +45,9 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
|||||||
super(mesh, material);
|
super(mesh, material);
|
||||||
this.frames = frames;
|
this.frames = frames;
|
||||||
this.lastFrameIndex = frames.length - 1;
|
this.lastFrameIndex = frames.length - 1;
|
||||||
|
|
||||||
|
this.animationSpriteSize = material.getTexture().getSpriteSize();
|
||||||
|
super.setScale(animationSpriteSize.x() * animationScale.x, animationSpriteSize.y() * animationScale.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -125,4 +131,40 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
|||||||
|
|
||||||
return iteration >= repeat;
|
return iteration >= repeat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScaleX(float scaleX) {
|
||||||
|
this.animationScale.x = scaleX;
|
||||||
|
super.setScaleX(animationSpriteSize.x() * scaleX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScaleY(float scaleY) {
|
||||||
|
this.animationScale.y = scaleY;
|
||||||
|
super.setScaleY(animationSpriteSize.y() * scaleY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScale(float scale) {
|
||||||
|
this.animationScale.x = scale;
|
||||||
|
this.animationScale.y = scale;
|
||||||
|
super.setScale(animationSpriteSize.x() * scale, animationSpriteSize.y() * scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScale(float scaleX, float scaleY) {
|
||||||
|
this.animationScale.x = scaleX;
|
||||||
|
this.animationScale.y = scaleY;
|
||||||
|
super.setScale(animationSpriteSize.x() * scaleX, animationSpriteSize.y() * scaleY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getScaleX() {
|
||||||
|
return animationScale.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getScaleY() {
|
||||||
|
return animationScale.y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user