示例#1
0
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        // Randomize seed
        RNG.Randomize();
        // Set the starting tile to middle of bounds
        Vector2 startTile = new Vector2((MinTileBound + MaxTileBound) / 2);

        startTile = startTile.Floor();
        // Create random walkers
        RandomWalker randomWalker = new RandomWalker(startTile, this);
        // Run simulation
        int numSteps = 0;

        while (this.GetUsedCells().Count < MaxCells && numSteps < MaxSteps)
        {
            randomWalker.Step();
            numSteps++;
        }
        // Fill in unused areas with walls
        Vector2 padTiles  = (Godot.OS.WindowSize / this.CellSize).Ceil();
        Rect2   floorRect = this.GetUsedRect();

        for (float i = floorRect.Position[0] - padTiles[0]; i < floorRect.End[0] + padTiles[0]; i++)
        {
            for (float j = floorRect.Position[1] - padTiles[1]; j < floorRect.End[1] + padTiles[1]; j++)
            {
                Vector2 curPos = new Vector2(i, j);
                if (this.GetCellv(curPos) == InvalidCell)
                {
                    this.SetCellv(curPos, 1);
                }
            }
        }
    }
        public AutoMove(RandomWalker randomWalker, LocalManager lm)
        {
            this.randomWalker = randomWalker;
            this.localManager = lm;

            randomWalker.StartCoroutine(MoveAround());
        }
示例#3
0
        // TODO: in XRML, at any rate, this seems to go in reverse and not come back
        public void TestRandomWalker()
        {
            var walker = new RandomWalker(20, 5, 10);

            for (var i = 0; i < 1000; ++i)
            {
                Console.WriteLine(string.Format("{0}", walker.Next()));
            }
        }
示例#4
0
    void Setup()
    {
        // prepare grid
        grid = new LevelTile[levelWidth, levelHeight];
        for (int x = 0; x < levelWidth - 1; x++)
        {
            for (int y = 0; y < levelHeight - 1; y++)
            {
                grid[x, y] = LevelTile.empty;
            }
        }

        //generate first walker
        walkers = new List <RandomWalker>();
        RandomWalker walker = new RandomWalker();

        walker.dir = RandomDirection();
        Vector2 pos = new Vector2(Mathf.RoundToInt(levelWidth / 2.0f), Mathf.RoundToInt(levelHeight / 2.0f));

        walker.pos = pos;
        walkers.Add(walker);
    }
示例#5
0
    void CreateFloors()
    {
        int iterations = 0;

        do
        {
            //create floor at position of every Walker
            foreach (RandomWalker walker in walkers)
            {
                grid[(int)walker.pos.x, (int)walker.pos.y] = LevelTile.floor;
            }

            //chance: destroy Walker
            int numberChecks = walkers.Count;
            for (int i = 0; i < numberChecks; i++)
            {
                if (Random.value < chanceWalkerDestoy && walkers.Count > 1)
                {
                    walkers.RemoveAt(i);
                    break;
                }
            }

            //chance: Walker pick new direction
            for (int i = 0; i < walkers.Count; i++)
            {
                if (Random.value < chanceWalkerChangeDir)
                {
                    RandomWalker thisWalker = walkers[i];
                    thisWalker.dir = RandomDirection();
                    walkers[i]     = thisWalker;
                }
            }

            //chance: spawn new Walker
            numberChecks = walkers.Count;
            for (int i = 0; i < numberChecks; i++)
            {
                if (Random.value < chanceWalkerSpawn && walkers.Count < maxWalkers)
                {
                    RandomWalker walker = new RandomWalker();
                    walker.dir = RandomDirection();
                    walker.pos = walkers[i].pos;
                    walkers.Add(walker);
                }
            }

            //move Walkers
            for (int i = 0; i < walkers.Count; i++)
            {
                RandomWalker walker = walkers[i];
                walker.pos += walker.dir;
                walkers[i]  = walker;
            }

            //avoid boarder of grid
            for (int i = 0; i < walkers.Count; i++)
            {
                RandomWalker walker = walkers[i];
                walker.pos.x = Mathf.Clamp(walker.pos.x, 1, levelWidth - 2);
                walker.pos.y = Mathf.Clamp(walker.pos.y, 1, levelHeight - 2);
                walkers[i]   = walker;
            }

            //check to exit loop
            if ((float)NumberOfFloors() / (float)grid.Length > percentToFill)
            {
                break;
            }
            iterations++;
        } while(iterations < iterationSteps);
    }
 public RandomEvent(RandomWalker r, int dir)
 {
     Walker    = r;
     Direction = dir;
 }
示例#7
0
 public void AddWalker(RandomWalker walker)
 {
     _walkers.Add(walker);
     this.pos.Add(zero);
     Debug.Log(string.Format("<color=cyan>Successfully Added: {0}</color>", walker.Name));
 }
示例#8
0
 public void RemoveWalker(RandomWalker walker)
 {
     _walkers.Remove(walker);
     Debug.Log(string.Format("<color=red>Successfully Removed: {0}</color>", walker.Name));
 }
示例#9
0
    void GenerateLevel()
    {
        // Create empty map
        grid = new bool[maxWidth, maxHeight];

        // Generate a walker with pos at the middle and random dir
        walker     = new RandomWalker();
        walker.dir = RandomDirection();
        Vector2 pos = new Vector2(Mathf.RoundToInt(maxWidth / 2.0f), Mathf.RoundToInt(maxHeight / 2.0f));

        walker.pos = pos;

        // Iterate
        for (int i = 0; i < iterationSteps; i++)
        {
            // Mark current position as a cell
            grid[(int)walker.pos.x, (int)walker.pos.y] = true;

            // Move and select new direction
            walker.pos += walker.dir;
            if (Random.value < chanceToChangeDirection)
            {
                walker.dir = RandomDirection();
            }
            walker.dir = RandomDirection();

            // Avoid borders
            walker.pos.x = Mathf.Clamp(walker.pos.x, 1, maxWidth - 2);
            walker.pos.y = Mathf.Clamp(walker.pos.y, 1, maxHeight - 2);
        }

        // Find: leftmost, topmost, bottommost
        int leftmostIndex   = maxWidth;
        int bottommostIndex = maxHeight;
        int topmostIndex    = 0;

        for (int x = 0; x < maxWidth; x++)
        {
            for (int y = 0; y < maxHeight; y++)
            {
                if (grid[x, y])
                {
                    if (x < leftmostIndex)
                    {
                        leftmostIndex = x;
                    }
                    if (y < bottommostIndex)
                    {
                        bottommostIndex = y;
                    }
                    if (y > topmostIndex)
                    {
                        topmostIndex = y;
                    }
                }
            }
        }

        // Find: top left, bottom right
        Vector2 topLeftCell = new Vector2(maxWidth, topmostIndex);
        Vector2 bottomRight = new Vector2(0, bottommostIndex);

        for (int x = 0; x < maxWidth; x++)
        {
            for (int y = 0; y < maxHeight; y++)
            {
                if (grid[x, y] && y == topmostIndex)
                {
                    if (x < topLeftCell.x)
                    {
                        topLeftCell.x = x;
                    }
                }
                if (grid[x, y] && y == bottommostIndex)
                {
                    if (x > bottomRight.x)
                    {
                        bottomRight.x = x;
                    }
                }
            }
        }

        // Instantiate prefabs
        for (int x = 0; x < maxWidth; x++)
        {
            for (int y = 0; y < maxHeight; y++)
            {
                if (grid[x, y])
                {
                    // Find the position and instantiate
                    int        posX     = (x - maxWidth / 2) * (cellWidth + corridorLength);
                    int        posY     = (y - maxHeight / 2) * (cellHeight + corridorLength);
                    Vector3    p        = new Vector3(posX, posY);
                    GameObject thisCell = Instantiate(cellPrefab, p, Quaternion.identity, transform);

                    // Find cells position
                    int     cellX   = x - maxWidth / 2 + Mathf.Abs(leftmostIndex - maxWidth);
                    int     cellY   = y - maxHeight / 2 + Mathf.Abs(bottommostIndex - maxHeight);
                    Vector2 cellPos = new Vector2(cellX, cellY);

                    // If it has a cell to its right or bottom enable these corridors
                    bool[] c = new bool[2];
                    c[0] = grid[x + 1, y];
                    c[1] = grid[x, y - 1];

                    // Check neighbouring cells to remove walls
                    bool[] w = new bool[4];
                    w[0] = !grid[x, y + 1];
                    w[1] = !grid[x + 1, y];
                    w[2] = !grid[x, y - 1];
                    w[3] = !grid[x - 1, y];

                    // Set player spawn and exit as topleft and bottomright
                    bool playerSpawn = x == topLeftCell.x && y == topLeftCell.y;
                    bool exit        = x == bottomRight.x && y == bottomRight.y;

                    // This acts like a constructor
                    thisCell.GetComponent <Cell>().Setup(cellPos, c, w, playerSpawn, exit);
                }
            }
        }
    }