mirror of
https://github.com/BlackMATov/unity-iso-tools.git
synced 2025-12-15 01:12:05 +07:00
go world refactor
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -130,24 +130,26 @@ namespace IsoTools {
|
|||||||
FixLastProperties();
|
FixLastProperties();
|
||||||
FixIsoPosition();
|
FixIsoPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OnEnable() {
|
||||||
|
MartDirtyIsoWorld();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if UNITY_EDITOR
|
||||||
void Update() {
|
void Update() {
|
||||||
var trans_pos = transform.position;
|
|
||||||
if ( !Mathf.Approximately(_lastTransform.x, trans_pos.x) ||
|
|
||||||
!Mathf.Approximately(_lastTransform.y, trans_pos.y) )
|
|
||||||
{
|
|
||||||
FixIsoPosition();
|
|
||||||
}
|
|
||||||
if ( Application.isEditor ) {
|
if ( Application.isEditor ) {
|
||||||
|
var trans_pos = transform.position;
|
||||||
|
if ( !Mathf.Approximately(_lastTransform.x, trans_pos.x) ||
|
||||||
|
!Mathf.Approximately(_lastTransform.y, trans_pos.y) )
|
||||||
|
{
|
||||||
|
FixIsoPosition();
|
||||||
|
}
|
||||||
if ( _lastPosition != _position ) Position = _position;
|
if ( _lastPosition != _position ) Position = _position;
|
||||||
if ( _lastSize != _size ) Size = _size;
|
if ( _lastSize != _size ) Size = _size;
|
||||||
if ( _lastSorting != _sorting ) Sorting = _sorting;
|
if ( _lastSorting != _sorting ) Sorting = _sorting;
|
||||||
if ( _lastAlignment != _alignment ) Alignment = _alignment;
|
if ( _lastAlignment != _alignment ) Alignment = _alignment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
void OnEnable() {
|
|
||||||
MartDirtyIsoWorld();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} // namespace IsoTools
|
} // namespace IsoTools
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace IsoTools {
|
namespace IsoTools {
|
||||||
@@ -10,24 +11,17 @@ namespace IsoTools {
|
|||||||
Isometric,
|
Isometric,
|
||||||
UpDown
|
UpDown
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>World tile type.</summary>
|
|
||||||
public TileTypes TileType = TileTypes.Isometric;
|
|
||||||
/// <summary>Isometric tile size.</summary>
|
|
||||||
public float TileSize = 32.0f;
|
|
||||||
/// <summary>Start sorting depth value.</summary>
|
|
||||||
public float MinDepth = 0.0f;
|
|
||||||
/// <summary>Step sorting depth value.</summary>
|
|
||||||
public float MaxDepth = 100.0f;
|
|
||||||
|
|
||||||
class ObjectInfo {
|
class ObjectInfo {
|
||||||
public IsoObject IsoObject;
|
public IsoObject IsoObject;
|
||||||
public bool Visited;
|
public bool Visited;
|
||||||
public int BeginDepend;
|
public int BeginDepend;
|
||||||
public int EndDepend;
|
public int EndDepend;
|
||||||
|
|
||||||
public ObjectInfo(IsoObject obj) {
|
public ObjectInfo(IsoObject obj) {
|
||||||
IsoObject = obj;
|
IsoObject = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset(int first_depend) {
|
public void Reset(int first_depend) {
|
||||||
Visited = false;
|
Visited = false;
|
||||||
BeginDepend = first_depend;
|
BeginDepend = first_depend;
|
||||||
@@ -35,9 +29,57 @@ namespace IsoTools {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _dirty = true;
|
bool _dirty = true;
|
||||||
float _lastTileSize = 0.0f;
|
HashSet<IsoObject> _dirtyObjects = new HashSet<IsoObject>();
|
||||||
TileTypes _lastTileType = TileTypes.Isometric;
|
|
||||||
|
TileTypes _lastTileType = TileTypes.Isometric;
|
||||||
|
float _lastTileSize = 0.0f;
|
||||||
|
float _lastMinDepth = 0.0f;
|
||||||
|
float _lastMaxDepth = 0.0f;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
public TileTypes _tileType = TileTypes.Isometric;
|
||||||
|
/// <summary>World tile type.</summary>
|
||||||
|
public TileTypes TileType {
|
||||||
|
get { return _tileType; }
|
||||||
|
set {
|
||||||
|
_tileType = value;
|
||||||
|
ChangeSortingProperty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
public float _tileSize = 32.0f;
|
||||||
|
/// <summary>Isometric tile size.</summary>
|
||||||
|
public float TileSize {
|
||||||
|
get { return _tileSize; }
|
||||||
|
set {
|
||||||
|
_tileSize = value;
|
||||||
|
ChangeSortingProperty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
public float _minDepth = 0.0f;
|
||||||
|
/// <summary>Min sorting depth value.</summary>
|
||||||
|
public float MinDepth {
|
||||||
|
get { return _minDepth; }
|
||||||
|
set {
|
||||||
|
_minDepth = value;
|
||||||
|
ChangeSortingProperty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
public float _maxDepth = 100.0f;
|
||||||
|
/// <summary>Max sorting depth value.</summary>
|
||||||
|
public float MaxDepth {
|
||||||
|
get { return _maxDepth; }
|
||||||
|
set {
|
||||||
|
_maxDepth = value;
|
||||||
|
ChangeSortingProperty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -48,14 +90,14 @@ namespace IsoTools {
|
|||||||
_dirty = true;
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marks world for resorting one object only
|
/// Marks world for resorting one object only
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj">Isometric object for resorting.</param>
|
/// <param name="obj">Isometric object for resorting.</param>
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
public void MarkDirty(IsoObject obj) {
|
public void MarkDirty(IsoObject obj) {
|
||||||
if ( !_dirty ) {
|
_dirtyObjects.Add(obj);
|
||||||
_manualSort(obj);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
@@ -129,51 +171,32 @@ namespace IsoTools {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _fixAllTransforms() {
|
void FixAllTransforms() {
|
||||||
var objects = _scanObjects(false);
|
var objects = ScanObjects();
|
||||||
foreach ( var obj in objects ) {
|
foreach ( var obj in objects ) {
|
||||||
obj.IsoObject.FixTransform();
|
obj.IsoObject.FixTransform();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _fixTileSize() {
|
void ChangeSortingProperty() {
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
_fixAllTransforms();
|
FixAllTransforms();
|
||||||
_lastTileSize = TileSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
void _fixTileType() {
|
|
||||||
MarkDirty();
|
|
||||||
_fixAllTransforms();
|
|
||||||
_lastTileType = TileType;
|
_lastTileType = TileType;
|
||||||
}
|
_lastTileSize = TileSize;
|
||||||
|
_lastMinDepth = MinDepth;
|
||||||
void _fixDirty() {
|
_lastMaxDepth = MaxDepth;
|
||||||
_manualSort();
|
|
||||||
Debug.Log("Resort!");
|
|
||||||
_dirty = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void _fixDisable() {
|
|
||||||
var objects = _scanObjects(false);
|
|
||||||
foreach ( var obj in objects ) {
|
|
||||||
obj.IsoObject.ResetIsoWorld();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IList<ObjectInfo> _scanObjects(bool onlySorting) {
|
IList<ObjectInfo> ScanObjects() {
|
||||||
var iso_objects = GameObject.FindObjectsOfType<IsoObject>();
|
var iso_objects = GameObject.FindObjectsOfType<IsoObject>();
|
||||||
var objects = new List<ObjectInfo>(iso_objects.Length);
|
var objects = new List<ObjectInfo>(iso_objects.Length);
|
||||||
foreach ( var iso_object in iso_objects ) {
|
foreach ( var iso_object in iso_objects ) {
|
||||||
if ( !onlySorting || iso_object.Sorting ) {
|
objects.Add(new ObjectInfo(iso_object));
|
||||||
var info = new ObjectInfo(iso_object);
|
|
||||||
objects.Add(info);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return objects;
|
return objects;
|
||||||
}
|
}
|
||||||
|
|
||||||
IList<int> _scanDepends(IList<ObjectInfo> objects) {
|
IList<int> ScanDepends(IList<ObjectInfo> objects) {
|
||||||
var depends = new List<int>(objects.Count);
|
var depends = new List<int>(objects.Count);
|
||||||
foreach ( var obj_a in objects ) {
|
foreach ( var obj_a in objects ) {
|
||||||
obj_a.Reset(depends.Count);
|
obj_a.Reset(depends.Count);
|
||||||
@@ -196,16 +219,15 @@ namespace IsoTools {
|
|||||||
return depends;
|
return depends;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _manualSort() {
|
void ManualSort(IList<ObjectInfo> objects) {
|
||||||
var objects = _scanObjects(true);
|
var depends = ScanDepends(objects);
|
||||||
var depends = _scanDepends(objects);
|
|
||||||
var depth = MinDepth;
|
var depth = MinDepth;
|
||||||
foreach ( var info in objects ) {
|
foreach ( var info in objects ) {
|
||||||
_placeObject(info, objects, depends, ref depth);
|
PlaceObject(info, objects, depends, ref depth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _isDepends(IsoObject obj_ao, IsoObject obj_bo) {
|
bool IsDepends(IsoObject obj_ao, IsoObject obj_bo) {
|
||||||
if ( obj_ao != obj_bo ) {
|
if ( obj_ao != obj_bo ) {
|
||||||
var max_ax = obj_ao.Position.x + obj_ao.Size.x;
|
var max_ax = obj_ao.Position.x + obj_ao.Size.x;
|
||||||
var max_ay = obj_ao.Position.y + obj_ao.Size.y;
|
var max_ay = obj_ao.Position.y + obj_ao.Size.y;
|
||||||
@@ -219,72 +241,95 @@ namespace IsoTools {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _manualSort(IsoObject obj) {
|
void ManualSort(IsoObject obj, IList<ObjectInfo> objects) {
|
||||||
var objects = _scanObjects(true);
|
|
||||||
var min_depth = float.MinValue;
|
var min_depth = float.MinValue;
|
||||||
foreach ( var obj_b in objects ) {
|
foreach ( var obj_b in objects ) {
|
||||||
if ( _isDepends(obj, obj_b.IsoObject) ) {
|
if ( IsDepends(obj, obj_b.IsoObject) ) {
|
||||||
min_depth = Mathf.Max(min_depth, obj_b.IsoObject.transform.position.z);
|
min_depth = Mathf.Max(min_depth, obj_b.IsoObject.transform.position.z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var max_depth = float.MaxValue;
|
var max_depth = float.MaxValue;
|
||||||
foreach ( var obj_a in objects ) {
|
foreach ( var obj_a in objects ) {
|
||||||
if ( _isDepends(obj_a.IsoObject, obj) ) {
|
if ( IsDepends(obj_a.IsoObject, obj) ) {
|
||||||
max_depth = Mathf.Min(max_depth, obj_a.IsoObject.transform.position.z);
|
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 ) {
|
if ( min_depth == float.MinValue ) {
|
||||||
min_depth = MinDepth;
|
min_depth = MinDepth;
|
||||||
}
|
}
|
||||||
//TODO: Epsilon!!!!!
|
if ( max_depth == float.MaxValue ) {
|
||||||
if ( Mathf.Abs(max_depth - min_depth) <= Mathf.Epsilon ) {
|
max_depth = MaxDepth;
|
||||||
|
}
|
||||||
|
//TODO: magic number
|
||||||
|
var min_depth_step = 0.01f;
|
||||||
|
if ( Mathf.Abs(max_depth - min_depth) < min_depth_step ) {
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
} else {
|
} else {
|
||||||
_placeObject(obj, (min_depth + max_depth) / 2.0f);
|
PlaceObject(obj, (min_depth + max_depth) / 2.0f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _placeObject(IsoObject obj, float depth) {
|
void PlaceObject(IsoObject obj, float depth) {
|
||||||
var pos = obj.gameObject.transform.position;
|
var pos = obj.gameObject.transform.position;
|
||||||
obj.gameObject.transform.position = new Vector3(pos.x, pos.y, depth);
|
obj.gameObject.transform.position = new Vector3(pos.x, pos.y, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _placeObject(ObjectInfo info, IList<ObjectInfo> objects, IList<int> depends, ref float depth) {
|
void PlaceObject(ObjectInfo info, IList<ObjectInfo> objects, IList<int> depends, ref float depth) {
|
||||||
if ( !info.Visited ) {
|
if ( !info.Visited ) {
|
||||||
info.Visited = true;
|
info.Visited = true;
|
||||||
for ( int i = info.BeginDepend; i < info.EndDepend && i < depends.Count; ++i ) {
|
for ( int i = info.BeginDepend; i < info.EndDepend && i < depends.Count; ++i ) {
|
||||||
var object_index = depends[i];
|
var object_index = depends[i];
|
||||||
var obj = objects[object_index];
|
var obj = objects[object_index];
|
||||||
_placeObject(obj, objects, depends, ref depth);
|
PlaceObject(obj, objects, depends, ref depth);
|
||||||
}
|
}
|
||||||
_placeObject(info.IsoObject, depth);
|
PlaceObject(info.IsoObject, depth);
|
||||||
depth += (MaxDepth - MinDepth) / objects.Count;
|
depth += (MaxDepth - MinDepth) / objects.Count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SmartSort() {
|
||||||
|
if ( _dirty || _dirtyObjects.Count > 0 ) {
|
||||||
|
var objects = ScanObjects().Where(p => p.IsoObject.Sorting).ToList();
|
||||||
|
if ( _dirty ) {
|
||||||
|
ManualSort(objects);
|
||||||
|
} else {
|
||||||
|
foreach ( var obj in _dirtyObjects ) {
|
||||||
|
ManualSort(obj, objects);
|
||||||
|
if ( _dirty ) {
|
||||||
|
ManualSort(objects);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_dirty = false;
|
||||||
|
_dirtyObjects.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Start() {
|
void Start() {
|
||||||
_fixTileSize();
|
ChangeSortingProperty();
|
||||||
_fixTileType();
|
ManualSort(ScanObjects());
|
||||||
_fixDirty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LateUpdate() {
|
void LateUpdate() {
|
||||||
if ( _lastTileSize != TileSize ) {
|
if ( Application.isEditor ) {
|
||||||
_fixTileSize();
|
if ( _lastTileType != _tileType ) TileType = _tileType;
|
||||||
}
|
if ( !Mathf.Approximately(_lastTileSize, _tileSize) ) TileSize = _tileSize;
|
||||||
if ( _lastTileType != TileType ) {
|
if ( !Mathf.Approximately(_lastMinDepth, _minDepth) ) MinDepth = _minDepth;
|
||||||
_fixTileType();
|
if ( !Mathf.Approximately(_lastMaxDepth, _maxDepth) ) MaxDepth = _maxDepth;
|
||||||
}
|
|
||||||
if ( _dirty ) {
|
|
||||||
_fixDirty();
|
|
||||||
}
|
}
|
||||||
|
SmartSort();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnEnable() {
|
||||||
|
MarkDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDisable() {
|
void OnDisable() {
|
||||||
_fixDisable();
|
var objects = ScanObjects();
|
||||||
|
foreach ( var obj in objects ) {
|
||||||
|
obj.IsoObject.ResetIsoWorld();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace IsoTools
|
} // namespace IsoTools
|
||||||
@@ -17,7 +17,7 @@ Global
|
|||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(MonoDevelopProperties) = preSolution
|
GlobalSection(MonoDevelopProperties) = preSolution
|
||||||
StartupItem = Assembly-CSharp.csproj
|
StartupItem = Assembly-CSharp.csproj
|
||||||
Policies = $0
|
Policies = $0
|
||||||
$0.TextStylePolicy = $1
|
$0.TextStylePolicy = $1
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ Global
|
|||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(MonoDevelopProperties) = preSolution
|
GlobalSection(MonoDevelopProperties) = preSolution
|
||||||
StartupItem = Assembly-CSharp.csproj
|
StartupItem = Assembly-CSharp.csproj
|
||||||
Policies = $0
|
Policies = $0
|
||||||
$0.TextStylePolicy = $1
|
$0.TextStylePolicy = $1
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
<Properties>
|
<Properties>
|
||||||
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="MonoDevelop.Default" />
|
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
|
||||||
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/IsoTools/Scripts/IsoWorld.cs">
|
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/IsoTools/Scripts/IsoObject.cs">
|
||||||
<Files>
|
<Files>
|
||||||
<File FileName="Assets/IsoTools/Scripts/IsoWorld.cs" Line="269" Column="19" />
|
<File FileName="Assets/IsoTools/Scripts/IsoWorld.cs" Line="22" Column="21" />
|
||||||
<File FileName="Assets/IsoTools/Scripts/IsoObject.cs" Line="136" Column="34" />
|
<File FileName="Assets/IsoTools/Scripts/IsoObject.cs" Line="126" Column="9" />
|
||||||
|
<File FileName="Assets/IsoTools/Examples/Scripts/IsoAutoController.cs" Line="13" Column="28" />
|
||||||
</Files>
|
</Files>
|
||||||
</MonoDevelop.Ide.Workbench>
|
</MonoDevelop.Ide.Workbench>
|
||||||
<MonoDevelop.Ide.DebuggingService.Breakpoints>
|
<MonoDevelop.Ide.DebuggingService.Breakpoints>
|
||||||
|
|||||||
Reference in New Issue
Block a user