Skip to content

Commit

Permalink
Merge pull request #17 from fourlastor-alexandria/fix-viewport
Browse files Browse the repository at this point in the history
Fix viewport
  • Loading branch information
fourlastor committed Apr 18, 2023
2 parents 51ac786 + 6b0b2b2 commit 14621fb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ By default, each animation will be played as `PlayMode.LOOP`, you can specify a

## Parallax images

You can create a `new ParallaxImage(textureOrDrawable, factor)` to have an image which moves slower/faster than the `Camera` movement.
You can create a `new ParallaxImage(textureOrDrawable, factorX, factorY)` to have an image which moves slower/faster than the `Camera` movement.

Currently `ParallaxImage` fills the entire viewport, and the factor is applied to both X and Y.
Currently `ParallaxImage` fills the entire viewport.

## Ashley extension

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public StageSystem(Stage stage, Class<? extends Enum<?>> layers, ComponentMapper

@Override
public void update(float deltaTime) {
stage.getViewport().apply();
stage.act(deltaTime);
stage.draw();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
*/
public class ParallaxImage extends Actor {

private final float factor;
private final Vector2 factor;

private final Vector2 currentDelta = new Vector2();
private final TiledDrawable drawable;

public ParallaxImage(TextureRegion textureRegion, float factor) {
this(new TiledDrawable(textureRegion), factor);
public ParallaxImage(TextureRegion textureRegion, float factorX, float factorY) {
this(new TiledDrawable(textureRegion), factorX, factorY);
}

public ParallaxImage(TextureRegionDrawable textureRegionDrawable, float factor) {
this(new TiledDrawable(textureRegionDrawable), factor);
public ParallaxImage(TextureRegionDrawable textureRegionDrawable, float factorX, float factorY) {
this(new TiledDrawable(textureRegionDrawable), new Vector2(factorX, factorY));
}

public ParallaxImage(TiledDrawable drawable, float factor) {
public ParallaxImage(TiledDrawable drawable, Vector2 factor) {
super();
setBounds(0f, 0f, drawable.getMinWidth(), drawable.getMinHeight());
setPosition(0f, 0f);
Expand All @@ -48,8 +48,8 @@ public void act(float delta) {
super.act(delta);
Camera camera = Objects.requireNonNull(getStage()).getCamera();

currentDelta.x = -(camera.position.x * factor);
currentDelta.y = -(camera.position.y * factor);
currentDelta.x = -(camera.position.x * factor.x);
currentDelta.y = -(camera.position.y * factor.y);
}

@Override
Expand Down

0 comments on commit 14621fb

Please sign in to comment.