Skip to content

Instantly share code, notes, and snippets.

@orblazer
Created May 22, 2018 00:48
Show Gist options
  • Save orblazer/536a78fb18243f5219e8da8523d3ecee to your computer and use it in GitHub Desktop.
Save orblazer/536a78fb18243f5219e8da8523d3ecee to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.Tilemaps;
using System.Collections;
using System.Collections.Generic;
public enum BlockMaterial { GRASS, SAND, WATER }
public class MapGenerator : MonoBehaviour
{
public Tilemap map;
public Vector2 mapSize = new Vector2(100, 100);
public int seed = -1;
public float detailScale = 1;
public float heightScale = 1;
public Block[] availableBlocks;
// Use this for initialization
void Start()
{
if (seed < 0)
{
seed = Random.Range(0, 9999) + Random.Range(0, 9999) + Random.Range(0, 9999) + Random.Range(0, 9999);
Debug.Log("Generate seed : " + seed);
}
for (int x = 0; x < mapSize.x; x++)
{
for (int y = 0; y < mapSize.y; y++)
{
int height = (int)(Mathf.PerlinNoise((x + seed) / detailScale, (y + seed) / detailScale) * heightScale);
Debug.Log(x + "," + y + " : " + height);
foreach (Block block in availableBlocks)
{
if (height <= block.minHeight)
{
Vector3Int pos = new Vector3Int(x, y, (int)map.transform.position.z);
map.SetTile(pos, block.tile);
}
}
}
}
}
[System.Serializable]
public struct Block
{
public BlockMaterial type;
public int minHeight;
public Tile tile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment