Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Created April 18, 2020 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProfAndreaPollini/2b67da2133e421a201ee6dcf29d5623a to your computer and use it in GitHub Desktop.
Save ProfAndreaPollini/2b67da2133e421a201ee6dcf29d5623a to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using UnityEngine.Tilemaps;
public class Minimap : MonoBehaviour
{
enum CellKind { Empty, Room,Wall,Door };
public int mapSize=20;
public Tile roomTile;
public Tile wallTile;
public Tile emptyTile;
public Tile doorTile;
private CellKind[,] Map;
private Tilemap tileMap;
struct MapWalker
{
public Vector2Int pos;
public Vector2Int dir;
public float rotationChance;
public MapWalker(Vector2Int pos,
Vector2Int dir)
{
this.pos = pos;
this.dir = dir;
rotationChance = Random.value;
}
}
List<MapWalker> walkers = new List<MapWalker>();
// Start is called before the first frame update
void Start()
{
GameObject gridPanel = GameObject.Find("GridPanel");
tileMap = gridPanel.GetComponentInChildren<Tilemap>();
Map = new CellKind[mapSize,mapSize];
for (int col = 0; col < Map.GetUpperBound(0); col++)
for (int row = 0; row < Map.GetUpperBound(1); row++)
Map[col, row] = CellKind.Empty;
GenerateMap();
GenerateWalls();
PlaceDoors();
for (int col = 0; col < Map.GetUpperBound(0); col++)
for (int row = 0; row < Map.GetUpperBound(1); row++)
{
if (Map[col, row] == CellKind.Room)
{
tileMap.SetTile(new Vector3Int(col, row, 0), roomTile);
} else if (Map[col, row] == CellKind.Wall)
{
tileMap.SetTile(new Vector3Int(col, row, 0), wallTile);
}
else if (Map[col, row] == CellKind.Empty)
{
tileMap.SetTile(new Vector3Int(col, row, 0), emptyTile);
}
else if (Map[col, row] == CellKind.Door)
{
tileMap.SetTile(new Vector3Int(col, row, 0), doorTile);
}
}
}
private bool CheckKind(int col, int row, CellKind kind)
{
return Map[col, row] == kind;
}
private void PlaceDoors()
{
for (int col = 0; col < Map.GetUpperBound(0); col++)
for (int row = 0; row < Map.GetUpperBound(1); row++)
{
if (CheckKind(col, row,CellKind.Room))
{
if (CheckKind(col-1, row,CellKind.Wall) && CheckKind(col+1, row, CellKind.Wall) &&
CheckKind(col, row-1, CellKind.Room) && CheckKind(col, row+1, CellKind.Room))
{
Map[col, row] = CellKind.Door;
}
if (CheckKind(col - 1, row, CellKind.Room) && CheckKind(col + 1, row, CellKind.Room) &&
CheckKind(col, row - 1, CellKind.Wall) && CheckKind(col, row + 1, CellKind.Wall))
{
Map[col, row] = CellKind.Door;
}
}
}
}
private void GenerateWalls()
{
for (int col = 1; col < Map.GetUpperBound(0)-1; col++)
for (int row = Map.GetUpperBound(1) - 1; row >0 ; row--)
{
if (Map[col , row] == CellKind.Empty && Map[col+1, row] == CellKind.Room)
{
Map[col, row] = CellKind.Wall;
} else if (Map[col, row] == CellKind.Empty && Map[col, row-1] == CellKind.Room)
{
Map[col, row] = CellKind.Wall;
} else
if (Map[col, row] == CellKind.Empty && Map[col, row + 1] == CellKind.Room)
{
Map[col, row] = CellKind.Wall;
} else
if (Map[col, row] == CellKind.Empty && Map[col-1, row ] == CellKind.Room)
{
Map[col, row] = CellKind.Wall;
}
}
}
private float RoomDensity()
{
var countRooms = 0;
for (int col = 0; col < Map.GetUpperBound(0) ; col++)
for (int row = 0; row < Map.GetUpperBound(1) ; row++)
{
if (Map[col, row] == CellKind.Room)
countRooms++;
}
return countRooms / (Map.Length);
}
private void GenerateMap()
{
var pos = new Vector2Int(Map.GetUpperBound(0) / 2, Map.GetUpperBound(1) / 2);
var dir = GetRandomDir();
walkers.AddRange(new MapWalker[] { new MapWalker(pos, dir),
new MapWalker(pos, dir),
new MapWalker(pos, dir),
new MapWalker(pos, dir)});
int steps = 0;
while(RoomDensity() < 0.05f && steps++ < 150)
{
for (var w = 0; w < walkers.Count; w++)
{
var walker = walkers[w];
//tileMap.SetTile(new Vector3Int(walker.pos.x, walker.pos.y, 0), roomTile);
Map[walker.pos.x, walker.pos.y] = CellKind.Room;
var newPos = walker.pos + walker.dir;
var newDir = walker.dir;
if (Random.value > walker.rotationChance)
{
newDir = GetRandomDir();
}
newPos.x = Mathf.Clamp(newPos.x,2,mapSize-3);
newPos.y = Mathf.Clamp(newPos.y, 2, mapSize-3);
walkers[w] = new MapWalker(newPos, newDir);
}
if (Random.value > 0.7)
{
var idxToKill = Mathf.FloorToInt(Random.value * walkers.Count);
var walker = walkers[idxToKill];
walkers[idxToKill] = new MapWalker(walker.pos, GetRandomDir());
Debug.LogFormat($"Kill {idxToKill}");
}
}
}
private Vector2Int GetRandomDir()
{
float rnd = Mathf.FloorToInt(Random.value*3.99f);
switch(rnd)
{
case 0:
return new Vector2Int(0, -1);
case 1:
return new Vector2Int(0, 1);
case 2:
return new Vector2Int(-1, 0);
default:
return new Vector2Int(1, 0);
}
}
// Update is called once per frame
void Update()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment