예제 #1
0
        public override void UpdateData(bool withOutputs = true)
        {
            m_NoiseParentSize = noiseGraph.Size;
            m_Input           = GetPort("m_Input").GetInputValue <List <GridShape> >();

            m_result = new float[m_NoiseParentSize.x * m_NoiseParentSize.y];

            Random.InitState(noiseGraph.Seed);

            if (m_Input == null)
            {
                return;
            }

            //construct mst
            List <EdgeData> allEdges = new List <EdgeData>();

            for (int starts = 0; starts < m_Input.Count; starts++)
            {
                for (int ends = 0; ends < starts; ends++)
                {
                    if (starts == ends)
                    {
                        continue;
                    }


                    EdgeData ed = new EdgeData();
                    ed.a      = starts;
                    ed.b      = ends;
                    ed.weight = Vector2Int.Distance(m_Input[starts].position, m_Input[ends].position);
                    allEdges.Add(ed);
                }
            }

            List <int>      processedIndices = new List <int>();
            List <EdgeData> minimumEdges     = new List <EdgeData>();

            processedIndices.Add(Random.Range(0, m_Input.Count));

            for (int i = 0; i < m_Input.Count - 1; i++)
            {
                float    lowDistance = Mathf.Infinity;
                EdgeData target      = null;
                foreach (EdgeData ed in allEdges)
                {
                    if (processedIndices.Contains(ed.a) ^ processedIndices.Contains(ed.b))
                    {
                        if (ed.weight <= lowDistance)
                        {
                            lowDistance = ed.weight;
                            target      = ed;
                        }
                    }
                }

                minimumEdges.Add(target);
                allEdges.Remove(target);
                if (!processedIndices.Contains(target.a))
                {
                    processedIndices.Add(target.a);
                }
                if (!processedIndices.Contains(target.b))
                {
                    processedIndices.Add(target.b);
                }
            }


            for (int i = 0; i < AdditionalConnectionsToMake; i++)
            {
                float    lowDistance = Mathf.Infinity;
                EdgeData target      = null;
                foreach (EdgeData ed in allEdges)
                {
                    if (ed.weight <= lowDistance)
                    {
                        lowDistance = ed.weight;
                        target      = ed;
                    }
                }
                if (target == null)
                {
                    break;
                }
                minimumEdges.Add(target);
                allEdges.Remove(target);
            }
            foreach (EdgeData ed in minimumEdges)
            {
                GridShape a = m_Input[ed.a];
                GridShape b = m_Input[ed.b];


                Vector2Int startPos          = a.position;
                Vector2Int endPos            = b.position;
                bool       axisAlignedPlaced = false;

                switch (RoomPositionSample)
                {
                case HallwayAttach.RandomInside:
                    startPos = a.GetRandomIndexInShape();
                    endPos   = b.GetRandomIndexInShape();
                    break;

                case HallwayAttach.AxisAligned:
                    axisAlignedPlaced = ConnectGridsAxisAligned(a, b, ref startPos, ref endPos);
                    break;
                }
                //will need to support multiple lines here for proper axis aligned.
                if (axisAlignedPlaced)
                {
                    continue;
                }
                m_Shapes = GetLineBetweenPoints(startPos, endPos);
                List <Vector2Int> m_targetPositions = new List <Vector2Int>();

                foreach (GridShape gs in m_Shapes)
                {
                    gs.GetIndicesInShape(ref m_targetPositions);
                }
                foreach (Vector2Int pos in m_targetPositions)
                {
                    if (GridToArray(pos) < m_result.Length && GridToArray(pos) >= 0)
                    {
                        m_result[GridToArray(pos)] = MapValue;
                    }
                }
            }
            base.UpdateData(withOutputs);
        }