diff --git a/Assets/Scripts/Generation/DungeonGenerator.cs b/Assets/Scripts/Generation/DungeonGenerator.cs index 88d7f99..85adea8 100644 --- a/Assets/Scripts/Generation/DungeonGenerator.cs +++ b/Assets/Scripts/Generation/DungeonGenerator.cs @@ -127,11 +127,13 @@ public class DungeonGenerator { foreach ( GenRoom r in rooms ) { 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++ ) { - r.tiles.Add(new Vector2Int(x1, y1), Room.TileType.WALL); + int xMode = (x1 == r.bounds.x) ? -1 : (x1 == r.bounds.x + r.bounds.width - 1) ? 1 : 0; + int yMode = (y1 == r.bounds.y) ? -1 : (y1 == r.bounds.y + r.bounds.height - 1) ? 1 : 0; + r.tiles.Add(new Vector2Int(x1, y1), new GenTile(Room.TileType.WALL, GenTile.GetPosition(xMode,yMode))); } 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++ ) { - r.tiles[new Vector2Int(x1, y1)] = Room.TileType.GROUND; + r.tiles[new Vector2Int(x1, y1)].type = Room.TileType.GROUND; } allDoors.UnionWith(r.AllDoors()); foreach ( Vector2Int v in r.AllDoors() ) { @@ -139,7 +141,7 @@ public class DungeonGenerator { if ( !r.bounds.Contains(v) ) throw new NotSupportedException("This is a bug where doors land in the wrong room. It should have been fixed."); else - r.tiles[v] = Room.TileType.DOOR; + r.tiles[v].type = Room.TileType.DOOR; } } @@ -150,17 +152,19 @@ public class DungeonGenerator { for (int y1 = r.bounds.y; y1 < r.bounds.y + r.bounds.height; y1++) { Vector2Int pos1 = new Vector2Int(x1, y1); - if (path.tiles.ContainsKey(pos1)) - path.tiles[pos1] = Room.TileType.GROUND; - else - path.tiles.Add(pos1, Room.TileType.GROUND); - for (int x2 = x1 - 1; x2 <= x1 + 1; x2++) - for (int y2 = y1 - 1; y2 <= y1 + 1; y2++) - { + if (path.tiles.ContainsKey(pos1)) + path.tiles[pos1].type = Room.TileType.GROUND; + else + path.tiles.Add(pos1, new GenTile(Room.TileType.GROUND)); + for (int x2 = x1 - 1; x2 <= x1 + 1; x2++) + { + for (int y2 = y1 - 1; y2 <= y1 + 1; y2++) + { Vector2Int pos2 = new Vector2Int(x2, y2); - if (!path.tiles.ContainsKey(pos2) && !allDoors.Contains(pos2)) - path.tiles.Add(pos2, Room.TileType.WALL); - } + if (!path.tiles.ContainsKey(pos2) && !allDoors.Contains(pos2)) + path.tiles.Add(pos2, new GenTile(Room.TileType.WALL)); + } + } } if (r.AllDoors().Count > 0) throw new NotSupportedException("Paths should not have any doors"); diff --git a/Assets/Scripts/Generation/GenRoom.cs b/Assets/Scripts/Generation/GenRoom.cs index 2b9fa56..cd5fe54 100644 --- a/Assets/Scripts/Generation/GenRoom.cs +++ b/Assets/Scripts/Generation/GenRoom.cs @@ -4,7 +4,7 @@ using UnityEngine; public class GenRoom { // ---Internal for generation only--- - // TODO make them package protcted please + // TODO make them package protected please public RectInt bounds = new RectInt(); public HashSet doorsUp = new HashSet(); @@ -17,7 +17,7 @@ public class GenRoom { // The position of the anchor of the room in world space. This should be the top left corner of the room, but may be any point in the world. public Vector2Int roomPosition; // All positions are in room space relative to the room's anchor - public Dictionary tiles = new Dictionary(); + public Dictionary tiles = new Dictionary(); public float Distance(GenRoom r) { return Math.Abs(GetCenter().x - r.GetCenter().x) + Math.Abs(GetCenter().y - r.GetCenter().y); diff --git a/Assets/Scripts/Generation/GenTile.cs b/Assets/Scripts/Generation/GenTile.cs new file mode 100644 index 0000000..e66509e --- /dev/null +++ b/Assets/Scripts/Generation/GenTile.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class GenTile +{ + public enum Position + { + TOP_LEFT = 0, + TOP = 1, + TOP_RIGHT = 2, + LEFT = 3, + CENTER = 4, + RIGHT = 5, + BOTTOM_LEFT = 6, + BOTTOM = 7, + BOTTOM_RIGHT = 8 + } + + public Room.TileType type; + public Position position; + + public GenTile() + { + + } + + public GenTile(Room.TileType type) + { + this.type = type; + } + + public GenTile(Room.TileType type, Position position) + { + this.type = type; + this.position = position; + } + + public static Position GetPosition(int xMode, int yMode) { + return (Position) ((xMode + 1) + (yMode + 1) * 3); + } +} diff --git a/Assets/Scripts/Generation/GenTile.cs.meta b/Assets/Scripts/Generation/GenTile.cs.meta new file mode 100644 index 0000000..63c77e7 --- /dev/null +++ b/Assets/Scripts/Generation/GenTile.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1b8dce5bdb3204011a32ee1b512a4296 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Generation/GenerationProcessor.cs b/Assets/Scripts/Generation/GenerationProcessor.cs index d376a1f..b39ec94 100644 --- a/Assets/Scripts/Generation/GenerationProcessor.cs +++ b/Assets/Scripts/Generation/GenerationProcessor.cs @@ -11,58 +11,53 @@ public class GenerationProcessor { this.prefabs = prefabs; } - public GameObject ProcessRoom(Dictionary d) { + public GameObject ProcessRoom(Dictionary tiles) { GameObject root = new GameObject { name = "Room" }; - foreach ( Vector2Int v in d.Keys ) { - bool left = false; - bool top = false; - bool right = false; - bool bottom = false; - // left bound - if ( d.ContainsKey(v + new Vector2Int(-1, 0)) ) { - if ( d[v + new Vector2Int(-1, 0)] == d[v] ) { - left = true; - } - } - // top bound - if ( d.ContainsKey(v + new Vector2Int(0, 1)) ) { - if ( d[v + new Vector2Int(0, 1)] == d[v] ) { - top = true; - } - } - // right bound - if ( d.ContainsKey(v + new Vector2Int(1, 0)) ) { - if ( d[v + new Vector2Int(1, 0)] == d[v] ) { - right = true; - } - } - // bottom bound - if ( d.ContainsKey(v + new Vector2Int(0, -1)) ) { - if ( d[v + new Vector2Int(0, -1)] == d[v] ) { - bottom = true; - } - } - ExtendedTileType type = ExtendedTileType.Ground; - // --------------------------------------------------------------------------------------------------------------------------------------------- - // ^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~ - // - // *** W A R N I N G B A D C O D E A H E A D ! ! ! *** - // __________________________________________________________________________ - // - // DON'T WATCH, UNLESS YOU WANT TO GET TRAUMATIZED! - // - // ^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~^~!~ - // --------------------------------------------------------------------------------------------------------------------------------------------- - switch ( d[v] ) { + foreach ( Vector2Int v in tiles.Keys ) { + ExtendedTileType type = ExtendedTileType.Ground; + int rotation = 0; + switch ( tiles[v].type ) { case Room.TileType.WALL: - type = ExtendedTileType.BorderSingle; - if ( top && left && d.ContainsKey(v + new Vector2Int(-1, -1)) || top && right && d.ContainsKey(v + new Vector2Int(1, -1)) || right && bottom && d.ContainsKey(v + new Vector2Int(1, 1)) || left && bottom && d.ContainsKey(v + new Vector2Int(-1, 1)) ) { - type = ExtendedTileType.BorderOuter; - } else if ( top && left || top && right || right && bottom || left && bottom ) { - type = ExtendedTileType.BorderInner; - } + type = getCorrectWallType(tiles, v); + switch (type) + { + case ExtendedTileType.BorderSingle: + switch (tiles[v].position) + { + case GenTile.Position.BOTTOM: + rotation = 0; + break; + case GenTile.Position.LEFT: + rotation = 90; + break; + case GenTile.Position.TOP: + rotation = 180; + break; + case GenTile.Position.RIGHT: + rotation = 270; + break; + } + break; + case ExtendedTileType.BorderInner: + switch (tiles[v].position) + { + case GenTile.Position.BOTTOM_LEFT: + rotation = 0; + break; + case GenTile.Position.TOP_LEFT: + rotation = 90; + break; + case GenTile.Position.TOP_RIGHT: + rotation = 180; + break; + case GenTile.Position.BOTTOM_RIGHT: + rotation = 270; + break; + } + break; + } break; case Room.TileType.GROUND: type = ExtendedTileType.Ground; @@ -71,68 +66,137 @@ public class GenerationProcessor { type = ExtendedTileType.Door; break; case Room.TileType.ROCK: - type = ExtendedTileType.Rock; - if ( top && !right && !left && !bottom ) { - type = ExtendedTileType.RockU; - } - if ( left && !right && !bottom && !top ) { - type = ExtendedTileType.RockL; - } - if ( right && !bottom && !left && !top ) { - type = ExtendedTileType.RockR; - } - if ( bottom && !right && !left && !top ) { - type = ExtendedTileType.RockD; - } - if ( left && top && !bottom && !right ) { - type = ExtendedTileType.RockLU; - } - if ( left && right && !top && !bottom ) { - type = ExtendedTileType.RockLR; - } - if ( left && bottom && !right && !top ) { - type = ExtendedTileType.RockLD; - } - if ( top && right && !left && !bottom ) { - type = ExtendedTileType.RockUR; - } - if ( top && bottom && !left && !right ) { - type = ExtendedTileType.RockUD; - } - if ( right && bottom && !top && !left ) { - type = ExtendedTileType.RockRD; - } - - if ( left && top && bottom && !right ) { - type = ExtendedTileType.RockLUD; - } - if ( left && top && right && !bottom ) { - type = ExtendedTileType.RockLUR; - } - if ( top && right && bottom && !left ) { - type = ExtendedTileType.RockURD; - } - if ( left && right && bottom && !top ) { - type = ExtendedTileType.RockLRD; - } - if ( left && top && right && bottom ) { - type = ExtendedTileType.RockLURD; - } + type = getCorrectRockType(tiles, v); break; } - CreateGOFromType(v, type, root); + CreateGOFromType(v, rotation, type, root); } return root; } - private GameObject CreateGOFromType(Vector2 v, ExtendedTileType t, GameObject root) { + private GameObject CreateGOFromType(Vector2 v, int rotation, ExtendedTileType t, GameObject root) { GameObject tmp = null; if ( prefabs.ContainsKey(t) && root != null ) { - tmp = GameObject.Instantiate(prefabs[t], root.transform); + tmp = Object.Instantiate(prefabs[t], root.transform); tmp.transform.position = v; + tmp.transform.Rotate(new Vector3(0, 0, rotation)); } return tmp; } -} + + private int CountSpecificNeighbours(Dictionary tiles, Vector2Int position, Room.TileType type) { + int counter = 0; + Vector2Int toCheck = position + new Vector2Int(0, -1); + if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == type) + counter++; + toCheck = position + new Vector2Int(-1, 0); + if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == type) + counter++; + toCheck = position + new Vector2Int(0, 1); + if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == type) + counter++; + toCheck = position + new Vector2Int(1, 0); + if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == type) + counter++; + return counter; + } + + private ExtendedTileType getCorrectWallType(Dictionary tiles, Vector2Int position){ + int groundNumber = CountSpecificNeighbours(tiles, position, Room.TileType.GROUND); + switch(groundNumber){ + case 0: + return ExtendedTileType.BorderInner; + case 2: + return ExtendedTileType.BorderOuter; + default: + return ExtendedTileType.BorderSingle; + } + } + + private ExtendedTileType getCorrectRockType(Dictionary tiles, Vector2Int position){ + + ExtendedTileType type = ExtendedTileType.Rock; + + bool left = false; + bool top = false; + bool right = false; + bool bottom = false; + + Vector2Int toCheck = position + new Vector2Int(0, -1); + if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK) + bottom = true; + toCheck = position + new Vector2Int(-1, 0); + if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK) + left = true; + toCheck = position + new Vector2Int(0, 1); + if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK) + top = true; + toCheck = position + new Vector2Int(1, 0); + if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK) + right = true; + + if (top && !right && !left && !bottom) + { + return ExtendedTileType.RockU; + } + if (left && !right && !bottom && !top) + { + return ExtendedTileType.RockL; + } + if (right && !bottom && !left && !top) + { + return ExtendedTileType.RockR; + } + if (bottom && !right && !left && !top) + { + return ExtendedTileType.RockD; + } + if (left && top && !bottom && !right) + { + return ExtendedTileType.RockLU; + } + if (left && right && !top && !bottom) + { + return ExtendedTileType.RockLR; + } + if (left && bottom && !right && !top) + { + return ExtendedTileType.RockLD; + } + if (top && right && !left && !bottom) + { + return ExtendedTileType.RockUR; + } + if (top && bottom && !left && !right) + { + return ExtendedTileType.RockUD; + } + if (right && bottom && !top && !left) + { + return ExtendedTileType.RockRD; + } + if (left && top && bottom && !right) + { + return ExtendedTileType.RockLUD; + } + if (left && top && right && !bottom) + { + return ExtendedTileType.RockLUR; + } + if (top && right && bottom && !left) + { + return ExtendedTileType.RockURD; + } + if (left && right && bottom && !top) + { + return ExtendedTileType.RockLRD; + } + if (left && top && right && bottom) + { + return ExtendedTileType.RockLURD; + } + return type; + } +} \ No newline at end of file