コード例 #1
0
 public bool Intersects(GenChunkRect rect)
 {
     return
         (Left < rect.Right &&
          Right > rect.Left &&
          Top < rect.Bottom &&
          Bottom > rect.Top);
 }
コード例 #2
0
        private GenChunkAddedStatus GenAddChunk(Random rand, Stack <GenEndPoint> endPoints, List <GenEndPoint> looseEnds, List <GenChunkRect> rects, ChunkTemplate[] templates, ref int area, int maxDepth = 0, bool first = false)
        {
            ChunkTemplate template;

            Chunk newChunk;
            List <GenEndPoint> endsToAdd;

            if (first)
            {
                template = templates[rand.Next(templates.Length)];
                newChunk = new Chunk(0, 0, template, this);
                Chunks.Add(newChunk);
                endsToAdd = new List <GenEndPoint>();
                for (int i = 0; i < 4; ++i)
                {
                    foreach (ChunkConnector connector in template.GetConnectors((ConnectorFace)i))
                    {
                        endsToAdd.Add(new GenEndPoint(newChunk, connector, 0));
                    }
                }

                while (endsToAdd.Count > 0)
                {
                    int index = rand.Next(endsToAdd.Count);
                    endPoints.Push(endsToAdd[index]);
                    endsToAdd.RemoveAt(index);
                }

                rects.Add(new GenChunkRect(newChunk));
                area += newChunk.Area;

                return(GenChunkAddedStatus.Added);
            }

            List <ChunkTemplate> validTemplates = new List <ChunkTemplate>();

            GenEndPoint endPoint = endPoints.Pop();

            ConnectorFace oppositeFace = (ConnectorFace)(((int)endPoint.Face + 2) % 4);

            foreach (ChunkTemplate temp in templates)
            {
                if (temp.GetConnectors(oppositeFace, endPoint.Size, endPoint.Skin).Length != 0)
                {
                    validTemplates.Add(temp);
                }
            }

            if (validTemplates.Count == 0)
            {
                endPoints.Push(endPoint);
                return(GenChunkAddedStatus.ImpossibleToAdd);
            }

            template = validTemplates[rand.Next(validTemplates.Count)];

            ChunkConnector[] cons = template.GetConnectors(oppositeFace, endPoint.Size, endPoint.Skin);

            if (cons.Length == 0)
            {
                endPoints.Push(endPoint);
                return(GenChunkAddedStatus.NotAdded);
            }

            ChunkConnector con = cons[rand.Next(cons.Length)];

            int x = endPoint.X - con.X;
            int y = endPoint.Y - con.Y;

            switch (endPoint.Face)
            {
            case ConnectorFace.Left:
                --x; break;

            case ConnectorFace.Top:
                --y; break;

            case ConnectorFace.Right:
                ++x; break;

            case ConnectorFace.Bottom:
                ++y; break;
            }

            GenChunkRect newRect = new GenChunkRect(x, y, template);

            foreach (GenChunkRect rect in rects)
            {
                if (rect.Intersects(newRect))
                {
                    endPoints.Push(endPoint);
                    return(GenChunkAddedStatus.NotAdded);
                }
            }

            newChunk = new Chunk(x, y, template, this);
            Chunks.Add(newChunk);
            endsToAdd = new List <GenEndPoint>();
            for (int i = 0; i < 4; ++i)
            {
                foreach (ChunkConnector connector in template.GetConnectors((ConnectorFace)i))
                {
                    if (connector.X != con.X || connector.Y != con.Y)
                    {
                        GenEndPoint end = new GenEndPoint(newChunk, connector, endPoint.Depth + 1);
                        if (maxDepth != 0 && endPoint.Depth >= maxDepth)
                        {
                            looseEnds.Add(end);
                        }
                        else
                        {
                            endsToAdd.Add(end);
                        }
                    }
                }
            }

            while (endsToAdd.Count > 0)
            {
                int index = rand.Next(endsToAdd.Count);
                endPoints.Push(endsToAdd[index]);
                endsToAdd.RemoveAt(index);
            }

            rects.Add(newRect);
            area += newChunk.Area;

            return(GenChunkAddedStatus.Added);
        }