From 89fa84b1e9c1ec76b02714fdfcaa374b7c0026e3 Mon Sep 17 00:00:00 2001 From: Laszlo Kocsis Date: Thu, 25 Jul 2024 15:13:58 +0200 Subject: [PATCH 1/2] feat(bsp): added skeleton file --- Assets/Src/Maze/BSP.meta | 8 +++ Assets/Src/Maze/BSP/BSPGenerator.cs | 68 ++++++++++++++++++++++++ Assets/Src/Maze/BSP/BSPGenerator.cs.meta | 11 ++++ 3 files changed, 87 insertions(+) create mode 100644 Assets/Src/Maze/BSP.meta create mode 100644 Assets/Src/Maze/BSP/BSPGenerator.cs create mode 100644 Assets/Src/Maze/BSP/BSPGenerator.cs.meta diff --git a/Assets/Src/Maze/BSP.meta b/Assets/Src/Maze/BSP.meta new file mode 100644 index 0000000..50597ea --- /dev/null +++ b/Assets/Src/Maze/BSP.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: af9e9bcb93baf4c74acd2bb27de886b7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Src/Maze/BSP/BSPGenerator.cs b/Assets/Src/Maze/BSP/BSPGenerator.cs new file mode 100644 index 0000000..7f85434 --- /dev/null +++ b/Assets/Src/Maze/BSP/BSPGenerator.cs @@ -0,0 +1,68 @@ +using UnityEngine; + +public class BSPGenerator : Generator +{ + + private bool isCheeseArea(int x, int y) + { + return x > 90 && y > 90; + } + + private bool isSpawnArea(int x, int y) + { + return x < 10 && y < 10; + } + + private bool isEdge(int x, int y, int size) + { + return x == 0 || y == 0 || x == size - 1 || y == size - 1; + } + + public override Vector2Int GenerateCheese(Maze maze, Vector3[] playerPositions, Vector2Int? currentCheese) + { + return new Vector2Int(95, 95); + } + + public override Maze GenerateMaze(int size) + { + var maze = new Maze(size); + // Fill the maze with walls + for (int x = 0; x < size; x++) + { + for (int y = 0; y < size; y++) + { + maze.Set(x, y, true); + } + } + + // BSP algorithm + + var x0 = 50; + + + + // End of BSP algorithm + + + // DO NOT CHANGE + // Set walls and free spawn and cheese areas + for (int x = 0; x < size; x++) + { + for (int y = 0; y < size; y++) + { + if (isEdge(x, y, size)) + { + maze.Set(x, y, true); + continue; + } + if (isSpawnArea(x, y) || isCheeseArea(x, y)) + { + maze.Set(x, y, false); + continue; + } + } + } + + return maze; + } +} diff --git a/Assets/Src/Maze/BSP/BSPGenerator.cs.meta b/Assets/Src/Maze/BSP/BSPGenerator.cs.meta new file mode 100644 index 0000000..483a000 --- /dev/null +++ b/Assets/Src/Maze/BSP/BSPGenerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 44c0d6ba59d584c7bbe7540a7fa3f988 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From c2b190e3b645b40ff121d0f67e1895fc2d8bccb8 Mon Sep 17 00:00:00 2001 From: Imre Racz Date: Thu, 25 Jul 2024 15:38:52 +0200 Subject: [PATCH 2/2] feat(bsp): added basic particle slicing --- Assets/Src/Maze/BSP/BSPGenerator.cs | 60 +++++++++++++++++++++++----- Assets/Src/Maze/BSP/Particle.cs | 15 +++++++ Assets/Src/Maze/BSP/Particle.cs.meta | 11 +++++ 3 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 Assets/Src/Maze/BSP/Particle.cs create mode 100644 Assets/Src/Maze/BSP/Particle.cs.meta diff --git a/Assets/Src/Maze/BSP/BSPGenerator.cs b/Assets/Src/Maze/BSP/BSPGenerator.cs index 7f85434..d0d8952 100644 --- a/Assets/Src/Maze/BSP/BSPGenerator.cs +++ b/Assets/Src/Maze/BSP/BSPGenerator.cs @@ -1,4 +1,7 @@ using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using System; public class BSPGenerator : Generator { @@ -23,21 +26,60 @@ public override Vector2Int GenerateCheese(Maze maze, Vector3[] playerPositions, return new Vector2Int(95, 95); } - public override Maze GenerateMaze(int size) - { - var maze = new Maze(size); - // Fill the maze with walls - for (int x = 0; x < size; x++) - { - for (int y = 0; y < size; y++) + private List BreakDown(List breakdown) { + List breakdownList = new List(); + foreach (Particle particle in breakdown) { + if (Math.Abs(particle.x0 - particle.x1) < 4) { + breakdownList.Add(particle); + continue; + }; + int x = UnityEngine.Random.Range(particle.x0, particle.x1); + breakdownList.Add(new Particle(particle.x0, x, particle.y0, particle.y1)); + breakdownList.Add(new Particle(x, particle.x1, particle.y0, particle.y1)); + } + + List breakdownListFinal = new List(); + foreach (Particle particle in breakdownList) { + if (Math.Abs(particle.y0 - particle.y1) < 4) { + breakdownListFinal.Add(particle); + continue; + } + int y = UnityEngine.Random.Range(particle.y0, particle.y1); + breakdownListFinal.Add(new Particle(particle.x0, particle.x1, particle.y0, y)); + breakdownListFinal.Add(new Particle(particle.x0, particle.x1, y, particle.y1)); + } + return breakdownListFinal; + } + + private void SliceMaze(Maze maze) { + var particles = new List() { + new Particle(0, maze.Size, 0, maze.Size) + }; + + while (particles.Count < 1000) { + particles = BreakDown(particles); + } + + for(int i = 0; i < particles.Count; i++) { + if (i%2 == 0) continue; + var particle = particles[i]; + for (int x = particle.x0; x < particle.x1; x++) { - maze.Set(x, y, true); + for (int y = particle.y0; y < particle.y1; y++) + { + maze.Set(x, y, true); + } } } + } + + public override Maze GenerateMaze(int size) + { + var maze = new Maze(size); // BSP algorithm - var x0 = 50; + SliceMaze(maze); diff --git a/Assets/Src/Maze/BSP/Particle.cs b/Assets/Src/Maze/BSP/Particle.cs new file mode 100644 index 0000000..f2a8d8b --- /dev/null +++ b/Assets/Src/Maze/BSP/Particle.cs @@ -0,0 +1,15 @@ +class Particle +{ + public int x0; + public int x1; + public int y0; + public int y1; + + public Particle(int x0, int x1, int y0, int y1) + { + this.x0 = x0; + this.x1 = x1; + this.y0 = y0; + this.y1 = y1; + } +} diff --git a/Assets/Src/Maze/BSP/Particle.cs.meta b/Assets/Src/Maze/BSP/Particle.cs.meta new file mode 100644 index 0000000..095605c --- /dev/null +++ b/Assets/Src/Maze/BSP/Particle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6d239d635b7654189bd692265397075e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: