Skip to content

Commit d4d8dd0

Browse files
committed
一旦コミット
1 parent fc9bbe8 commit d4d8dd0

File tree

3 files changed

+115
-152
lines changed

3 files changed

+115
-152
lines changed

Assets/PhysicsLayers/Scripts/Common/CacheableArray.cs

Lines changed: 63 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,105 +6,80 @@
66

77
namespace a3geek.PhysicsLayers.Common
88
{
9-
public sealed class CacheableArray<T>
9+
public sealed class CacheableArray<T> where T : class
1010
{
11-
public uint Capacity { get; private set; }
11+
public T this[int index]
12+
{
13+
get { return this.array[index]; }
14+
set { this.array[index] = value; }
15+
}
16+
17+
public int Capacity { get; private set; }
18+
public int Tail { get; private set; }
1219

13-
private int tail = 0;
1420
private T[] array = null;
1521

16-
17-
public CacheableArray(uint capacity)
22+
23+
public CacheableArray(int capacity)
1824
{
1925
this.Capacity = capacity;
26+
27+
this.Tail = 0;
2028
this.array = new T[capacity];
2129
}
2230

23-
public void CacheCompaction()
31+
public void Add(T item)
2432
{
33+
if(this.array.Length == this.Tail)
34+
{
35+
this.Capacity *= 2;
36+
Array.Resize(ref this.array, checked(this.Capacity));
37+
}
2538

39+
this.array[this.Tail++] = item;
2640
}
27-
}
28-
}
29-
30-
//using System.Collections;
31-
//using System.Collections.Generic;
32-
//using UnityEngine;
33-
34-
//public class NewBehaviourScript6 : MonoBehaviour
35-
//{
36-
// private class Test
37-
// {
38-
// public int id = 0;
39-
// }
40-
41-
// public int count = 10;
42-
// public float addRate = 0.25f;
43-
// public float removeRate = 0.25f;
44-
45-
46-
// private void Start()
47-
// {
48-
// var arr = new Test[this.count];
49-
// int tail = 0;
50-
51-
// for(var i = 0; i < arr.Length; i++)
52-
// {
53-
// if(Random.value <= this.addRate)
54-
// {
55-
// arr[tail++] = new Test() { id = i };
56-
// }
57-
// }
5841

59-
// for(var i = 0; i < arr.Length; i++)
60-
// {
61-
// var a = arr[i];
62-
// if(a != null && Random.value <= this.removeRate)
63-
// {
64-
// arr[i] = null;
65-
// a = null;
66-
// }
67-
68-
// Debug.Log(a + " : " + (a == null ? ("_" + i) : a.id.ToString()));
69-
// }
70-
// Debug.Log("");
71-
72-
// int count = 0;
73-
74-
// var j = tail - 1;
75-
// for(var i = 0; i < arr.Length; i++)
76-
// {
77-
// if(arr[i] == null)
78-
// {
79-
// for(var done = false; done == false && i < j; j--)
80-
// {
81-
// if(arr[j] != null)
82-
// {
83-
// arr[i] = arr[j];
84-
// arr[j] = null;
85-
86-
// done = true;
87-
// count++;
88-
// break;
89-
// }
90-
// }
91-
92-
// if(i >= j)
93-
// {
94-
// tail = i;
95-
// break;
96-
// }
97-
// }
98-
// }
99-
100-
// for(var i = 0; i < arr.Length; i++)
101-
// {
102-
// var a = arr[i];
103-
// Debug.Log(a + " : " + (a == null ? ("_" + i) : a.id.ToString()));
104-
// }
105-
// Debug.Log("");
42+
public void Remove(T item)
43+
{
44+
for(var i = 0; i < this.array.Length; i++)
45+
{
46+
if(this.array[i] == item)
47+
{
48+
this.array[i] = null;
49+
50+
return;
51+
}
52+
}
53+
}
10654

107-
// Debug.Log("Tail : " + tail);
108-
// Debug.Log("Count : " + count);
109-
// }
110-
//}
55+
public void CacheCompaction()
56+
{
57+
var arr = this.array;
58+
var j = this.Tail - 1;
59+
60+
for(var i = 0; i < this.array.Length && j >= 0; i++)
61+
{
62+
if(arr[i] == null)
63+
{
64+
do
65+
{
66+
if(arr[j] != null)
67+
{
68+
arr[i] = arr[j];
69+
arr[j] = null;
70+
71+
break;
72+
}
73+
}
74+
while(--j >= 0 && i < j);
75+
76+
if(i >= j)
77+
{
78+
this.Tail = i;
79+
break;
80+
}
81+
}
82+
}
83+
}
84+
}
85+
}

Assets/PhysicsLayers/Scripts/Components/CollisionInfosSetter.cs

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace a3geek.PhysicsLayers
77
{
88
using Layers.Abstracts;
9+
using Common;
910

1011
using AbsCollLayer = Layers.Abstracts.AbstractCollisionLayer<Collider>;
1112
using AbsCollLayer2D = Layers.Abstracts.AbstractCollisionLayer<Collider2D>;
@@ -15,13 +16,29 @@ public partial class LayersManager
1516
public sealed class CollisionInfosSetter
1617
{
1718
private LayersManager manager = null;
18-
private Dictionary<int, List<AbsCollLayer2D>> collLayers2D = new Dictionary<int, List<AbsCollLayer2D>>();
19-
private Dictionary<int, List<AbsCollLayer>> collLayers = new Dictionary<int, List<AbsCollLayer>>();
19+
20+
private Dictionary<int, CacheableArray<AbsCollLayer2D>> collLayers2D = new Dictionary<int, CacheableArray<AbsCollLayer2D>>();
21+
private Dictionary<int, CacheableArray<AbsCollLayer>> collLayers = new Dictionary<int, CacheableArray<AbsCollLayer>>();
2022

2123

2224
public CollisionInfosSetter(LayersManager manager)
2325
{
2426
this.manager = manager;
27+
28+
var capacity = manager.cacheCapacity;
29+
var unityIDs = manager.UnityLayerIDs;
30+
for(var i = 0; i < unityIDs.Length; i++)
31+
{
32+
this.collLayers.Add(unityIDs[i], new CacheableArray<AbsCollLayer>(capacity));
33+
this.collLayers2D.Add(unityIDs[i], new CacheableArray<AbsCollLayer2D>(capacity));
34+
}
35+
36+
var physicsIDs = manager.PhysicsLayerIDs;
37+
for(var i = 0; i < physicsIDs.Length; i++)
38+
{
39+
this.collLayers.Add(physicsIDs[i], new CacheableArray<AbsCollLayer>(capacity));
40+
this.collLayers2D.Add(physicsIDs[i], new CacheableArray<AbsCollLayer2D>(capacity));
41+
}
2542
}
2643

2744
public void Management(AbsCollLayer layer)
@@ -38,89 +55,60 @@ public void Management(AbsCollLayer layer)
3855
public void Management(AbsCollLayer2D layer)
3956
{
4057
var layerID = this.SetIgnoreCollisions(layer, this.collLayers2D, lay => layer.IgnoreCollisions(lay.Colliders, true));
41-
if(layerID < 0)
42-
{
43-
return;
44-
}
45-
4658
this.collLayers2D[layerID].Add(layer);
4759
}
4860

4961
public void UnManagement(AbsCollLayer layer)
5062
{
51-
var layerID = layer.LayerID;
52-
if(layerID < 0)
53-
{
54-
return;
55-
}
56-
57-
this.CheckDicKey(layerID);
58-
this.collLayers[layerID].Remove(layer);
63+
this.collLayers[layer.LayerID].Remove(layer);
5964
}
6065

6166
public void UnManagement(AbsCollLayer2D layer)
6267
{
63-
var layerID = layer.LayerID;
64-
if(layerID < 0)
65-
{
66-
return;
67-
}
68-
69-
this.CheckDicKey(layerID);
70-
this.collLayers2D[layerID].Remove(layer);
68+
this.collLayers2D[layer.LayerID].Remove(layer);
7169
}
7270

7371
public void ResetIgnoreCollision(AbsCollLayer layer)
7472
{
75-
this.SetIgnoreCollisions(layer, this.collLayers, lay => lay.IgnoreCollisions(layer.Colliders, false));
73+
this.SetIgnoreCollisions(
74+
layer,
75+
this.collLayers,
76+
otherLayer => otherLayer.IgnoreCollisions(layer.Colliders, false)
77+
);
7678
}
7779

7880
public void ResetIgnoreCollision(AbsCollLayer2D layer)
7981
{
80-
this.SetIgnoreCollisions(layer, this.collLayers2D, lay2D => lay2D.IgnoreCollisions(layer.Colliders, false));
82+
this.SetIgnoreCollisions(
83+
layer,
84+
this.collLayers2D,
85+
otherLayer => otherLayer.IgnoreCollisions(layer.Colliders, false)
86+
);
8187
}
82-
83-
private int SetIgnoreCollisions<T1, T2>(T1 layer, Dictionary<int, List<T2>> layersDic, Action<T2> setter)
88+
89+
private int SetIgnoreCollisions<T1, T2>(T1 layer, Dictionary<int, CacheableArray<T2>> layersDic, Action<T2> setter)
8490
where T1 : AbstractLayer where T2 : AbstractLayer
8591
{
8692
var layerID = layer.LayerID;
87-
if(this.manager == null || layerID < 0)
93+
94+
var ignoreLayerIDs = this.manager.AllLayerInfos.GetIgnoreLayerIDs(layerID);
95+
for(var i = 0; i < ignoreLayerIDs.Length; i++)
8896
{
89-
return -1;
97+
var layers = layersDic[ignoreLayerIDs[i]];
98+
for(var j = 0; j < layers.Tail; j++)
99+
{
100+
var lay = layers[j];
101+
if(lay == null)
102+
{
103+
continue;
104+
}
105+
106+
setter(lay);
107+
}
90108
}
91-
92-
this.CheckDicKey(layerID);
93-
94-
//var ignoreLayerIDs = this.AllLayerInfos.GetIgnoreLayerIDs(layerID);
95-
//if(ignoreLayerIDs.Count() <= 0)
96-
//{
97-
// return layerID;
98-
//}
99-
100-
//foreach(var id in ignoreLayerIDs)
101-
//{
102-
// List<T2> colls = null;
103-
// if(layersDic.TryGetValue(id, out colls) == true)
104-
// {
105-
// colls.ForEach(coll => setter(coll));
106-
// }
107-
//}
108-
109+
109110
return layerID;
110111
}
111-
112-
private void CheckDicKey(int layerID)
113-
{
114-
if(this.collLayers.ContainsKey(layerID) == false)
115-
{
116-
this.collLayers.Add(layerID, new List<AbsCollLayer>());
117-
}
118-
119-
if(this.collLayers2D.ContainsKey(layerID) == false)
120-
{
121-
this.collLayers2D.Add(layerID, new List<AbsCollLayer2D>());
122-
}
123-
}
124112
}
125113
}
126114
}

Assets/PhysicsLayers/Scripts/LayersManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public Dictionary<int, string> PhysicsLayers
6262
{
6363
get { return this.AllLayerInfos.PhysicsLayers; }
6464
}
65-
public IEnumerable<int> PhysicsLayerIDs
65+
public int[] PhysicsLayerIDs
6666
{
6767
get { return this.AllLayerInfos.PhysicsLayerIDs; }
6868
}
69-
public IEnumerable<string> PhysicsLayerNames
69+
public string[] PhysicsLayerNames
7070
{
7171
get { return this.AllLayerInfos.PhysicsLayerNames; }
7272
}
@@ -75,11 +75,11 @@ public Dictionary<int, string> UnityLayers
7575
{
7676
get { return this.AllLayerInfos.UnityLayers; }
7777
}
78-
public IEnumerable<int> UnityLayerIDs
78+
public int[] UnityLayerIDs
7979
{
8080
get { return this.AllLayerInfos.UnityLayerIDs; }
8181
}
82-
public IEnumerable<string> UnityLayerNames
82+
public string[] UnityLayerNames
8383
{
8484
get { return this.AllLayerInfos.UnityLayerNames; }
8585
}

0 commit comments

Comments
 (0)