Implement your own algorithm

TileWorldCreator allows you to implement your own map algorithm. This tutorial guides you step by step to implement a very simple noise algorithm.

Basically we only have to create a multidimensional bool array which we will modify and pass to TileWorldCreator. All needed properties can be implemented in the TileWorldCreator inspector as well to access them easily.

Let's start with a new C# script. Name it

Noise.cs

Next we have to use the Namespace TileWorld add it to the new script.

using TileWorld;

To make sure TileWorldCreator finds our new algorithm we have to inherit from TileWorldAlgorithms and implement the IAlgorithms interface.

public class Noise : TileWorldAlgorithms, IAlgorithms
{

}

Implementing the IAlogrithms gives us a new bool[,] Generate return method, this is where we return the modified bool array, optionally we can also return a start and end position. Those will be used by the Object scatter.

public class Noise : TileWorldAlgorithms, IAlgorithms
{
    public bool[,] Generate (TileWorldConfiguration _config, out _startPosition, out _endPosition )
    {

    }
}

Create a new multidim. bool array and a width and height integer.

bool[,] returnMap;
int width;
int height;

now we create our bool array based on the width and height of the map. We can access all TileWorldCreator properties by using the disposed _config property in the Generate return method.

public class 04_Noise : TileWorldAlgorithms, IAlgorithms
{

    bool[,] returnMap;
    int width;
    int height;

    // implemented return method from iAlogrithm
    public bool[,] Generate (TileWorldConfiguration _config, out _startPosition, out _endPosition)
    {
        // assign width and height
        width = _config.global.width;
        height = _config.global.height;

        returnMap = new bool[width, height];



        return returnMap;
    }

}

Let's create the noise algorithm. There's one important thing to remember when generating a map. After the map generation TileWorldCreator does an optimization pass which removes every single tile cell. This is because of the fact that TileWorldCreator only needs 5 tiles. Therefore the map has to be optimized in some cases. That means that the minimum cell size is 2x2 tiles. if it would be smaller the cell would be cleared by the optimization pass. So let's add our simple noise algorithm:

bool[,] GenerateNoise(bool[,] _map)
{
    for (int y = 0; y < height; y ++)
    {
        for (int x = 0; x < width; x ++)
        {
            var _rnd = Random.Range(0f, 1f);
            if (_rnd < 0.5f)
            {
                for (int a = 0; a < 2; a ++)
                {
                    for (int b = 0; b < 2; b ++)
                    {
                        if (x + a < width && y + b < height)
                        {
                            _map[x + a, y + b] = true;
                        }
                    }
                }
            }
        }
    }

    // return modified map
    return _map;
}

Now let's put everything together

using TileWorld;

public class 04_Noise : TileWorldAlgorithms, IAlgorithms
{

    bool[,] returnMap;
    int width;
    int height;

    // implemented return method from iAlogrithm
    public bool[,] Generate (TileWorldConfiguration _config, out Vector3 _startPosition, out Vector3 _endPosition )
    {
        // assign width and height
        width = _config.global.width;
        height = _config.global.height;

        returnMap = new bool[width, height];

        // generate the map with a random noise
        returnMap = GenerateNoise(returnMap);

        // return zero start and end position
        _startPosition = Vector3.zero;
        _endPosition = Vector3.zero;

        // return the map to TileWorldCreator
        return returnMap;
    }


    bool[,] GenerateNoise(bool[,] _map)
    {
        for (int y = 0; y < height; y ++)
        {
            for (int x = 0; x < width; x ++)
            {
                var _rnd = Random.Range(0f, 1f);
                if (_rnd < 0.5f)
                {
                    for (int a = 0; a < 2; a ++)
                    {
                        for (int b = 0; b < 2; b ++)
                        {
                            if (x + a < width || y + b < height)
                            {
                                _map[x + a, y + b] = true;
                            }
                        }
                    }
                }
            }
        }

        // return modified map
        return _map;
    }

}

Save your script, after the compilation you'll find your new algorithm in the TileWorldCreator settings tab, in the algorithm dropdown list.