Improve ImageLayer model on the :game module side

This commit is contained in:
2021-02-22 13:57:54 +01:00
parent 02e9554b1a
commit 5b01d3bee9
4 changed files with 45 additions and 30 deletions

View File

@@ -12,45 +12,56 @@ public class ImageLayer implements Layer {
private final float mapWidth;
private final float mapHeight;
@NonNull
private Image image;
private float imageInitialWidth;
private float imageInitialHeight;
private float gcd;
private float x;
private float y;
private float scaleX;
private float scaleY;
@NonNull
private final ImageLayerMode mode;
private ImageLayerMode mode;
public ImageLayer(GameMap map, Image image, ImageLayerMode mode, float opacity, float x, float y) {
private boolean parallax;
public ImageLayer(GameMap map, Image image, float opacity, float x, float y, float scaleX, float scaleY, ImageLayerMode mode, boolean parallax) {
this.mapWidth = map.getWidth();
this.mapHeight = map.getHeight();
this.mode = mode;
setImage(image);
setOpacity(opacity);
setPosition(x, y);
}
public void setImage(Image image) {
this.image = image;
this.imageInitialWidth = image.getInitialWidth();
this.imageInitialHeight = image.getInitialHeight();
this.gcd = image.getGcd();
this.x = x;
this.y = y;
this.scaleX = scaleX;
this.scaleY = scaleY;
this.mode = mode;
this.parallax = parallax;
this.image.getMaterial().setAlpha(opacity);
this.image.setPosition(x, y);
switch (mode) {
case NORMAL -> image.setScale(image.getGcd() * imageInitialWidth, image.getGcd() * imageInitialHeight);
case FIT_MAP -> image.setScale(mapWidth / imageInitialWidth, mapHeight / imageInitialHeight);
case NORMAL -> image.setScale(mapWidth / imageInitialWidth, mapHeight / imageInitialHeight);
case FIT_MAP -> image.setScale(image.getGcd() * imageInitialWidth * scaleX, image.getGcd() * imageInitialHeight * scaleY);
}
}
public void setOpacity(float opacity) {
this.image.getMaterial().setAlpha(opacity);
}
public void setPosition(float x, float y) {
image.setPosition(x, y);
}
@Override
public void render(Window window, Camera camera, ShaderManager shaderManager) {
if (image != null) {
if (parallax) {
var cameraPosition = camera.getPosition();
image.setPosition(cameraPosition.x + x, cameraPosition.y + y);
}
if (mode == ImageLayerMode.FIT_SCREEN) {
image.setScale(window.getWidth() / imageInitialWidth, window.getHeight() / imageInitialHeight);
}

View File

@@ -2,6 +2,6 @@ package com.bartlomiejpluta.base.game.map.layer.image;
public enum ImageLayerMode {
NORMAL,
FIT_SCREEN,
FIT_MAP
FIT_MAP,
FIT_SCREEN
}

View File

@@ -97,8 +97,8 @@ public class GameMap implements Renderable, Updatable {
return layers.size() - 1;
}
public int createImageLayer(Image image, ImageLayerMode imageDisplayMode, float opacity, float x, float y) {
layers.add(new ImageLayer(this, image, imageDisplayMode, opacity, x, y));
public int createImageLayer(Image image, float opacity, float x, float y, float scaleX, float scaleY, ImageLayerMode mode, boolean parallax) {
layers.add(new ImageLayer(this, image, opacity, x, y, scaleX, scaleY, mode, parallax));
return layers.size() - 1;
}
@@ -145,12 +145,6 @@ public class GameMap implements Renderable, Updatable {
return this;
}
public GameMap setImage(int layerIndex, Image image) {
((ImageLayer) layers.get(layerIndex)).setImage(image);
return this;
}
public boolean isMovementPossible(int layerIndex, Movement movement) {
var target = movement.getTargetCoordinate();

View File

@@ -94,13 +94,23 @@ public class ProtobufMapDeserializer extends MapDeserializer {
private void deserializeImageLayer(GameMap map, GameMapProto.Layer proto) {
var protoImageLayer = proto.getImageLayer();
var image = imageManager.loadImage(proto.getImageLayer().getImageUID());
var image = imageManager.loadImage(protoImageLayer.getImageUID());
var mode = switch (proto.getImageLayer().getMode()) {
case NORMAL -> ImageLayerMode.NORMAL;
case FIT_MAP -> ImageLayerMode.FIT_MAP;
case FIT_SCREEN -> ImageLayerMode.FIT_SCREEN;
};
map.createImageLayer(image, mode, protoImageLayer.getOpacity() / 100.0f, protoImageLayer.getX(), protoImageLayer.getY());
map.createImageLayer(
image,
protoImageLayer.getOpacity() / 100.0f,
protoImageLayer.getX(),
protoImageLayer.getY(),
(float) protoImageLayer.getScaleX(),
(float) protoImageLayer.getScaleY(),
mode,
protoImageLayer.getParallax()
);
}
}