1
0
Fork 0

Format generation code | Move GenerationProcessor

This commit is contained in:
Unknown 2018-04-22 16:14:10 +02:00
parent d305464ee0
commit 308dcc8281
6 changed files with 229 additions and 335 deletions

View file

@ -1,43 +1,38 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class DungeonGenerator { public class DungeonGenerator {
public DungeonGenerator() { public DungeonGenerator() {
} }
public const int TUNNEL_THICKNESS = 4; public const int TUNNEL_THICKNESS = 4;
public GenRoom start; public GenRoom start;
public GenRoom end; public GenRoom end;
public HashSet<GenRoom> rooms; public HashSet<GenRoom> rooms;
public GenRoom path; public GenRoom path;
public void generate() public void generate() {
{
int minRoomSize = 50; int minRoomSize = 50;
rooms = new HashSet<GenRoom>(); rooms = new HashSet<GenRoom>();
for (int i = 0; i < 7 + (int)(UnityEngine.Random.value * 4); i++) for ( int i = 0; i < 7 + ( int ) ( UnityEngine.Random.value * 4 ); i++ ) {
{ GenRoom room = new GenRoom();
GenRoom room = new GenRoom(); room.bounds.width = ( ( 15 + ( int ) ( UnityEngine.Random.value * 20 ) ) / 2 ) * 2;
room.bounds.width = ((15 + (int)(UnityEngine.Random.value * 20)) / 2) * 2; room.bounds.height = ( ( 15 + ( int ) ( UnityEngine.Random.value * 20 ) ) / 2 ) * 2;
room.bounds.height = ((15 + (int)(UnityEngine.Random.value * 20)) / 2) * 2; rooms.Add(room);
rooms.Add(room);
} }
while (true) while ( true ) {
{outest: outest:
foreach (GenRoom r1 in rooms) foreach ( GenRoom r1 in rooms ) {
{ foreach ( GenRoom r2 in rooms ) {
foreach (GenRoom r2 in rooms) if ( r1 == r2 )
{
if (r1 == r2)
continue; continue;
Vector2Int p1 = new Vector2Int(r1.bounds.x + r1.bounds.width / 2, r1.bounds.y + r1.bounds.height / 2); Vector2Int p1 = new Vector2Int(r1.bounds.x + r1.bounds.width / 2, r1.bounds.y + r1.bounds.height / 2);
Vector2Int p2 = new Vector2Int(r2.bounds.x + r2.bounds.width / 2, r2.bounds.y + r2.bounds.height / 2); Vector2Int p2 = new Vector2Int(r2.bounds.x + r2.bounds.width / 2, r2.bounds.y + r2.bounds.height / 2);
if (Math.Pow(Vector2Int.Distance(p1, p2), 2) < 2 * minRoomSize * minRoomSize + 2) if ( Math.Pow(Vector2Int.Distance(p1, p2), 2) < 2 * minRoomSize * minRoomSize + 2 ) {
{ r2.bounds.x += ( int ) ( ( UnityEngine.Random.value - 0.5 ) * 5 );
r2.bounds.x += (int)((UnityEngine.Random.value - 0.5) * 5); r2.bounds.y += ( int ) ( ( UnityEngine.Random.value - 0.5 ) * 2.5 );
r2.bounds.y += (int)((UnityEngine.Random.value - 0.5) * 2.5);
goto outest; goto outest;
} }
} }
@ -45,68 +40,61 @@ using System.Collections.Generic;
break; break;
} }
HashSet<GenVertex> Q = new HashSet<GenVertex>(); HashSet<GenVertex> Q = new HashSet<GenVertex>();
foreach (GenRoom r in rooms) foreach ( GenRoom r in rooms )
Q.Add(new GenVertex(r)); Q.Add(new GenVertex(r));
GenVertex root = null; GenVertex root = null;
foreach (GenVertex v in Q) { foreach ( GenVertex v in Q ) {
if (root == null || v.r.bounds.x < root.r.bounds.x) if ( root == null || v.r.bounds.x < root.r.bounds.x )
root = v; root = v;
} }
root.value = 0; root.value = 0;
HashSet<GenEdge> E = new HashSet<GenEdge>(); HashSet<GenEdge> E = new HashSet<GenEdge>();
HashSet<GenEdge> G = new HashSet<GenEdge>(); HashSet<GenEdge> G = new HashSet<GenEdge>();
HashSet<GenVertex> F = new HashSet<GenVertex>(); HashSet<GenVertex> F = new HashSet<GenVertex>();
foreach (GenVertex r1 in Q) foreach ( GenVertex r1 in Q ) {
{ foreach ( GenVertex r2 in Q ) {
foreach (GenVertex r2 in Q) outer:
{ outer: if ( r1 == r2 )
if (r1 == r2) goto outer;
goto outer; foreach ( GenEdge e in E )
foreach (GenEdge e in E) if ( e.r2 == r1 && e.r1 == r2 )
if (e.r2 == r1 && e.r1 == r2) goto outer;
goto outer; E.Add(new GenEdge(r1, r2));
E.Add(new GenEdge(r1, r2));
} }
} }
F.Add(root); F.Add(root);
Q.Remove(root); Q.Remove(root);
while (Q.Count > 0) while ( Q.Count > 0 ) {
{ GenEdge start2 = null;
GenEdge start2 = null; foreach ( GenEdge e in E ) {
foreach (GenEdge e in E) if ( F.Contains(e.r1) ^ F.Contains(e.r2) ) {
{ if ( start2 == null || e.dist < start2.dist ) {
if (F.Contains(e.r1) ^ F.Contains(e.r2)) { start2 = e;
if (start2 == null || e.dist < start2.dist) { }
start2 = e; }
}
}
} }
Q.Remove(start2.r2); Q.Remove(start2.r2);
Q.Remove(start2.r1); Q.Remove(start2.r1);
F.Add(start2.r2); F.Add(start2.r2);
F.Add(start2.r1); F.Add(start2.r1);
E.Remove(start2); E.Remove(start2);
G.Add(start2); G.Add(start2);
if (start2.r1.value < start2.r2.value) if ( start2.r1.value < start2.r2.value ) {
{ start2.r2.value = ( float ) ( start2.r1.value + start2.dist );
start2.r2.value = (float)(start2.r1.value + start2.dist); } else {
} start2.r1.value = ( float ) ( start2.r2.value + start2.dist );
else
{
start2.r1.value = (float)(start2.r2.value + start2.dist);
} }
} }
// G list of edges // G list of edges
// rooms list of rooms // rooms list of rooms
HashSet<GenRoom> rooms2 = new HashSet<GenRoom>(); HashSet<GenRoom> rooms2 = new HashSet<GenRoom>();
foreach (GenEdge ed in G) foreach ( GenEdge ed in G ) {
{
// horizontal // horizontal
float diff1 = ed.r1.r.bounds.y - ed.r2.r.bounds.y - ed.r2.r.bounds.height + TUNNEL_THICKNESS; float diff1 = ed.r1.r.bounds.y - ed.r2.r.bounds.y - ed.r2.r.bounds.height + TUNNEL_THICKNESS;
float diff2 = ed.r2.r.bounds.y - ed.r1.r.bounds.y - ed.r1.r.bounds.height + TUNNEL_THICKNESS; float diff2 = ed.r2.r.bounds.y - ed.r1.r.bounds.y - ed.r1.r.bounds.height + TUNNEL_THICKNESS;
@ -115,141 +103,124 @@ using System.Collections.Generic;
float diff3 = ed.r1.r.bounds.x - ed.r2.r.bounds.x - ed.r2.r.bounds.width + TUNNEL_THICKNESS; float diff3 = ed.r1.r.bounds.x - ed.r2.r.bounds.x - ed.r2.r.bounds.width + TUNNEL_THICKNESS;
float diff4 = ed.r2.r.bounds.x - ed.r1.r.bounds.x - ed.r1.r.bounds.width + TUNNEL_THICKNESS; float diff4 = ed.r2.r.bounds.x - ed.r1.r.bounds.x - ed.r1.r.bounds.width + TUNNEL_THICKNESS;
if (diff1 < 0 && diff2 < 0) if ( diff1 < 0 && diff2 < 0 ) {
{
addStraightHorizontal(rooms2, ed); addStraightHorizontal(rooms2, ed);
} } else if ( diff3 < 0 && diff4 < 0 ) {
else if (diff3 < 0 && diff4 < 0)
{
addStraightVertical(rooms2, ed); addStraightVertical(rooms2, ed);
} } else
else
addCurve(rooms2, ed); addCurve(rooms2, ed);
} }
path = new GenRoom(); path = new GenRoom();
foreach (GenRoom r in rooms2) foreach ( GenRoom r in rooms2 ) {
{ for ( int x1 = r.bounds.x; x1 < r.bounds.x + r.bounds.width; x1++ )
for (int x1 = r.bounds.x; x1 < r.bounds.x + r.bounds.width; x1++) for ( int y1 = r.bounds.y; y1 < r.bounds.y + r.bounds.height; y1++ ) {
for (int y1 = r.bounds.y; y1 < r.bounds.y + r.bounds.height; y1++) path.tiles.Add(new Vector2Int(x1, y1), Room.TileType.GROUND);
{ for ( int x2 = x1 - 1; x2 <= x1 + 1; x2++ )
path.tiles.Add(new Vector2Int(x1, y1), Room.TileType.GROUND); for ( int y2 = y1 - 1; y2 <= y1 + 1; y2++ ) {
for (int x2 = x1 - 1; x2 <= x1 + 1; x2++) if ( !path.tiles.ContainsKey(new Vector2Int(x2, y2)) )
for (int y2 = y1 - 1; y2 <= y1 + 1; y2++) path.tiles.Add(new Vector2Int(x2, y2), Room.TileType.WALL);
{
if (!path.tiles.ContainsKey(new Vector2Int(x2, y2)))
path.tiles.Add(new Vector2Int(x2, y2), Room.TileType.WALL);
} }
} }
foreach (Vector2Int v in r.allDoors()) foreach ( Vector2Int v in r.allDoors() )
r.tiles.Add(v, Room.TileType.DOOR); r.tiles.Add(v, Room.TileType.DOOR);
} }
foreach (GenRoom r in rooms) foreach ( GenRoom r in rooms ) {
{ for ( int x1 = r.bounds.x; x1 < r.bounds.x + r.bounds.width; x1++ )
for (int x1 = r.bounds.x; x1 < r.bounds.x + r.bounds.width; x1++) for ( int y1 = r.bounds.y; y1 < r.bounds.y + r.bounds.height; y1++ ) {
for (int y1 = r.bounds.y; y1 < r.bounds.y + r.bounds.height; y1++) r.tiles.Add(new Vector2Int(x1, y1), Room.TileType.WALL);
{
r.tiles.Add(new Vector2Int(x1, y1), Room.TileType.WALL);
} }
for (int x1 = r.bounds.x + 1; x1 < r.bounds.x + r.bounds.width - 1; x1++) for ( int x1 = r.bounds.x + 1; x1 < r.bounds.x + r.bounds.width - 1; x1++ )
for (int y1 = r.bounds.y + 1; y1 < r.bounds.y + r.bounds.height - 1; y1++) for ( int y1 = r.bounds.y + 1; y1 < r.bounds.y + r.bounds.height - 1; y1++ ) {
{ r.tiles.Add(new Vector2Int(x1, y1), Room.TileType.GROUND);
r.tiles.Add(new Vector2Int(x1, y1), Room.TileType.GROUND);
} }
foreach (Vector2Int v in r.allDoors()) foreach ( Vector2Int v in r.allDoors() )
r.tiles.Add(v, Room.TileType.DOOR); r.tiles.Add(v, Room.TileType.DOOR);
} }
rooms.Add(path); rooms.Add(path);
start = root.r; start = root.r;
end = null; foreach (GenRoom r in rooms) end = null; foreach ( GenRoom r in rooms ) {
{ if ( end == null || r.bounds.x > end.bounds.x )
if (end== null || r.bounds.x > end.bounds.x)
end = r; end = r;
} }
} }
public static void addStraightHorizontal(HashSet<GenRoom> rooms, GenEdge ed) public static void addStraightHorizontal(HashSet<GenRoom> rooms, GenEdge ed) {
{ GenRoom righter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r1.r : ed.r2.r;
GenRoom righter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r1.r : ed.r2.r; GenRoom lefter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r2.r : ed.r1.r;
GenRoom lefter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r2.r : ed.r1.r; GenRoom tunnel = new GenRoom();
GenRoom tunnel = new GenRoom();
int minX = Math.Min(ed.r1.r.bounds.x + ed.r1.r.bounds.width, ed.r2.r.bounds.x + ed.r2.r.bounds.width); int minX = Math.Min(ed.r1.r.bounds.x + ed.r1.r.bounds.width, ed.r2.r.bounds.x + ed.r2.r.bounds.width);
int minY = Math.Max(ed.r1.r.bounds.y, ed.r2.r.bounds.y); int minY = Math.Max(ed.r1.r.bounds.y, ed.r2.r.bounds.y);
int maxX = Math.Max(ed.r1.r.bounds.x, ed.r2.r.bounds.x); int maxX = Math.Max(ed.r1.r.bounds.x, ed.r2.r.bounds.x);
int maxY = Math.Min(ed.r1.r.bounds.y + ed.r1.r.bounds.height, ed.r2.r.bounds.y + ed.r2.r.bounds.height); int maxY = Math.Min(ed.r1.r.bounds.y + ed.r1.r.bounds.height, ed.r2.r.bounds.y + ed.r2.r.bounds.height);
tunnel.bounds.x = minX; tunnel.bounds.x = minX;
tunnel.bounds.y = (minY + maxY) / 2 - TUNNEL_THICKNESS / 2; tunnel.bounds.y = ( minY + maxY ) / 2 - TUNNEL_THICKNESS / 2;
tunnel.bounds.width = (maxX - minX); tunnel.bounds.width = ( maxX - minX );
tunnel.bounds.height = TUNNEL_THICKNESS; tunnel.bounds.height = TUNNEL_THICKNESS;
rooms.Add(tunnel); rooms.Add(tunnel);
for (int i = 0; i < TUNNEL_THICKNESS; i++) for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
{ lefter.doorsRight.Add(new Vector2Int(tunnel.bounds.x - 1, tunnel.bounds.y + i));
lefter.doorsRight.Add(new Vector2Int(tunnel.bounds.x - 1, tunnel.bounds.y + i)); righter.doorsLeft.Add(new Vector2Int(tunnel.bounds.x + tunnel.bounds.width, tunnel.bounds.y + i));
righter.doorsLeft.Add(new Vector2Int(tunnel.bounds.x + tunnel.bounds.width, tunnel.bounds.y + i));
} }
} }
public static void addStraightVertical(HashSet<GenRoom> rooms, GenEdge ed) public static void addStraightVertical(HashSet<GenRoom> rooms, GenEdge ed) {
{ GenRoom higher = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r1.r : ed.r2.r;
GenRoom higher = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r1.r : ed.r2.r; GenRoom lower = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r2.r : ed.r1.r;
GenRoom lower = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r2.r : ed.r1.r; GenRoom tunnel = new GenRoom();
GenRoom tunnel = new GenRoom();
int minX = Math.Max(ed.r1.r.bounds.x, ed.r2.r.bounds.x); int minX = Math.Max(ed.r1.r.bounds.x, ed.r2.r.bounds.x);
int minY = Math.Min(ed.r1.r.bounds.y + ed.r1.r.bounds.height, ed.r2.r.bounds.y + ed.r2.r.bounds.height); int minY = Math.Min(ed.r1.r.bounds.y + ed.r1.r.bounds.height, ed.r2.r.bounds.y + ed.r2.r.bounds.height);
int maxX = Math.Min(ed.r1.r.bounds.x + ed.r1.r.bounds.width, ed.r2.r.bounds.x + ed.r2.r.bounds.width); int maxX = Math.Min(ed.r1.r.bounds.x + ed.r1.r.bounds.width, ed.r2.r.bounds.x + ed.r2.r.bounds.width);
int maxY = Math.Max(ed.r1.r.bounds.y, ed.r2.r.bounds.y); int maxY = Math.Max(ed.r1.r.bounds.y, ed.r2.r.bounds.y);
tunnel.bounds.x = (minX + maxX) / 2 - TUNNEL_THICKNESS / 2; tunnel.bounds.x = ( minX + maxX ) / 2 - TUNNEL_THICKNESS / 2;
tunnel.bounds.y = minY; tunnel.bounds.y = minY;
tunnel.bounds.width = TUNNEL_THICKNESS; tunnel.bounds.width = TUNNEL_THICKNESS;
tunnel.bounds.height = (maxY - minY); tunnel.bounds.height = ( maxY - minY );
rooms.Add(tunnel); rooms.Add(tunnel);
for (int i = 0; i < TUNNEL_THICKNESS; i++) for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
{ lower.doorsUp.Add(new Vector2Int(tunnel.bounds.x + i, tunnel.bounds.y + tunnel.bounds.height));
lower.doorsUp.Add(new Vector2Int(tunnel.bounds.x + i, tunnel.bounds.y + tunnel.bounds.height)); higher.doorsDown.Add(new Vector2Int(tunnel.bounds.x + i, tunnel.bounds.y - 1));
higher.doorsDown.Add(new Vector2Int(tunnel.bounds.x + i, tunnel.bounds.y - 1));
} }
} }
public static void addCurve(HashSet<GenRoom> rooms, GenEdge ed) public static void addCurve(HashSet<GenRoom> rooms, GenEdge ed) {
{ GenRoom higher = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r1.r : ed.r2.r;
GenRoom higher = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r1.r : ed.r2.r; GenRoom lower = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r2.r : ed.r1.r;
GenRoom lower = ed.r1.r.getCenter().y > ed.r2.r.getCenter().y ? ed.r2.r : ed.r1.r; GenRoom righter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r1.r : ed.r2.r;
GenRoom righter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r1.r : ed.r2.r; GenRoom lefter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r2.r : ed.r1.r;
GenRoom lefter = ed.r1.r.getCenter().x > ed.r2.r.getCenter().x ? ed.r2.r : ed.r1.r;
RectInt r = new RectInt(lefter.getCenter().x, lower.getCenter().y, righter.getCenter().x - lefter.getCenter().x, higher.getCenter().y - lower.getCenter().y); RectInt r = new RectInt(lefter.getCenter().x, lower.getCenter().y, righter.getCenter().x - lefter.getCenter().x, higher.getCenter().y - lower.getCenter().y);
GenRoom verticalLefter = new GenRoom(); GenRoom verticalLefter = new GenRoom();
verticalLefter.bounds.x = r.x - TUNNEL_THICKNESS / 2; verticalLefter.bounds.x = r.x - TUNNEL_THICKNESS / 2;
verticalLefter.bounds.y = r.y - TUNNEL_THICKNESS / 2; verticalLefter.bounds.y = r.y - TUNNEL_THICKNESS / 2;
verticalLefter.bounds.width = TUNNEL_THICKNESS; verticalLefter.bounds.width = TUNNEL_THICKNESS;
verticalLefter.bounds.height = r.height + TUNNEL_THICKNESS; verticalLefter.bounds.height = r.height + TUNNEL_THICKNESS;
GenRoom horizontalLower = new GenRoom(); GenRoom horizontalLower = new GenRoom();
horizontalLower.bounds.x = r.x - TUNNEL_THICKNESS / 2; horizontalLower.bounds.x = r.x - TUNNEL_THICKNESS / 2;
horizontalLower.bounds.y = r.y - TUNNEL_THICKNESS / 2; horizontalLower.bounds.y = r.y - TUNNEL_THICKNESS / 2;
horizontalLower.bounds.width = r.width + TUNNEL_THICKNESS; horizontalLower.bounds.width = r.width + TUNNEL_THICKNESS;
horizontalLower.bounds.height = TUNNEL_THICKNESS; horizontalLower.bounds.height = TUNNEL_THICKNESS;
GenRoom verticalRighter = new GenRoom(); GenRoom verticalRighter = new GenRoom();
verticalRighter.bounds.x = r.x + r.width - TUNNEL_THICKNESS / 2; verticalRighter.bounds.x = r.x + r.width - TUNNEL_THICKNESS / 2;
verticalRighter.bounds.y = r.y - TUNNEL_THICKNESS / 2; verticalRighter.bounds.y = r.y - TUNNEL_THICKNESS / 2;
verticalRighter.bounds.width = TUNNEL_THICKNESS; verticalRighter.bounds.width = TUNNEL_THICKNESS;
verticalRighter.bounds.height = r.height + TUNNEL_THICKNESS; verticalRighter.bounds.height = r.height + TUNNEL_THICKNESS;
GenRoom horizontalHigher = new GenRoom(); GenRoom horizontalHigher = new GenRoom();
horizontalHigher.bounds.x = r.x - TUNNEL_THICKNESS / 2; horizontalHigher.bounds.x = r.x - TUNNEL_THICKNESS / 2;
horizontalHigher.bounds.y = r.y + r.height - TUNNEL_THICKNESS / 2; horizontalHigher.bounds.y = r.y + r.height - TUNNEL_THICKNESS / 2;
horizontalHigher.bounds.width = r.width + TUNNEL_THICKNESS; horizontalHigher.bounds.width = r.width + TUNNEL_THICKNESS;
horizontalHigher.bounds.height = TUNNEL_THICKNESS; horizontalHigher.bounds.height = TUNNEL_THICKNESS;
if (lower == lefter) if ( lower == lefter ) {
{
horizontalLower.bounds.x = r.x + lower.bounds.width / 2; horizontalLower.bounds.x = r.x + lower.bounds.width / 2;
horizontalLower.bounds.width = r.width - lower.bounds.width / 2 + TUNNEL_THICKNESS / 2; horizontalLower.bounds.width = r.width - lower.bounds.width / 2 + TUNNEL_THICKNESS / 2;
horizontalHigher.bounds.width = r.width - higher.bounds.width / 2 + TUNNEL_THICKNESS / 2; horizontalHigher.bounds.width = r.width - higher.bounds.width / 2 + TUNNEL_THICKNESS / 2;
@ -258,8 +229,7 @@ using System.Collections.Generic;
verticalLefter.bounds.height = r.height - lower.bounds.height / 2 + TUNNEL_THICKNESS / 2; verticalLefter.bounds.height = r.height - lower.bounds.height / 2 + TUNNEL_THICKNESS / 2;
verticalRighter.bounds.height = r.height - higher.bounds.height / 2 + TUNNEL_THICKNESS / 2; verticalRighter.bounds.height = r.height - higher.bounds.height / 2 + TUNNEL_THICKNESS / 2;
} }
if (lower == righter) if ( lower == righter ) {
{
horizontalHigher.bounds.x = r.x + higher.bounds.width / 2; horizontalHigher.bounds.x = r.x + higher.bounds.width / 2;
horizontalHigher.bounds.width = r.width - higher.bounds.width / 2 + TUNNEL_THICKNESS / 2; horizontalHigher.bounds.width = r.width - higher.bounds.width / 2 + TUNNEL_THICKNESS / 2;
horizontalLower.bounds.width = r.width - lower.bounds.width / 2 + TUNNEL_THICKNESS / 2; horizontalLower.bounds.width = r.width - lower.bounds.width / 2 + TUNNEL_THICKNESS / 2;
@ -269,118 +239,83 @@ using System.Collections.Generic;
verticalLefter.bounds.height = r.height - higher.bounds.height / 2 + TUNNEL_THICKNESS / 2; verticalLefter.bounds.height = r.height - higher.bounds.height / 2 + TUNNEL_THICKNESS / 2;
} }
bool flip = UnityEngine.Random.value > 0.5; bool flip = UnityEngine.Random.value > 0.5;
bool diffX = ed.r2.r.getCenter().x - ed.r1.r.getCenter().x > 0; bool diffX = ed.r2.r.getCenter().x - ed.r1.r.getCenter().x > 0;
bool diffY = ed.r2.r.getCenter().y - ed.r1.r.getCenter().y > 0; bool diffY = ed.r2.r.getCenter().y - ed.r1.r.getCenter().y > 0;
bool addHorizontal1 = false, addHorizontal2 = false, addVertical1 = false, addVertical2 = false; bool addHorizontal1 = false, addHorizontal2 = false, addVertical1 = false, addVertical2 = false;
if (diffX && diffY) if ( diffX && diffY ) {
{ if ( flip ) {
if (flip)
{
addVertical1 = true; addVertical1 = true;
addHorizontal2 = true; addHorizontal2 = true;
} } else {
else
{
addVertical2 = true; addVertical2 = true;
addHorizontal1 = true; addHorizontal1 = true;
} }
} } else if ( diffX && !diffY ) {
else if (diffX && !diffY) if ( flip ) {
{
if (flip)
{
addVertical2 = true; addVertical2 = true;
addHorizontal2 = true; addHorizontal2 = true;
} } else {
else
{
addVertical1 = true; addVertical1 = true;
addHorizontal1 = true; addHorizontal1 = true;
} }
} } else if ( !diffX && diffY ) {
else if (!diffX && diffY) if ( flip ) {
{
if (flip)
{
addVertical1 = true; addVertical1 = true;
addHorizontal1 = true; addHorizontal1 = true;
} } else {
else
{
addVertical2 = true; addVertical2 = true;
addHorizontal2 = true; addHorizontal2 = true;
} }
} } else if ( !diffX && !diffY ) {
else if (!diffX && !diffY) if ( flip ) {
{
if (flip)
{
addVertical2 = true; addVertical2 = true;
addHorizontal1 = true; addHorizontal1 = true;
} } else {
else
{
addVertical1 = true; addVertical1 = true;
addHorizontal2 = true; addHorizontal2 = true;
} }
} }
if (addHorizontal1) if ( addHorizontal1 ) {
{ rooms.Add(horizontalLower);
rooms.Add(horizontalLower); if ( lower == lefter )
if (lower == lefter) for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
for (int i = 0; i < TUNNEL_THICKNESS; i++) lower.doorsRight.Add(new Vector2Int(horizontalLower.bounds.x - 1, horizontalLower.bounds.y + i));
{ } else
lower.doorsRight.Add(new Vector2Int(horizontalLower.bounds.x - 1, horizontalLower.bounds.y + i)); for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
} lower.doorsLeft.Add(new Vector2Int(horizontalLower.bounds.x + horizontalLower.bounds.width, horizontalLower.bounds.y + i));
else
for (int i = 0; i < TUNNEL_THICKNESS; i++)
{
lower.doorsLeft.Add(new Vector2Int(horizontalLower.bounds.x + horizontalLower.bounds.width, horizontalLower.bounds.y + i));
} }
} }
if (addHorizontal2) if ( addHorizontal2 ) {
{ rooms.Add(horizontalHigher);
rooms.Add(horizontalHigher); if ( lower == righter )
if (lower == righter) for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
for (int i = 0; i < TUNNEL_THICKNESS; i++) higher.doorsRight.Add(new Vector2Int(horizontalHigher.bounds.x - 1, horizontalHigher.bounds.y + i));
{ } else
higher.doorsRight.Add(new Vector2Int(horizontalHigher.bounds.x - 1, horizontalHigher.bounds.y + i)); for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
} higher.doorsLeft.Add(new Vector2Int(horizontalHigher.bounds.x + horizontalHigher.bounds.width, horizontalHigher.bounds.y + i));
else
for (int i = 0; i < TUNNEL_THICKNESS; i++)
{
higher.doorsLeft.Add(new Vector2Int(horizontalHigher.bounds.x + horizontalHigher.bounds.width, horizontalHigher.bounds.y + i));
} }
} }
if (addVertical1) if ( addVertical1 ) {
{
rooms.Add(verticalLefter); rooms.Add(verticalLefter);
if (lower == lefter) if ( lower == lefter )
for (int i = 0; i < TUNNEL_THICKNESS; i++) for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
{ lower.doorsDown.Add(new Vector2Int(verticalLefter.bounds.x + i, verticalLefter.bounds.y - 1));
lower.doorsDown.Add(new Vector2Int(verticalLefter.bounds.x + i, verticalLefter.bounds.y - 1)); } else
} for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
else lower.doorsUp.Add(new Vector2Int(verticalLefter.bounds.x + i, verticalLefter.bounds.y + verticalLefter.bounds.height));
for (int i = 0; i < TUNNEL_THICKNESS; i++)
{
lower.doorsUp.Add(new Vector2Int(verticalLefter.bounds.x + i, verticalLefter.bounds.y + verticalLefter.bounds.height));
} }
} }
if (addVertical2) if ( addVertical2 ) {
{ rooms.Add(verticalRighter);
rooms.Add(verticalRighter); if ( lower == righter )
if (lower == righter) for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
for (int i = 0; i < TUNNEL_THICKNESS; i++) higher.doorsDown.Add(new Vector2Int(verticalRighter.bounds.x + i, verticalRighter.bounds.y - 1));
{ } else
higher.doorsDown.Add(new Vector2Int(verticalRighter.bounds.x + i, verticalRighter.bounds.y - 1)); for ( int i = 0; i < TUNNEL_THICKNESS; i++ ) {
} higher.doorsUp.Add(new Vector2Int(verticalRighter.bounds.x + i, verticalRighter.bounds.y + verticalRighter.bounds.height));
else
for (int i = 0; i < TUNNEL_THICKNESS; i++)
{
higher.doorsUp.Add(new Vector2Int(verticalRighter.bounds.x + i, verticalRighter.bounds.y + verticalRighter.bounds.height));
} }
} }
} }
} }

View file

@ -1,13 +1,11 @@
using System; using System;
using UnityEngine; using UnityEngine;
public class GenEdge public class GenEdge {
{
public GenVertex r1, r2; public GenVertex r1, r2;
public double dist; public double dist;
public GenEdge(GenVertex r1, GenVertex r2) public GenEdge(GenVertex r1, GenVertex r2) {
{
this.r1 = r1; this.r1 = r1;
this.r2 = r2; this.r2 = r2;
dist = r1.r.distance(r2.r); dist = r1.r.distance(r2.r);

View file

@ -2,32 +2,28 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class GenRoom public class GenRoom {
{ public RectInt bounds = new RectInt();
public RectInt bounds = new RectInt(); public Dictionary<Vector2Int, Room.TileType> tiles = new Dictionary<Vector2Int, Room.TileType>();
public Dictionary<Vector2Int, Room.TileType> tiles = new Dictionary<Vector2Int, Room.TileType>(); public HashSet<Vector2Int> doorsUp = new HashSet<Vector2Int>();
public HashSet<Vector2Int> doorsUp = new HashSet<Vector2Int>(); public HashSet<Vector2Int> doorsDown = new HashSet<Vector2Int>();
public HashSet<Vector2Int> doorsDown = new HashSet<Vector2Int>(); public HashSet<Vector2Int> doorsLeft = new HashSet<Vector2Int>();
public HashSet<Vector2Int> doorsLeft = new HashSet<Vector2Int>(); public HashSet<Vector2Int> doorsRight = new HashSet<Vector2Int>();
public HashSet<Vector2Int> doorsRight = new HashSet<Vector2Int>();
public float distance(GenRoom r) public float distance(GenRoom r) {
{
return Math.Abs(getCenter().x - r.getCenter().x) + Math.Abs(getCenter().y - r.getCenter().y); return Math.Abs(getCenter().x - r.getCenter().x) + Math.Abs(getCenter().y - r.getCenter().y);
} }
public Vector2Int getCenter() public Vector2Int getCenter() {
{ return new Vector2Int(( int ) bounds.center.x, ( int ) bounds.center.y);
return new Vector2Int((int)bounds.center.x, (int)bounds.center.y);
} }
public HashSet<Vector2Int> allDoors() public HashSet<Vector2Int> allDoors() {
{ HashSet<Vector2Int> ret = new HashSet<Vector2Int>();
HashSet<Vector2Int> ret = new HashSet<Vector2Int>(); ret.UnionWith(doorsUp);
ret.UnionWith(doorsUp); ret.UnionWith(doorsDown);
ret.UnionWith(doorsDown); ret.UnionWith(doorsLeft);
ret.UnionWith(doorsLeft); ret.UnionWith(doorsRight);
ret.UnionWith(doorsRight);
return ret; return ret;
} }
} }

View file

@ -1,15 +1,13 @@
using System; using System;
using UnityEngine; using UnityEngine;
public class GenVertex public class GenVertex {
{
public GenRoom r; public GenRoom r;
public float value; public float value;
public GenVertex(GenRoom r) public GenVertex(GenRoom r) {
{
this.r = r; this.r = r;
value = float.PositiveInfinity; value = float.PositiveInfinity;
} }
} }

View file

@ -3,57 +3,44 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class GenerationProcessor { public class GenerationProcessor {
public enum ExtendedTileType public enum ExtendedTileType {
{
BorderOuter, BorderInner, BorderSingle, Ground, Door, Rock, RockL, RockU, RockR, RockD, RockLU, RockLR, RockLD, RockUR, RockUD, RockRD, RockLURD, RockLUD, RockLUR, RockURD, RockLRD BorderOuter, BorderInner, BorderSingle, Ground, Door, Rock, RockL, RockU, RockR, RockD, RockLU, RockLR, RockLD, RockUR, RockUD, RockRD, RockLURD, RockLUD, RockLUR, RockURD, RockLRD
} }
Dictionary<ExtendedTileType, GameObject> prefabs; Dictionary<ExtendedTileType, GameObject> prefabs;
public GenerationProcessor(Dictionary<ExtendedTileType, GameObject> prefabs) public GenerationProcessor(Dictionary<ExtendedTileType, GameObject> prefabs) {
{
this.prefabs = prefabs; this.prefabs = prefabs;
} }
public GameObject ProcessRoom(Dictionary<Vector2, Room.TileType> d) public GameObject ProcessRoom(Dictionary<Vector2, Room.TileType> d) {
{ GameObject root = new GameObject {
GameObject root = new GameObject
{
name = "Room" name = "Room"
}; };
foreach (Vector2 v in d.Keys) foreach ( Vector2 v in d.Keys ) {
{
bool left = false; bool left = false;
bool top = false; bool top = false;
bool right = false; bool right = false;
bool bottom = false; bool bottom = false;
// left bound // left bound
if(d.ContainsKey(v + new Vector2(-1, 0))) if ( d.ContainsKey(v + new Vector2(-1, 0)) ) {
{ if ( d[v + new Vector2(-1, 0)] == d[v] ) {
if(d[v + new Vector2(-1, 0)] == d[v])
{
left = true; left = true;
} }
} }
// top bound // top bound
if (d.ContainsKey(v + new Vector2(0, 1))) if ( d.ContainsKey(v + new Vector2(0, 1)) ) {
{ if ( d[v + new Vector2(0, 1)] == d[v] ) {
if (d[v + new Vector2(0, 1)] == d[v])
{
top = true; top = true;
} }
} }
// right bound // right bound
if (d.ContainsKey(v + new Vector2(1, 0))) if ( d.ContainsKey(v + new Vector2(1, 0)) ) {
{ if ( d[v + new Vector2(1, 0)] == d[v] ) {
if (d[v + new Vector2(1, 0)] == d[v])
{
right = true; right = true;
} }
} }
// bottom bound // bottom bound
if (d.ContainsKey(v + new Vector2(0, -1))) if ( d.ContainsKey(v + new Vector2(0, -1)) ) {
{ if ( d[v + new Vector2(0, -1)] == d[v] ) {
if (d[v + new Vector2(0, -1)] == d[v])
{
bottom = true; bottom = true;
} }
} }
@ -68,15 +55,12 @@ public class GenerationProcessor {
// //
// ^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~ // ^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~
// --------------------------------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------------------------------
switch (d[v]) switch ( d[v] ) {
{
case Room.TileType.WALL: case Room.TileType.WALL:
type = ExtendedTileType.BorderSingle; type = ExtendedTileType.BorderSingle;
if(top && left && d.ContainsKey(v + new Vector2(-1, -1)) || top && right && d.ContainsKey(v + new Vector2(1, -1)) || right && bottom && d.ContainsKey(v + new Vector2(1, 1)) || left && bottom && d.ContainsKey(v + new Vector2(-1, 1))) if ( top && left && d.ContainsKey(v + new Vector2(-1, -1)) || top && right && d.ContainsKey(v + new Vector2(1, -1)) || right && bottom && d.ContainsKey(v + new Vector2(1, 1)) || left && bottom && d.ContainsKey(v + new Vector2(-1, 1)) ) {
{
type = ExtendedTileType.BorderOuter; type = ExtendedTileType.BorderOuter;
} else if(top && left || top && right || right && bottom || left && bottom) } else if ( top && left || top && right || right && bottom || left && bottom ) {
{
type = ExtendedTileType.BorderInner; type = ExtendedTileType.BorderInner;
} }
break; break;
@ -88,65 +72,50 @@ public class GenerationProcessor {
break; break;
case Room.TileType.ROCK: case Room.TileType.ROCK:
type = ExtendedTileType.Rock; type = ExtendedTileType.Rock;
if (top && !right && !left && !bottom) if ( top && !right && !left && !bottom ) {
{
type = ExtendedTileType.RockU; type = ExtendedTileType.RockU;
} }
if (left && !right && !bottom && !top) if ( left && !right && !bottom && !top ) {
{
type = ExtendedTileType.RockL; type = ExtendedTileType.RockL;
} }
if (right && !bottom && !left && !top) if ( right && !bottom && !left && !top ) {
{
type = ExtendedTileType.RockR; type = ExtendedTileType.RockR;
} }
if (bottom && !right && !left && !top) if ( bottom && !right && !left && !top ) {
{
type = ExtendedTileType.RockD; type = ExtendedTileType.RockD;
} }
if (left && top && !bottom && !right) if ( left && top && !bottom && !right ) {
{
type = ExtendedTileType.RockLU; type = ExtendedTileType.RockLU;
} }
if (left && right && !top && !bottom) if ( left && right && !top && !bottom ) {
{
type = ExtendedTileType.RockLR; type = ExtendedTileType.RockLR;
} }
if (left && bottom && !right && !top) if ( left && bottom && !right && !top ) {
{
type = ExtendedTileType.RockLD; type = ExtendedTileType.RockLD;
} }
if (top && right && !left && !bottom) if ( top && right && !left && !bottom ) {
{
type = ExtendedTileType.RockUR; type = ExtendedTileType.RockUR;
} }
if (top && bottom && !left && !right) if ( top && bottom && !left && !right ) {
{
type = ExtendedTileType.RockUD; type = ExtendedTileType.RockUD;
} }
if (right && bottom && !top && !left) if ( right && bottom && !top && !left ) {
{
type = ExtendedTileType.RockRD; type = ExtendedTileType.RockRD;
} }
if (left && top && bottom && !right) if ( left && top && bottom && !right ) {
{
type = ExtendedTileType.RockLUD; type = ExtendedTileType.RockLUD;
} }
if (left && top && right && !bottom) if ( left && top && right && !bottom ) {
{
type = ExtendedTileType.RockLUR; type = ExtendedTileType.RockLUR;
} }
if (top && right && bottom && !left) if ( top && right && bottom && !left ) {
{
type = ExtendedTileType.RockURD; type = ExtendedTileType.RockURD;
} }
if (left && right && bottom && !top) if ( left && right && bottom && !top ) {
{
type = ExtendedTileType.RockLRD; type = ExtendedTileType.RockLRD;
} }
if (left && top && right && bottom) if ( left && top && right && bottom ) {
{
type = ExtendedTileType.RockLURD; type = ExtendedTileType.RockLURD;
} }
break; break;
@ -158,11 +127,9 @@ public class GenerationProcessor {
return root; return root;
} }
private GameObject CreateGOFromType(Vector2 v, ExtendedTileType t, GameObject root) private GameObject CreateGOFromType(Vector2 v, ExtendedTileType t, GameObject root) {
{
GameObject tmp = null; GameObject tmp = null;
if (prefabs.ContainsKey(t) && root != null) if ( prefabs.ContainsKey(t) && root != null ) {
{
tmp = GameObject.Instantiate(prefabs[t], root.transform); tmp = GameObject.Instantiate(prefabs[t], root.transform);
tmp.transform.position = v; tmp.transform.position = v;
} }