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();
|
||||
|
||||
void setBlocking(boolean blocking);
|
||||
|
||||
void changeEntitySet(String entitySetUid);
|
||||
}
|
||||
|
||||
@@ -209,6 +209,11 @@ public abstract class EntityDelegate implements Entity {
|
||||
entity.setBlocking(blocking);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeEntitySet(String entitySetUid) {
|
||||
entity.changeEntitySet(entitySetUid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float 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.screen.Screen;
|
||||
import com.bartlomiejpluta.base.engine.common.init.Initianizable;
|
||||
import com.bartlomiejpluta.base.engine.gc.OffHeapGarbageCollector;
|
||||
import com.bartlomiejpluta.base.engine.logic.GameLogic;
|
||||
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.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
@@ -23,6 +26,7 @@ public class DefaultGameEngine implements GameEngine {
|
||||
private final ThreadManager threadManager;
|
||||
private final GameLogic logic;
|
||||
private final OffHeapGarbageCollector garbageCollector;
|
||||
private final List<Initianizable> initianizables;
|
||||
|
||||
private final ChronoMeter chrono = new ChronoMeter();
|
||||
|
||||
@@ -51,6 +55,11 @@ public class DefaultGameEngine implements GameEngine {
|
||||
log.info("Initializing game engine");
|
||||
screen.init();
|
||||
chrono.init();
|
||||
|
||||
initianizables.stream()
|
||||
.peek(i -> log.info("Initializing {}", i.getClass().getSimpleName()))
|
||||
.forEach(Initianizable::init);
|
||||
|
||||
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.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.util.mesh.MeshManager;
|
||||
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 Vector2ic entitySpriteDimension;
|
||||
|
||||
private Mesh mesh;
|
||||
|
||||
@Autowired
|
||||
public DefaultEntityManager(MeshManager meshManager, EntitySetManager entitySetManager, EntitySpriteConfiguration configuration) {
|
||||
this.meshManager = meshManager;
|
||||
@@ -45,16 +46,13 @@ public class DefaultEntityManager implements EntityManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity createEntity(String entitySetUid) {
|
||||
var material = entitySetManager.loadObject(entitySetUid);
|
||||
return new DefaultEntity(buildMesh(material), material, spriteDirectionRows, spriteDefaultRows);
|
||||
public void init() {
|
||||
mesh = meshManager.createQuad(1, 1, 0.5f, 1);
|
||||
}
|
||||
|
||||
private Mesh buildMesh(Material material) {
|
||||
var texture = material.getTexture();
|
||||
var spriteWidth = texture.getWidth() / (float) entitySpriteDimension.x();
|
||||
var spriteHeight = texture.getHeight() / (float) entitySpriteDimension.y();
|
||||
return meshManager.createQuad(spriteWidth, spriteHeight, spriteWidth / 2, spriteHeight * 0.9f);
|
||||
@Override
|
||||
public Entity createEntity(String entitySetUid) {
|
||||
return new DefaultEntity(mesh, entitySetManager, spriteDirectionRows, spriteDefaultRows, entitySetUid);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.bartlomiejpluta.base.engine.world.entity.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.engine.common.init.Initianizable;
|
||||
import com.bartlomiejpluta.base.internal.gc.Cleanable;
|
||||
|
||||
public interface EntityManager extends Cleanable {
|
||||
public interface EntityManager extends Initianizable, Cleanable {
|
||||
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.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.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.world.entity.manager.EntitySetManager;
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
import com.bartlomiejpluta.base.util.math.MathUtil;
|
||||
import lombok.EqualsAndHashCode;
|
||||
@@ -17,10 +17,15 @@ import org.joml.Vector2i;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class DefaultEntity extends MovableSprite implements Entity {
|
||||
private final EntitySetManager entitySetManager;
|
||||
private final Map<Direction, Integer> spriteDirectionRows;
|
||||
private final Map<Direction, Vector2fc> spriteDefaultRows;
|
||||
private final Vector2f entityScale = new Vector2f(1, 1);
|
||||
private Vector2fc entitySetSize;
|
||||
|
||||
private int animationSpeed = 100;
|
||||
|
||||
@@ -31,11 +36,22 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
@Setter
|
||||
private boolean blocking;
|
||||
|
||||
public DefaultEntity(Mesh mesh, Material material, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows) {
|
||||
super(mesh, material);
|
||||
public DefaultEntity(Mesh mesh, EntitySetManager entitySetManager, Map<Direction, Integer> spriteDirectionRows, Map<Direction, Vector2fc> spriteDefaultRows, String entitySetUid) {
|
||||
super(mesh, entitySetManager.loadObject(requireNonNull(entitySetUid)));
|
||||
this.entitySetManager = entitySetManager;
|
||||
this.spriteDirectionRows = spriteDirectionRows;
|
||||
this.faceDirection = Direction.DOWN;
|
||||
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
|
||||
@@ -111,4 +127,40 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
public void onRemove(ObjectLayer layer) {
|
||||
// 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 int framesToCrossOneTile = 1;
|
||||
|
||||
private enum PlacingMode {BY_POSITION, BY_COORDINATES}
|
||||
|
||||
;
|
||||
|
||||
private PlacingMode placingMode;
|
||||
|
||||
@Getter
|
||||
private Movement movement;
|
||||
|
||||
@@ -103,6 +109,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
coordinates.x = x;
|
||||
coordinates.y = y;
|
||||
super.setPosition((x + 0.5f) * coordinateStepSize.x + positionOffset.x, (y + 0.5f) * coordinateStepSize.y + positionOffset.y);
|
||||
placingMode = PlacingMode.BY_COORDINATES;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -110,6 +117,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
super.setPosition(x, y);
|
||||
coordinates.x = (int) ((x - positionOffset.x) / coordinateStepSize.x);
|
||||
coordinates.y = (int) ((y - positionOffset.y) / coordinateStepSize.y);
|
||||
placingMode = PlacingMode.BY_POSITION;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -117,6 +125,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
super.setPosition(position);
|
||||
coordinates.x = (int) ((position.x() - positionOffset.x) / coordinateStepSize.x);
|
||||
coordinates.y = (int) ((position.y() - positionOffset.y) / coordinateStepSize.y);
|
||||
placingMode = PlacingMode.BY_POSITION;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -128,10 +137,9 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
coordinateStepSize.x = x;
|
||||
coordinateStepSize.y = y;
|
||||
|
||||
if (!position.equals(0, 0)) {
|
||||
setPosition(position);
|
||||
} else {
|
||||
setCoordinates(coordinates);
|
||||
switch (placingMode) {
|
||||
case BY_POSITION -> setPosition(position);
|
||||
case BY_COORDINATES -> setCoordinates(coordinates);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user