MINING QUEST: ‘Food For Thought’ - Solutions

Quest: Mining Quest
Challenge: Food For Thought

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

Hi there,

I started on this quest because I wanted to see if I can handle a two diamond quest and dabble with procedural generation.

It took me a while to see how everything works. On my first attempts, the enemies were poking holes in the map. So the rocks tiles need to be layered over the ground tiles. The other option would probably to change the attack method, but that was not the challenge.

I changed the InstantiateFloorTile method to take in a prefab and array, so I could use it for different cases.

So here is the solution for quest 1/2:

private void GenerateFloor()
    {
        for (int x = 0; x < xSize; x++)
        {
            for (int y = 0; y < ySize; y++)
            {
                // Put down ground tiles everywhere
                InstantiateFloorTile(x, y, groundTilePrefab, groundSprites);

                // Challenge 1: Put rocks on some of them
                if (Random.Range(0f, 100f) < percentRocks)
                {
                    InstantiateFloorTile(x, y, rockTilePrefab, rockSprites);
                } 
                else 
                {
                    // Challenge 2: Put down food on some empty tiles
                    if (Random.Range(0f, 100f) < percentFoodOnEmpty)
                    {
                        InstantiateFloorTile(x, y, foodPrefab, foodSprites);
                    }
                }
            }
        }
    }

I’m going with 50% rocks and 10% food on empty tiles.

1 Like

Here’s what I have so far. Lots of magic numbers that need to be assigned to variables, I think.

private void GenerateFloor()
    {
        for (int x = 0; x < 20; x++)
        {
            for (int y = 0; y < 20; y++)
            {
                InstantiateFloorTile(x, y);
                int randomNumber = Random.Range(0, 100);
                if (randomNumber <= 50)
                {
                    InstantiateRocks(x, y);
                }
                else if (randomNumber >= 90)
                {
                    InstantiateFood(x, y);
                }
                else if (randomNumber >= 85 && randomNumber <= 88)
                {
                    SpawnEnemies(x, y);
                }
            }
        }
    }

@mystic_mill - u may find you end up (often?) with no enemies = no danger and the very off chance of lots/all enemies…

I’m usually averaging around 6 or so enemies. Tbh I wrote this code a while back and haven’t tested it in a while. It’s basically just testing each floor tile to see whether an enemy will spawn there (hence the low percent chance). The biggest problem so far is that the enemies mostly spawn in the left lower quadrant so I need to figure that out. :slight_smile:

1 Like

NM, make that 4 enemies on average.