Skip to content

Commit

Permalink
Fix remote positions missing of MirrorXY (#148)
Browse files Browse the repository at this point in the history
* Fix remote positions missing of MirrorXY

* Fix ApplyRandomTransform() of HexagonalRuleTile missing MirrorXY case

* Simplified

* Fix RuleOverrideTile does not refresh when add/remove rule

* Fix random rotation calculation mistake

* Caching all RuleTile neighbor positions for Tilemap to speedup refresh affected tiles

* Fix cache data will not update when rule change
  • Loading branch information
johnsoncodehk authored and ChuanXin-Unity committed Nov 6, 2019
1 parent 41e4770 commit 9f18d66
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 159 deletions.
37 changes: 6 additions & 31 deletions Editor/Tiles/HexagonalRuleTile/HexagonalRuleTileEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,14 @@ public override BoundsInt GetRuleGUIBounds(BoundsInt bounds, RuleTile.TilingRule
break;
}
}
if (extendNeighbor)
{
bounds.xMin--;
bounds.yMin--;
bounds.xMax++;
bounds.yMax++;
}
bounds.xMin = Mathf.Min(bounds.xMin, -1);
bounds.yMin = Mathf.Min(bounds.yMin, -1);
bounds.xMax = Mathf.Max(bounds.xMax, 2);
bounds.yMax = Mathf.Max(bounds.yMax, 2);
return bounds;
return base.GetRuleGUIBounds(bounds, rule);
}

public override Vector2 GetMatrixSize(BoundsInt bounds)
{
var hexTile = tile as HexagonalRuleTile;
Vector2 size = base.GetMatrixSize(bounds);

if (hexTile.m_FlatTop)
{
float x = size.x;
float y = size.y;
size.x = y;
size.y = x;
}

return size;
return hexTile.m_FlatTop ? new Vector2(size.y, size.x) : size;
}

public override void RuleMatrixOnGUI(RuleTile tile, Rect rect, BoundsInt bounds, RuleTile.TilingRule tilingRule)
Expand Down Expand Up @@ -158,12 +138,8 @@ public override void RuleMatrixOnGUI(RuleTile tile, Rect rect, BoundsInt bounds,
{
Vector3Int pos = new Vector3Int(x, y, 0);
Vector2 offset = new Vector2(x - bounds.xMin, -y + bounds.yMax - 1);
Rect r;

if (flatTop)
r = new Rect(rect.xMin + offset.y * w, rect.yMax - offset.x * h - h, w - 1, h - 1);
else
r = new Rect(rect.xMin + offset.x * w, rect.yMin + offset.y * h, w - 1, h - 1);
Rect r = flatTop ? new Rect(rect.xMin + offset.y * w, rect.yMax - offset.x * h - h, w - 1, h - 1)
: new Rect(rect.xMin + offset.x * w, rect.yMin + offset.y * h, w - 1, h - 1);

if (y % 2 != 0)
{
Expand All @@ -182,16 +158,15 @@ public override void RuleMatrixOnGUI(RuleTile tile, Rect rect, BoundsInt bounds,
}
if (RuleNeighborUpdate(r, tilingRule, neighbors, pos))
{
tile.UpdateRemoteRulePositions();
tile.UpdateNeighborPositions();
}
}
else
{
// Center
RuleTransformOnGUI(r, tilingRule.m_RuleTransform);
if (RuleTransformUpdate(r, tilingRule))
{
tile.UpdateRemoteRulePositions();
tile.UpdateNeighborPositions();
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Editor/Tiles/IsometricRuleTile/IsometricRuleTileEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public override void RuleMatrixOnGUI(RuleTile ruleTile, Rect rect, BoundsInt bou
for (int y = 0; y <= bounds.size.y; y++)
{
float left = rect.xMin + d * y;
float right = rect.xMax - d * (bounds.size.y - y);
float top = rect.yMin + d * y;
float right = rect.xMax - d * (bounds.size.y - y);
float bottom = rect.yMax - d * (bounds.size.y - y);
Handles.DrawLine(new Vector3(left, bottom), new Vector3(right, top));
}
for (int x = 0; x <= bounds.size.x; x++)
{
float left = rect.xMin + d * x;
float right = rect.xMax - d * (bounds.size.x - x);
float top = rect.yMax - d * x;
float right = rect.xMax - d * (bounds.size.x - x);
float bottom = rect.yMin + d * (bounds.size.x - x);
Handles.DrawLine(new Vector3(left, bottom), new Vector3(right, top));
}
Expand All @@ -53,8 +53,8 @@ public override void RuleMatrixOnGUI(RuleTile ruleTile, Rect rect, BoundsInt bou
float iconSize = rect.width / (bounds.size.x + bounds.size.y);
var rect2 = new Rect(rect);
rect2.xMin += iconSize * 0.5f;
rect2.xMax -= iconSize * 0.5f;
rect2.yMin += iconSize * 0.5f;
rect2.xMax -= iconSize * 0.5f;
rect2.yMax -= iconSize * 0.5f;
iconSize = rect2.width / (bounds.size.x + bounds.size.y - 1);
float p = Mathf.Pow(2, 0.5f);
Expand Down Expand Up @@ -82,15 +82,15 @@ public override void RuleMatrixOnGUI(RuleTile ruleTile, Rect rect, BoundsInt bou
}
if (RuleNeighborUpdate(r, tilingRule, neighbors, pos))
{
tile.UpdateRemoteRulePositions();
tile.UpdateNeighborPositions();
}
}
else
{
RuleTransformOnGUI(r, tilingRule.m_RuleTransform);
if (RuleTransformUpdate(r, tilingRule))
{
tile.UpdateRemoteRulePositions();
tile.UpdateNeighborPositions();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Editor/Tiles/RuleTile/RuleTileEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void OnEnable()
m_ReorderableList.drawHeaderCallback = OnDrawHeader;
m_ReorderableList.drawElementCallback = OnDrawElement;
m_ReorderableList.elementHeightCallback = GetElementHeight;
m_ReorderableList.onReorderCallback = ListUpdated;
m_ReorderableList.onChangedCallback = ListUpdated;
m_ReorderableList.onAddCallback = OnAddElement;
}

Expand Down Expand Up @@ -424,15 +424,15 @@ public virtual void RuleMatrixOnGUI(RuleTile tile, Rect rect, BoundsInt bounds,
}
if (RuleNeighborUpdate(r, tilingRule, neighbors, pos))
{
tile.UpdateRemoteRulePositions();
tile.UpdateNeighborPositions();
}
}
else
{
RuleTransformOnGUI(r, tilingRule.m_RuleTransform);
if (RuleTransformUpdate(r, tilingRule))
{
tile.UpdateRemoteRulePositions();
tile.UpdateNeighborPositions();
}
}
}
Expand Down
41 changes: 0 additions & 41 deletions Runtime/Tiles/HexagonalRuleTile/HexagonalRuleTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,7 @@ public class HexagonalRuleTile<T> : HexagonalRuleTile
public class HexagonalRuleTile : RuleTile
{

/// <summary>
/// Returns the number of neighbors a Rule Tile can have.
/// </summary>
public int neighborCount => 6;

public override int m_RotationAngle => 60;
public override Vector3Int[] m_NearbyNeighborPositions => new Vector3Int[] {
new Vector3Int(-1, 1, 0),
new Vector3Int(0, 1, 0),
new Vector3Int(-1, 0, 0),
new Vector3Int(1, 0, 0),
new Vector3Int(-1, -1, 0),
new Vector3Int(0, -1, 0),
};
public override bool IsNearbyNeighborPosition(Vector3Int position)
{
return (position.x >= -1 && position.x <= 0 && position.y >= -1 && position.y <= 1) || position == Vector3Int.right;
}

private static float[] m_CosAngleArr1 = {
Mathf.Cos(0 * Mathf.Deg2Rad),
Expand Down Expand Up @@ -125,30 +108,6 @@ protected override Vector3Int GetOffsetPositionReverse(Vector3Int position, Vect
return location;
}

/// <summary>
/// Returns a random transform matrix given the random transform rule.
/// </summary>
/// <param name="type">Random transform rule.</param>
/// <param name="original">The original transform matrix.</param>
/// <param name="perlinScale">The Perlin Scale factor of the Tile.</param>
/// <param name="position">Position of the Tile on the Tilemap.</param>
/// <returns>A random transform matrix.</returns>
protected override Matrix4x4 ApplyRandomTransform(TilingRule.Transform type, Matrix4x4 original, float perlinScale, Vector3Int position)
{
float perlin = GetPerlinValue(position, perlinScale, 200000f);
switch (type)
{
case TilingRule.Transform.MirrorX:
return original * Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(perlin < 0.5 ? 1f : -1f, 1f, 1f));
case TilingRule.Transform.MirrorY:
return original * Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1f, perlin < 0.5 ? 1f : -1f, 1f));
case TilingRule.Transform.Rotated:
int angle = Mathf.Clamp(Mathf.FloorToInt(perlin * neighborCount), 0, neighborCount - 1) * (360 / neighborCount);
return Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -angle), Vector3.one);
}
return original;
}

/// <summary>
/// Gets a rotated position given its original position and the rotation in degrees.
/// </summary>
Expand Down
Loading

0 comments on commit 9f18d66

Please sign in to comment.