This commit is contained in:
2026-03-31 18:58:25 +07:00
108 changed files with 1122 additions and 2281 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e6a10948eca4f3f4eaeda0611c778875
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Crawler : Maze
{
public override void Generate()
{
//for (int i = 0; i < 2; i++)
// CrawlV();
//for(int i = 0; i < 3; i++)
// CrawlH();
}
void CrawlV()
{
bool done = false;
int x = Random.Range(1,width-1);
int z = 1;
while (!done)
{
map[x, z] = 0;
if (Random.Range(0, 100) < 50)
x += Random.Range(-1, 2);
else
z += Random.Range(0, 2);
done |= (x < 1 || x >= width-1 || z < 1 || z >= depth-1);
}
}
void CrawlH()
{
bool done = false;
int x = 1;
int z = Random.Range(1,depth-1);
while (!done)
{
map[x, z] = 0;
if (Random.Range(0, 100) < 50)
x += Random.Range(0, 2);
else
z += Random.Range(-1, 2);
done |= (x < 1 || x >= width-1 || z < 1 || z >= depth-1);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7b5776943e6d841879f829c725bf4e6b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Extensions
{
private static System.Random rng = new System.Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7d9791b1b03c14f16a245b2d4577c5f9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,99 @@
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<MapLocation> directions = new List<MapLocation>() {
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;
// 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;
}
}
}
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);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1039d646c358a4bd5ac697b0446a2f7e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Prims : Maze
{
public override void Generate()
{
int x = 2;
int z = 2;
map[x, z] = 0;
List<MapLocation> walls = new List<MapLocation>();
walls.Add(new MapLocation(x + 1, z));
walls.Add(new MapLocation(x - 1, z));
walls.Add(new MapLocation(x, z + 1));
walls.Add(new MapLocation(x, z - 1));
int countloops = 0;
while (walls.Count > 0 && countloops < 5000)
{
int rwall = Random.Range(0, walls.Count);
x = walls[rwall].x;
z = walls[rwall].z;
walls.RemoveAt(rwall);
if (CountSquareNeighbours(x, z) == 1)
{
map[x, z] = 0;
walls.Add(new MapLocation(x + 1, z));
walls.Add(new MapLocation(x - 1, z));
walls.Add(new MapLocation(x, z + 1));
walls.Add(new MapLocation(x, z - 1));
}
countloops++;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f12a5f6746a454e08a295f64a34f5dcf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,25 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Recursive : Maze
{
public override void Generate()
{
Generate(5, 5);
}
void Generate(int x, int z)
{
if (CountSquareNeighbours(x, z) >= 2) return;
map[x, z] = 0;
directions.Shuffle();
Generate(x + directions[0].x, z + directions[0].z);
Generate(x + directions[1].x, z + directions[1].z);
Generate(x + directions[2].x, z + directions[2].z);
Generate(x + directions[3].x, z + directions[3].z);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 33bbdb95ccc4b4577a62495732a02d3e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,108 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Wilsons : Maze
{
List<MapLocation> notUsed = new List<MapLocation>();
public override void Generate()
{
//create a starting cell
int x = Random.Range(2, width - 1);
int z = Random.Range(2, depth - 1);
map[x, z] = 2;
while(GetAvailableCells() > 1)
RandomWalk();
}
int CountSquareMazeNeighbours(int x, int z)
{
int count = 0;
for (int d = 0; d < directions.Count; d++)
{
int nx = x + directions[d].x;
int nz = z + directions[d].z;
if (map[nx, nz] == 2)
{
count++;
}
}
return count;
}
int GetAvailableCells()
{
notUsed.Clear();
for (int z = 1; z < depth - 1; z++)
for (int x = 1; x < width - 1; x++)
{
if (CountSquareMazeNeighbours(x, z) == 0)
{
notUsed.Add(new MapLocation(x, z));
}
}
return notUsed.Count;
}
void RandomWalk()
{
List<MapLocation> inWalk = new List<MapLocation>();
int cx;
int cz;
int rstartIndex = Random.Range(0, notUsed.Count);
cx = notUsed[rstartIndex].x;
cz = notUsed[rstartIndex].z;
inWalk.Add(new MapLocation(cx, cz));
int loop = 0;
bool validPath = false;
while (cx > 0 && cx < width - 1 && cz > 0 && cz < depth - 1 && loop < 5000 && !validPath)
{
map[cx, cz] = 0;
if (CountSquareMazeNeighbours(cx, cz) > 1)
break;
int rd = Random.Range(0, directions.Count);
int nx = cx + directions[rd].x;
int nz = cz + directions[rd].z;
if (CountSquareNeighbours(nx, nz) < 2)
{
cx = nx;
cz = nz;
inWalk.Add(new MapLocation(cx, cz));
}
validPath = CountSquareMazeNeighbours(cx, cz) == 1;
loop++;
}
if (validPath)
{
map[cx, cz] = 0;
Debug.Log("PathFound");
foreach (MapLocation m in inWalk)
{
map[m.x, m.z] = 2;
}
inWalk.Clear();
}
else
{
foreach (MapLocation m in inWalk)
map[m.x, m.z] = 1;
inWalk.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 502e6aa24e7de43dfb6d20ecd0745176
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: