Refactor materials
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package com.bartlomiejpluta.base.engine.core.gl.object.color;
|
||||
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.shader.constant.UniformName;
|
||||
import com.bartlomiejpluta.base.internal.render.Renderable;
|
||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
||||
import lombok.Getter;
|
||||
import org.joml.*;
|
||||
|
||||
public class Color implements Material {
|
||||
private static final float[][] TEXTURE_COORDINATES = {{
|
||||
0, 1,
|
||||
0, 0,
|
||||
1, 0,
|
||||
1, 1
|
||||
}};
|
||||
|
||||
protected final Vector4f color = new Vector4f();
|
||||
|
||||
public Color() {
|
||||
this(1f, 1f, 1f, 1f);
|
||||
}
|
||||
|
||||
public Color(float red, float green, float blue, float alpha) {
|
||||
setColor(red, green, blue, alpha);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector4fc getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(Vector4fc color) {
|
||||
this.color.x = color.x();
|
||||
this.color.y = color.y();
|
||||
this.color.z = color.z();
|
||||
this.color.w = color.w();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(Vector3fc color) {
|
||||
this.color.x = color.x();
|
||||
this.color.y = color.y();
|
||||
this.color.z = color.z();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(float red, float green, float blue, float alpha) {
|
||||
this.color.x = red;
|
||||
this.color.y = green;
|
||||
this.color.z = blue;
|
||||
this.color.w = alpha;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(float red, float green, float blue) {
|
||||
this.color.x = red;
|
||||
this.color.y = green;
|
||||
this.color.z = blue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRed(float red) {
|
||||
this.color.x = red;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGreen(float green) {
|
||||
this.color.y = green;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlue(float blue) {
|
||||
this.color.z = blue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlpha(float alpha) {
|
||||
this.color.w = alpha;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRows() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColumns() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float[][] getTextureCoordinatesForAllFrames() {
|
||||
return TEXTURE_COORDINATES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
shaderManager.setUniform(UniformName.UNI_HAS_OBJECT_TEXTURE, false);
|
||||
shaderManager.setUniform(UniformName.UNI_OBJECT_COLOR, color);
|
||||
}
|
||||
}
|
||||
@@ -1,118 +1,31 @@
|
||||
package com.bartlomiejpluta.base.engine.core.gl.object.material;
|
||||
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.shader.constant.UniformName;
|
||||
import com.bartlomiejpluta.base.internal.render.Renderable;
|
||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
||||
import lombok.Getter;
|
||||
import org.joml.*;
|
||||
import org.joml.Vector3fc;
|
||||
import org.joml.Vector4fc;
|
||||
|
||||
public class Material implements Renderable {
|
||||
private final Vector4f color = new Vector4f();
|
||||
private final Vector2f spritePosition = new Vector2f(0, 0);
|
||||
public interface Material extends Renderable {
|
||||
Vector4fc getColor();
|
||||
|
||||
@Getter
|
||||
private final Texture texture;
|
||||
void setColor(Vector4fc color);
|
||||
|
||||
private Material(Texture texture, float red, float green, float blue, float alpha) {
|
||||
this.texture = texture;
|
||||
setColor(red, green, blue, alpha);
|
||||
}
|
||||
void setColor(Vector3fc color);
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
shaderManager.setUniform(UniformName.UNI_OBJECT_COLOR, color);
|
||||
void setColor(float red, float green, float blue, float alpha);
|
||||
|
||||
if (texture != null) {
|
||||
shaderManager.setUniform(UniformName.UNI_HAS_OBJECT_TEXTURE, true);
|
||||
shaderManager.setUniform(UniformName.UNI_TEXTURE_SAMPLER, 0);
|
||||
texture.activate();
|
||||
} else {
|
||||
shaderManager.setUniform(UniformName.UNI_HAS_OBJECT_TEXTURE, false);
|
||||
}
|
||||
}
|
||||
void setColor(float red, float green, float blue);
|
||||
|
||||
public Vector4fc getColor() {
|
||||
return color;
|
||||
}
|
||||
void setRed(float red);
|
||||
|
||||
public void setColor(Vector4fc color) {
|
||||
this.color.x = color.x();
|
||||
this.color.y = color.y();
|
||||
this.color.z = color.z();
|
||||
this.color.w = color.w();
|
||||
}
|
||||
void setGreen(float green);
|
||||
|
||||
public void setColor(Vector3fc color) {
|
||||
this.color.x = color.x();
|
||||
this.color.y = color.y();
|
||||
this.color.z = color.z();
|
||||
}
|
||||
void setBlue(float blue);
|
||||
|
||||
public void setColor(float red, float green, float blue, float alpha) {
|
||||
this.color.x = red;
|
||||
this.color.y = green;
|
||||
this.color.z = blue;
|
||||
this.color.w = alpha;
|
||||
}
|
||||
void setAlpha(float alpha);
|
||||
|
||||
public void setColor(float red, float green, float blue) {
|
||||
this.color.x = red;
|
||||
this.color.y = green;
|
||||
this.color.z = blue;
|
||||
}
|
||||
int getColumns();
|
||||
|
||||
public void setRed(float red) {
|
||||
this.color.x = red;
|
||||
}
|
||||
int getRows();
|
||||
|
||||
public void setGreen(float green) {
|
||||
this.color.y = green;
|
||||
}
|
||||
|
||||
public void setBlue(float blue) {
|
||||
this.color.z = blue;
|
||||
}
|
||||
|
||||
public void setAlpha(float alpha) {
|
||||
this.color.w = alpha;
|
||||
}
|
||||
|
||||
public Vector2fc getSpritePosition() {
|
||||
return spritePosition;
|
||||
}
|
||||
|
||||
public void setSpritePosition(Vector2fc spritePosition) {
|
||||
if (texture != null) {
|
||||
var size = texture.getSpriteFragment();
|
||||
this.spritePosition.x = size.x() * spritePosition.x();
|
||||
this.spritePosition.y = size.y() * spritePosition.y();
|
||||
}
|
||||
}
|
||||
|
||||
public void setSpritePosition(float x, float y) {
|
||||
if (texture != null) {
|
||||
var size = texture.getSpriteFragment();
|
||||
this.spritePosition.x = size.x() * x;
|
||||
this.spritePosition.y = size.y() * y;
|
||||
}
|
||||
}
|
||||
|
||||
public static Material colored(float red, float green, float blue, float alpha) {
|
||||
return new Material(null, red, green, blue, alpha);
|
||||
}
|
||||
|
||||
public static Material textured(Texture texture) {
|
||||
return new Material(texture, 1, 1, 1, 1);
|
||||
}
|
||||
|
||||
public static Material textured(Texture texture, float red, float green, float blue, float alpha) {
|
||||
return new Material(texture, red, green, blue, alpha);
|
||||
}
|
||||
|
||||
public static Material textured(Texture texture, float alpha) {
|
||||
return new Material(texture, 1, 1, 1, alpha);
|
||||
}
|
||||
float[][] getTextureCoordinatesForAllFrames();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package com.bartlomiejpluta.base.engine.core.gl.object.texture;
|
||||
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.color.Color;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.shader.constant.UniformName;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.internal.gc.Disposable;
|
||||
import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
||||
import lombok.Getter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
@@ -15,7 +21,7 @@ import static org.lwjgl.opengl.GL13.glActiveTexture;
|
||||
import static org.lwjgl.stb.STBImage.stbi_failure_reason;
|
||||
import static org.lwjgl.stb.STBImage.stbi_load_from_memory;
|
||||
|
||||
public class Texture implements Disposable {
|
||||
public class Texture extends Color implements Material, Disposable {
|
||||
private static final int DESIRED_CHANNELS = 4;
|
||||
|
||||
private final int textureId;
|
||||
@@ -41,7 +47,6 @@ public class Texture implements Disposable {
|
||||
@Getter
|
||||
private final Vector2fc spriteSize;
|
||||
|
||||
|
||||
Texture(String textureFilename, ByteBuffer buffer, int rows, int columns) {
|
||||
try (var stack = MemoryStack.stackPush()) {
|
||||
var widthBuffer = stack.mallocInt(1);
|
||||
@@ -74,10 +79,11 @@ public class Texture implements Disposable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float[][] getTextureCoordinatesForAllFrames() {
|
||||
var array = new float[rows*columns][];
|
||||
var array = new float[rows * columns][];
|
||||
|
||||
for (var i=0; i<array.length; ++i) {
|
||||
for (var i = 0; i < array.length; ++i) {
|
||||
array[i] = getTextureCoordinates(i);
|
||||
}
|
||||
|
||||
@@ -109,7 +115,11 @@ public class Texture implements Disposable {
|
||||
};
|
||||
}
|
||||
|
||||
public void activate() {
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
shaderManager.setUniform(UniformName.UNI_HAS_OBJECT_TEXTURE, true);
|
||||
shaderManager.setUniform(UniformName.UNI_OBJECT_COLOR, color);
|
||||
shaderManager.setUniform(UniformName.UNI_TEXTURE_SAMPLER, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
}
|
||||
|
||||
@@ -58,9 +58,8 @@ public class DefaultAnimationManager implements AnimationManager {
|
||||
|
||||
var source = configuration.projectFile("animations", asset.getSource());
|
||||
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||
var material = Material.textured(texture);
|
||||
|
||||
return new DefaultAnimation(material, animationFrames);
|
||||
return new DefaultAnimation(texture, animationFrames);
|
||||
}
|
||||
|
||||
private Vector2fc[] createFrames(int rows, int columns) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.api.move.AnimationMovement;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.Movement;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
import com.bartlomiejpluta.base.util.path.Path;
|
||||
import com.bartlomiejpluta.base.util.path.PathExecutor;
|
||||
@@ -39,7 +40,7 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
private boolean enabled = true;
|
||||
|
||||
@Getter
|
||||
private PathExecutor<Animation> pathExecutor = new PathExecutor<>(this);
|
||||
private final PathExecutor<Animation> pathExecutor = new PathExecutor<>(this);
|
||||
private boolean finishOnEnd;
|
||||
private boolean finishOnFail;
|
||||
|
||||
@@ -50,12 +51,12 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
@Getter
|
||||
private final CompletableFuture<Animation> future = new CompletableFuture<>();
|
||||
|
||||
public DefaultAnimation(Material material, @NonNull Vector2fc[] frames) {
|
||||
super(material);
|
||||
public DefaultAnimation(Texture texture, @NonNull Vector2fc[] frames) {
|
||||
super(texture);
|
||||
this.frames = frames;
|
||||
this.lastFrameIndex = frames.length - 1;
|
||||
|
||||
this.animationSpriteSize = material.getTexture().getSpriteSize();
|
||||
this.animationSpriteSize = texture.getSpriteSize();
|
||||
super.setScale(animationSpriteSize.x() * animationScale.x, animationSpriteSize.y() * animationScale.y);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@ package com.bartlomiejpluta.base.engine.world.character.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.engine.common.manager.AssetManager;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.world.character.asset.CharacterSetAsset;
|
||||
|
||||
public interface CharacterSetManager extends AssetManager<CharacterSetAsset, Material> {
|
||||
public interface CharacterSetManager extends AssetManager<CharacterSetAsset, Texture> {
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartlomiejpluta.base.engine.world.character.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.TextureManager;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
||||
@@ -33,7 +34,7 @@ public class DefaultCharacterSetManager implements CharacterSetManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Material loadObject(String uid) {
|
||||
public Texture loadObject(String uid) {
|
||||
var asset = assets.get(uid);
|
||||
|
||||
if (asset == null) {
|
||||
@@ -41,8 +42,7 @@ public class DefaultCharacterSetManager implements CharacterSetManager {
|
||||
}
|
||||
|
||||
var source = configuration.projectFile("charsets", asset.getSource());
|
||||
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||
|
||||
return Material.textured(texture);
|
||||
return textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class CharacterInstantAnimation {
|
||||
CharacterInstantAnimation(DefaultCharacter character, int firstFrame) {
|
||||
this.character = character;
|
||||
this.firstFrame = firstFrame;
|
||||
this.lastFrame = character.getMaterial().getTexture().getColumns() - 1;
|
||||
this.lastFrame = character.getMaterial().getColumns() - 1;
|
||||
}
|
||||
|
||||
public State update() {
|
||||
|
||||
@@ -8,12 +8,14 @@ import com.bartlomiejpluta.base.api.move.CharacterMovement;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.Movement;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.world.character.manager.CharacterSetManager;
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
import com.bartlomiejpluta.base.lib.event.EventHandler;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
@@ -42,6 +44,7 @@ public class DefaultCharacter extends MovableSprite implements Character {
|
||||
private final Map<Direction, Integer> spriteDirectionRows;
|
||||
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
||||
private final Vector2f characterScale = new Vector2f(1, 1);
|
||||
private Texture texture;
|
||||
private Vector2fc characterSetSize;
|
||||
|
||||
private final EventHandler eventHandler = new EventHandler();
|
||||
@@ -64,27 +67,25 @@ public class DefaultCharacter extends MovableSprite implements Character {
|
||||
|
||||
private final Queue<CharacterInstantAnimation> instantAnimations = new LinkedList<>();
|
||||
|
||||
public DefaultCharacter(CharacterSetManager characterSetManager, int defaultSpriteColumn, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows, String characterSetUid) {
|
||||
super(createMaterial(characterSetManager, characterSetUid));
|
||||
public DefaultCharacter(CharacterSetManager characterSetManager, int defaultSpriteColumn, @NonNull Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows, @NonNull String characterSetUid) {
|
||||
this(characterSetManager.loadObject(characterSetUid), characterSetManager, defaultSpriteColumn, spriteDirectionRows, spriteDefaultRows);
|
||||
}
|
||||
|
||||
private DefaultCharacter(@NonNull Texture texture, @NonNull CharacterSetManager characterSetManager, int defaultSpriteColumn, @NonNull Map<Direction, Integer> spriteDirectionRows, @NonNull Map<Direction, Vector2fc> spriteDefaultRows) {
|
||||
super(texture);
|
||||
this.defaultSpriteColumn = defaultSpriteColumn;
|
||||
this.characterSetManager = characterSetManager;
|
||||
this.spriteDirectionRows = spriteDirectionRows;
|
||||
this.faceDirection = Direction.DOWN;
|
||||
this.spriteDefaultRows = spriteDefaultRows;
|
||||
|
||||
var texture = getMaterial().getTexture();
|
||||
if (texture != null) {
|
||||
this.characterSetSize = texture.getSpriteSize();
|
||||
super.setScale(characterSetSize.x() * characterScale.x, characterSetSize.y() * characterScale.y);
|
||||
}
|
||||
this.texture = texture;
|
||||
this.characterSetSize = texture.getSpriteSize();
|
||||
super.setScale(characterSetSize.x() * characterScale.x, characterSetSize.y() * characterScale.y);
|
||||
|
||||
setDefaultAnimationFrame();
|
||||
}
|
||||
|
||||
private static Material createMaterial(CharacterSetManager characterSetManager, String characterSetUid) {
|
||||
return characterSetUid != null ? characterSetManager.loadObject(requireNonNull(characterSetUid)) : Material.colored(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAnimationEnabled() {
|
||||
return animationEnabled;
|
||||
@@ -112,13 +113,13 @@ public class DefaultCharacter extends MovableSprite implements Character {
|
||||
|
||||
@Override
|
||||
protected boolean shouldAnimate() {
|
||||
return animationEnabled && getMaterial().getTexture() != null && (isMoving() || !instantAnimations.isEmpty());
|
||||
return animationEnabled && (isMoving() || !instantAnimations.isEmpty());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Vector2fc[] getSpriteAnimationFramesPositions() {
|
||||
var row = spriteDirectionRows.get(faceDirection);
|
||||
var frames = getMaterial().getTexture().getColumns();
|
||||
var frames = getMaterial().getColumns();
|
||||
var array = new Vector2f[frames];
|
||||
|
||||
for (int column = 0; column < frames; ++column) {
|
||||
@@ -141,15 +142,10 @@ public class DefaultCharacter extends MovableSprite implements Character {
|
||||
|
||||
@Override
|
||||
public void changeCharacterSet(String characterSetUid) {
|
||||
setMaterial(createMaterial(characterSetManager, characterSetUid));
|
||||
var texture = this.getMaterial().getTexture();
|
||||
|
||||
if (texture != null) {
|
||||
this.characterSetSize = texture.getSpriteSize();
|
||||
super.setScale(characterSetSize.x() * characterScale.x, characterSetSize.y() * characterScale.y);
|
||||
} else {
|
||||
this.characterSetSize = null;
|
||||
}
|
||||
this.texture = characterSetManager.loadObject(characterSetUid);
|
||||
this.characterSetSize = texture.getSpriteSize();
|
||||
super.setScale(characterSetSize.x() * characterScale.x, characterSetSize.y() * characterScale.y);
|
||||
setMaterial(texture);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartlomiejpluta.base.engine.world.icon.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.TextureManager;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
||||
@@ -38,7 +39,7 @@ public class DefaultIconSetManager implements IconSetManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Material loadObject(String uid) {
|
||||
public Texture loadObject(String uid) {
|
||||
var asset = assets.get(uid);
|
||||
|
||||
if (asset == null) {
|
||||
@@ -46,9 +47,7 @@ public class DefaultIconSetManager implements IconSetManager {
|
||||
}
|
||||
|
||||
var source = configuration.projectFile("iconsets", asset.getSource());
|
||||
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||
|
||||
return Material.textured(texture);
|
||||
return textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,7 +3,8 @@ package com.bartlomiejpluta.base.engine.world.icon.manager;
|
||||
import com.bartlomiejpluta.base.engine.common.manager.AssetManager;
|
||||
import com.bartlomiejpluta.base.engine.common.manager.ByteBufferAssetManager;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.world.icon.asset.IconSetAsset;
|
||||
|
||||
public interface IconSetManager extends AssetManager<IconSetAsset, Material>, ByteBufferAssetManager<IconSetAsset> {
|
||||
public interface IconSetManager extends AssetManager<IconSetAsset, Texture>, ByteBufferAssetManager<IconSetAsset> {
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@ import com.bartlomiejpluta.base.api.event.Event;
|
||||
import com.bartlomiejpluta.base.api.event.EventType;
|
||||
import com.bartlomiejpluta.base.api.icon.Icon;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.world.icon.manager.IconSetManager;
|
||||
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
||||
import com.bartlomiejpluta.base.lib.event.EventHandler;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
@@ -42,15 +44,16 @@ public class DefaultIcon extends Sprite implements Icon {
|
||||
private int zIndex;
|
||||
|
||||
public DefaultIcon(IconSetManager iconSetManager, String iconSetUid, int row, int column) {
|
||||
super(iconSetManager.loadObject(iconSetUid));
|
||||
this.iconSetManager = iconSetManager;
|
||||
getMaterial().setSpritePosition(column, row);
|
||||
this(iconSetManager, iconSetManager.loadObject(iconSetUid), iconSetUid, row, column);
|
||||
}
|
||||
|
||||
var texture = getMaterial().getTexture();
|
||||
if (texture != null) {
|
||||
this.iconSetSize = texture.getSpriteSize();
|
||||
super.setScale(iconSetSize.x() * iconScale.x, iconSetSize.y() * iconScale.y);
|
||||
}
|
||||
public DefaultIcon(@NonNull IconSetManager iconSetManager, @NonNull Texture texture, @NonNull String iconSetUid, int row, int column) {
|
||||
super(texture);
|
||||
this.iconSetManager = iconSetManager;
|
||||
setFrame(row, column);
|
||||
|
||||
this.iconSetSize = texture.getSpriteSize();
|
||||
super.setScale(iconSetSize.x() * iconScale.x, iconSetSize.y() * iconScale.y);
|
||||
|
||||
this.iconSetUid = iconSetUid;
|
||||
this.iconSetRow = row;
|
||||
@@ -69,27 +72,23 @@ public class DefaultIcon extends Sprite implements Icon {
|
||||
|
||||
@Override
|
||||
public void changeIcon(int row, int column) {
|
||||
getMaterial().setSpritePosition(column, row);
|
||||
setFrame(row, column);
|
||||
this.iconSetRow = row;
|
||||
this.iconSetColumn = column;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeIcon(String iconSetUid, int row, int column) {
|
||||
setMaterial(iconSetManager.loadObject(iconSetUid));
|
||||
getMaterial().setSpritePosition(column, row);
|
||||
var texture = iconSetManager.loadObject(iconSetUid);
|
||||
setMaterial(texture);
|
||||
setFrame(row, column);
|
||||
|
||||
this.iconSetUid = iconSetUid;
|
||||
this.iconSetRow = row;
|
||||
this.iconSetColumn = column;
|
||||
|
||||
var texture = getMaterial().getTexture();
|
||||
if (texture != null) {
|
||||
this.iconSetSize = texture.getSpriteSize();
|
||||
super.setScale(iconSetSize.x() * iconScale.x, iconSetSize.y() * iconScale.y);
|
||||
} else {
|
||||
this.iconSetSize = null;
|
||||
}
|
||||
this.iconSetSize = texture.getSpriteSize();
|
||||
super.setScale(iconSetSize.x() * iconScale.x, iconSetSize.y() * iconScale.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -61,10 +61,9 @@ public class DefaultImageManager implements ImageManager {
|
||||
var gcd = MathUtil.gcdEuclidean(width, height);
|
||||
var initialWidth = width / gcd;
|
||||
var initialHeight = height / gcd;
|
||||
var material = Material.textured(texture);
|
||||
log.info("Creating new image on asset with UID: [{}]", uid);
|
||||
|
||||
return new DefaultImage(material, initialWidth, initialHeight, gcd);
|
||||
return new DefaultImage(texture, initialWidth, initialHeight, gcd);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.map.layer.color.ColorLayer;
|
||||
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.color.Color;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.util.mesh.MeshManager;
|
||||
import com.bartlomiejpluta.base.engine.world.map.layer.base.BaseLayer;
|
||||
@@ -15,13 +16,13 @@ import org.joml.Matrix4fc;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
public class DefaultColorLayer extends BaseLayer implements ColorLayer {
|
||||
private final Color color;
|
||||
private final ColorPlane colorPlane;
|
||||
private final Material material;
|
||||
|
||||
public DefaultColorLayer(@NonNull GameMap map, @NonNull MeshManager meshManager, float red, float green, float blue, float alpha) {
|
||||
super(map);
|
||||
this.color = new Color(meshManager, red, green, blue, alpha);
|
||||
this.material = color.getMaterial();
|
||||
this.colorPlane = new ColorPlane(meshManager, red, green, blue, alpha);
|
||||
this.material = colorPlane.getMaterial();
|
||||
|
||||
setScale(map.getWidth(), map.getHeight());
|
||||
}
|
||||
@@ -58,94 +59,94 @@ public class DefaultColorLayer extends BaseLayer implements ColorLayer {
|
||||
|
||||
@Override
|
||||
public Vector2fc getPosition() {
|
||||
return color.getPosition();
|
||||
return colorPlane.getPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(float x, float y) {
|
||||
color.setPosition(x, y);
|
||||
colorPlane.setPosition(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector2fc position) {
|
||||
color.setPosition(position);
|
||||
colorPlane.setPosition(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePosition(float x, float y) {
|
||||
color.movePosition(x, y);
|
||||
colorPlane.movePosition(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePosition(Vector2fc position) {
|
||||
color.movePosition(position);
|
||||
colorPlane.movePosition(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getRotation() {
|
||||
return color.getRotation();
|
||||
return colorPlane.getRotation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotation(float rotation) {
|
||||
color.setRotation(rotation);
|
||||
colorPlane.setRotation(rotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveRotation(float rotation) {
|
||||
color.moveRotation(rotation);
|
||||
colorPlane.moveRotation(rotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScaleX() {
|
||||
return color.getScaleX();
|
||||
return colorPlane.getScaleX();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScaleX(float scale) {
|
||||
color.setScaleX(scale);
|
||||
colorPlane.setScaleX(scale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScaleY() {
|
||||
return color.getScaleY();
|
||||
return colorPlane.getScaleY();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScaleY(float scale) {
|
||||
color.setScaleY(scale);
|
||||
colorPlane.setScaleY(scale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScale(float scale) {
|
||||
color.setScale(scale);
|
||||
colorPlane.setScale(scale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScale(float scaleX, float scaleY) {
|
||||
color.setScale(scaleX, scaleY);
|
||||
colorPlane.setScale(scaleX, scaleY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float euclideanDistance(Placeable other) {
|
||||
return color.euclideanDistance(other);
|
||||
return colorPlane.euclideanDistance(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matrix4fc getModelMatrix() {
|
||||
return color.getModelMatrix();
|
||||
return colorPlane.getModelMatrix();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
color.render(screen, camera, shaderManager);
|
||||
colorPlane.render(screen, camera, shaderManager);
|
||||
super.render(screen, camera, shaderManager);
|
||||
}
|
||||
|
||||
private static class Color extends Sprite {
|
||||
public Color(@NonNull MeshManager meshManager, float red, float green, float blue, float alpha) {
|
||||
private static class ColorPlane extends Sprite {
|
||||
public ColorPlane(@NonNull MeshManager meshManager, float red, float green, float blue, float alpha) {
|
||||
// TODO (Custom Mesh): super(meshManager.createQuad(1, 1, 0, 0), Material.colored(red, green, blue, alpha));
|
||||
super(Material.colored(red, green, blue, alpha));
|
||||
super(new Color(red, green, blue, alpha));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ import com.bartlomiejpluta.base.internal.render.ShaderManager;
|
||||
|
||||
public class TileChunk extends Model implements Placeable, Renderable, Disposable, BoundingBox {
|
||||
private final Texture tileSet;
|
||||
private final Material material;
|
||||
private final Mesh mesh;
|
||||
private final int chunkSize;
|
||||
private final QuadTemplate template;
|
||||
@@ -30,7 +29,6 @@ public class TileChunk extends Model implements Placeable, Renderable, Disposabl
|
||||
|
||||
public TileChunk(Texture tileSet, int chunkSize, float originX, float originY) {
|
||||
this.tileSet = tileSet;
|
||||
this.material = Material.textured(tileSet);
|
||||
this.chunkSize = chunkSize;
|
||||
this.mesh = new Mesh(chunkSize * chunkSize);
|
||||
this.template = new QuadTemplate(tileSet.getSpriteSize().x(), tileSet.getSpriteSize().y(), originX, originY);
|
||||
@@ -64,7 +62,7 @@ public class TileChunk extends Model implements Placeable, Renderable, Disposabl
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
material.render(screen, camera, shaderManager);
|
||||
tileSet.render(screen, camera, shaderManager);
|
||||
|
||||
shaderManager.setUniform(UniformName.UNI_MODEL_MATRIX, getModelMatrix());
|
||||
shaderManager.setUniform(UniformName.UNI_PROJECTION_MATRIX, camera.getProjectionMatrix());
|
||||
|
||||
@@ -38,13 +38,7 @@ public abstract class Sprite extends LocationableModel implements Renderable, Bo
|
||||
}
|
||||
|
||||
private void updateTextureCoordinates() {
|
||||
var texture = material.getTexture();
|
||||
|
||||
if (texture == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
textureCoordinates = texture.getTextureCoordinatesForAllFrames();
|
||||
this.textureCoordinates = material.getTextureCoordinatesForAllFrames();
|
||||
}
|
||||
|
||||
public void setMaterial(@NonNull Material material) {
|
||||
@@ -53,7 +47,7 @@ public abstract class Sprite extends LocationableModel implements Renderable, Bo
|
||||
}
|
||||
|
||||
public void setFrame(int row, int column) {
|
||||
setFrame(row * material.getTexture().getColumns() + column);
|
||||
setFrame(row * material.getColumns() + column);
|
||||
}
|
||||
|
||||
public void setFrame(int id) {
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
package com.bartlomiejpluta.base.engine.world.tileset.model;
|
||||
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.Texture;
|
||||
import com.bartlomiejpluta.base.engine.world.object.Sprite;
|
||||
import lombok.Getter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
@Getter
|
||||
public class Tile extends Sprite {
|
||||
private final int id;
|
||||
private final int tileSetRow;
|
||||
private final int tileSetColumn;
|
||||
private final Vector2f tileScale = new Vector2f(1, 1);
|
||||
private final Vector2fc tileSpriteSize;
|
||||
|
||||
public Tile setLocation(int row, int column) {
|
||||
var stepSize = getMaterial().getTexture().getSpriteSize();
|
||||
setPosition(column * stepSize.x(), row * stepSize.y());
|
||||
return this;
|
||||
}
|
||||
|
||||
public Tile(Texture tileSet, int id) {
|
||||
super(Material.textured(tileSet));
|
||||
this.id = id;
|
||||
this.tileSetRow = id / tileSet.getColumns();
|
||||
this.tileSetColumn = id % tileSet.getColumns();
|
||||
getMaterial().setSpritePosition(tileSetColumn, tileSetRow);
|
||||
tileSpriteSize = tileSet.getSpriteSize();
|
||||
|
||||
super.setScale(tileSpriteSize.x() * tileScale.x, tileSpriteSize.y() * tileScale.y);
|
||||
}
|
||||
|
||||
public Tile(Texture tileSet, int row, int column) {
|
||||
super(Material.textured(tileSet));
|
||||
this.tileSetRow = row;
|
||||
this.tileSetColumn = column;
|
||||
this.id = row * tileSet.getColumns() + column;
|
||||
getMaterial().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