Create Moveable Object

This commit is contained in:
2021-01-31 12:36:42 +01:00
parent 8d1f78cb1d
commit 845844d15b
11 changed files with 159 additions and 37 deletions

View File

@@ -76,15 +76,12 @@ public class Mesh implements Renderable {
glDeleteVertexArrays(vaoId);
}
public static Mesh quad(float width, float height) {
var halfWidth = width/2;
var halfHeight = height/2;
public static Mesh quad(float width, float height, float originX, float originY) {
var vertices = new float[] {
-halfWidth, -halfHeight,
-halfWidth, halfHeight,
halfWidth, halfHeight,
halfWidth, -halfHeight
-originX, -originY,
-originX, height - originY,
width - originX, height - originY,
width - originX, - originY
};
var texCoords = new float[] { 0, 0, 0, 1, 1, 1, 1, 0 };

View File

@@ -1,5 +1,5 @@
package com.bartlomiejpluta.base.core.world.animation;
public interface Animator {
void animate(AnimationableObject[] objects);
void animate(Iterable<AnimationableObject> objects);
}

View File

@@ -6,7 +6,7 @@ import org.springframework.stereotype.Component;
public class DefaultAnimator implements Animator {
@Override
public void animate(AnimationableObject[] objects) {
public void animate(Iterable<AnimationableObject> objects) {
for (var object : objects) {
animate(object);
}

View File

@@ -1,27 +1,37 @@
package com.bartlomiejpluta.base.core.world.map;
import com.bartlomiejpluta.base.core.world.tileset.model.Tile;
import com.bartlomiejpluta.base.core.world.tileset.model.TileSet;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.joml.Vector2f;
import java.util.Arrays;
import java.util.Objects;
@Slf4j
public class GameMap {
private static final int LAYERS = 4;
private final TileSet tileSet;
private final Tile[][] map;
private final float scale;
@Getter
private final Vector2f stepSize;
@Getter
private final int rows;
@Getter
private final int cols;
public GameMap(int rows, int cols, float scale) {
public GameMap(TileSet tileSet, int rows, int cols, float scale) {
this.tileSet = tileSet;
this.rows = rows;
this.cols = cols;
this.scale = scale;
this.stepSize = new Vector2f(this.scale * this.tileSet.getTileWidth(), this.scale * this.tileSet.getTileHeight());
map = new Tile[LAYERS][rows * cols];
}
@@ -32,9 +42,7 @@ public class GameMap {
private void recalculateTileGeometry(Tile tile, int i, int j) {
tile.setScale(scale);
var size = tile.getWidth();
var offset = size * scale;
tile.setPosition(i * offset, j * offset);
tile.setPosition(i * stepSize.x, j * stepSize.y);
}
public Tile[] getLayer(int layer) {

View File

@@ -4,19 +4,31 @@ import com.bartlomiejpluta.base.core.gl.render.Renderable;
import com.bartlomiejpluta.base.core.gl.shader.constant.UniformName;
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
import com.bartlomiejpluta.base.core.ui.Window;
import com.bartlomiejpluta.base.core.world.animation.AnimationableObject;
import com.bartlomiejpluta.base.core.world.animation.Animator;
import com.bartlomiejpluta.base.core.world.camera.Camera;
import com.bartlomiejpluta.base.core.world.object.RenderableObject;
import com.bartlomiejpluta.base.core.world.map.GameMap;
import lombok.AllArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@AllArgsConstructor
public class Scene implements Renderable {
private final Animator animator;
private final Camera camera;
private final List<AnimationableObject> objects = new ArrayList<>();
@Setter
private GameMap map;
public Scene addObject(AnimationableObject object) {
objects.add(object);
return this;
}
@Override
public void render(Window window, ShaderManager shaderManager) {
shaderManager.setUniform(UniformName.UNI_PROJECTION_MATRIX, camera.getProjectionMatrix(window));
@@ -26,11 +38,21 @@ public class Scene implements Renderable {
renderArray(map.getLayer(1), window, shaderManager);
// Player will be rendered here
renderList(objects, window, shaderManager);
animator.animate(objects);
renderArray(map.getLayer(2), window, shaderManager);
renderArray(map.getLayer(3), window, shaderManager);
}
private <T extends RenderableObject> void renderList(List<T> objects, Window window, ShaderManager shaderManager) {
for (var object : objects) {
if (object != null) {
renderObject(object, window, shaderManager);
}
}
}
private <T extends RenderableObject> void renderArray(T[] objects, Window window, ShaderManager shaderManager) {
for (var object : objects) {
if (object != null) {

View File

@@ -2,28 +2,19 @@ package com.bartlomiejpluta.base.core.world.tileset.manager;
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
import com.bartlomiejpluta.base.core.world.tileset.model.TileSet;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DefaultTileSetManager implements TileSetManager {
private final TextureManager textureManager;
private final int tileWidth;
private final int tileHeight;
@Autowired
public DefaultTileSetManager(
TextureManager textureManager,
@Value("${app.map.tile.width}") int tileWidth,
@Value("${app.map.tile.width}") int tileHeight) {
this.textureManager = textureManager;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
}
@Override
public TileSet createTileSet(String tileSetFileName, int rows, int columns) {
return new TileSet(textureManager.loadTexture(tileSetFileName), rows, columns, tileWidth, tileHeight);
var texture = textureManager.loadTexture(tileSetFileName);
return new TileSet(texture, rows, columns, texture.getWidth() / columns, texture.getHeight() / rows);
}
}

View File

@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.core.world.tileset.model;
import com.bartlomiejpluta.base.core.gl.object.material.Material;
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
import com.bartlomiejpluta.base.core.gl.object.texture.Texture;
import lombok.Getter;
public class TileSet {
private final Texture texture;
@@ -10,10 +11,14 @@ public class TileSet {
private final int columns;
private final float columnStep;
private final float rowStep;
private final int tileWidth;
private final int tileHeight;
private final Mesh mesh;
@Getter
private final int tileWidth;
@Getter
private final int tileHeight;
public TileSet(Texture texture, int rows, int columns, int tileWidth, int tileHeight) {
this.texture = texture;
this.rows = rows;
@@ -22,7 +27,7 @@ public class TileSet {
this.rowStep = 1/(float) rows;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.mesh = Mesh.quad(tileWidth, tileHeight);
this.mesh = Mesh.quad(tileWidth, tileHeight, 0, 0);
}
public Tile getTile(int m, int n) {