Introduce MeshManager and EntityManager
This commit is contained in:
@@ -1,28 +1,43 @@
|
||||
package com.bartlomiejpluta.base.game.logic;
|
||||
|
||||
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
|
||||
import com.bartlomiejpluta.base.core.gl.render.Renderer;
|
||||
import com.bartlomiejpluta.base.core.logic.GameLogic;
|
||||
import com.bartlomiejpluta.base.core.ui.Window;
|
||||
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||
import com.bartlomiejpluta.base.core.world.animation.Animator;
|
||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||
import com.bartlomiejpluta.base.core.world.scene.Scene;
|
||||
import com.bartlomiejpluta.base.core.world.map.GameMap;
|
||||
import com.bartlomiejpluta.base.core.world.movement.Direction;
|
||||
import com.bartlomiejpluta.base.core.world.scene.Scene;
|
||||
import com.bartlomiejpluta.base.core.world.tileset.manager.TileSetManager;
|
||||
import com.bartlomiejpluta.base.game.world.entity.Entity;
|
||||
import com.bartlomiejpluta.base.game.world.entity.EntityManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static org.lwjgl.glfw.GLFW.*;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class DefaultGameLogic implements GameLogic {
|
||||
private final Renderer renderer;
|
||||
private final TileSetManager tileSetManager;
|
||||
private final MeshManager meshManager;
|
||||
private final TextureManager textureManager;
|
||||
private final EntityManager entityManager;
|
||||
private final Animator animator;
|
||||
|
||||
private Camera camera;
|
||||
private GameMap map;
|
||||
private Scene scene;
|
||||
|
||||
private Entity player;
|
||||
|
||||
@Override
|
||||
public void init(Window window) {
|
||||
log.info("Initializing game logic");
|
||||
@@ -36,6 +51,7 @@ public class DefaultGameLogic implements GameLogic {
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.bartlomiejpluta.base.game.world.entity;
|
||||
|
||||
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.joml.Vector2f;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class DefaultEntityManager implements EntityManager {
|
||||
private final MeshManager meshManager;
|
||||
private final EntitySpriteConfiguration configuration;
|
||||
|
||||
@Override
|
||||
public Entity createEntity(Material material, Vector2f coordinateStepSize) {
|
||||
return new Entity(buildMesh(material), material, coordinateStepSize, configuration);
|
||||
}
|
||||
|
||||
private Mesh buildMesh(Material material) {
|
||||
var texture = material.getTexture();
|
||||
var dimension = configuration.getDimension().asVector();
|
||||
var spriteWidth = texture.getWidth() / (float) dimension.y;
|
||||
var spriteHeight = texture.getHeight() / (float) dimension.x;
|
||||
return meshManager.createQuad(spriteWidth, spriteHeight, spriteWidth / 2, spriteHeight);
|
||||
}
|
||||
}
|
||||
@@ -6,19 +6,12 @@ import com.bartlomiejpluta.base.core.world.movement.Direction;
|
||||
import com.bartlomiejpluta.base.core.world.movement.MovableObject;
|
||||
import lombok.Setter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Entity extends MovableObject {
|
||||
private static final Vector2i SPRITE_DIMENSION = new Vector2i(4, 4);
|
||||
private static final int DEFAULT_SPRITE = 0;
|
||||
private static final Map<Direction, Integer> SPRITE_ROWS = Map.of(
|
||||
Direction.DOWN, 0,
|
||||
Direction.LEFT, 1,
|
||||
Direction.RIGHT, 2,
|
||||
Direction.UP, 3
|
||||
);
|
||||
private final Map<Direction, Integer> spriteDirectionRows;
|
||||
private final int defaultSpriteColumn;
|
||||
|
||||
@Setter
|
||||
private int animationSpeed = 100;
|
||||
@@ -31,11 +24,6 @@ public class Entity extends MovableObject {
|
||||
return 100;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2i getSpriteSheetDimensions() {
|
||||
return SPRITE_DIMENSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldAnimate() {
|
||||
return isMoving();
|
||||
@@ -43,13 +31,13 @@ public class Entity extends MovableObject {
|
||||
|
||||
@Override
|
||||
public Vector2f[] getSpriteAnimationFramesPositions() {
|
||||
var row = SPRITE_ROWS.get(faceDirection);
|
||||
var row = spriteDirectionRows.get(faceDirection);
|
||||
return new Vector2f[]{new Vector2f(0, row), new Vector2f(1, row), new Vector2f(2, row), new Vector2f(3, row)};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setDefaultAnimationFrame() {
|
||||
setAnimationFrame(new Vector2f(DEFAULT_SPRITE, SPRITE_ROWS.get(faceDirection)));
|
||||
setAnimationFrame(new Vector2f(defaultSpriteColumn, spriteDirectionRows.get(faceDirection)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,14 +58,9 @@ public class Entity extends MovableObject {
|
||||
return framesToCrossOneTile;
|
||||
}
|
||||
|
||||
public Entity(Material material, Vector2f coordinateStepSize) {
|
||||
super(buildMesh(material), material, coordinateStepSize);
|
||||
}
|
||||
|
||||
private static Mesh buildMesh(Material material) {
|
||||
var texture = material.getTexture();
|
||||
var spriteWidth = texture.getWidth() / (float) SPRITE_DIMENSION.x;
|
||||
var spriteHeight = texture.getHeight() / (float) SPRITE_DIMENSION.y;
|
||||
return Mesh.quad(spriteWidth, spriteHeight, spriteWidth / 2, spriteHeight);
|
||||
public Entity(Mesh mesh, Material material, Vector2f coordinateStepSize, EntitySpriteConfiguration configuration) {
|
||||
super(mesh, material, coordinateStepSize, configuration.getDimension().asVector());
|
||||
defaultSpriteColumn = configuration.getDefaultSpriteColumn();
|
||||
spriteDirectionRows = configuration.getSpriteDirectionRows();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.bartlomiejpluta.base.game.world.entity;
|
||||
|
||||
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||
import org.joml.Vector2f;
|
||||
|
||||
public interface EntityManager {
|
||||
Entity createEntity(Material material, Vector2f coordinateStepSize);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.bartlomiejpluta.base.game.world.entity;
|
||||
|
||||
import com.bartlomiejpluta.base.core.world.movement.Direction;
|
||||
import lombok.Data;
|
||||
import org.joml.Vector2i;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "app.sprite.entity")
|
||||
public class EntitySpriteConfiguration {
|
||||
private EntitySpriteDimensionConfiguration dimension;
|
||||
private int defaultSpriteColumn;
|
||||
private Map<Direction, Integer> spriteDirectionRows;
|
||||
|
||||
@Data
|
||||
public static class EntitySpriteDimensionConfiguration {
|
||||
private int rows;
|
||||
private int cols;
|
||||
|
||||
public Vector2i asVector() {
|
||||
return new Vector2i(rows, cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,4 +5,18 @@ app:
|
||||
height: 480
|
||||
|
||||
core:
|
||||
targetUps: 50 # Updates per second
|
||||
targetUps: 50 # Updates per second
|
||||
|
||||
sprite:
|
||||
entity:
|
||||
dimension:
|
||||
rows: 4
|
||||
cols: 4
|
||||
|
||||
default-sprite-column: 0
|
||||
|
||||
sprite-direction-rows:
|
||||
down: 0
|
||||
left: 1
|
||||
right: 2
|
||||
up: 3
|
||||
|
||||
Reference in New Issue
Block a user