|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace a3geek.PhysicsLayers.Components |
| 8 | +{ |
| 9 | + using InternalManagements; |
| 10 | + |
| 11 | + [Serializable] |
| 12 | + public sealed class AllLayerInfos |
| 13 | + { |
| 14 | + public PhysicsLayerInfos PhysicsLayerInfos |
| 15 | + { |
| 16 | + get { return this.physicsLayerInfos; } |
| 17 | + } |
| 18 | + public UnityLayerInfos UnityLayerInfos |
| 19 | + { |
| 20 | + get { return this.unityLayerInfos; } |
| 21 | + } |
| 22 | + |
| 23 | + public int PhysicsLayerCount |
| 24 | + { |
| 25 | + get; private set; |
| 26 | + } |
| 27 | + public Dictionary<int, string> PhysicsLayers |
| 28 | + { |
| 29 | + get; private set; |
| 30 | + } |
| 31 | + public IEnumerable<int> PhysicsLayerIDs |
| 32 | + { |
| 33 | + get; private set; |
| 34 | + } |
| 35 | + public IEnumerable<string> PhysicsLayerNames |
| 36 | + { |
| 37 | + get; private set; |
| 38 | + } |
| 39 | + |
| 40 | + public Dictionary<int, string> UnityLayers |
| 41 | + { |
| 42 | + get; private set; |
| 43 | + } |
| 44 | + public IEnumerable<int> UnityLayerIDs |
| 45 | + { |
| 46 | + get; private set; |
| 47 | + } |
| 48 | + public IEnumerable<string> UnityLayerNames |
| 49 | + { |
| 50 | + get; private set; |
| 51 | + } |
| 52 | + |
| 53 | + [SerializeField] |
| 54 | + private PhysicsLayerInfos physicsLayerInfos = new PhysicsLayerInfos(); |
| 55 | + [SerializeField] |
| 56 | + private UnityLayerInfos unityLayerInfos = new UnityLayerInfos(); |
| 57 | + |
| 58 | + private Dictionary<int, IEnumerable<int>> ignoreLayersCache = new Dictionary<int, IEnumerable<int>>(); |
| 59 | + |
| 60 | + |
| 61 | + public AllLayerInfos() |
| 62 | + { |
| 63 | + this.PhysicsLayers = new Dictionary<int, string>(); |
| 64 | + this.PhysicsLayerIDs = new List<int>(); |
| 65 | + this.PhysicsLayerNames = new List<string>(); |
| 66 | + |
| 67 | + this.UnityLayers = new Dictionary<int, string>(); |
| 68 | + this.UnityLayerIDs = new List<int>(); |
| 69 | + this.UnityLayerNames = new List<string>(); |
| 70 | + } |
| 71 | + |
| 72 | + public void UpdateCache() |
| 73 | + { |
| 74 | + this.PhysicsLayerCount = this.PhysicsLayerInfos.LayerCount; |
| 75 | + this.PhysicsLayers = this.PhysicsLayerInfos.Layers; |
| 76 | + this.PhysicsLayerIDs = this.PhysicsLayerInfos.LayerIDs; |
| 77 | + this.PhysicsLayerNames = this.PhysicsLayerInfos.LayerNames; |
| 78 | + |
| 79 | + this.UnityLayers = this.UnityLayerInfos.Layers; |
| 80 | + this.UnityLayerIDs = this.UnityLayerInfos.LayerIDs; |
| 81 | + this.UnityLayerNames = this.UnityLayerInfos.LayerNames; |
| 82 | + |
| 83 | + foreach(var layerID in this.UnityLayerIDs.Concat(this.PhysicsLayerIDs)) |
| 84 | + { |
| 85 | + this.ignoreLayersCache[layerID] = this.GetIgnoreIDs(layerID); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public IEnumerable<int> GetIgnoreLayerIDs(int layerID) |
| 90 | + { |
| 91 | + IEnumerable<int> ie = null; |
| 92 | + this.ignoreLayersCache.TryGetValue(layerID, out ie); |
| 93 | + |
| 94 | + return ie ?? new List<int>(); |
| 95 | + } |
| 96 | + |
| 97 | + private IEnumerable<int> GetIgnoreIDs(int layerID) |
| 98 | + { |
| 99 | + if(layerID < LayersManager.UnityLayerCount) |
| 100 | + { |
| 101 | + return this.PhysicsLayerInfos.GetEnumerable() |
| 102 | + .Where(layer => |
| 103 | + { |
| 104 | + var layerCollision = layer[layerID]; |
| 105 | + return layerCollision != null && layerCollision.Collision == false; |
| 106 | + }) |
| 107 | + .Select(layer => layer.LayerID); |
| 108 | + } |
| 109 | + |
| 110 | + var physicsLayer = this.PhysicsLayerInfos[layerID]; |
| 111 | + return physicsLayer == null ? new List<int>() : physicsLayer.GetEnumerable() |
| 112 | + .Where(layerCollision => layerCollision.Collision == false) |
| 113 | + .Select(layerCollision => layerCollision.LayerID); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments