mirror of
https://github.com/BlackMATov/unity-iso-tools.git
synced 2026-01-06 11:50:56 +07:00
Lazy sorting
This commit is contained in:
32
Assets/Scripts/IsoAutoController.cs
Normal file
32
Assets/Scripts/IsoAutoController.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class IsoAutoController : MonoBehaviour {
|
||||
|
||||
public float StepTicks = 0.5f;
|
||||
public float StepRndTicks = 0.5f;
|
||||
|
||||
void Start() {
|
||||
StartCoroutine("Move");
|
||||
}
|
||||
|
||||
WaitForSeconds RndWait() {
|
||||
return new WaitForSeconds(StepTicks + Random.Range(0.0f, StepRndTicks));
|
||||
}
|
||||
|
||||
IEnumerator Move() {
|
||||
var iso_object = GetComponent<IsoObject>();
|
||||
if ( iso_object ) {
|
||||
for (;;) {
|
||||
yield return RndWait();
|
||||
iso_object.Position += new Vector3(1, 0, 0);
|
||||
yield return RndWait();
|
||||
iso_object.Position += new Vector3(0, 1, 0);
|
||||
yield return RndWait();
|
||||
iso_object.Position += new Vector3(-1, 0, 0);
|
||||
yield return RndWait();
|
||||
iso_object.Position += new Vector3(0, -1, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Assets/Scripts/IsoAutoController.cs.meta
Normal file
8
Assets/Scripts/IsoAutoController.cs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1840b23373986f942bf6e0c702745e63
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -2,10 +2,6 @@
|
||||
using System.Collections;
|
||||
|
||||
public class IsoController : MonoBehaviour {
|
||||
|
||||
void Start () {
|
||||
}
|
||||
|
||||
void Update () {
|
||||
var iso_object = gameObject.GetComponent<IsoObject>();
|
||||
if ( iso_object ) {
|
||||
@@ -29,4 +25,4 @@ public class IsoController : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,19 +5,23 @@ using System.Collections;
|
||||
[ExecuteInEditMode]
|
||||
public class IsoObject : MonoBehaviour {
|
||||
|
||||
IsoWorld _iso_world = null;
|
||||
Vector3 _lastPosition = Vector3.zero;
|
||||
Vector3 _lastTransform = Vector3.zero;
|
||||
|
||||
[SerializeField]
|
||||
private Vector3 _position = Vector3.zero;
|
||||
private Vector3 _lastPosition = Vector3.zero;
|
||||
public Vector3 Position {
|
||||
Vector3 _position = Vector3.zero;
|
||||
public Vector3 Position {
|
||||
get { return _position; }
|
||||
set {
|
||||
if ( Alignment ) {
|
||||
_position.Set(Mathf.Round(value.x), Mathf.Round(value.y), value.z);
|
||||
_position.Set(Mathf.Round(value.x), Mathf.Round(value.y), Mathf.Round(value.z));
|
||||
} else {
|
||||
_position = value;
|
||||
}
|
||||
_lastPosition = _position;
|
||||
FixTransform();
|
||||
GetIsoWorld().MarkDirty();
|
||||
_lastPosition = _position;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,36 +29,52 @@ public class IsoObject : MonoBehaviour {
|
||||
public bool Alignment = true;
|
||||
|
||||
void Start() {
|
||||
GetIsoWorld().MarkDirty();
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if ( _lastPosition != _position ) {
|
||||
Position = _position;
|
||||
}
|
||||
if ( _lastTransform != gameObject.transform.position ) {
|
||||
FixIsoPosition();
|
||||
}
|
||||
/*
|
||||
if ( Selection.Contains(gameObject) ) {
|
||||
FixIsoPosition();
|
||||
} else {
|
||||
FixTransform();
|
||||
}*/
|
||||
}
|
||||
|
||||
void OnRenderObject() {
|
||||
Update();
|
||||
}
|
||||
|
||||
IsoWorld GetIsoWorld() {
|
||||
if ( !_iso_world ) {
|
||||
_iso_world = GameObject.FindObjectOfType<IsoWorld>();
|
||||
}
|
||||
if ( !_iso_world ) {
|
||||
throw new UnityException("IsoObject. IsoWorld not found!");
|
||||
}
|
||||
return _iso_world;
|
||||
}
|
||||
|
||||
void FixTransform() {
|
||||
var iso_world = GameObject.FindObjectOfType<IsoWorld>();
|
||||
if ( !iso_world ) {
|
||||
return;
|
||||
}
|
||||
var pos = iso_world.IsoToScreen(Position);
|
||||
var pos = GetIsoWorld().IsoToScreen(Position);
|
||||
var depth = gameObject.transform.position.z;
|
||||
gameObject.transform.position = new Vector3(pos.x, pos.y, depth);
|
||||
var trans = new Vector3(pos.x, pos.y, depth);
|
||||
gameObject.transform.position = trans;
|
||||
_lastPosition = trans;
|
||||
}
|
||||
|
||||
void FixIsoPosition() {
|
||||
var iso_world = GameObject.FindObjectOfType<IsoWorld>();
|
||||
if ( !iso_world ) {
|
||||
return;
|
||||
var trans = gameObject.transform.position;
|
||||
Position = GetIsoWorld().ScreenToIso(new Vector2(trans.x, trans.y), Position.z);
|
||||
_lastTransform = trans;
|
||||
if ( Application.isEditor ) {
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
var pos = gameObject.transform.position;
|
||||
Position = iso_world.ScreenToIso(new Vector2(pos.x, pos.y), Position.z);
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,11 @@ using System.Collections.Generic;
|
||||
[ExecuteInEditMode]
|
||||
public class IsoWorld : MonoBehaviour {
|
||||
|
||||
private class ObjectInfo {
|
||||
public float TileSize = 32.0f;
|
||||
public float StartDepth = 0.0f;
|
||||
public float StepDepth = 0.1f;
|
||||
|
||||
class ObjectInfo {
|
||||
public IsoObject IsoObject;
|
||||
public bool Visited;
|
||||
public int BeginDepend;
|
||||
@@ -20,12 +24,13 @@ public class IsoWorld : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
public float TileSize = 32.0f;
|
||||
public float StartDepth = 0.0f;
|
||||
public float StepDepth = 0.1f;
|
||||
|
||||
bool _dirty = true;
|
||||
List<int> _depends = new List<int>();
|
||||
List<ObjectInfo> _objects = new List<ObjectInfo>();
|
||||
|
||||
public void MarkDirty() {
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
public Vector2 IsoToScreen(Vector3 pos) {
|
||||
return new Vector2(
|
||||
@@ -102,12 +107,19 @@ public class IsoWorld : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
void Start () {
|
||||
void Start() {
|
||||
}
|
||||
|
||||
void Update () {
|
||||
_scanObjects();
|
||||
_scanDepends();
|
||||
_manualSort();
|
||||
void Update() {
|
||||
if ( _dirty ) {
|
||||
_scanObjects();
|
||||
_scanDepends();
|
||||
_manualSort();
|
||||
_dirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
void LateUpdate() {
|
||||
Update();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user