コード例 #1
0
        //Randomly places blocks on the tile. No check iwhether
        private void CreateBlocks()
        {
            Blocks            = new Block[wblocks, hblocks];
            bool[,] blockGrid = new bool[wblocks, hblocks];

            //Prepare grid (currently a random dumb layout, replace with a proper algorithm)
            for (int w = 0; w < wblocks; ++w)
            {
                for (int h = 0; h < hblocks; ++h)
                {
                    blockGrid[w, h] = false;

                    //Generate solid ground
                    if (h > GroundLevel)
                    {
                        blockGrid[w, h] = true;
                        continue;
                    }

                    //Only add blocks other than ground when not a Screen tile
                    if (Type != TileType.Screen)
                    {
                        if (w < 4 || w > wblocks - 5)
                        {
                            blockGrid[w, hblocks - 3] = true;
                            continue;
                        }

                        int tmp = AlgorithmHelper.GetRandom(0, 15);
                        if (tmp > 3 && tmp < 7)
                        {
                            blockGrid[w, h] = true;
                        }
                        if (tmp == 1 && w < wblocks - 2)
                        {
                            blockGrid[w + 1, h] = true;
                        }
                        if (tmp == 5 && h < hblocks - 2)
                        {
                            blockGrid[w, h + 1] = true;
                        }
                    }
                }
            }

            //Generate textures according to the grid
            bool borderAbove;
            bool borderLeft;
            bool borderRight;
            bool borderBelow;


            for (int w = 0; w < wblocks; ++w)
            {
                for (int h = 0; h < hblocks; ++h)
                {
                    if (blockGrid[w, h])
                    {
                        //if there is a block above this one
                        if (h > 0 && blockGrid[w, h - 1])
                        {
                            borderAbove = false;
                        }
                        else
                        {
                            borderAbove = true;
                        }

                        //if there is a block below this one
                        if (h < hblocks - 1 && blockGrid[w, h + 1])
                        {
                            borderBelow = false;
                        }
                        else
                        {
                            borderBelow = true;
                        }

                        //if there is a block left of this one
                        if (w > 0 && blockGrid[w - 1, h])
                        {
                            borderLeft = false;
                        }
                        else
                        {
                            borderLeft = true;
                        }

                        //if there is a block right of this one
                        if (w < wblocks - 1 && blockGrid[w + 1, h])
                        {
                            borderRight = false;
                        }
                        else
                        {
                            borderRight = true;
                        }


                        //Assign texture accordingly
                        //If block is topmost, use a different texture
                        Texture2D texture = borderAbove ? GraphicsHelper.CopyTexture(BlockTexture) : GraphicsHelper.CopyTexture(BlockTopmostTexture); //need to COPY THE TEXTURE HERE
                        if (Background.OutlineBlocks)
                        {
                            GraphicsHelper.OutlineRectangleSide(texture, Color.LightGray, 4, borderLeft, borderAbove, borderRight, borderBelow);
                        }

                        Blocks[w, h] = new Block(texture, borderAbove);
                    }
                }
            }
        }