Refactor code
This commit is contained in:
@@ -18,6 +18,9 @@ public class Texture {
|
|||||||
|
|
||||||
private final int textureId;
|
private final int textureId;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final String fileName;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final int width;
|
private final int width;
|
||||||
|
|
||||||
@@ -46,6 +49,8 @@ public class Texture {
|
|||||||
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
|
fileName = textureFilename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import com.bartlomiejpluta.samplegame.core.gl.shader.manager.ShaderManager;
|
|||||||
import com.bartlomiejpluta.samplegame.core.ui.Window;
|
import com.bartlomiejpluta.samplegame.core.ui.Window;
|
||||||
import com.bartlomiejpluta.samplegame.core.world.camera.Camera;
|
import com.bartlomiejpluta.samplegame.core.world.camera.Camera;
|
||||||
import com.bartlomiejpluta.samplegame.core.world.object.RenderableObject;
|
import com.bartlomiejpluta.samplegame.core.world.object.RenderableObject;
|
||||||
import com.bartlomiejpluta.samplegame.game.map.GameMap;
|
import com.bartlomiejpluta.samplegame.game.world.map.GameMap;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ public class Scene implements Renderable {
|
|||||||
renderArray(map.getLayer(0), window, shaderManager);
|
renderArray(map.getLayer(0), window, shaderManager);
|
||||||
renderArray(map.getLayer(1), window, shaderManager);
|
renderArray(map.getLayer(1), window, shaderManager);
|
||||||
|
|
||||||
// The player will be here
|
// Player will be rendered here
|
||||||
|
|
||||||
renderArray(map.getLayer(2), window, shaderManager);
|
renderArray(map.getLayer(2), window, shaderManager);
|
||||||
renderArray(map.getLayer(3), window, shaderManager);
|
renderArray(map.getLayer(3), window, shaderManager);
|
||||||
@@ -35,6 +35,12 @@ public class Scene implements Renderable {
|
|||||||
private <T extends RenderableObject> void renderArray(T[] objects, Window window, ShaderManager shaderManager) {
|
private <T extends RenderableObject> void renderArray(T[] objects, Window window, ShaderManager shaderManager) {
|
||||||
for (var object : objects) {
|
for (var object : objects) {
|
||||||
if (object != null) {
|
if (object != null) {
|
||||||
|
renderObject(object, window, shaderManager);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T extends RenderableObject> void renderObject(T object, Window window, ShaderManager shaderManager) {
|
||||||
shaderManager.setUniform(UniformName.UNI_MODEL_MATRIX, object.getModelMatrix());
|
shaderManager.setUniform(UniformName.UNI_MODEL_MATRIX, object.getModelMatrix());
|
||||||
shaderManager.setUniform(UniformName.UNI_OBJECT_COLOR, object.getMaterial().getColor());
|
shaderManager.setUniform(UniformName.UNI_OBJECT_COLOR, object.getMaterial().getColor());
|
||||||
shaderManager.setUniform(UniformName.UNI_HAS_OBJECT_TEXTURE, object.getMaterial().hasTexture());
|
shaderManager.setUniform(UniformName.UNI_HAS_OBJECT_TEXTURE, object.getMaterial().hasTexture());
|
||||||
@@ -42,8 +48,6 @@ public class Scene implements Renderable {
|
|||||||
|
|
||||||
object.render(window, shaderManager);
|
object.render(window, shaderManager);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cleanUp() {
|
public void cleanUp() {
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import com.bartlomiejpluta.samplegame.core.logic.GameLogic;
|
|||||||
import com.bartlomiejpluta.samplegame.core.ui.Window;
|
import com.bartlomiejpluta.samplegame.core.ui.Window;
|
||||||
import com.bartlomiejpluta.samplegame.core.world.camera.Camera;
|
import com.bartlomiejpluta.samplegame.core.world.camera.Camera;
|
||||||
import com.bartlomiejpluta.samplegame.core.world.scene.Scene;
|
import com.bartlomiejpluta.samplegame.core.world.scene.Scene;
|
||||||
import com.bartlomiejpluta.samplegame.game.tile.TileSetManager;
|
import com.bartlomiejpluta.samplegame.game.world.map.GameMap;
|
||||||
|
import com.bartlomiejpluta.samplegame.game.world.tileset.manager.TileSetManager;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -15,11 +16,13 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
public class DefaultGameLogic implements GameLogic {
|
public class DefaultGameLogic implements GameLogic {
|
||||||
|
private static final float SCALE = 3.0f;
|
||||||
private final Renderer renderer;
|
private final Renderer renderer;
|
||||||
private final TileSetManager tileSetManager;
|
private final TileSetManager tileSetManager;
|
||||||
|
|
||||||
private final Camera camera = new Camera();
|
private Camera camera;
|
||||||
private final Scene scene = new Scene(camera);
|
private GameMap map;
|
||||||
|
private Scene scene;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(Window window) {
|
public void init(Window window) {
|
||||||
@@ -34,7 +37,6 @@ public class DefaultGameLogic implements GameLogic {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -45,5 +47,6 @@ public class DefaultGameLogic implements GameLogic {
|
|||||||
@Override
|
@Override
|
||||||
public void cleanUp() {
|
public void cleanUp() {
|
||||||
renderer.cleanUp();
|
renderer.cleanUp();
|
||||||
|
scene.cleanUp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
55
app/src/main/java/com/bartlomiejpluta/samplegame/game/sprite/BaseSprite.java
Executable file
55
app/src/main/java/com/bartlomiejpluta/samplegame/game/sprite/BaseSprite.java
Executable file
@@ -0,0 +1,55 @@
|
|||||||
|
package com.bartlomiejpluta.samplegame.game.sprite;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.samplegame.core.gl.object.material.Material;
|
||||||
|
import com.bartlomiejpluta.samplegame.core.gl.object.mesh.Mesh;
|
||||||
|
import com.bartlomiejpluta.samplegame.core.gl.object.texture.Texture;
|
||||||
|
import com.bartlomiejpluta.samplegame.core.world.object.RenderableObject;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class BaseSprite extends RenderableObject {
|
||||||
|
private static final int[] ELEMENTS = new int[]{
|
||||||
|
0, 1, 2,
|
||||||
|
2, 3, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
private final int row;
|
||||||
|
private final int col;
|
||||||
|
private final int tileWidth;
|
||||||
|
private final int tileHeight;
|
||||||
|
|
||||||
|
public BaseSprite(Texture texture, int row, int col, int tileWidth, int tileHeight) {
|
||||||
|
super(buildTileMesh(texture, row, col, tileWidth, tileHeight));
|
||||||
|
this.row = row;
|
||||||
|
this.col = col;
|
||||||
|
this.tileWidth = tileWidth;
|
||||||
|
this.tileHeight = tileHeight;
|
||||||
|
setMaterial(Material.textured(texture));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Mesh buildTileMesh(Texture texture, int row, int col, int tileWidth, int tileHeight) {
|
||||||
|
var vertices = getVertices(tileWidth, tileHeight);
|
||||||
|
var texCoords = getTextureCoordinates(row, col, tileWidth, tileHeight, texture.getWidth(), texture.getHeight());
|
||||||
|
return new Mesh(vertices, texCoords, ELEMENTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float[] getVertices(int tileWidth, int tileHeight) {
|
||||||
|
var halfWidth = tileWidth / 2;
|
||||||
|
var halfHeight = tileHeight / 2;
|
||||||
|
return new float[]{
|
||||||
|
-halfWidth, -halfHeight,
|
||||||
|
-halfWidth, halfHeight,
|
||||||
|
halfWidth, halfHeight,
|
||||||
|
halfWidth, -halfHeight
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float[] getTextureCoordinates(int col, int row, int tileWidth, int tileHeight, int textureWidth, int textureHeight) {
|
||||||
|
return new float[]{
|
||||||
|
(col * tileWidth) / (float) textureWidth, (row * tileHeight) / (float) textureHeight,
|
||||||
|
(col * tileWidth) / (float) textureWidth, ((row + 1) * tileHeight) / (float) textureHeight,
|
||||||
|
((col + 1) * tileWidth) / (float) textureWidth, ((row + 1) * tileHeight) / (float) textureHeight,
|
||||||
|
((col + 1) * tileWidth) / (float) textureWidth, (row * tileHeight) / (float) textureHeight
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
package com.bartlomiejpluta.samplegame.game.tile;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.samplegame.core.gl.object.material.Material;
|
|
||||||
import com.bartlomiejpluta.samplegame.core.gl.object.mesh.Mesh;
|
|
||||||
import com.bartlomiejpluta.samplegame.core.gl.object.texture.Texture;
|
|
||||||
import com.bartlomiejpluta.samplegame.core.world.object.RenderableObject;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
public class Tile extends RenderableObject {
|
|
||||||
private static final int[] ELEMENTS = new int[]{
|
|
||||||
0, 1, 2,
|
|
||||||
2, 3, 0
|
|
||||||
};
|
|
||||||
|
|
||||||
private final int row;
|
|
||||||
private final int col;
|
|
||||||
private final int size;
|
|
||||||
|
|
||||||
Tile(Texture texture, int row, int col, int size) {
|
|
||||||
super(buildTileMesh(texture, row, col, size));
|
|
||||||
this.row = row;
|
|
||||||
this.col = col;
|
|
||||||
this.size = size;
|
|
||||||
setMaterial(Material.textured(texture));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Mesh buildTileMesh(Texture texture, int row, int col, int size) {
|
|
||||||
var vertices = getVertices(size);
|
|
||||||
var texCoords = getTextureCoordinates(row, col, size, texture.getWidth(), texture.getHeight());
|
|
||||||
return new Mesh(vertices, texCoords, ELEMENTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static float[] getVertices(int tileSize) {
|
|
||||||
var half = tileSize / 2;
|
|
||||||
return new float[]{
|
|
||||||
-half, -half,
|
|
||||||
-half, half,
|
|
||||||
half, half,
|
|
||||||
half, -half
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static float[] getTextureCoordinates(int col, int row, int tileSize, int textureWidth, int textureHeight) {
|
|
||||||
return new float[]{
|
|
||||||
(col * tileSize) / (float) textureWidth, (row * tileSize) / (float) textureHeight,
|
|
||||||
(col * tileSize) / (float) textureWidth, ((row + 1) * tileSize) / (float) textureHeight,
|
|
||||||
((col + 1) * tileSize) / (float) textureWidth, ((row + 1) * tileSize) / (float) textureHeight,
|
|
||||||
((col + 1) * tileSize) / (float) textureWidth, (row * tileSize) / (float) textureHeight
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package com.bartlomiejpluta.samplegame.game.tile;
|
|
||||||
|
|
||||||
public interface TileSetManager {
|
|
||||||
TileSet createTileSet(String tileSetFileName);
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.bartlomiejpluta.samplegame.game.map;
|
package com.bartlomiejpluta.samplegame.game.world.map;
|
||||||
|
|
||||||
import com.bartlomiejpluta.samplegame.game.tile.Tile;
|
import com.bartlomiejpluta.samplegame.game.world.tileset.model.Tile;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartlomiejpluta.samplegame.game.tile;
|
package com.bartlomiejpluta.samplegame.game.world.tileset.manager;
|
||||||
|
|
||||||
import com.bartlomiejpluta.samplegame.core.gl.object.texture.TextureManager;
|
import com.bartlomiejpluta.samplegame.core.gl.object.texture.TextureManager;
|
||||||
|
import com.bartlomiejpluta.samplegame.game.world.tileset.model.TileSet;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.bartlomiejpluta.samplegame.game.world.tileset.manager;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.samplegame.game.world.tileset.model.TileSet;
|
||||||
|
|
||||||
|
public interface TileSetManager {
|
||||||
|
TileSet createTileSet(String tileSetFileName);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.bartlomiejpluta.samplegame.game.world.tileset.model;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.samplegame.core.gl.object.texture.Texture;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.samplegame.game.sprite.BaseSprite;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class Tile extends BaseSprite {
|
||||||
|
private final int size;
|
||||||
|
|
||||||
|
public Tile(Texture texture, int row, int col, int size) {
|
||||||
|
super(texture, row, col, size, size);
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.bartlomiejpluta.samplegame.game.tile;
|
package com.bartlomiejpluta.samplegame.game.world.tileset.model;
|
||||||
|
|
||||||
import com.bartlomiejpluta.samplegame.core.gl.object.texture.Texture;
|
import com.bartlomiejpluta.samplegame.core.gl.object.texture.Texture;
|
||||||
|
|
||||||
Reference in New Issue
Block a user