From de2271c0b28dda11b8109a9efd1a144cefae57cc Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Mon, 16 Feb 2015 01:03:04 +0600 Subject: [PATCH] begin search for one object sorting --- Assets/IsoTools/Scripts/IsoObject.cs | 6 +-- Assets/IsoTools/Scripts/IsoWorld.cs | 67 +++++++++++++++++++++++++--- UnityIso-csharp.sln | 2 +- UnityIso.sln | 2 +- UnityIso.userprefs | 6 +-- 5 files changed, 69 insertions(+), 14 deletions(-) diff --git a/Assets/IsoTools/Scripts/IsoObject.cs b/Assets/IsoTools/Scripts/IsoObject.cs index 858e41c..9353d2f 100644 --- a/Assets/IsoTools/Scripts/IsoObject.cs +++ b/Assets/IsoTools/Scripts/IsoObject.cs @@ -9,7 +9,7 @@ namespace IsoTools { public class IsoObject : MonoBehaviour { Transform _transform = null; - Vector3 _lastTransform = Vector3.zero; + Vector2 _lastTransform = Vector2.zero; Vector3 _lastPosition = Vector3.zero; Vector3 _lastSize = Vector3.zero; bool _lastSorting = false; @@ -122,7 +122,7 @@ namespace IsoTools { void MartDirtyIsoWorld() { var iso_world = GetIsoWorld(); if ( iso_world && Sorting ) { - iso_world.MarkDirty(); + iso_world.MarkDirty(this); } } @@ -141,7 +141,7 @@ namespace IsoTools { } void Update() { - if ( _lastTransform != _transform.position ) { + if ( _lastTransform.x != _transform.position.x || _lastTransform.y != _transform.position.y ) { FixIsoPosition(); } if ( Application.isEditor ) { diff --git a/Assets/IsoTools/Scripts/IsoWorld.cs b/Assets/IsoTools/Scripts/IsoWorld.cs index 357b1d2..e9ec11c 100644 --- a/Assets/IsoTools/Scripts/IsoWorld.cs +++ b/Assets/IsoTools/Scripts/IsoWorld.cs @@ -12,13 +12,13 @@ namespace IsoTools { } /// World tile type. - public TileTypes TileType = TileTypes.Isometric; + public TileTypes TileType = TileTypes.Isometric; /// Isometric tile size. - public float TileSize = 32.0f; + public float TileSize = 32.0f; /// Start sorting depth value. - public float StartDepth = 0.0f; + public float MinDepth = 0.0f; /// Step sorting depth value. - public float StepDepth = 0.1f; + public float MaxDepth = 100.0f; class ObjectInfo { public IsoObject IsoObject; @@ -47,6 +47,16 @@ namespace IsoTools { public void MarkDirty() { _dirty = true; } + + /// + /// Marks world for resorting one object only + /// + /// Isometric object for resorting. + public void MarkDirty(IsoObject obj) { + if ( !_dirty ) { + _manualSort(obj); + } + } // ------------------------------------------------------------------------ /// @@ -127,17 +137,20 @@ namespace IsoTools { } void _fixTileSize() { + MarkDirty(); _fixAllTransforms(); _lastTileSize = TileSize; } void _fixTileType() { + MarkDirty(); _fixAllTransforms(); _lastTileType = TileType; } void _fixDirty() { _manualSort(); + Debug.Log("Resort!"); _dirty = false; } @@ -186,12 +199,54 @@ namespace IsoTools { void _manualSort() { var objects = _scanObjects(true); var depends = _scanDepends(objects); - var depth = StartDepth; + var depth = MinDepth; foreach ( var info in objects ) { _placeObject(info, objects, depends, ref depth); } } + bool _isDepends(IsoObject obj_ao, IsoObject obj_bo) { + if ( obj_ao != obj_bo ) { + var max_ax = obj_ao.Position.x + obj_ao.Size.x; + var max_ay = obj_ao.Position.y + obj_ao.Size.y; + if ( obj_bo.Position.x < max_ax && obj_bo.Position.y < max_ay ) { + var max_bz = obj_bo.Position.z + obj_bo.Size.z; + if ( obj_ao.Position.z < max_bz ) { + return true; + } + } + } + return false; + } + + void _manualSort(IsoObject obj) { + var objects = _scanObjects(true); + var min_depth = float.MinValue; + foreach ( var obj_b in objects ) { + if ( _isDepends(obj, obj_b.IsoObject) ) { + min_depth = Mathf.Max(min_depth, obj_b.IsoObject.transform.position.z); + } + } + var max_depth = float.MaxValue; + foreach ( var obj_a in objects ) { + if ( _isDepends(obj_a.IsoObject, obj) ) { + max_depth = Mathf.Min(max_depth, obj_a.IsoObject.transform.position.z); + } + } + if ( max_depth == float.MaxValue ) { + max_depth = MaxDepth; + } + if ( min_depth == float.MinValue ) { + min_depth = MinDepth; + } + //TODO: Epsilon!!!!! + if ( Mathf.Abs(max_depth - min_depth) <= Mathf.Epsilon ) { + MarkDirty(); + } else { + _placeObject(obj, (min_depth + max_depth) / 2.0f); + } + } + void _placeObject(IsoObject obj, float depth) { var pos = obj.gameObject.transform.position; obj.gameObject.transform.position = new Vector3(pos.x, pos.y, depth); @@ -206,7 +261,7 @@ namespace IsoTools { _placeObject(obj, objects, depends, ref depth); } _placeObject(info.IsoObject, depth); - depth += StepDepth; + depth += (MaxDepth - MinDepth) / objects.Count; } } diff --git a/UnityIso-csharp.sln b/UnityIso-csharp.sln index 7e11405..4495f34 100644 --- a/UnityIso-csharp.sln +++ b/UnityIso-csharp.sln @@ -17,7 +17,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(MonoDevelopProperties) = preSolution + GlobalSection(MonoDevelopProperties) = preSolution StartupItem = Assembly-CSharp.csproj Policies = $0 $0.TextStylePolicy = $1 diff --git a/UnityIso.sln b/UnityIso.sln index dc2eebb..36ea20d 100644 --- a/UnityIso.sln +++ b/UnityIso.sln @@ -17,7 +17,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(MonoDevelopProperties) = preSolution + GlobalSection(MonoDevelopProperties) = preSolution StartupItem = Assembly-CSharp.csproj Policies = $0 $0.TextStylePolicy = $1 diff --git a/UnityIso.userprefs b/UnityIso.userprefs index 9d1e2af..2ea8c33 100644 --- a/UnityIso.userprefs +++ b/UnityIso.userprefs @@ -1,9 +1,9 @@  - + - - + +