Exemple #1
0
    public RiverDriver(IFluidSimulation simulation)
    {
        _simulation = simulation;

        for (int y = 0; y < _simulation.Height; ++y)
        {
            for (int x = 0; x < _simulation.Width; ++x)
            {
                if ((y < _simulation.Height / 3 && x < _simulation.Width * 5 / 8)
                    || (y > _simulation.Height * 2 / 3) || (x > _simulation.Width * 7 / 8)
                    || ((x > _simulation.Width * 5 / 8 && y > _simulation.Height / 3
                         && Mathf.Pow(((float)x / _simulation.Width - 5.0f / 8) * 4, 2)
                         + Mathf.Pow(((float)y / _simulation.Height - 1.0f / 3) * 3, 2) > 1))
                    || (x > _simulation.Width * 24 / 32 && x < _simulation.Width * 25 / 32
                        && y > _simulation.Height * 2 / 16 && y < _simulation.Height * 4 / 16))
                {
                    _simulation.AddWater(x, y, 2.0f);
                    _simulation.SetSolid(x, y, true);
                }
            }
        }
    }
Exemple #2
0
    public MazeDriver(IFluidSimulation simulation)
    {
        _simulation = simulation;

        for (int y = 0; y < _simulation.Height; ++y)
        {
            for (int x = 0; x < _simulation.Width; ++x)
            {
                _simulation.SetHeight(x, y, 1.0f);
                _simulation.SetSolid(x, y, true);
            }
        }

        int[,] maze = new int[_mazeSize, _mazeSize];
        for (int y = 0; y < _mazeSize; y += 2)
        {
            for (int x = 0; x < _mazeSize; x += 2)
            {
                maze[x,y] = 1;
            }
        }

        int numWalls = 2 * (_mazeSize/2) * ((_mazeSize/2) - 1);
        int[] walls = new int[numWalls];
        for (int i = 0; i < numWalls; ++i)
        {
            walls[i] = i;
        }
        System.Random rng = new System.Random();
        for (int i = 0; i < numWalls-1; ++i)
        {
            int swapWith = rng.Next(numWalls - i) + i;
            if (swapWith != i)
            {
                int old = walls[i];
                walls[i] = walls[swapWith];
                walls[swapWith] = old;
            }
        }

        maze[_mazeSize/4 * 2, _mazeSize/4 * 2] = 2;
        int count = (_mazeSize/2) * (_mazeSize/2) - 1;
        int pos = 0;
        while (count > 0)
        {
            ++pos;
            if (pos >= numWalls) pos = 0;

            int x, y, dx, dy;
            if (walls[pos] < numWalls / 2)
            {
                x = 2 * (walls[pos] % (_mazeSize/2));
                y = 1 + 2 * (walls[pos] / (_mazeSize/2));
                dx = 0;
                dy = 1;
            }
            else
            {
                y = 2 * ((walls[pos] - numWalls/2) % (_mazeSize/2));
                x = 1 + 2 * ((walls[pos] - numWalls/2) / (_mazeSize/2));
                dy = 0;
                dx = 1;
            }
            if (maze[x, y] != 0)
                continue;
            if (maze[x+dx, y+dy] != 2 && maze[x-dx, y-dy] != 2)
                continue;
            if (maze[x+dx, y+dy] == 2 && maze[x-dx, y-dy] == 2)
                continue;

            maze[x+dx, y+dy] = 2;
            maze[x-dx, y-dy] = 2;
            maze[x, y] = 2;
            --count;
        }

        for (int y = 0; y < _mazeSize; ++y)
        {
            for (int x = 0; x < _mazeSize; ++x)
            {
                if (maze[x, y] != 0)
                    ClearSquare(x, y);
            }
        }
    }