Exemplo n.º 1
0
            /// <summary>
            /// Generates the decor vertices for the specified tile.
            /// </summary>
            /// <param name="x">The tile x coordinate.</param>
            /// <param name="y">The tile y coordinate.</param>
            /// <param name="color">The color tint to apply.</param>
            private void GenerateDecorVertices(int x, int y, Color color)
            {
                var  dri       = decorRenderInfo.decor;
                int  n         = dri.Length;
                Bits connected = TileRendererUtils.GetDecorConnectionBits(x, y, queryLayer);

                for (int i = 0; i < n; i++)
                {
                    var  decor    = dri[i];
                    var  variants = decor.variants;
                    Bits required = decor.requiredConnections;
                    if (variants != null && variants.Length > 0 && (connected & required) ==
                        required && (connected & decor.forbiddenConnections) == 0)
                    {
                        // Use a seeded random noise to determine if the tops spawn here
                        float score = PerlinSimplexNoise.noise((float)(i + x + connected) *
                                                               SIMPLEX_SCALE.x, (float)(i + y + connected) * SIMPLEX_SCALE.y);
                        if (score >= decor.probabilityCutoff)
                        {
                            TileRendererUtils.AddDecorVertexInfo(ref decor, x, y, score, color,
                                                                 decorTriangles);
                        }
                    }
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Checks the connectivity of a cell.
        /// </summary>
        /// <param name="x">The cell's x coordinate.</param>
        /// <param name="y">The cell's y coordinate.</param>
        /// <param name="queryLayer">The object layer to check.</param>
        /// <returns>A bit mask of the neighboring cells in all 8 directions that have the same
        /// building def as this cell on the same layer.</returns>
        public static Bits GetConnectionBits(int x, int y, int queryLayer)
        {
            Bits        bits = 0;
            int         w = Grid.WidthInCells, cell = y * w + x;
            var         go  = Grid.Objects[cell, queryLayer];
            BuildingDef def = null;

            if (go != null && go.TryGetComponent(out Building building))
            {
                def = building.Def;
            }
            // Below
            if (y > 0)
            {
                int cellsBelow = cell - w;
                if (x > 0 && MatchesDef(cellsBelow - 1, queryLayer, def))
                {
                    bits |= Bits.DownLeft;
                }
                if (MatchesDef(cellsBelow, queryLayer, def))
                {
                    bits |= Bits.Down;
                }
                if (x < w - 1 && MatchesDef(cellsBelow + 1, queryLayer, def))
                {
                    bits |= Bits.DownRight;
                }
            }
            if (x > 0 && MatchesDef(cell - 1, queryLayer, def))
            {
                bits |= Bits.Left;
            }
            if (x < w - 1 && MatchesDef(cell + 1, queryLayer, def))
            {
                bits |= Bits.Right;
            }
            // Above
            if (y < Grid.HeightInCells - 1)
            {
                int cellsAbove = cell + w;
                if (x > 0 && MatchesDef(cellsAbove - 1, queryLayer, def))
                {
                    bits |= Bits.UpLeft;
                }
                if (MatchesDef(cellsAbove, queryLayer, def))
                {
                    bits |= Bits.Up;
                }
                if (x < w - 1 && MatchesDef(cellsAbove + 1, queryLayer, def))
                {
                    bits |= Bits.UpRight;
                }
            }
            return(bits);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks the tile decor connectivity of a cell.
        /// </summary>
        /// <param name="x">The cell's x coordinate.</param>
        /// <param name="y">The cell's y coordinate.</param>
        /// <param name="queryLayer">The object layer to check.</param>
        /// <returns>A bit mask of the neighboring cells in all 8 directions that have a
        /// building on the requested layer.</returns>
        public static Bits GetDecorConnectionBits(int x, int y, int queryLayer)
        {
            Bits bits = 0;
            int  w = Grid.WidthInCells, cell = y * w + x;
            var  go = Grid.Objects[cell, queryLayer];

            // Below
            if (y > 0)
            {
                int cellsBelow = cell - w;
                if (x > 0 && Grid.Objects[cellsBelow - 1, queryLayer] != null)
                {
                    bits |= Bits.DownLeft;
                }
                if (Grid.Objects[cellsBelow, queryLayer] != null)
                {
                    bits |= Bits.Down;
                }
                if (x < w - 1 && Grid.Objects[cellsBelow + 1, queryLayer] != null)
                {
                    bits |= Bits.DownRight;
                }
            }
            if (x > 0 && IsDecorConnectable(go, Grid.Objects[cell - 1, queryLayer]))
            {
                bits |= Bits.Left;
            }
            if (x < w - 1 && IsDecorConnectable(go, Grid.Objects[cell + 1, queryLayer]))
            {
                bits |= Bits.Right;
            }
            // Above
            if (y < Grid.HeightInCells - 1)
            {
                int cellsAbove = cell + w;
                if (x > 0 && Grid.Objects[cellsAbove - 1, queryLayer] != null)
                {
                    bits |= Bits.UpLeft;
                }
                if (Grid.Objects[cellsAbove, queryLayer] != null)
                {
                    bits |= Bits.Up;
                }
                if (x < w - 1 && Grid.Objects[cellsAbove + 1, queryLayer] != null)
                {
                    bits |= Bits.UpRight;
                }
            }
            return(bits);
        }
Exemplo n.º 4
0
            /// <summary>
            /// Generates the vertices for the specified tile.
            /// </summary>
            /// <param name="x">The tile x coordinate.</param>
            /// <param name="y">The tile y coordinate.</param>
            /// <param name="color">The color tint to apply.</param>
            private void GenerateVertices(int x, int y, Color color)
            {
                // Difficult to optimize this further as bits can have any combination
                Bits connected = TileRendererUtils.GetConnectionBits(x, y, queryLayer);
                int  n         = atlasInfo.Length;

                for (int i = 0; i < n; i++)
                {
                    var  ai      = atlasInfo[i];
                    Bits require = ai.requiredConnections;
                    // If all required bits, and no forbidden bits, render this one
                    if ((require & connected) == require && (ai.forbiddenConnections &
                                                             connected) == 0)
                    {
                        TileRendererUtils.AddVertexInfo(ai, x, y, connected, color);
                        break;
                    }
                }
            }
Exemplo n.º 5
0
        /// <summary>
        /// Generates the vertices for one tile in the mesh.
        /// </summary>
        /// <param name="atlasInfo">The texture atlas to use for rendering.</param>
        /// <param name="x">The bottom left X coordinate.</param>
        /// <param name="y">The bottom left Y coordinate.</param>
        /// <param name="connected">The sides which are connected.</param>
        /// <param name="color">The tint color to use when rendering.</param>
        public static void AddVertexInfo(this AtlasInfo atlasInfo, int x, int y,
                                         Bits connected, Color color)
        {
            float   size     = Grid.CellSizeInMeters;
            Vector2 botLeft  = new Vector2(x, y);
            Vector2 topRight = new Vector2(x + size, y + size);
            Vector2 uvWX     = new Vector2(atlasInfo.uvBox.x, atlasInfo.uvBox.w);
            Vector2 uvYZ     = new Vector2(atlasInfo.uvBox.z, atlasInfo.uvBox.y);
            var     vertices = MeshUtil.vertices;
            var     uvs      = MeshUtil.uvs;
            var     indices  = MeshUtil.indices;
            var     colors   = MeshUtil.colours;

            if ((connected & Bits.Left) == 0)
            {
                botLeft.x -= WORLD_TRIM_SIZE;
            }
            else
            {
                uvWX.x += TRIM_UV_SIZE.x;
            }
            if ((connected & Bits.Right) == 0)
            {
                topRight.x += WORLD_TRIM_SIZE;
            }
            else
            {
                uvYZ.x -= TRIM_UV_SIZE.x;
            }
            if ((connected & Bits.Up) == 0)
            {
                topRight.y += WORLD_TRIM_SIZE;
            }
            else
            {
                uvYZ.y -= TRIM_UV_SIZE.y;
            }
            if ((connected & Bits.Down) == 0)
            {
                botLeft.y -= WORLD_TRIM_SIZE;
            }
            else
            {
                uvWX.y += TRIM_UV_SIZE.y;
            }
            int v0 = vertices.Count;

            vertices.Add(botLeft);
            vertices.Add(new Vector2(topRight.x, botLeft.y));
            vertices.Add(topRight);
            vertices.Add(new Vector2(botLeft.x, topRight.y));
            uvs.Add(uvWX);
            uvs.Add(new Vector2(uvYZ.x, uvWX.y));
            uvs.Add(uvYZ);
            uvs.Add(new Vector2(uvWX.x, uvYZ.y));
            indices.Add(v0);
            indices.Add(v0 + 1);
            indices.Add(v0 + 2);
            indices.Add(v0);
            indices.Add(v0 + 2);
            indices.Add(v0 + 3);
            colors.Add(color);
            colors.Add(color);
            colors.Add(color);
            colors.Add(color);
        }