refactoring process

This commit is contained in:
2016-12-07 18:41:45 +07:00
parent d1fecf808e
commit d74728b4df
15 changed files with 327 additions and 154 deletions

View File

@@ -65,7 +65,7 @@ namespace IsoTools.Physics {
void OnDrawGizmosSelected() {
var iso_object = GetComponent<IsoObject>();
if ( iso_object && iso_object.isoWorld ) {
IsoUtils.DrawCube(
IsoUtils.DrawIsoCube(
iso_object.isoWorld,
iso_object.position + offset,
size,

View File

@@ -82,23 +82,23 @@ namespace IsoTools.Physics {
var iso_object = GetComponent<IsoObject>();
if ( iso_object && iso_object.isoWorld ) {
if ( radius * 2 < height ) {
IsoUtils.DrawCube(
IsoUtils.DrawIsoCube(
iso_object.isoWorld,
iso_object.position + offset,
new Vector3(radius * 2.0f, radius * 2.0f, height - radius),
Color.green);
IsoUtils.DrawSphere(
IsoUtils.DrawIsoSphere(
iso_object.isoWorld,
iso_object.position + offset - IsoUtils.Vec3FromZ(height * 0.5f - radius),
radius,
Color.green);
IsoUtils.DrawSphere(
IsoUtils.DrawIsoSphere(
iso_object.isoWorld,
iso_object.position + offset + IsoUtils.Vec3FromZ(height * 0.5f - radius),
radius,
Color.green);
} else {
IsoUtils.DrawSphere(
IsoUtils.DrawIsoSphere(
iso_object.isoWorld,
iso_object.position + offset,
radius,

View File

@@ -65,7 +65,7 @@ namespace IsoTools.Physics {
void OnDrawGizmosSelected() {
var iso_object = GetComponent<IsoObject>();
if ( iso_object && iso_object.isoWorld ) {
IsoUtils.DrawSphere(
IsoUtils.DrawIsoSphere(
iso_object.isoWorld,
iso_object.position + offset,
radius,

View File

@@ -39,7 +39,7 @@ namespace IsoTools.Tiled {
void OnDrawGizmos() {
var iso_object = GetComponent<IsoObject>();
if ( isShowGrid && iso_object && iso_object.isoWorld ) {
IsoUtils.DrawGrid(
IsoUtils.DrawIsoGrid(
iso_object.isoWorld,
iso_object.position, iso_object.size,
IsoUtils.ColorChangeA(Color.green, 0.5f));

View File

@@ -1,22 +1,18 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
namespace IsoTools.Internal {
public class IsoAssocList<T> {
IsoList<T> _list;
Dictionary<T, int> _dict;
IEqualityComparer<T> _comparer;
IsoList<T> _list;
Dictionary<T, int> _dict;
public IsoAssocList() {
_list = new IsoList<T>();
_dict = new Dictionary<T, int>();
_comparer = EqualityComparer<T>.Default;
_list = new IsoList<T>();
_dict = new Dictionary<T, int>();
}
public IsoAssocList(int capacity) {
_list = new IsoList<T>(capacity);
_dict = new Dictionary<T, int>(capacity);
_comparer = EqualityComparer<T>.Default;
_list = new IsoList<T>(capacity);
_dict = new Dictionary<T, int>(capacity);
}
public T this[int index] {
@@ -41,22 +37,27 @@ namespace IsoTools.Internal {
return _dict.ContainsKey(item);
}
public void Add(T item) {
if ( !_dict.ContainsKey(item) ) {
_dict.Add(item, _list.Count);
_list.Push(item);
public bool Add(T item) {
if ( _dict.ContainsKey(item) ) {
return false;
}
_dict.Add(item, _list.Count);
_list.Push(item);
return true;
}
public void Remove(T item) {
public bool Remove(T item) {
int index;
if ( _dict.TryGetValue(item, out index) ) {
_dict.Remove(item);
var reordered =_list.UnorderedRemoveAt(index);
if ( !_comparer.Equals(reordered, item) ) {
_dict[reordered] = index;
_list.UnorderedRemoveAt(index);
if ( index != _list.Count ) {
_dict[_list[index]] = index;
}
return true;
}
return false;
}
public void Clear() {

View File

@@ -58,14 +58,12 @@ namespace IsoTools.Internal {
_size = 0;
}
public T UnorderedRemoveAt(int index) {
public void UnorderedRemoveAt(int index) {
if ( (uint)index >= (uint)_size ) {
throw new IndexOutOfRangeException();
}
var last = _data[_size - 1];
_data[index] = last;
_data[--_size] = default(T);
return last;
_data[index] = _data[--_size];
_data[_size] = default(T);
}
public T this[int index] {

View File

@@ -0,0 +1,73 @@
using UnityEngine;
namespace IsoTools.Internal {
public struct IsoMinMax {
public float min;
public float max;
public IsoMinMax(float min, float max) : this() {
this.min = min;
this.max = max;
}
public IsoMinMax(IsoMinMax other) : this() {
min = other.min;
max = other.max;
}
public float size {
get { return max - min; }
}
public float center {
get { return min + (max - min) * 0.5f; }
}
public void Set(float min, float max) {
this.min = min;
this.max = max;
}
public void Set(IsoMinMax other) {
min = other.min;
max = other.max;
}
public void Translate(float delta) {
min += delta;
max += delta;
}
public bool Contains(float other) {
return other >= min && other <= max;
}
public bool Contains(IsoMinMax other) {
return
max >= other.max &&
min <= other.min;
}
public bool Overlaps(IsoMinMax other) {
return
max > other.min &&
min < other.max;
}
public bool Approximately(IsoMinMax other) {
return
Mathf.Approximately(min, other.min) &&
Mathf.Approximately(max, other.max);
}
public static IsoMinMax zero {
get { return new IsoMinMax(); }
}
public static IsoMinMax Merge(IsoMinMax a, IsoMinMax b) {
return new IsoMinMax(
a.min < b.min ? a.min : b.min,
a.max > b.max ? a.max : b.max);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9f0468643e5324b3296f095d6b127253
timeCreated: 1480524466
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,36 @@
using System;
namespace IsoTools.Internal {
public interface IsoIPool<T> {
T Take();
void Release(T item);
}
public abstract class IsoPool<T> : IsoIPool<T> {
IsoList<T> _items;
public IsoPool(int capacity) {
_items = new IsoList<T>(capacity);
for ( var i = 0; i < capacity; ++i ) {
_items.Push(CreateItem());
}
}
public T Take() {
return _items.Count > 0
? _items.Pop()
: CreateItem();
}
public void Release(T item) {
if ( item == null ) {
throw new ArgumentNullException("item");
}
CleanUpItem(item);
_items.Push(item);
}
public abstract T CreateItem();
public virtual void CleanUpItem(T item) {}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9518383bac81445daaba54a14e6b87ce
timeCreated: 1481092506
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,100 @@
using UnityEngine;
namespace IsoTools.Internal {
public struct IsoRect {
public IsoMinMax x;
public IsoMinMax y;
public IsoRect(float min_x, float min_y, float max_x, float max_y) : this() {
x.Set(min_x, max_x);
y.Set(min_y, max_y);
}
public IsoRect(Vector2 min, Vector2 max) : this() {
x.Set(min.x, max.x);
y.Set(min.y, max.y);
}
public IsoRect(IsoMinMax minmax_x, IsoMinMax minmax_y) : this() {
x.Set(minmax_x);
y.Set(minmax_y);
}
public IsoRect(IsoRect other) : this() {
x.Set(other.x);
y.Set(other.y);
}
public Vector2 size {
get { return new Vector2(x.size, y.size); }
}
public Vector2 center {
get { return new Vector2(x.center, y.center); }
}
public void Set(float min_x, float min_y, float max_x, float max_y) {
x.Set(min_x, max_x);
y.Set(min_y, max_y);
}
public void Set(Vector2 min, Vector2 max) {
x.Set(min.x, max.x);
y.Set(min.y, max.y);
}
public void Set(IsoMinMax minmax_x, IsoMinMax minmax_y) {
x.Set(minmax_x);
y.Set(minmax_y);
}
public void Set(IsoRect other) {
x.Set(other.x);
y.Set(other.y);
}
public void Translate(float delta_x, float delta_y) {
x.Translate(delta_x);
y.Translate(delta_y);
}
public void Translate(Vector2 delta) {
x.Translate(delta.x);
y.Translate(delta.y);
}
public bool Contains(Vector2 other) {
return
x.Contains(other.x) &&
y.Contains(other.y);
}
public bool Contains(IsoRect other) {
return
x.Contains(other.x) &&
y.Contains(other.y);
}
public bool Overlaps(IsoRect other) {
return
x.Overlaps(other.x) &&
y.Overlaps(other.y);
}
public bool Approximately(IsoRect other) {
return
x.Approximately(other.x) &&
y.Approximately(other.y);
}
public static IsoRect zero {
get { return new IsoRect(); }
}
public static IsoRect Merge(IsoRect a, IsoRect b) {
return new IsoRect(
IsoMinMax.Merge(a.x, b.x),
IsoMinMax.Merge(a.y, b.y));
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1cf642c7164d740708f2d931a8ccde23
timeCreated: 1480524425
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -26,96 +26,6 @@ namespace IsoTools.Internal {
public static readonly int FloatBeautifierDigits = 4;
// ---------------------------------------------------------------------
//
// Rect
//
// ---------------------------------------------------------------------
public struct Rect {
public MinMax x;
public MinMax y;
public Rect(float x_min, float y_min, float x_max, float y_max) : this() {
x = new MinMax(x_min, x_max);
y = new MinMax(y_min, y_max);
}
public Vector2 size {
get { return new Vector2(x.size, y.size); }
}
public Vector2 center {
get { return new Vector2(x.center, y.center); }
}
public void Set(float x_min, float y_min, float x_max, float y_max) {
x.Set(x_min, x_max);
y.Set(y_min, y_max);
}
public bool Overlaps(Rect other) {
return
x.Overlaps(other.x) &&
y.Overlaps(other.y);
}
public bool Approximately(Rect other) {
return
x.Approximately(other.x) &&
y.Approximately(other.y);
}
public static Rect zero {
get { return new Rect(); }
}
}
// ---------------------------------------------------------------------
//
// MinMax
//
// ---------------------------------------------------------------------
public struct MinMax {
public float min;
public float max;
public MinMax(float min, float max) : this() {
this.min = min;
this.max = max;
}
public float size {
get { return max - min; }
}
public float center {
get { return min / 2.0f + max / 2.0f; }
}
public void Set(float min, float max) {
this.min = min;
this.max = max;
}
public bool Overlaps(MinMax other) {
return
max > other.min &&
min < other.max;
}
public bool Approximately(MinMax other) {
return
Mathf.Approximately(min, other.min) &&
Mathf.Approximately(max, other.max);
}
public static MinMax zero {
get { return new MinMax(); }
}
}
// ---------------------------------------------------------------------
//
// Abs/Min/Max
@@ -542,44 +452,45 @@ namespace IsoTools.Internal {
// ---------------------------------------------------------------------
#if UNITY_EDITOR
static void DrawTop(IsoWorld iso_world, Vector3 pos, Vector3 size) {
static void DrawIsoCubeTop(IsoWorld iso_world, Vector3 pos, Vector3 size) {
if ( iso_world ) {
var points = new Vector3[]{
iso_world.IsoToScreen(pos),
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromX (size.x)),
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromXY(size.x, size.y)),
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromY (size.y)),
iso_world.IsoToScreen(pos)
};
Handles.DrawLine(points[0], points[1]);
Handles.DrawLine(points[1], points[2]);
Handles.DrawLine(points[2], points[3]);
Handles.DrawLine(points[3], points[0]);
var point0 = iso_world.IsoToScreen(pos);
var point1 = iso_world.IsoToScreen(pos + IsoUtils.Vec3FromX (size.x));
var point2 = iso_world.IsoToScreen(pos + IsoUtils.Vec3FromXY(size.x, size.y));
var point3 = iso_world.IsoToScreen(pos + IsoUtils.Vec3FromY (size.y));
Handles.DrawLine(point0, point1);
Handles.DrawLine(point1, point2);
Handles.DrawLine(point2, point3);
Handles.DrawLine(point3, point0);
}
}
static void DrawVert(IsoWorld iso_world, Vector3 pos, Vector3 size) {
static void DrawIsoCubeVert(IsoWorld iso_world, Vector3 pos, Vector3 size) {
if ( iso_world ) {
Handles.DrawLine(
iso_world.IsoToScreen(pos),
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromZ(size.z)));
var point0 = iso_world.IsoToScreen(pos);
var point1 = iso_world.IsoToScreen(pos + IsoUtils.Vec3FromZ(size.z));
Handles.DrawLine(point0, point1);
}
}
public static void DrawCube(IsoWorld iso_world, Vector3 center, Vector3 size, Color color) {
public static void DrawIsoCube(
IsoWorld iso_world, Vector3 center, Vector3 size, Color color)
{
if ( iso_world ) {
Handles.color = color;
var pos = center - size * 0.5f;
DrawTop (iso_world, pos, size);
DrawTop (iso_world, pos + IsoUtils.Vec3FromZ (size.z), size);
DrawVert(iso_world, pos, size);
DrawVert(iso_world, pos + IsoUtils.Vec3FromX (size.x), size);
DrawVert(iso_world, pos + IsoUtils.Vec3FromY (size.y), size);
DrawVert(iso_world, pos + IsoUtils.Vec3FromXY(size.x, size.y), size);
DrawIsoCubeTop (iso_world, pos, size);
DrawIsoCubeTop (iso_world, pos + IsoUtils.Vec3FromZ (size.z), size);
DrawIsoCubeVert(iso_world, pos, size);
DrawIsoCubeVert(iso_world, pos + IsoUtils.Vec3FromX (size.x), size);
DrawIsoCubeVert(iso_world, pos + IsoUtils.Vec3FromY (size.y), size);
DrawIsoCubeVert(iso_world, pos + IsoUtils.Vec3FromXY(size.x, size.y), size);
}
}
public static void DrawSphere(IsoWorld iso_world, Vector3 pos, float radius, Color color) {
public static void DrawIsoSphere(
IsoWorld iso_world, Vector3 pos, float radius, Color color)
{
if ( iso_world ) {
Handles.color = color;
Handles.RadiusHandle(
@@ -589,7 +500,9 @@ namespace IsoTools.Internal {
}
}
public static void DrawGrid(IsoWorld iso_world, Vector3 pos, Vector3 size, Color color) {
public static void DrawIsoGrid(
IsoWorld iso_world, Vector3 pos, Vector3 size, Color color)
{
if ( iso_world ) {
Handles.color = color;
var size_x = Mathf.RoundToInt(size.x);
@@ -606,6 +519,18 @@ namespace IsoTools.Internal {
}
}
}
public static void DrawRect(IsoRect rect, Color color) {
Handles.color = color;
var point0 = new Vector2(rect.x.min, rect.y.min);
var point1 = new Vector2(rect.x.max, rect.y.min);
var point2 = new Vector2(rect.x.max, rect.y.max);
var point3 = new Vector2(rect.x.min, rect.y.max);
Handles.DrawLine(point0, point1);
Handles.DrawLine(point1, point2);
Handles.DrawLine(point2, point3);
Handles.DrawLine(point3, point0);
}
#endif
}
}

View File

@@ -194,8 +194,8 @@ namespace IsoTools {
public class InternalState {
public bool Dirty = false;
public bool Placed = false;
public IsoUtils.Rect ScreenRect = IsoUtils.Rect.zero;
public IsoUtils.MinMax MinMax3d = IsoUtils.MinMax.zero;
public IsoRect ScreenRect = IsoRect.zero;
public IsoMinMax MinMax3d = IsoMinMax.zero;
public float Offset3d = 0.0f;
public Vector2 MinSector = Vector2.zero;
public Vector2 MaxSector = Vector2.zero;
@@ -276,7 +276,7 @@ namespace IsoTools {
var r = iso_world.IsoToScreen(position + IsoUtils.Vec3FromX(size.x)).x;
var b = iso_world.IsoToScreen(position).y;
var t = iso_world.IsoToScreen(position + size).y;
Internal.ScreenRect = new IsoUtils.Rect(l, b, r, t);
Internal.ScreenRect = new IsoRect(l, b, r, t);
}
}
@@ -344,7 +344,7 @@ namespace IsoTools {
void OnDrawGizmos() {
if ( isShowBounds && isoWorld ) {
IsoUtils.DrawCube(isoWorld, position + size * 0.5f, size, Color.red);
IsoUtils.DrawIsoCube(isoWorld, position + size * 0.5f, size, Color.red);
}
}
#endif

View File

@@ -6,6 +6,10 @@ using System.Collections.Generic;
using UnityEditor;
#endif
#if UNITY_5_5_OR_NEWER
using UnityEngine.Profiling;
#endif
namespace IsoTools {
[ExecuteInEditMode, DisallowMultipleComponent]
public class IsoWorld : MonoBehaviour {
@@ -401,9 +405,9 @@ namespace IsoTools {
}
}
IsoUtils.MinMax IsoObjectMinMax3D(IsoObject iso_object) {
IsoMinMax IsoObjectMinMax3D(IsoObject iso_object) {
bool inited = false;
var result = IsoUtils.MinMax.zero;
var result = IsoMinMax.zero;
var renderers = GetIsoObjectRenderers(iso_object);
for ( int i = 0, e = renderers.Count; i < e; ++i ) {
var bounds = renderers[i].bounds;
@@ -421,11 +425,11 @@ namespace IsoTools {
}
} else {
inited = true;
result = new IsoUtils.MinMax(minbounds, maxbounds);
result = new IsoMinMax(minbounds, maxbounds);
}
}
}
return inited ? result : IsoUtils.MinMax.zero;
return inited ? result : IsoMinMax.zero;
}
bool IsIsoObjectVisible(IsoObject iso_object) {