Improve ImageLayer model on the :game module side
This commit is contained in:
@@ -12,45 +12,56 @@ public class ImageLayer implements Layer {
|
|||||||
private final float mapWidth;
|
private final float mapWidth;
|
||||||
private final float mapHeight;
|
private final float mapHeight;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
private Image image;
|
private Image image;
|
||||||
private float imageInitialWidth;
|
private float imageInitialWidth;
|
||||||
private float imageInitialHeight;
|
private float imageInitialHeight;
|
||||||
|
private float gcd;
|
||||||
|
|
||||||
|
private float x;
|
||||||
|
private float y;
|
||||||
|
private float scaleX;
|
||||||
|
private float scaleY;
|
||||||
|
|
||||||
@NonNull
|
@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.mapWidth = map.getWidth();
|
||||||
this.mapHeight = map.getHeight();
|
this.mapHeight = map.getHeight();
|
||||||
this.mode = mode;
|
|
||||||
|
|
||||||
setImage(image);
|
|
||||||
setOpacity(opacity);
|
|
||||||
setPosition(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setImage(Image image) {
|
|
||||||
this.image = image;
|
this.image = image;
|
||||||
this.imageInitialWidth = image.getInitialWidth();
|
this.imageInitialWidth = image.getInitialWidth();
|
||||||
this.imageInitialHeight = image.getInitialHeight();
|
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) {
|
switch (mode) {
|
||||||
case NORMAL -> image.setScale(image.getGcd() * imageInitialWidth, image.getGcd() * imageInitialHeight);
|
case NORMAL -> image.setScale(mapWidth / imageInitialWidth, mapHeight / imageInitialHeight);
|
||||||
case FIT_MAP -> 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
|
@Override
|
||||||
public void render(Window window, Camera camera, ShaderManager shaderManager) {
|
public void render(Window window, Camera camera, ShaderManager shaderManager) {
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
|
if (parallax) {
|
||||||
|
var cameraPosition = camera.getPosition();
|
||||||
|
image.setPosition(cameraPosition.x + x, cameraPosition.y + y);
|
||||||
|
}
|
||||||
|
|
||||||
if (mode == ImageLayerMode.FIT_SCREEN) {
|
if (mode == ImageLayerMode.FIT_SCREEN) {
|
||||||
image.setScale(window.getWidth() / imageInitialWidth, window.getHeight() / imageInitialHeight);
|
image.setScale(window.getWidth() / imageInitialWidth, window.getHeight() / imageInitialHeight);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ package com.bartlomiejpluta.base.game.map.layer.image;
|
|||||||
|
|
||||||
public enum ImageLayerMode {
|
public enum ImageLayerMode {
|
||||||
NORMAL,
|
NORMAL,
|
||||||
FIT_SCREEN,
|
FIT_MAP,
|
||||||
FIT_MAP
|
FIT_SCREEN
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,8 +97,8 @@ public class GameMap implements Renderable, Updatable {
|
|||||||
return layers.size() - 1;
|
return layers.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int createImageLayer(Image image, ImageLayerMode imageDisplayMode, float opacity, float x, float 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, imageDisplayMode, opacity, x, y));
|
layers.add(new ImageLayer(this, image, opacity, x, y, scaleX, scaleY, mode, parallax));
|
||||||
|
|
||||||
return layers.size() - 1;
|
return layers.size() - 1;
|
||||||
}
|
}
|
||||||
@@ -145,12 +145,6 @@ public class GameMap implements Renderable, Updatable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameMap setImage(int layerIndex, Image image) {
|
|
||||||
((ImageLayer) layers.get(layerIndex)).setImage(image);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isMovementPossible(int layerIndex, Movement movement) {
|
public boolean isMovementPossible(int layerIndex, Movement movement) {
|
||||||
var target = movement.getTargetCoordinate();
|
var target = movement.getTargetCoordinate();
|
||||||
|
|
||||||
|
|||||||
@@ -94,13 +94,23 @@ public class ProtobufMapDeserializer extends MapDeserializer {
|
|||||||
|
|
||||||
private void deserializeImageLayer(GameMap map, GameMapProto.Layer proto) {
|
private void deserializeImageLayer(GameMap map, GameMapProto.Layer proto) {
|
||||||
var protoImageLayer = proto.getImageLayer();
|
var protoImageLayer = proto.getImageLayer();
|
||||||
var image = imageManager.loadImage(proto.getImageLayer().getImageUID());
|
var image = imageManager.loadImage(protoImageLayer.getImageUID());
|
||||||
|
|
||||||
var mode = switch (proto.getImageLayer().getMode()) {
|
var mode = switch (proto.getImageLayer().getMode()) {
|
||||||
case NORMAL -> ImageLayerMode.NORMAL;
|
case NORMAL -> ImageLayerMode.NORMAL;
|
||||||
case FIT_MAP -> ImageLayerMode.FIT_MAP;
|
case FIT_MAP -> ImageLayerMode.FIT_MAP;
|
||||||
case FIT_SCREEN -> ImageLayerMode.FIT_SCREEN;
|
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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user