Make EntityManager shares common Mesh 1x1 between all entities, which in turn scale it to theirs dimensions | add support for dynamic replacing entity sets
This commit is contained in:
@@ -30,4 +30,6 @@ public interface Entity extends Placeable, Movable, Renderable, Updatable {
|
|||||||
boolean isBlocking();
|
boolean isBlocking();
|
||||||
|
|
||||||
void setBlocking(boolean blocking);
|
void setBlocking(boolean blocking);
|
||||||
|
|
||||||
|
void changeEntitySet(String entitySetUid);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -209,6 +209,11 @@ public abstract class EntityDelegate implements Entity {
|
|||||||
entity.setBlocking(blocking);
|
entity.setBlocking(blocking);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void changeEntitySet(String entitySetUid) {
|
||||||
|
entity.changeEntitySet(entitySetUid);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
entity.update(dt);
|
entity.update(dt);
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.bartlomiejpluta.base.engine.common.init;
|
||||||
|
|
||||||
|
public interface Initianizable {
|
||||||
|
void init();
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.engine.core.engine;
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
|
import com.bartlomiejpluta.base.engine.common.init.Initianizable;
|
||||||
import com.bartlomiejpluta.base.engine.gc.OffHeapGarbageCollector;
|
import com.bartlomiejpluta.base.engine.gc.OffHeapGarbageCollector;
|
||||||
import com.bartlomiejpluta.base.engine.logic.GameLogic;
|
import com.bartlomiejpluta.base.engine.logic.GameLogic;
|
||||||
import com.bartlomiejpluta.base.engine.thread.ThreadManager;
|
import com.bartlomiejpluta.base.engine.thread.ThreadManager;
|
||||||
@@ -13,6 +14,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
@@ -23,6 +26,7 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
private final ThreadManager threadManager;
|
private final ThreadManager threadManager;
|
||||||
private final GameLogic logic;
|
private final GameLogic logic;
|
||||||
private final OffHeapGarbageCollector garbageCollector;
|
private final OffHeapGarbageCollector garbageCollector;
|
||||||
|
private final List<Initianizable> initianizables;
|
||||||
|
|
||||||
private final ChronoMeter chrono = new ChronoMeter();
|
private final ChronoMeter chrono = new ChronoMeter();
|
||||||
|
|
||||||
@@ -51,6 +55,11 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
log.info("Initializing game engine");
|
log.info("Initializing game engine");
|
||||||
screen.init();
|
screen.init();
|
||||||
chrono.init();
|
chrono.init();
|
||||||
|
|
||||||
|
initianizables.stream()
|
||||||
|
.peek(i -> log.info("Initializing {}", i.getClass().getSimpleName()))
|
||||||
|
.forEach(Initianizable::init);
|
||||||
|
|
||||||
logic.init(screen, context);
|
logic.init(screen, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package com.bartlomiejpluta.base.engine.world.entity.manager;
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
|
||||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||||
import com.bartlomiejpluta.base.engine.util.mesh.MeshManager;
|
import com.bartlomiejpluta.base.engine.util.mesh.MeshManager;
|
||||||
import com.bartlomiejpluta.base.engine.world.entity.config.EntitySpriteConfiguration;
|
import com.bartlomiejpluta.base.engine.world.entity.config.EntitySpriteConfiguration;
|
||||||
@@ -29,6 +28,8 @@ public class DefaultEntityManager implements EntityManager {
|
|||||||
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
||||||
private final Vector2ic entitySpriteDimension;
|
private final Vector2ic entitySpriteDimension;
|
||||||
|
|
||||||
|
private Mesh mesh;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public DefaultEntityManager(MeshManager meshManager, EntitySetManager entitySetManager, EntitySpriteConfiguration configuration) {
|
public DefaultEntityManager(MeshManager meshManager, EntitySetManager entitySetManager, EntitySpriteConfiguration configuration) {
|
||||||
this.meshManager = meshManager;
|
this.meshManager = meshManager;
|
||||||
@@ -45,16 +46,13 @@ public class DefaultEntityManager implements EntityManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Entity createEntity(String entitySetUid) {
|
public void init() {
|
||||||
var material = entitySetManager.loadObject(entitySetUid);
|
mesh = meshManager.createQuad(1, 1, 0.5f, 1);
|
||||||
return new DefaultEntity(buildMesh(material), material, spriteDirectionRows, spriteDefaultRows);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mesh buildMesh(Material material) {
|
@Override
|
||||||
var texture = material.getTexture();
|
public Entity createEntity(String entitySetUid) {
|
||||||
var spriteWidth = texture.getWidth() / (float) entitySpriteDimension.x();
|
return new DefaultEntity(mesh, entitySetManager, spriteDirectionRows, spriteDefaultRows, entitySetUid);
|
||||||
var spriteHeight = texture.getHeight() / (float) entitySpriteDimension.y();
|
|
||||||
return meshManager.createQuad(spriteWidth, spriteHeight, spriteWidth / 2, spriteHeight * 0.9f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package com.bartlomiejpluta.base.engine.world.entity.manager;
|
package com.bartlomiejpluta.base.engine.world.entity.manager;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||||
|
import com.bartlomiejpluta.base.engine.common.init.Initianizable;
|
||||||
import com.bartlomiejpluta.base.internal.gc.Cleanable;
|
import com.bartlomiejpluta.base.internal.gc.Cleanable;
|
||||||
|
|
||||||
public interface EntityManager extends Cleanable {
|
public interface EntityManager extends Initianizable, Cleanable {
|
||||||
Entity createEntity(String entitySetUid);
|
Entity createEntity(String entitySetUid);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import com.bartlomiejpluta.base.api.entity.Entity;
|
|||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
import com.bartlomiejpluta.base.api.move.Movement;
|
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.mesh.Mesh;
|
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||||
|
import com.bartlomiejpluta.base.engine.world.entity.manager.EntitySetManager;
|
||||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||||
import com.bartlomiejpluta.base.util.math.MathUtil;
|
import com.bartlomiejpluta.base.util.math.MathUtil;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@@ -17,10 +17,15 @@ import org.joml.Vector2i;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class DefaultEntity extends MovableSprite implements Entity {
|
public class DefaultEntity extends MovableSprite implements Entity {
|
||||||
|
private final EntitySetManager entitySetManager;
|
||||||
private final Map<Direction, Integer> spriteDirectionRows;
|
private final Map<Direction, Integer> spriteDirectionRows;
|
||||||
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
||||||
|
private final Vector2f entityScale = new Vector2f(1, 1);
|
||||||
|
private Vector2fc entitySetSize;
|
||||||
|
|
||||||
private int animationSpeed = 100;
|
private int animationSpeed = 100;
|
||||||
|
|
||||||
@@ -31,11 +36,22 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean blocking;
|
private boolean blocking;
|
||||||
|
|
||||||
public DefaultEntity(Mesh mesh, Material material, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows) {
|
public DefaultEntity(Mesh mesh, EntitySetManager entitySetManager, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows, String entitySetUid) {
|
||||||
super(mesh, material);
|
super(mesh, entitySetManager.loadObject(requireNonNull(entitySetUid)));
|
||||||
|
this.entitySetManager = entitySetManager;
|
||||||
this.spriteDirectionRows = spriteDirectionRows;
|
this.spriteDirectionRows = spriteDirectionRows;
|
||||||
this.faceDirection = Direction.DOWN;
|
this.faceDirection = Direction.DOWN;
|
||||||
this.spriteDefaultRows = spriteDefaultRows;
|
this.spriteDefaultRows = spriteDefaultRows;
|
||||||
|
|
||||||
|
this.entitySetSize = material.getTexture().getSpriteSize();
|
||||||
|
super.setScale(entitySetSize.x() * entityScale.x, entitySetSize.y() * entityScale.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void changeEntitySet(String entitySetUid) {
|
||||||
|
this.material = entitySetManager.loadObject(requireNonNull(entitySetUid));
|
||||||
|
this.entitySetSize = material.getTexture().getSpriteSize();
|
||||||
|
super.setScale(entitySetSize.x() * entityScale.x, entitySetSize.y() * entityScale.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -111,4 +127,40 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
|||||||
public void onRemove(ObjectLayer layer) {
|
public void onRemove(ObjectLayer layer) {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScaleX(float scaleX) {
|
||||||
|
this.entityScale.x = scaleX;
|
||||||
|
super.setScaleX(entitySetSize.x() * scaleX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScaleY(float scaleY) {
|
||||||
|
this.entityScale.y = scaleY;
|
||||||
|
super.setScaleY(entitySetSize.y() * scaleY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScale(float scale) {
|
||||||
|
this.entityScale.x = scale;
|
||||||
|
this.entityScale.y = scale;
|
||||||
|
super.setScale(entitySetSize.x() * scale, entitySetSize.y() * scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScale(float scaleX, float scaleY) {
|
||||||
|
this.entityScale.x = scaleX;
|
||||||
|
this.entityScale.y = scaleY;
|
||||||
|
super.setScale(entitySetSize.x() * scaleX, entitySetSize.y() * scaleY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getScaleX() {
|
||||||
|
return entityScale.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getScaleY() {
|
||||||
|
return entityScale.y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,12 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
|||||||
private Vector2f movementVector;
|
private Vector2f movementVector;
|
||||||
private int framesToCrossOneTile = 1;
|
private int framesToCrossOneTile = 1;
|
||||||
|
|
||||||
|
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
||||||
|
|
||||||
|
;
|
||||||
|
|
||||||
|
private PlacingMode placingMode;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private Movement movement;
|
private Movement movement;
|
||||||
|
|
||||||
@@ -103,6 +109,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
|||||||
coordinates.x = x;
|
coordinates.x = x;
|
||||||
coordinates.y = y;
|
coordinates.y = y;
|
||||||
super.setPosition((x + 0.5f) * coordinateStepSize.x + positionOffset.x, (y + 0.5f) * coordinateStepSize.y + positionOffset.y);
|
super.setPosition((x + 0.5f) * coordinateStepSize.x + positionOffset.x, (y + 0.5f) * coordinateStepSize.y + positionOffset.y);
|
||||||
|
placingMode = PlacingMode.BY_COORDINATES;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -110,6 +117,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
|||||||
super.setPosition(x, y);
|
super.setPosition(x, y);
|
||||||
coordinates.x = (int) ((x - positionOffset.x) / coordinateStepSize.x);
|
coordinates.x = (int) ((x - positionOffset.x) / coordinateStepSize.x);
|
||||||
coordinates.y = (int) ((y - positionOffset.y) / coordinateStepSize.y);
|
coordinates.y = (int) ((y - positionOffset.y) / coordinateStepSize.y);
|
||||||
|
placingMode = PlacingMode.BY_POSITION;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -117,6 +125,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
|||||||
super.setPosition(position);
|
super.setPosition(position);
|
||||||
coordinates.x = (int) ((position.x() - positionOffset.x) / coordinateStepSize.x);
|
coordinates.x = (int) ((position.x() - positionOffset.x) / coordinateStepSize.x);
|
||||||
coordinates.y = (int) ((position.y() - positionOffset.y) / coordinateStepSize.y);
|
coordinates.y = (int) ((position.y() - positionOffset.y) / coordinateStepSize.y);
|
||||||
|
placingMode = PlacingMode.BY_POSITION;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -128,10 +137,9 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
|||||||
coordinateStepSize.x = x;
|
coordinateStepSize.x = x;
|
||||||
coordinateStepSize.y = y;
|
coordinateStepSize.y = y;
|
||||||
|
|
||||||
if (!position.equals(0, 0)) {
|
switch (placingMode) {
|
||||||
setPosition(position);
|
case BY_POSITION -> setPosition(position);
|
||||||
} else {
|
case BY_COORDINATES -> setCoordinates(coordinates);
|
||||||
setCoordinates(coordinates);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user