An open source Danmaku development kit for Unity3D.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Make DanmakuPool enumerable

james713 8a5050f8 949c9365

+70 -16
+70 -16
Assets/src/Core/DanmakuPool.cs
··· 8 8 9 9 namespace DanmakU { 10 10 11 - public class DanmakuPool : IDisposable { 11 + public class DanmakuPool : IEnumerable<Danmaku>, IDisposable { 12 12 13 - public int ActiveCount; 13 + public int ActiveCount { get; private set; } 14 14 15 15 public NativeArray<Vector2> Positions; 16 16 public NativeArray<float> Rotations; ··· 54 54 return new UpdateContext(this, dependency); 55 55 } 56 56 57 - void DestroyInternal(int index) { 58 - ActiveCount--; 59 - Swap(ref Positions, index, ActiveCount); 60 - Swap(ref Rotations, index, ActiveCount); 61 - Swap(ref Speeds, index, ActiveCount); 62 - Swap(ref AngularSpeeds, index, ActiveCount); 63 - Swap(ref Colors, index, ActiveCount); 64 - } 65 - 57 + /// <summary> 58 + /// Retrieves a new Danmaku from the pool. 59 + /// </summary> 60 + /// <remarks> 61 + /// The Danmaku's data is not cleared upon retrieval. There may be 62 + /// invalid or imporperly initialized values. 63 + /// </remarks> 64 + /// <returns></returns> 66 65 public Danmaku Get() => new Danmaku(this, ActiveCount++); 67 66 67 + /// <summary> 68 + /// Retrieves a batch of new Danmaku from the pool. 69 + /// </summary> 70 + /// <param name="danmaku">an array of danmaku to write the values to.</param> 71 + /// <param name="count">the number of danmaku to create. Must be less than or equal to the length of of danmaku.</param> 68 72 public void Get(Danmaku[] danmaku, int count) { 69 73 for (var i = 0; i < count; i++) { 70 74 danmaku[i] = new Danmaku(this, ActiveCount + i); ··· 72 76 ActiveCount += count; 73 77 } 74 78 75 - void Swap<T>(ref NativeArray<T> array, int a, int b) where T : struct { 76 - T temp = array[a]; 77 - array[a] = array[b]; 78 - array[b] = temp; 79 - } 79 + /// <summary> 80 + /// Destroys all danmaku in the pool. 81 + /// </summary> 82 + public void Clear() => ActiveCount = 0; 80 83 81 84 public void Dispose() { 82 85 Positions.Dispose(); ··· 90 93 Transforms.Dispose(); 91 94 } 92 95 96 + public Enumerator GetEnumerator() => new Enumerator(this, 0, ActiveCount); 97 + IEnumerator<Danmaku> IEnumerable<Danmaku>.GetEnumerator() => GetEnumerator(); 98 + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 99 + 93 100 internal void Destroy(Danmaku deactivate) => Deactivated.Push(deactivate.Index); 94 101 102 + void Swap<T>(ref NativeArray<T> array, int a, int b) where T : struct { 103 + T temp = array[a]; 104 + array[a] = array[b]; 105 + array[b] = temp; 106 + } 107 + 108 + void DestroyInternal(int index) { 109 + ActiveCount--; 110 + Swap(ref Positions, index, ActiveCount); 111 + Swap(ref Rotations, index, ActiveCount); 112 + Swap(ref Speeds, index, ActiveCount); 113 + Swap(ref AngularSpeeds, index, ActiveCount); 114 + Swap(ref Colors, index, ActiveCount); 115 + } 116 + 95 117 public struct UpdateContext : IDisposable { 96 118 97 119 const int kBatchSize = 32; ··· 131 153 } 132 154 133 155 } 156 + 157 + public struct Enumerator : IEnumerator<Danmaku> { 158 + 159 + readonly DanmakuPool pool; 160 + readonly int start; 161 + readonly int end; 162 + int index; 163 + 164 + public Danmaku Current => new Danmaku(pool, index); 165 + object IEnumerator.Current => Current; 166 + 167 + internal Enumerator(DanmakuPool pool, int start, int count) { 168 + this.pool = pool; 169 + this.start = start; 170 + this.end = start + count; 171 + index = -1; 172 + } 173 + 174 + public bool MoveNext() { 175 + if (index < 0) { 176 + index = start; 177 + } else { 178 + index++; 179 + } 180 + return index < end; 181 + } 182 + 183 + public void Reset() => index = -1; 184 + public void Dispose() {} 185 + 186 + } 187 + 134 188 } 135 189 136 190 }