コード例 #1
0
    // Spawn skeletons in random locations along the world map in appropriate locations
    // A location is appropriate if it has no wall-tiles around it i.e it has eight neighbours
    void spawnSkeletons(bool[,] worldMap)
    {
        // init empty skelMap
        bool[,] skelMap = new bool[width, height];
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                skelMap[x, y] = false;
            }
        }


        int skelsPlaced = 0;

        while (skelsPlaced < skeletons)
        {
            int x = rand.getRandomRanged(width);
            int y = rand.getRandomRanged(height);
            if (countAliveNeighbours(worldMap, x, y) == 8)
            {
                skelMap[x, y] = true;
                skelsPlaced++;
            }
        }

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                if (skelMap[x, y] == true)
                {
                    GameObject t = Instantiate(skeleton, new Vector3(x, y, 0), Quaternion.identity);
                    t.transform.parent = skeletonsHolderInstance.transform;
                }
            }
        }
    }