Skip to content

Commit

Permalink
added support for looping background
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed Oct 17, 2023
1 parent 4760ea8 commit 70124a3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Assets/LDtkUnity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- Fixed a bug where reordering IntGrid value definitions would use the wrong tile references, and in some cases, cause an exception
- Changed the icons for the imported Project/Level/Tileset to match with the icons from LDtk 1.5

- Added support for looping level backgrounds
- Make the level's looping background pivot to the bottom left for perfect accuracy
- Added a LDtkIid for the LDtkJson root object from LDtk [1.2.0](https://ldtk.io/json/#changes;1.2.0)
- Added support for individual tile alpha from LDtk [1.3.1](https://ldtk.io/json/#changes;1.3.1)
- Added safety-check if a tileset definition's `relPath` was null or empty
Expand Down
20 changes: 15 additions & 5 deletions Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevelBackground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,21 @@ private void BuildBackgroundTexture()
{
return;
}


_layerSortingOrder.Next();

SpriteRenderer renderer = CreateGameObject("_BgImage");
renderer.sprite = sprite;

_layerSortingOrder.Next();
renderer.sortingOrder = _layerSortingOrder.SortingOrderValue;
if (_level.LevelBgPos is BgPos.Repeat)
{
renderer.drawMode = SpriteDrawMode.Tiled;
renderer.size = _worldSpaceSize;
}

ManipulateImageTransform(renderer.transform);
}

private void ManipulateColorTransform(Transform trans)
{
trans.parent = _levelTransform.transform;
Expand All @@ -90,7 +95,12 @@ private void ManipulateImageTransform(Transform trans)
{
LevelBackgroundPosition bgPos = _level.BgPos;
Vector2 scale = bgPos.UnityScale;
Vector2 levelPosition = LDtkCoordConverter.LevelBackgroundImagePosition(bgPos.UnityTopLeftPx, _importer.PixelsPerUnit, _level.PxHei);

Vector2 levelPosition = new Vector2(0, 0)
{
x = _worldSpaceSize.x * _level.BgPivotX,
y = _worldSpaceSize.y * (1 - _level.BgPivotY)
};

trans.parent = _levelTransform.transform;
trans.localPosition = levelPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public LDtkBackgroundArtifactFactory(string assetName, Texture2D srcTex, int pix
{
Rect rect = lvl.BgPos.UnityCropRect;
rect.position = LDtkCoordConverter.LevelBackgroundImageSliceCoord(rect.position, srcTex.height, rect.height);
_slicer = new LDtkTextureSpriteSlicer(srcTex, rect, pixelsPerUnit, Vector2.up);

Vector2 pivot = lvl.UnityBgPivot;
pivot.y = 1f - pivot.y;

_slicer = new LDtkTextureSpriteSlicer(srcTex, rect, pixelsPerUnit, pivot);
_assetName = assetName;
}

Expand Down
8 changes: 0 additions & 8 deletions Assets/LDtkUnity/Runtime/Tools/LDtkCoordConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ public static Vector2 EntityPivotOffset(Vector2 pivot, Vector2 size)
Vector2 offset = Vector2.right * pivotSize.x * -2;
return pivotSize + offset;
}

//always float based; the source background slice is composed of floats
public static Vector2 LevelBackgroundImagePosition(Vector2 topLeftPx, int pixelsPerUnit, int levelPxHei)
{
topLeftPx = NegateY(topLeftPx);
topLeftPx += Vector2.up * (levelPxHei);
return topLeftPx / pixelsPerUnit;
}

public static Rect ImageSlice(Rect pos, int textureHeight)
{
Expand Down

0 comments on commit 70124a3

Please sign in to comment.