Make TileSetManager shares common Mesh 1x1 among all tiles
This commit is contained in:
@@ -20,7 +20,7 @@ public class DefaultMeshManager implements MeshManager {
|
|||||||
var mesh = quads.get(dim);
|
var mesh = quads.get(dim);
|
||||||
|
|
||||||
if(mesh == null) {
|
if(mesh == null) {
|
||||||
log.info("Creating [w:{}, h:{} | O:{},{}] and putting it into the cache", width, height, originX, originY);
|
log.info("Creating [w:{}, h:{} | O:{}, {}] mesh and putting it into the cache", width, height, originX, originY);
|
||||||
mesh = Mesh.quad(width, height, originX, originY);
|
mesh = Mesh.quad(width, height, originX, originY);
|
||||||
quads.put(dim, mesh);
|
quads.put(dim, mesh);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.engine.world.tileset.manager;
|
package com.bartlomiejpluta.base.engine.world.tileset.manager;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.TextureManager;
|
import com.bartlomiejpluta.base.engine.core.gl.object.texture.TextureManager;
|
||||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||||
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
||||||
@@ -23,6 +24,12 @@ public class DefaultTileSetManager implements TileSetManager {
|
|||||||
private final Map<String, TileSet> tileSets = new HashMap<>();
|
private final Map<String, TileSet> tileSets = new HashMap<>();
|
||||||
private final Map<String, TileSetAsset> assets = new HashMap<>();
|
private final Map<String, TileSetAsset> assets = new HashMap<>();
|
||||||
private final ProjectConfiguration configuration;
|
private final ProjectConfiguration configuration;
|
||||||
|
private Mesh mesh;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
this.mesh = meshManager.createQuad(1, 1, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerAsset(TileSetAsset asset) {
|
public void registerAsset(TileSetAsset asset) {
|
||||||
@@ -43,8 +50,6 @@ public class DefaultTileSetManager implements TileSetManager {
|
|||||||
|
|
||||||
var source = configuration.projectFile("tilesets", asset.getSource());
|
var source = configuration.projectFile("tilesets", asset.getSource());
|
||||||
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||||
var size = texture.getSpriteSize();
|
|
||||||
var mesh = meshManager.createQuad(size.x(), size.y(), 0, 0);
|
|
||||||
tileset = new TileSet(texture, mesh);
|
tileset = new TileSet(texture, mesh);
|
||||||
log.info("Loading tile set from assets to cache under the key: [{}]", uid);
|
log.info("Loading tile set from assets to cache under the key: [{}]", uid);
|
||||||
tileSets.put(uid, tileset);
|
tileSets.put(uid, tileset);
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package com.bartlomiejpluta.base.engine.world.tileset.manager;
|
package com.bartlomiejpluta.base.engine.world.tileset.manager;
|
||||||
|
|
||||||
|
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.tileset.asset.TileSetAsset;
|
import com.bartlomiejpluta.base.engine.world.tileset.asset.TileSetAsset;
|
||||||
import com.bartlomiejpluta.base.engine.world.tileset.model.TileSet;
|
import com.bartlomiejpluta.base.engine.world.tileset.model.TileSet;
|
||||||
import com.bartlomiejpluta.base.internal.gc.Cleanable;
|
import com.bartlomiejpluta.base.internal.gc.Cleanable;
|
||||||
|
|
||||||
public interface TileSetManager extends AssetManager<TileSetAsset, TileSet>, Cleanable {
|
public interface TileSetManager extends Initianizable, AssetManager<TileSetAsset, TileSet>, Cleanable {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,16 @@ import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
|||||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||||
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import org.joml.Vector2f;
|
||||||
|
import org.joml.Vector2fc;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public class Tile extends Sprite {
|
public class Tile extends Sprite {
|
||||||
private final int id;
|
private final int id;
|
||||||
private final int tileSetRow;
|
private final int tileSetRow;
|
||||||
private final int tileSetColumn;
|
private final int tileSetColumn;
|
||||||
|
private final Vector2f tileScale = new Vector2f(1, 1);
|
||||||
|
private final Vector2fc tileSpriteSize;
|
||||||
|
|
||||||
public Tile setCoordinates(int row, int column) {
|
public Tile setCoordinates(int row, int column) {
|
||||||
var stepSize = material.getTexture().getSpriteSize();
|
var stepSize = material.getTexture().getSpriteSize();
|
||||||
@@ -24,6 +28,9 @@ public class Tile extends Sprite {
|
|||||||
this.tileSetRow = id / tileSet.getColumns();
|
this.tileSetRow = id / tileSet.getColumns();
|
||||||
this.tileSetColumn = id % tileSet.getColumns();
|
this.tileSetColumn = id % tileSet.getColumns();
|
||||||
material.setSpritePosition(tileSetColumn, tileSetRow);
|
material.setSpritePosition(tileSetColumn, tileSetRow);
|
||||||
|
tileSpriteSize = tileSet.getSpriteSize();
|
||||||
|
|
||||||
|
super.setScale(tileSpriteSize.x() * tileScale.x, tileSpriteSize.y() * tileScale.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile(Mesh mesh, Texture tileSet, int row, int column) {
|
public Tile(Mesh mesh, Texture tileSet, int row, int column) {
|
||||||
@@ -32,5 +39,43 @@ public class Tile extends Sprite {
|
|||||||
this.tileSetColumn = column;
|
this.tileSetColumn = column;
|
||||||
this.id = row * tileSet.getColumns() + column;
|
this.id = row * tileSet.getColumns() + column;
|
||||||
material.setSpritePosition(tileSetColumn, tileSetRow);
|
material.setSpritePosition(tileSetColumn, tileSetRow);
|
||||||
|
tileSpriteSize = tileSet.getSpriteSize();
|
||||||
|
|
||||||
|
super.setScale(tileSpriteSize.x() * tileScale.x, tileSpriteSize.y() * tileScale.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScaleX(float scaleX) {
|
||||||
|
this.tileScale.x = scaleX;
|
||||||
|
super.setScaleX(tileSpriteSize.x() * scaleX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScaleY(float scaleY) {
|
||||||
|
this.tileScale.y = scaleY;
|
||||||
|
super.setScaleY(tileSpriteSize.x() * scaleY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScale(float scale) {
|
||||||
|
this.tileScale.x = scale;
|
||||||
|
this.tileScale.y = scale;
|
||||||
|
super.setScale(tileSpriteSize.x() * scale, tileSpriteSize.y() * scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScale(float scaleX, float scaleY) {
|
||||||
|
this.tileScale.x = scaleX;
|
||||||
|
this.tileScale.y = scaleY;
|
||||||
|
super.setScale(tileSpriteSize.x() * scaleX, tileSpriteSize.y() * scaleY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getScaleX() {
|
||||||
|
return tileScale.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getScaleY() {
|
||||||
|
return tileScale.y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user