Introduce MeshManager and EntityManager

This commit is contained in:
2021-01-31 23:22:47 +01:00
parent a04538874b
commit 050fa14631
12 changed files with 157 additions and 37 deletions

View File

@@ -0,0 +1,33 @@
package com.bartlomiejpluta.base.core.util.mesh;
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
import lombok.Data;
import org.joml.Vector2f;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class DefaultMeshManager implements MeshManager {
private final Map<QuadDimension, Mesh> quads = new HashMap<>();
@Override
public Mesh createQuad(float width, float height, float originX, float originY) {
var dim = new QuadDimension(new Vector2f(width, height), new Vector2f(originX, originY));
var mesh = quads.get(dim);
if(mesh == null) {
mesh = Mesh.quad(width, height, originX, originY);
quads.put(dim, mesh);
}
return mesh;
}
@Data
private static class QuadDimension {
private final Vector2f size;
private final Vector2f origin;
}
}

View File

@@ -0,0 +1,7 @@
package com.bartlomiejpluta.base.core.util.mesh;
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
public interface MeshManager {
Mesh createQuad(float width, float height, float originX, float originY);
}

View File

@@ -7,18 +7,15 @@ import org.joml.Vector2f;
import org.joml.Vector2i;
public abstract class AnimationableObject extends RenderableObject {
public AnimationableObject(Mesh mesh, Material material) {
public AnimationableObject(Mesh mesh, Material material, Vector2i spriteSheetDimension) {
super(mesh);
setMaterial(material);
var dimensions = getSpriteSheetDimensions();
material.setSpriteSize(1/(float) dimensions.x, 1/(float) dimensions.y);
material.setSpriteSize(1 / (float) spriteSheetDimension.x, 1 / (float) spriteSheetDimension.y);
}
// Returns time in ms between frames
public abstract int getAnimationSpeed();
public abstract Vector2i getSpriteSheetDimensions();
public abstract boolean shouldAnimate();
public abstract Vector2f[] getSpriteAnimationFramesPositions();

View File

@@ -68,8 +68,8 @@ public abstract class MovableObject extends AnimationableObject implements Updat
return setCoordinates(coordinates.x, coordinates.y);
}
public MovableObject(Mesh mesh, Material material, Vector2f coordinateStepSize) {
super(mesh, material);
public MovableObject(Mesh mesh, Material material, Vector2f coordinateStepSize, Vector2i spriteSheetDimensions) {
super(mesh, material, spriteSheetDimensions);
this.coordinateStepSize = coordinateStepSize;
setCoordinates(0, 0);
}

View File

@@ -1,6 +1,7 @@
package com.bartlomiejpluta.base.core.world.tileset.manager;
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
import com.bartlomiejpluta.base.core.world.tileset.model.TileSet;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
@@ -11,10 +12,14 @@ import org.springframework.stereotype.Component;
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DefaultTileSetManager implements TileSetManager {
private final TextureManager textureManager;
private final MeshManager meshManager;
@Override
public TileSet createTileSet(String tileSetFileName, int rows, int columns) {
var texture = textureManager.loadTexture(tileSetFileName);
return new TileSet(texture, rows, columns, texture.getWidth() / columns, texture.getHeight() / rows);
var tileWidth = texture.getWidth() / columns;
var tileHeight = texture.getHeight() / rows;
var mesh = meshManager.createQuad(tileWidth, tileHeight, 0, 0);
return new TileSet(mesh, texture, rows, columns, tileWidth, tileHeight);
}
}

View File

@@ -19,7 +19,7 @@ public class TileSet {
@Getter
private final int tileHeight;
public TileSet(Texture texture, int rows, int columns, int tileWidth, int tileHeight) {
public TileSet(Mesh mesh, Texture texture, int rows, int columns, int tileWidth, int tileHeight) {
this.texture = texture;
this.rows = rows;
this.columns = columns;
@@ -27,7 +27,7 @@ public class TileSet {
this.rowStep = 1/(float) rows;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.mesh = Mesh.quad(tileWidth, tileHeight, 0, 0);
this.mesh = mesh;
}
public Tile getTile(int m, int n) {