Esempio n. 1
0
        public override void OnCreated()
        {
            base.OnCreated();

            var bundles = CollectionPooler <WireBundle, BitVector32> .dictionary.GetObject();

            for (int i = 0; i < Int2.edges4.Count; i++)
            {
                if (!neighbors[1 << i])
                {
                    continue;
                }

                Int2 position = mainPosition + Int2.edges4[i];
                Wire wire     = WorldUtility.GetTile <Wire>(position);

                WireBundle bundle     = wire.Wires;
                var        directions = bundles.TryGetValue(bundle);

                wire.RecalculateNeighbors();

                directions[1 << i] = true;
                bundles[bundle]    = directions;
            }

            foreach (KeyValuePair <WireBundle, BitVector32> pair in bundles)
            {
                if (Wires == null)
                {
                    Wires = pair.Key;
                    continue;
                }

                Wires.Merge(pair.Key);

                for (int i = 0; i < Int2.edges4.Count; i++)
                {
                    if (!pair.Value[1 << i])
                    {
                        continue;
                    }

                    Int2 position = mainPosition + Int2.edges4[i];
                    Wire wire     = WorldUtility.GetTile <Wire>(position);

                    FloodFill(wire, current => current.Wires = Wires);
                }
            }

            if (bundles.Count == 0)
            {
                Wires = new WireBundle();
            }
            CollectionPooler <WireBundle, BitVector32> .dictionary.ReleaseObject(bundles);
        }
Esempio n. 2
0
        public void Connect(WireBundle bundle)
        {
            if (Connected)
            {
                throw new Exception($"Disconnect the existing bundle '{ConnectedBundle}' first!");
            }

            ConnectedBundle = bundle;
            bundle.JoinPort(this);

            DebugHelperUnity.Log($"Connected to {bundle}");
        }
Esempio n. 3
0
        public override void OnRemoved()
        {
            base.OnRemoved();

            //If the wire has no neighbor, then no bundle to reorganize
            if (neighbors.Data == 0)
            {
                return;
            }

            //Recalculate neighbors
            for (int i = 0; i < Int2.edges4.Count; i++)
            {
                if (!neighbors[1 << i])
                {
                    continue;
                }

                Int2 position = mainPosition + Int2.edges4[i];
                Wire wire     = WorldUtility.GetTile <Wire>(position);

                wire.RecalculateNeighbors();
            }

            //If the wire has one neighbor, that bundle will not change
            if (neighbors.Data.IsPowerOfTwo())
            {
                return;
            }

            //Search for all branches
            var branches = CollectionPooler <HashSet <Wire> > .list.GetObject();

            for (int i = 0; i < Int2.edges4.Count; i++)
            {
                if (!neighbors[1 << i])
                {
                    continue;
                }

                Int2 position = mainPosition + Int2.edges4[i];
                Wire wire     = WorldUtility.GetTile <Wire>(position);

                if (branches.Any(hashset => hashset.Contains(wire)))
                {
                    continue;
                }

                var visited = CollectionPooler <Wire> .hashSet.GetObject();

                FloodFill(wire, current => visited.Add(current));

                branches.Add(visited);
            }

            if (branches.Count == 1)
            {
                return;
            }
            Assert.AreNotEqual(branches.Count, 0);

            //Reorganize wire bundles
            for (int i = 1; i < branches.Count; i++)             //NOTE: Start at one
            {
                HashSet <Wire> visited = branches[i];
                WireBundle     bundle  = Wires.Split
                                         (
                    port =>
                {
                    Wire wire = WorldUtility.GetTile <Wire>(port.Position + port.Direction);
                    return(wire != null && visited.Contains(wire));
                }
                                         );

                foreach (Wire wire in visited)
                {
                    wire.Wires = bundle;
                }
                CollectionPooler <Wire> .hashSet.ReleaseObject(visited);
            }

            CollectionPooler <HashSet <Wire> > .list.ReleaseObject(branches);
        }