如何处理基于瓷砖的游戏中的液体流动?

时间:2012-12-25 21:48:18

标签: c# xna tile

就像标题所说,我需要在基于磁贴的游戏中处理液体。

信息:每个图块都是16xPixels,它有一个WaterLevel(字节)。 水应该与其附近的其他瓷砖平均,因为每个瓷砖都有水位。但我似乎无法做到。

我希望它像在这个视频中一样: http://www.youtube.com/watch?v=3v9sH638nr0

但我不能这样做。帮助

if (WaterLevel > 0)
        {
            //Bottom
            if (Program.Game.CurrentMap.Tiles[(int)TileLocation.X, (int)TileLocation.Y + 1].Collide == false && Program.Game.CurrentMap.Tiles[(int)TileLocation.X, (int)TileLocation.Y + 1].WaterLevel != 255)
            {
                if ((int)Program.Game.CurrentMap.Tiles[(int)TileLocation.X, (int)TileLocation.Y + 1].WaterLevel + (int)WaterLevel > 255)
                {
                    WaterLevel -= (byte)(255 - Program.Game.CurrentMap.Tiles[(int)TileLocation.X, (int)TileLocation.Y + 1].WaterLevel);
                    Program.Game.CurrentMap.Tiles[(int)TileLocation.X, (int)TileLocation.Y + 1].WaterLevel = 255;
                }
                else
                {
                    Program.Game.CurrentMap.Tiles[(int)TileLocation.X, (int)TileLocation.Y + 1].WaterLevel += WaterLevel;
                    this.WaterLevel = 0;
                }
            }
            //Left or Right
            else
            {
                bool Left = Program.Game.CurrentMap.Tiles[(int)TileLocation.X - 1, (int)TileLocation.Y].Collide;
                bool Right = Program.Game.CurrentMap.Tiles[(int)TileLocation.X + 1, (int)TileLocation.Y].Collide;
                byte Divider = 1;
                int Average = WaterLevel;
                if (Left)
                {
                    Divider += 1;
                    Average += Program.Game.CurrentMap.Tiles[(int)TileLocation.X - 1, (int)TileLocation.Y].WaterLevel;
                }
                if (Right)
                {
                    Divider += 1;
                    Average += Program.Game.CurrentMap.Tiles[(int)TileLocation.X + 1, (int)TileLocation.Y].WaterLevel;
                }
                WaterLevel = (byte)(Average / Divider);
                Average /= Divider;
                Divider -= 1;
                if (Left)
                {
                    Program.Game.CurrentMap.Tiles[(int)TileLocation.X - 1, (int)TileLocation.Y].WaterLevel = (byte)(Average / Divider);
                    Average /= Divider;
                    Divider -= 1;
                }
                if (Right)
                {
                    Program.Game.CurrentMap.Tiles[(int)TileLocation.X + 1, (int)TileLocation.Y].WaterLevel = (byte)(Average / Divider);
                    Average /= Divider;
                    Divider -= 1;
                }
            }
        }

0 个答案:

没有答案