From ca23b945dfcab3ff6ed957282d5281c8bdd8f29f Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 27 Apr 2019 19:04:27 +0200 Subject: [PATCH] cose --- Assets/Prefabs/Map.prefab | 18 +++++- Assets/Scenes/Steffo.unity | 36 ++++++++++-- Assets/Scripts/Map.cs | 81 ++++++++++++++------------ Assets/Scripts/Monster.cs | 2 +- Assets/Sprites/PurpleTileset2.png.meta | 24 +++++++- ProjectSettings/TagManager.asset | 3 +- 6 files changed, 119 insertions(+), 45 deletions(-) diff --git a/Assets/Prefabs/Map.prefab b/Assets/Prefabs/Map.prefab index e8043ff..a7687dc 100644 --- a/Assets/Prefabs/Map.prefab +++ b/Assets/Prefabs/Map.prefab @@ -45,8 +45,22 @@ MonoBehaviour: m_EditorClassIdentifier: mapSize: 30 roomsToGenerate: 5 + minRoomSize: 2 + maxRoomSize: 6 + maxRoomIterations: 100 wallSprite: {fileID: 21300000, guid: f5be5362b22aa6c48993a720ec6c9b84, type: 3} - roomSprite: {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} - corridorSprite: {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + floorSprites: + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} + - {fileID: 21300000, guid: fa4c9841fd8a5fb4f94ff78c95301065, type: 3} tilePrefab: {fileID: 3219556109866242471, guid: 0e0b618b5499bbb40b076c0291d55487, type: 3} diff --git a/Assets/Scenes/Steffo.unity b/Assets/Scenes/Steffo.unity index d68be2b..7dd5a86 100644 --- a/Assets/Scenes/Steffo.unity +++ b/Assets/Scenes/Steffo.unity @@ -341,15 +341,43 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 6907631410461015020, guid: cd714ea41cb7c454994bf0f5ad99abd1, type: 3} - propertyPath: wallSprite + propertyPath: floorSprites.Array.size + value: 6 + objectReference: {fileID: 0} + - target: {fileID: 6907631410461015020, guid: cd714ea41cb7c454994bf0f5ad99abd1, + type: 3} + propertyPath: roomsToGenerate + value: 10 + objectReference: {fileID: 0} + - target: {fileID: 6907631410461015020, guid: cd714ea41cb7c454994bf0f5ad99abd1, + type: 3} + propertyPath: floorSprites.Array.data[2] value: - objectReference: {fileID: 21300000, guid: 0121970e693ee0a4d987bd5fe694ad67, + objectReference: {fileID: 21300000, guid: fa4c9841fd8a5fb4f94ff78c95301065, type: 3} - target: {fileID: 6907631410461015020, guid: cd714ea41cb7c454994bf0f5ad99abd1, type: 3} - propertyPath: corridorSprite + propertyPath: floorSprites.Array.data[3] value: - objectReference: {fileID: 21300000, guid: f5be5362b22aa6c48993a720ec6c9b84, + objectReference: {fileID: 21300000, guid: fa4c9841fd8a5fb4f94ff78c95301065, + type: 3} + - target: {fileID: 6907631410461015020, guid: cd714ea41cb7c454994bf0f5ad99abd1, + type: 3} + propertyPath: floorSprites.Array.data[4] + value: + objectReference: {fileID: 21300000, guid: fa4c9841fd8a5fb4f94ff78c95301065, + type: 3} + - target: {fileID: 6907631410461015020, guid: cd714ea41cb7c454994bf0f5ad99abd1, + type: 3} + propertyPath: floorSprites.Array.data[5] + value: + objectReference: {fileID: 21300000, guid: fa4c9841fd8a5fb4f94ff78c95301065, + type: 3} + - target: {fileID: 6907631410461015020, guid: cd714ea41cb7c454994bf0f5ad99abd1, + type: 3} + propertyPath: floorSprites.Array.data[1] + value: + objectReference: {fileID: 21300000, guid: e0e60c9a4ff0b8e429e6cfe316da9bbf, type: 3} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: cd714ea41cb7c454994bf0f5ad99abd1, type: 3} diff --git a/Assets/Scripts/Map.cs b/Assets/Scripts/Map.cs index 182bf0a..3c5b1f7 100644 --- a/Assets/Scripts/Map.cs +++ b/Assets/Scripts/Map.cs @@ -18,10 +18,10 @@ public class MapRoom { public readonly Vector2Int end; public readonly int mapSize; - public MapRoom(int mapSize, int maxRoomSize) { + public MapRoom(int mapSize, int maxRoomSize, int minRoomSize) { this.mapSize = mapSize; - start = new Vector2Int(Random.Range(0, mapSize), Random.Range(0, mapSize)); - end = new Vector2Int(Random.Range(0, mapSize), Random.Range(0, mapSize)); + start = new Vector2Int(Random.Range(1, mapSize-1), Random.Range(1, mapSize-1)); + end = new Vector2Int(Random.Range(1, mapSize-1), Random.Range(1, mapSize-1)); if(start.x > end.x) { int swap = start.x; start.x = end.x; @@ -40,36 +40,30 @@ public class MapRoom { end.y--; start.y++; } - } - - public bool RightCheck() { - return end.x < mapSize-1; + while(end.x - start.x < minRoomSize) { + end.x++; + start.x--; + } + while(end.y - start.y < minRoomSize) { + end.y++; + start.y--; + } + start.Clamp(new Vector2Int(1, 1), new Vector2Int(mapSize-1, mapSize-1)); + end.Clamp(new Vector2Int(1, 1), new Vector2Int(mapSize-1, mapSize-1)); } public Vector2Int RightCorridorAttachment() { return new Vector2Int(end.x+1, Random.Range(start.y, end.y+1)); } - public bool LeftCheck() { - return start.x > 0; - } - public Vector2Int LeftCorridorAttachment() { return new Vector2Int(start.x-1, Random.Range(start.y, end.y+1)); } - - public bool TopCheck() { - return end.y < mapSize-1; - } public Vector2Int TopCorridorAttachment() { return new Vector2Int(Random.Range(start.x, end.x+1), end.y+1); } - public bool BottomCheck() { - return start.y > 0; - } - public Vector2Int BottomCorridorAttachment() { return new Vector2Int(Random.Range(start.x, end.x+1), start.y-1); } @@ -90,10 +84,10 @@ public class MapCorridor { public MapCorridor(MapRoom from, MapRoom to, int mapSize) { List corridorModes = new List(); //Find allowed CorridorModes - if(from.end.y <= to.start.y && from.BottomCheck() && to.TopCheck()) corridorModes.Add(CorridorModes.bottomToTop); - if(from.start.y >= to.end.y && from.TopCheck() && to.BottomCheck()) corridorModes.Add(CorridorModes.topToBottom); - if(from.end.x <= to.start.y && from.RightCheck() && to.LeftCheck()) corridorModes.Add(CorridorModes.rightToLeft); - if(from.start.y >= to.end.y && from.LeftCheck() && to.RightCheck()) corridorModes.Add(CorridorModes.leftToRight); + if(from.end.y <= to.start.y) corridorModes.Add(CorridorModes.bottomToTop); + if(from.start.y >= to.end.y) corridorModes.Add(CorridorModes.topToBottom); + if(from.end.x <= to.start.x) corridorModes.Add(CorridorModes.rightToLeft); + if(from.start.x >= to.end.x) corridorModes.Add(CorridorModes.leftToRight); //Select and use a corridor mode if(corridorModes.Count < 1) throw new ImpossibleCorridorError(); CorridorModes corridorMode = corridorModes[Random.Range(0, corridorModes.Count)]; @@ -115,6 +109,9 @@ public class MapCorridor { } //50% horizontal_priority = Random.Range(0f, 1f) >= 0.5f; + + start.Clamp(new Vector2Int(1, 1), new Vector2Int(mapSize-1, mapSize-1)); + end.Clamp(new Vector2Int(1, 1), new Vector2Int(mapSize-1, mapSize-1)); } } @@ -126,6 +123,9 @@ public class Map : MonoBehaviour [BeforeStartAttribute] public int roomsToGenerate = 5; + [BeforeStartAttribute] + public int minRoomSize = 2; + [BeforeStartAttribute] public int maxRoomSize = 6; @@ -136,10 +136,7 @@ public class Map : MonoBehaviour public Sprite wallSprite; [BeforeStartAttribute] - public Sprite roomSprite; - - [BeforeStartAttribute] - public Sprite corridorSprite; + public List floorSprites; [BeforeStartAttribute] public GameObject tilePrefab; @@ -156,7 +153,12 @@ public class Map : MonoBehaviour public bool CanMoveTo(Vector2Int direction) { - return GetTile(direction).walkable; + try { + return GetTile(direction).walkable; + } + catch(System.ArgumentOutOfRangeException) { + return false; + } } private void InitTile(Vector2Int position, bool walkable, Sprite tileSprite, bool roomPart) { @@ -183,7 +185,7 @@ public class Map : MonoBehaviour private void PlaceRoom(MapRoom mr) { for(int x = mr.start.x; x <= mr.end.x; x++) { for(int y = mr.start.y; y <= mr.end.y; y++) { - InitTile(new Vector2Int(x, y), true, roomSprite, true); + InitTile(new Vector2Int(x, y), true, GetFloorTileSprite(), true); } } } @@ -202,17 +204,17 @@ public class Map : MonoBehaviour private void PlaceCorridor(MapCorridor mc) { Vector2Int cursor = new Vector2Int(mc.start.x, mc.start.y); - InitTile(cursor, true, corridorSprite, false); + InitTile(cursor, true, GetFloorTileSprite(), false); if(mc.horizontal_priority) { while(cursor.x != mc.end.x) { if(cursor.x > mc.end.x) cursor.x--; else cursor.x++; - InitTile(cursor, true, corridorSprite, false); + InitTile(cursor, true, GetFloorTileSprite(), false); } while(cursor.y != mc.end.y) { if(cursor.y > mc.end.y) cursor.y--; else cursor.y++; - InitTile(cursor, true, corridorSprite, false); + InitTile(cursor, true, GetFloorTileSprite(), false); } } else @@ -220,12 +222,12 @@ public class Map : MonoBehaviour while(cursor.y != mc.end.y) { if(cursor.y > mc.end.y) cursor.y--; else cursor.y++; - InitTile(cursor, true, corridorSprite, false); + InitTile(cursor, true, GetFloorTileSprite(), false); } while(cursor.x != mc.end.x) { if(cursor.x > mc.end.x) cursor.x--; else cursor.x++; - InitTile(cursor, true, corridorSprite, false); + InitTile(cursor, true, GetFloorTileSprite(), false); } } } @@ -235,11 +237,13 @@ public class Map : MonoBehaviour int roomIterations = 0; while(rooms.Count < roomsToGenerate && roomIterations < maxRoomIterations) { roomIterations++; - MapRoom room = new MapRoom(mapSize, maxRoomSize); + MapRoom room = new MapRoom(mapSize, maxRoomSize, minRoomSize); if(ScanRoom(room)) { + //Fill with the room PlaceRoom(room); rooms.Add(room); } + //Place a corridor if(rooms.Count > 1) { MapRoom from = rooms[rooms.Count-2]; MapRoom to = rooms[rooms.Count-1]; @@ -247,11 +251,16 @@ public class Map : MonoBehaviour MapCorridor corridor = new MapCorridor(from, to, mapSize); PlaceCorridor(corridor); } - catch (ImpossibleCorridorError) {} + catch (ImpossibleCorridorError) { + } } } } + private Sprite GetFloorTileSprite() { + return floorSprites[Random.Range(0, floorSprites.Count)]; + } + private void Start() { //Initialize everything diff --git a/Assets/Scripts/Monster.cs b/Assets/Scripts/Monster.cs index f84b5da..e9efb1a 100644 --- a/Assets/Scripts/Monster.cs +++ b/Assets/Scripts/Monster.cs @@ -16,7 +16,7 @@ public class Monster : MonoBehaviour private Map map; // Start is called before the first frame update - void Start() + private void Start() { map = GameObject.FindGameObjectWithTag("Map").GetComponent(); hasSpottedPlayer = false; diff --git a/Assets/Sprites/PurpleTileset2.png.meta b/Assets/Sprites/PurpleTileset2.png.meta index 9866600..ead95e1 100644 --- a/Assets/Sprites/PurpleTileset2.png.meta +++ b/Assets/Sprites/PurpleTileset2.png.meta @@ -45,7 +45,7 @@ TextureImporter: spriteMeshType: 1 alignment: 0 spritePivot: {x: 0.5, y: 0.5} - spritePixelsToUnits: 100 + spritePixelsToUnits: 40 spriteBorder: {x: 0, y: 0, z: 0, w: 0} spriteGenerateFallbackPhysicsShape: 1 alphaUsage: 1 @@ -69,6 +69,28 @@ TextureImporter: allowsAlphaSplitting: 0 overridden: 0 androidETC2FallbackOverride: 0 + - serializedVersion: 2 + buildTarget: Standalone + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + - serializedVersion: 2 + buildTarget: WebGL + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 spriteSheet: serializedVersion: 2 sprites: [] diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 1c92a78..82e2d19 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -3,7 +3,8 @@ --- !u!78 &1 TagManager: serializedVersion: 2 - tags: [] + tags: + - Map layers: - Default - TransparentFX