Skip to content

Commit 20d7fad

Browse files
committed
Update to current sorting code from Kingdom of Night
1 parent 3929340 commit 20d7fad

15 files changed

+791
-128
lines changed

.vsconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": "1.0",
3+
"components": [
4+
"Microsoft.VisualStudio.Workload.ManagedGame"
5+
]
6+
}

Assets/Resources.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Resources/BillingMode.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"androidStore":"GooglePlay"}

Assets/Resources/BillingMode.json.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/IsoSpriteSorting.cs

Lines changed: 94 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
45

@@ -7,26 +8,39 @@ public class IsoSpriteSorting : MonoBehaviour
78
public bool isMovable;
89
public bool renderBelowAll;
910

11+
[NonSerialized]
12+
public bool registered = false;
1013
[NonSerialized]
1114
public bool forceSort;
1215

1316
[NonSerialized]
14-
public List<IsoSpriteSorting> staticDependencies = new List<IsoSpriteSorting>(16);
17+
public readonly List<IsoSpriteSorting> staticDependencies = new List<IsoSpriteSorting>(16);
18+
[NonSerialized]
19+
public readonly List<IsoSpriteSorting> inverseStaticDependencies = new List<IsoSpriteSorting>(16);
1520
[NonSerialized]
16-
public List<IsoSpriteSorting> inverseStaticDependencies = new List<IsoSpriteSorting>(16);
17-
public List<IsoSpriteSorting> movingDependencies = new List<IsoSpriteSorting>(8);
21+
public readonly List<IsoSpriteSorting> movingDependencies = new List<IsoSpriteSorting>(8);
1822

1923
private readonly List<IsoSpriteSorting> visibleStaticDependencies = new List<IsoSpriteSorting>(16);
20-
private List<IsoSpriteSorting> activeDependencies = new List<IsoSpriteSorting>(16);
21-
public List<IsoSpriteSorting> ActiveDependencies
24+
25+
private int visibleStaticLastRefreshFrame = 0;
26+
public List<IsoSpriteSorting> VisibleStaticDependencies
2227
{
2328
get
2429
{
25-
activeDependencies.Clear();
26-
IsoSpriteSortingManager.FilterListByVisibility(staticDependencies, visibleStaticDependencies);
27-
activeDependencies.AddRange(visibleStaticDependencies);
28-
activeDependencies.AddRange(movingDependencies);
29-
return activeDependencies;
30+
if (visibleStaticLastRefreshFrame < Time.frameCount)
31+
{
32+
IsoSpriteSortingManager.FilterListByVisibility(staticDependencies, visibleStaticDependencies);
33+
visibleStaticLastRefreshFrame = Time.frameCount;
34+
}
35+
return visibleStaticDependencies;
36+
}
37+
}
38+
39+
public List<IsoSpriteSorting> VisibleMovingDependencies
40+
{
41+
get
42+
{
43+
return movingDependencies;
3044
}
3145
}
3246

@@ -40,21 +54,67 @@ public enum SortType
4054

4155
public Vector3 SorterPositionOffset = new Vector3();
4256
public Vector3 SorterPositionOffset2 = new Vector3();
57+
public Renderer[] renderersToSort;
4358

4459
private Transform t;
4560

61+
public void SetupStaticCache()
62+
{
63+
RefreshBounds();
64+
RefreshPoint1();
65+
RefreshPoint2();
66+
}
67+
68+
private void RefreshBounds()
69+
{
70+
cachedBounds = new Bounds2D(renderersToSort[0].bounds);
71+
}
72+
73+
private void RefreshPoint1()
74+
{
75+
cachedPoint1 = SorterPositionOffset + t.position;
76+
}
77+
78+
private void RefreshPoint2()
79+
{
80+
cachedPoint2 = SorterPositionOffset2 + t.position;
81+
}
82+
83+
private int lastPoint1CalculatedFrame;
84+
private Vector2 cachedPoint1;
4685
private Vector3 SortingPoint1
4786
{
4887
get
4988
{
50-
return SorterPositionOffset + t.position;
89+
if (isMovable)
90+
{
91+
int frameCount = Time.frameCount;
92+
if (frameCount != lastPoint1CalculatedFrame)
93+
{
94+
lastPoint1CalculatedFrame = frameCount;
95+
RefreshPoint1();
96+
}
97+
}
98+
return cachedPoint1;
5199
}
52100
}
101+
102+
private int lastPoint2CalculatedFrame;
103+
private Vector2 cachedPoint2;
53104
private Vector3 SortingPoint2
54105
{
55106
get
56107
{
57-
return SorterPositionOffset2 + t.position;
108+
if (isMovable)
109+
{
110+
int frameCount = Time.frameCount;
111+
if (frameCount != lastPoint2CalculatedFrame)
112+
{
113+
lastPoint2CalculatedFrame = frameCount;
114+
RefreshPoint2();
115+
}
116+
}
117+
return cachedPoint2;
58118
}
59119
}
60120

@@ -69,20 +129,6 @@ public Vector3 AsPoint
69129
}
70130
}
71131

72-
public bool IsNear(Vector3 point, float distance)
73-
{
74-
if (sortType == SortType.Point)
75-
{
76-
return Vector2.Distance(point, SortingPoint1) <= distance;
77-
}
78-
else
79-
{
80-
bool nearPoint1 = Vector2.Distance(point, SortingPoint1) <= distance;
81-
bool nearPoint2 = Vector2.Distance(point, SortingPoint2) <= distance;
82-
return nearPoint1 || nearPoint2;
83-
}
84-
}
85-
86132
private float SortingLineCenterHeight
87133
{
88134
get
@@ -99,8 +145,6 @@ private float SortingLineCenterHeight
99145
}
100146
}
101147

102-
public Renderer[] renderersToSort;
103-
104148
#if UNITY_EDITOR
105149
public void SortScene()
106150
{
@@ -118,27 +162,28 @@ public void SortScene()
118162
}
119163
#endif
120164

121-
void Awake()
165+
private void Awake()
166+
{
167+
t = transform; //This needs to be here AND in the setup function
168+
}
169+
170+
IEnumerator Start()
122171
{
123172
if (Application.isPlaying)
124173
{
125174
IsoSpriteSortingManager temp = IsoSpriteSortingManager.Instance; //bring the instance into existence so the Update function will run;
175+
yield return null;
126176
Setup();
127177
}
128178
}
129179

130180
private void Setup()
131181
{
132-
t = transform;
182+
t = transform; //This needs to be here AND in the Awake function
133183
if (renderersToSort == null || renderersToSort.Length == 0)
134184
{
135-
renderersToSort = new Renderer[] { GetComponent<Renderer>() };
136-
}
137-
if (!isMovable)
138-
{
139-
cachedBounds = new Bounds2D(renderersToSort[0].bounds);
185+
renderersToSort = GetComponentsInChildren<Renderer>();
140186
}
141-
System.Array.Sort(renderersToSort, (a, b) => a.sortingOrder.CompareTo(b.sortingOrder));
142187
IsoSpriteSortingManager.RegisterSprite(this);
143188
}
144189

@@ -149,10 +194,12 @@ public static int CompairIsoSortersBasic(IsoSpriteSorting sprite1, IsoSpriteSort
149194
return y2.CompareTo(y1);
150195
}
151196

197+
//A result of -1 means sprite1 is above sprite2 in physical space
152198
public static int CompareIsoSorters(IsoSpriteSorting sprite1, IsoSpriteSorting sprite2)
153199
{
154200
if (sprite1.sortType == SortType.Point && sprite2.sortType == SortType.Point)
155201
{
202+
//Debug.Log(sprite1.name + " - " + sprite1.SortingPoint1 + " sprite2: " + sprite2.name + " - " + sprite2.SortingPoint1);
156203
return sprite2.SortingPoint1.y.CompareTo(sprite1.SortingPoint1.y);
157204
}
158205
else if (sprite1.sortType == SortType.Line && sprite2.sortType == SortType.Line)
@@ -183,22 +230,22 @@ private static int CompareLineAndLine(IsoSpriteSorting line1, IsoSpriteSorting l
183230
int comp1 = ComparePointAndLine(line1Point1, line2);
184231
int comp2 = ComparePointAndLine(line1Point2, line2);
185232
int oneVStwo = int.MinValue;
186-
if (comp1 == comp2)
233+
if (comp1 == comp2) //Both points in line 1 are above or below line2
187234
{
188235
oneVStwo = comp1;
189236
}
190237

191238
int comp3 = ComparePointAndLine(line2Point1, line1);
192239
int comp4 = ComparePointAndLine(line2Point2, line1);
193240
int twoVSone = int.MinValue;
194-
if (comp3 == comp4)
241+
if (comp3 == comp4) //Both points in line 2 are above or below line1
195242
{
196243
twoVSone = -comp3;
197244
}
198245

199246
if (oneVStwo != int.MinValue && twoVSone != int.MinValue)
200247
{
201-
if (oneVStwo == twoVSone)
248+
if (oneVStwo == twoVSone) //the two comparisons agree about the ordering
202249
{
203250
return oneVStwo;
204251
}
@@ -274,18 +321,21 @@ public int RendererSortingOrder
274321
}
275322

276323
private Bounds2D cachedBounds;
324+
private int lastBoundsCalculatedFrame = 0;
277325
public Bounds2D TheBounds
278326
{
279327
get
280328
{
281329
if (isMovable)
282330
{
283-
return new Bounds2D(renderersToSort[0].bounds);
284-
}
285-
else
286-
{
287-
return cachedBounds;
331+
int frameCount = Time.frameCount;
332+
if (frameCount != lastBoundsCalculatedFrame)
333+
{
334+
lastBoundsCalculatedFrame = frameCount;
335+
RefreshBounds();
336+
}
288337
}
338+
return cachedBounds;
289339
}
290340
}
291341

Assets/Scripts/IsoSpriteSortingEditor.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@ public void OnSceneGUI()
1111
{
1212
IsoSpriteSorting myTarget = (IsoSpriteSorting)target;
1313

14-
myTarget.SorterPositionOffset = Handles.FreeMoveHandle(myTarget.transform.position + myTarget.SorterPositionOffset, Quaternion.identity, 0.08f * HandleUtility.GetHandleSize(myTarget.transform.position), Vector3.zero, Handles.DotCap) - myTarget.transform.position;
14+
myTarget.SorterPositionOffset = Handles.FreeMoveHandle(
15+
myTarget.transform.position + myTarget.SorterPositionOffset,
16+
Quaternion.identity,
17+
0.08f * HandleUtility.GetHandleSize(myTarget.transform.position),
18+
Vector3.zero,
19+
Handles.DotHandleCap
20+
) - myTarget.transform.position;
1521
if (myTarget.sortType == IsoSpriteSorting.SortType.Line)
1622
{
17-
myTarget.SorterPositionOffset2 = Handles.FreeMoveHandle(myTarget.transform.position + myTarget.SorterPositionOffset2, Quaternion.identity, 0.08f * HandleUtility.GetHandleSize(myTarget.transform.position), Vector3.zero, Handles.DotCap) - myTarget.transform.position;
23+
myTarget.SorterPositionOffset2 = Handles.FreeMoveHandle(
24+
myTarget.transform.position + myTarget.SorterPositionOffset2,
25+
Quaternion.identity,
26+
0.08f * HandleUtility.GetHandleSize(myTarget.transform.position),
27+
Vector3.zero,
28+
Handles.DotHandleCap
29+
) - myTarget.transform.position;
1830
Handles.DrawLine(myTarget.transform.position + myTarget.SorterPositionOffset, myTarget.transform.position + myTarget.SorterPositionOffset2);
19-
2031
}
2132
if (GUI.changed)
2233
{

0 commit comments

Comments
 (0)