so i followed this tutorial on making procedurally generated 2D islands and i followed a tutorial and modified it from sebastian lague. heres the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using System;
public class MapGenerator : MonoBehaviour
{
public RuleTile[] tiles;
public GameObject player;
Tilemap tileMap;
BoxCollider2D bounds;
public int width;
public int height;
public string seed;
public bool useRandomSeed;
[Range(0, 100)]
public int randomFillPercent;
public int[,] map;
// Start is called before the first frame update
void Start()
{
tileMap = GetComponentInChildren<Tilemap>();
bounds = GetComponent<BoxCollider2D>();
GenerateMap();
transform.position = new Vector2(-(width / 2), -(height / 2));
bounds.size = new Vector2(width, height);
bounds.offset = new Vector2((width / 2) + .5f, (height / 2) + .5f);
}
// Update is called once per frame
void Update()
{
if(map != null)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if(map[x,y] == 1)
{
tileMap.SetTile(new Vector3Int(x, y, 0), tiles[0]);
}
else
{
tileMap.SetTile(new Vector3Int(x, y, 0), tiles[1]);
}
}
}
}
}
void GenerateMap()
{
map = new int[width, height];
RandomFillMap();
for (int i = 0; i < 5; i++)
{
SmoothMap();
}
}
void RandomFillMap()
{
if (useRandomSeed)
{
seed = DateTime.Now.Ticks.ToString();
}
System.Random pseudoRandom = new System.Random(seed.GetHashCode());
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if(x == 0 || x == width - 1 || y == 0 || y == height - 1)
{
map[x, y] = 1;
}
else
{
map[x, y] = (pseudoRandom.Next(0, 100) < randomFillPercent) ? 1 : 0;
}
}
}
}
void SmoothMap()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int neighborWallTiles = GetSurroundingWallCount(x, y);
if (neighborWallTiles > 4)
map[x, y] = 1;
else if(neighborWallTiles < 4)
{
map[x, y] = 0;
}
}
}
}
int GetSurroundingWallCount(int gridX, int gridY)
{
int wallCount = 0;
for (int neighborX = gridX - 1; neighborX <= gridX + 1; neighborX++)
{
for (int neighborY = gridY - 1; neighborY <= gridY + 1; neighborY++)
{
if (neighborX >= 0 && neighborX < width && neighborY >= 0 && neighborY < height)
{
if (neighborX != gridX || neighborY != gridY)
{
wallCount += map[neighborX, neighborY];
}
}
else
{
wallCount++;
}
}
}
return wallCount;
}
}
its very close to sebastian lagues but i just implemented it to use tiles instead of gizmos. does anyone know how id add say more levels (sand between water and grass, stone above grass), and also how would i add items to those areas such and materials in the ground or bushes and plants in the ground? any help would be appreciated!