Skip to content

Commit

Permalink
feat: support parallax scroll factor. #9
Browse files Browse the repository at this point in the history
  • Loading branch information
闫茂源 committed Apr 17, 2024
1 parent 5d55dd6 commit ea9a89b
Showing 1 changed file with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import io.github.jmecn.tiled.core.GroupLayer;
import io.github.jmecn.tiled.core.Layer;
import io.github.jmecn.tiled.math2d.Point;
import io.github.jmecn.tiled.core.TiledMap;
import io.github.jmecn.tiled.enums.ZoomMode;
Expand All @@ -47,8 +49,9 @@ public class TiledMapAppState extends BaseAppState implements AnalogListener, Ac
public static final String ZOOMIN = "zoomin";
public static final String ZOOMOUT = "zoomout";
public static final String GRID = "grid";
public static final String PARALLAX = "parallax";

private static final String[] MAPPINGS = new String[] { LEFT, RIGHT, UP, DOWN, DRAG, ZOOMIN, ZOOMOUT, GRID };
private static final String[] MAPPINGS = new String[] { LEFT, RIGHT, UP, DOWN, DRAG, ZOOMIN, ZOOMOUT, GRID, PARALLAX };

// Tiled Map
private TiledMap map;
Expand All @@ -70,6 +73,11 @@ public class TiledMapAppState extends BaseAppState implements AnalogListener, Ac
private final ColorRGBA cursorColorUnavailable = new ColorRGBA(0.8f, 0.2f, 0.2f, 0.5f);
private boolean isCursorUpdated = true;

// The parallax
private boolean isParallaxEnabled = true;
private Vector2f parallaxOrigin = new Vector2f(0, 0);
private Vector2f parallaxDistance = new Vector2f(0, 0);

// The mapNode
private final Vector3f mapTranslation;
private float mapScale;
Expand Down Expand Up @@ -228,6 +236,7 @@ public void update(float tpf) {
mapScale = getMapScale();
spatial.setLocalTranslation(mapTranslation);
spatial.setLocalScale(mapScale, 1f, mapScale);
calculateMapParallax();

isMapUpdated = false;
}
Expand Down Expand Up @@ -273,7 +282,7 @@ public void setMap(TiledMap map) {
if (viewPort != null) {
viewPort.setBackgroundColor(map.getBackgroundColor());
}

if (this.map != map) {
this.map = map;
}
Expand All @@ -296,7 +305,7 @@ public void setMap(TiledMap map) {
mapRenderer = new OrthogonalRenderer(map);
}

Vector2f loc = mapRenderer.tileToScreenCoords(0, 0);
Vector2f loc = mapRenderer.pixelToScreenCoords(map.getParallaxOriginX(), map.getParallaxOriginY());
mapTranslation.set(loc.x, 0, loc.y);
isMapUpdated = true;

Expand Down Expand Up @@ -343,7 +352,53 @@ public void moveToPixel(float x, float y) {
}

private void moveMapVisual() {
map.getVisual().setLocalTranslation((float) Math.floor(mapTranslation.x), (float) Math.floor(mapTranslation.y), (float)Math.floor(mapTranslation.z));
float x = (float) Math.floor(mapTranslation.x);
float y = (float) Math.floor(mapTranslation.y);
float z = (float) Math.floor(mapTranslation.z);
map.getVisual().setLocalTranslation(x, y, z);

calculateMapParallax();
}

private void calculateMapParallax() {
if (isParallaxEnabled) {
// record the parallax origin
parallaxOrigin.set(map.getParallaxOriginX(), map.getParallaxOriginY());
Vector2f current = getCameraPixelCoordinate();
current.subtract(parallaxOrigin, parallaxDistance);
} else {
parallaxDistance.set(0, 0);
}

applyParallax(parallaxDistance);
}

private void applyParallax(Vector2f distance) {
for (Layer layer : map.getLayers()) {
if (!layer.isVisible()) {
continue;
}
applyParallax(layer, distance);
}
}

private void applyParallax(Layer layer, Vector2f distance) {
if (!layer.isVisible()) {
return;
}

if (layer instanceof GroupLayer) {
for (Layer child : ((GroupLayer) layer).getLayers()) {
applyParallax(child, distance);
}
} else {
// When the camera moves, the layer moves in relation to the camera by a factor of the parallax scrolling factor.
// As move mapVisual means move the map, so we need to move the layer in the opposite direction, 1.0-parallaxFactor
float x = (1f - layer.getParallaxX()) * distance.x;
float y = layer.getVisual().getLocalTranslation().y;
float z = (1f - layer.getParallaxY()) * distance.y;
layer.getVisual().setLocalTranslation(x, y, z);
}
}

public float getMapScale() {
Expand Down Expand Up @@ -534,7 +589,7 @@ private void drag() {

// move camera
mapTranslation.set(startLoc.add(stopPos.x, 0, -stopPos.y));
map.getVisual().setLocalTranslation(mapTranslation);
moveMapVisual();
}

private void moveCursor() {
Expand Down

0 comments on commit ea9a89b

Please sign in to comment.