Exemplo n.º 1
0
        public bool UpdateForeUV(int tileX, int tileY, bool updateVariation, bool rebakeChunk, bool updateAdjacentUVs)
        {
            if (!InTileBounds(tileX, tileY))
            {
                return(false);
            }
            int  chunkX     = (tileX >> Chunk.Bits);
            int  chunkY     = (tileY >> Chunk.Bits);
            int  chunkTileX = (tileX & Chunk.Modulo);
            int  chunkTileY = (tileY & Chunk.Modulo);
            Tile tile       = Chunks[chunkX, chunkY].Tiles[chunkTileX, chunkTileY];

            if (tile.Fore == Tile.Fores.None)
            {
                if (UpdateAdjacentForeUVs(tileX, tileY, chunkX, chunkY))
                {
                    BakeFore();
                }
                return(false);
            }
            ForeTileData tileData = Game1.ForeTileData[tile.Fore];

            if (updateVariation)
            {
                RandomizeForeVariation(chunkX, chunkY, chunkTileX, chunkTileY, tile, tileData);
            }
            if (UpdateForeUV(tileX, tileY, chunkX, chunkY, chunkTileX, chunkTileY, tile, tileData))
            {
                if (updateAdjacentUVs && UpdateAdjacentForeUVs(tileX, tileY, chunkX, chunkY))
                {
                    BakeFore();
                }
                if (rebakeChunk)
                {
                    if (Chunks[chunkX, chunkY].ForeTexture != null)
                    {
                        Chunks[chunkX, chunkY].BakeFore();
                        BakeFore();
                    }
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        public bool RandomizeForeVariation(int tileX, int tileY)
        {
            if (!InTileBounds(tileX, tileY))
            {
                return(false);
            }
            int  chunkX     = (tileX >> Chunk.Bits);
            int  chunkY     = (tileY >> Chunk.Bits);
            int  chunkTileX = (tileX & Chunk.Modulo);
            int  chunkTileY = (tileY & Chunk.Modulo);
            Tile tile       = Chunks[chunkX, chunkY].Tiles[chunkTileX, chunkTileY];

            if (tile.Fore == Tile.Fores.None)
            {
                return(false);
            }
            ForeTileData tileData = Game1.ForeTileData[tile.Fore];

            RandomizeForeVariation(chunkX, chunkY, chunkTileX, chunkTileY, tile, tileData);
            return(true);
        }
Exemplo n.º 3
0
        public void BakeFore()
        {
            if (ForeTexture == null)
            {
                ForeTexture              = new RenderTarget2D(Program.Game.GraphicsDevice, ForeTextureSize, ForeTextureSize, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
                ForeTexture.ContentLost += Fore_ContentLost;
            }
            SpriteBatch spriteBatch = Program.Game.Services.GetService <SpriteBatch>();

            Program.Game.GraphicsDevice.SetRenderTarget(ForeTexture);
            Program.Game.GraphicsDevice.Clear(Color.TransparentBlack);
            spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null);
            for (int x = 0; x < Size; x++)
            {
                for (int y = 0; y < Size; y++)
                {
                    if (Tiles[x, y].Fore != Tile.Fores.None)
                    {
                        ForeTileData tileData = Game1.ForeTileData[Tiles[x, y].Fore];
                        spriteBatch.Draw(tileData.Texture, new Rectangle((x * Tile.Size), (y * Tile.Size), Tile.Size, Tile.Size), ForeTileData.UV[(int)tileData.UVType][Tiles[x, y].ForeUV][Tiles[x, y].ForeVariation], Color.White);
                    }
                }
            }
#if DEBUG
            Game1.WorldBakeDrawCount      += (Program.Game.GraphicsDevice.Metrics.DrawCount - Game1.WorldBakeDrawCount);
            Game1.WorldBakeTextureCount   += (Program.Game.GraphicsDevice.Metrics.TextureCount - Game1.WorldBakeTextureCount);
            Game1.WorldBakeSpriteCount    += (Program.Game.GraphicsDevice.Metrics.SpriteCount - Game1.WorldBakeSpriteCount);
            Game1.WorldBakePrimitiveCount += (Program.Game.GraphicsDevice.Metrics.PrimitiveCount - Game1.WorldBakePrimitiveCount);
            Game1.WorldBakeTargetCount    += (Program.Game.GraphicsDevice.Metrics.TargetCount - Game1.WorldBakeTargetCount);
            Color color = (Color.Red * .5f);
            spriteBatch.Draw(Game1.Pixel, new Rectangle(0, 0, ForeTextureSize, 1), null, color, 0, Vector2.Zero, SpriteEffects.None, 0);
            spriteBatch.Draw(Game1.Pixel, new Rectangle((ForeTextureSize - 1), 1, 1, (ForeTextureSize - 1)), null, color, 0, Vector2.Zero, SpriteEffects.None, 0);
            spriteBatch.Draw(Game1.Pixel, new Rectangle(0, (ForeTextureSize - 1), (ForeTextureSize - 1), 1), null, color, 0, Vector2.Zero, SpriteEffects.None, 0);
            spriteBatch.Draw(Game1.Pixel, new Rectangle(0, 1, 1, (ForeTextureSize - 2)), null, color, 0, Vector2.Zero, SpriteEffects.None, 0);
#endif
            spriteBatch.End();
        }
Exemplo n.º 4
0
        internal void RandomizeForeVariation(int chunkX, int chunkY, int chunkTileX, int chunkTileY, Tile tile, ForeTileData tileData)
        {
            int maxVariations = ForeTileData.UV[(int)tileData.UVType][tile.ForeUV].Length;

            Chunks[chunkX, chunkY].Tiles[chunkTileX, chunkTileY].ForeVariation = (byte)Enumerable.Range(0, maxVariations).ElementAt(Game1.Random.Next(maxVariations));
        }
Exemplo n.º 5
0
        internal bool UpdateForeUV(int tileX, int tileY, int chunkX, int chunkY, int chunkTileX, int chunkTileY, Tile tile, ForeTileData tileData)
        {
            byte uv = 0;

            if (tileData.UVType == ForeTileData.UVTypes.tile1)
            {
                Tile?up   = GetTile(tileX, (tileY - 1)),
                    right = GetTile((tileX + 1), tileY),
                    down  = GetTile(tileX, (tileY + 1)),
                    left  = GetTile((tileX - 1), tileY);
                if (up.HasValue && (Array.IndexOf(tileData.AttachedTiles, up.Value.Fore) != -1))
                {
                    uv++;
                }
                if (!right.HasValue || (Array.IndexOf(tileData.AttachedTiles, right.Value.Fore) != -1))
                {
                    uv += 2;
                }
                if (!down.HasValue || (Array.IndexOf(tileData.AttachedTiles, down.Value.Fore) != -1))
                {
                    uv += 4;
                }
                if (!left.HasValue || (Array.IndexOf(tileData.AttachedTiles, left.Value.Fore) != -1))
                {
                    uv += 8;
                }
            }
            else if (tileData.UVType == ForeTileData.UVTypes.tile2)
            {
                Tile?up       = GetTile(tileX, (tileY - 1)),
                    right     = GetTile((tileX + 1), tileY),
                    down      = GetTile(tileX, (tileY + 1)),
                    left      = GetTile((tileX - 1), tileY),
                    upRight   = GetTile((tileX + 1), (tileY - 1)),
                    rightDown = GetTile((tileX + 1), (tileY + 1)),
                    downLeft  = GetTile((tileX - 1), (tileY + 1)),
                    leftUp    = GetTile((tileX - 1), (tileY - 1));
                if (up.HasValue && (Array.IndexOf(tileData.AttachedTiles, up.Value.Fore) != -1))
                {
                    uv++;
                }
                if (!right.HasValue || (Array.IndexOf(tileData.AttachedTiles, right.Value.Fore) != -1))
                {
                    uv += 2;
                }
                if (!down.HasValue || (Array.IndexOf(tileData.AttachedTiles, down.Value.Fore) != -1))
                {
                    uv += 4;
                }
                if (!left.HasValue || (Array.IndexOf(tileData.AttachedTiles, left.Value.Fore) != -1))
                {
                    uv += 8;
                }
                if (upRight.HasValue && (Array.IndexOf(tileData.AttachedTiles, upRight.Value.Fore) == -1))
                {
                    if (uv == 3)
                    {
                        uv = 16;
                    }
                    else if (uv == 7)
                    {
                        uv = 17;
                    }
                    else if (uv == 11)
                    {
                        uv = 18;
                    }
                    else if (uv == 15)
                    {
                        uv = 19;
                    }
                }
                if (!rightDown.HasValue || (Array.IndexOf(tileData.AttachedTiles, rightDown.Value.Fore) == -1))
                {
                    if (uv == 6)
                    {
                        uv = 20;
                    }
                    else if (uv == 7)
                    {
                        uv = 21;
                    }
                    else if (uv == 14)
                    {
                        uv = 22;
                    }
                    else if (uv == 15)
                    {
                        uv = 23;
                    }
                    else if (uv == 17)
                    {
                        uv = 24;
                    }
                    else if (uv == 19)
                    {
                        uv = 25;
                    }
                }
                if (!downLeft.HasValue || (Array.IndexOf(tileData.AttachedTiles, downLeft.Value.Fore) == -1))
                {
                    if (uv == 12)
                    {
                        uv = 26;
                    }
                    else if (uv == 13)
                    {
                        uv = 27;
                    }
                    else if (uv == 14)
                    {
                        uv = 28;
                    }
                    else if (uv == 15)
                    {
                        uv = 29;
                    }
                    else if (uv == 19)
                    {
                        uv = 30;
                    }
                    else if (uv == 22)
                    {
                        uv = 31;
                    }
                    else if (uv == 23)
                    {
                        uv = 32;
                    }
                    else if (uv == 25)
                    {
                        uv = 33;
                    }
                }
                if (leftUp.HasValue && (Array.IndexOf(tileData.AttachedTiles, leftUp.Value.Fore) == -1))
                {
                    if (uv == 9)
                    {
                        uv = 34;
                    }
                    else if (uv == 11)
                    {
                        uv = 35;
                    }
                    else if (uv == 13)
                    {
                        uv = 36;
                    }
                    else if (uv == 15)
                    {
                        uv = 37;
                    }
                    else if (uv == 18)
                    {
                        uv = 38;
                    }
                    else if (uv == 19)
                    {
                        uv = 39;
                    }
                    else if (uv == 23)
                    {
                        uv = 40;
                    }
                    else if (uv == 25)
                    {
                        uv = 41;
                    }
                    else if (uv == 27)
                    {
                        uv = 42;
                    }
                    else if (uv == 29)
                    {
                        uv = 43;
                    }
                    else if (uv == 30)
                    {
                        uv = 44;
                    }
                    else if (uv == 32)
                    {
                        uv = 45;
                    }
                    else if (uv == 33)
                    {
                        uv = 46;
                    }
                }
            }
            if (Chunks[chunkX, chunkY].Tiles[chunkTileX, chunkTileY].ForeUV == uv)
            {
                return(false);
            }
            Chunks[chunkX, chunkY].Tiles[chunkTileX, chunkTileY].ForeUV = uv;
            return(true);
        }