using System.Collections; using System.Collections.Generic; using UnityEngine; public class MapLocation { public int x; public int z; public MapLocation(int _x, int _z) { x = _x; z = _z; } } public class Maze : MonoBehaviour { public List directions = new List() { new MapLocation(1,0), new MapLocation(0,1), new MapLocation(-1,0), new MapLocation(0,-1) }; public int width = 30; //x length public int depth = 30; //z length public byte[,] map; public int scale = 6; public Transform _mapParentObjet; // Start is called before the first frame update void Start() { InitialiseMap(); Generate(); DrawMap(); } void InitialiseMap() { map = new byte[width,depth]; for (int z = 0; z < depth; z++) for (int x = 0; x < width; x++) { map[x, z] = 1; //1 = wall 0 = corridor } } public virtual void Generate() { for (int z = 0; z < depth; z++) for (int x = 0; x < width; x++) { if(Random.Range(0,100) < 50) map[x, z] = 0; //1 = wall 0 = corridor } } void DrawMap() { for (int z = 0; z < depth; z++) for (int x = 0; x < width; x++) { if (map[x, z] == 1) { Vector3 pos = new Vector3(x * scale, 0, z * scale); GameObject wall = GameObject.CreatePrimitive(PrimitiveType.Cube); wall.transform.localScale = new Vector3(scale, scale, scale); wall.transform.position = pos; wall.transform.SetParent(_mapParentObjet.transform); } } } public int CountSquareNeighbours(int x, int z) { int count = 0; if (x <= 0 || x >= width - 1 || z <= 0 || z >= depth - 1) return 5; if (map[x - 1, z] == 0) count++; if (map[x + 1, z] == 0) count++; if (map[x, z + 1] == 0) count++; if (map[x, z - 1] == 0) count++; return count; } public int CountDiagonalNeighbours(int x, int z) { int count = 0; if (x <= 0 || x >= width - 1 || z <= 0 || z >= depth - 1) return 5; if (map[x - 1, z - 1] == 0) count++; if (map[x + 1, z + 1] == 0) count++; if (map[x - 1, z + 1] == 0) count++; if (map[x + 1, z - 1] == 0) count++; return count; } public int CountAllNeighbours(int x, int z) { return CountSquareNeighbours(x,z) + CountDiagonalNeighbours(x,z); } }