From c2843200a728824d3ac2049c6caa4c59bf14b159 Mon Sep 17 00:00:00 2001 From: hy <87596507+Snow0406@users.noreply.github.com> Date: Fri, 9 May 2025 01:24:00 +0900 Subject: [PATCH] feat: MapGenerator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 알고리즘 작동 방식 1. 원형내 랜덤으로 방 배치 2. 서로 겹치는 방은 떨어트리기 3. 방 고르기 4. 복도 이어주기 --- .gitignore | 1 + Assets/0_Scenes/Map Generate.unity | 275 +++++++++++++++++++++ Assets/0_Scenes/Map Generate.unity.meta | 7 + Assets/1_Script/Map.meta | 3 + Assets/1_Script/Map/MapGenerator.cs | 299 +++++++++++++++++++++++ Assets/1_Script/Map/MapGenerator.cs.meta | 3 + 6 files changed, 588 insertions(+) create mode 100644 Assets/0_Scenes/Map Generate.unity create mode 100644 Assets/0_Scenes/Map Generate.unity.meta create mode 100644 Assets/1_Script/Map.meta create mode 100644 Assets/1_Script/Map/MapGenerator.cs create mode 100644 Assets/1_Script/Map/MapGenerator.cs.meta diff --git a/.gitignore b/.gitignore index ee7c00e..99bd03b 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,4 @@ crashlytics-build.properties /[Aa]ssets/[Ss]treamingAssets/aa.meta /[Aa]ssets/[Ss]treamingAssets/aa/* +/.idea diff --git a/Assets/0_Scenes/Map Generate.unity b/Assets/0_Scenes/Map Generate.unity new file mode 100644 index 0000000..3d37330 --- /dev/null +++ b/Assets/0_Scenes/Map Generate.unity @@ -0,0 +1,275 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 3 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 0 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 3 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + buildHeightMesh: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &1802181444 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1802181447} + - component: {fileID: 1802181446} + - component: {fileID: 1802181445} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &1802181445 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1802181444} + m_Enabled: 1 +--- !u!20 &1802181446 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1802181444} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_Iso: 200 + m_ShutterSpeed: 0.005 + m_Aperture: 16 + m_FocusDistance: 10 + m_FocalLength: 50 + m_BladeCount: 5 + m_Curvature: {x: 2, y: 11} + m_BarrelClipping: 0.25 + m_Anamorphism: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 1 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 1 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &1802181447 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1802181444} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1830898465 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1830898467} + - component: {fileID: 1830898466} + m_Layer: 0 + m_Name: MapGenerator + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1830898466 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1830898465} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8fdb3db0de214c69a1c56245514dcc63, type: 3} + m_Name: + m_EditorClassIdentifier: + totalRooms: 20 + mainRoomsCount: 10 + mapRadius: 60 + minRoomWidth: 8 + maxRoomWidth: 16 + minRoomHeight: 8 + maxRoomHeight: 16 + separationIterations: 50 + roomColor: {r: 0, g: 0, b: 1, a: 1} + mainRoomColor: {r: 1, g: 0, b: 0, a: 1} + corridorColor: {r: 0.06642128, g: 1, b: 0.043137252, a: 1} +--- !u!4 &1830898467 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1830898465} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1660057539 &9223372036854775807 +SceneRoots: + m_ObjectHideFlags: 0 + m_Roots: + - {fileID: 1802181447} + - {fileID: 1830898467} diff --git a/Assets/0_Scenes/Map Generate.unity.meta b/Assets/0_Scenes/Map Generate.unity.meta new file mode 100644 index 0000000..0c869be --- /dev/null +++ b/Assets/0_Scenes/Map Generate.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 4dc33b9f5eb3ae34f927ff4f2aeab6cc +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/1_Script/Map.meta b/Assets/1_Script/Map.meta new file mode 100644 index 0000000..e476466 --- /dev/null +++ b/Assets/1_Script/Map.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6dec96d472af453888b1a805041198c5 +timeCreated: 1746711648 \ No newline at end of file diff --git a/Assets/1_Script/Map/MapGenerator.cs b/Assets/1_Script/Map/MapGenerator.cs new file mode 100644 index 0000000..79b541e --- /dev/null +++ b/Assets/1_Script/Map/MapGenerator.cs @@ -0,0 +1,299 @@ +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class MapGenerator : MonoBehaviour +{ + public int totalRooms = 20; + public int mainRoomsCount = 10; + public float mapRadius = 50f; + public float minRoomWidth = 4f; + public float maxRoomWidth = 10f; + public float minRoomHeight = 4f; + public float maxRoomHeight = 10f; + public int separationIterations = 10; + + private readonly List _rooms = new(); + private List _mainRooms = new(); + private readonly List _connections = new(); + private readonly HashSet _corridorTiles = new(); + + private void Start() + { + GenerateMap(); + } + + public void GenerateMap() + { + ClearAll(); + GenerateRooms(); + SeparateRooms(); + SelectMainRooms(); + ConnectRoomsWithMST(); + CreateCorridors(); + + Debug.Log($"생성된 전체 방 수: {_rooms.Count}"); + Debug.Log($"메인 방 수: {_mainRooms.Count}"); + Debug.Log($"연결 수: {_connections.Count}"); + } + + private void ClearAll() + { + _rooms.Clear(); + _mainRooms.Clear(); + _connections.Clear(); + } + + private void GenerateRooms() + { + for (var i = 0; i < totalRooms; i++) + { + // 랜덤 위치 선택 + var angle = Random.Range(0f, Mathf.PI * 2); + var distance = Random.Range(0f, mapRadius); + var position = new Vector2( + Mathf.Cos(angle) * distance, + Mathf.Sin(angle) * distance + ); + + // 랜덤 크기 방 생성 + var width = Random.Range(minRoomWidth, maxRoomWidth); + var height = Random.Range(minRoomHeight, maxRoomHeight); + + _rooms.Add(new Room(position, new Vector2(width, height))); + } + } + + private void SeparateRooms() + { + for (var iter = 0; iter < separationIterations; iter++) + { + var stillOverlapping = false; + + for (var i = 0; i < _rooms.Count; i++) + for (var j = i + 1; j < _rooms.Count; j++) + if (_rooms[i].Overlaps(_rooms[j])) + { + stillOverlapping = true; + + // 방들이 겹치면 서로 밀어냄 + var direction = (_rooms[i].Position - _rooms[j].Position).normalized; + var overlap = _rooms[i].GetOverlapDistance(_rooms[j]); + + _rooms[i].Position += direction * overlap * 0.5f; + _rooms[j].Position -= direction * overlap * 0.5f; + } + + // 더 이상 겹치는 방이 없으면 종료 + if (!stillOverlapping) break; + } + } + + private void SelectMainRooms() + { + // 방 크기에 따라 정렬 (큰 것부터) + _rooms.Sort((a, b) => (b.Size.x * b.Size.y).CompareTo(a.Size.x * a.Size.y)); + + // 가장 큰 방 mainRoomsCount개만 선택 + _mainRooms = _rooms.Take(Mathf.Min(mainRoomsCount, _rooms.Count)).ToList(); + } + + private void ConnectRoomsWithMST() + { + var edges = new List(); + + // 모든 방 쌍에 대해 거리 계산 + for (var i = 0; i < _mainRooms.Count; i++) + for (var j = i + 1; j < _mainRooms.Count; j++) + { + var dist = Vector2.Distance(_mainRooms[i].Position, _mainRooms[j].Position); + edges.Add(new Edge(i, j, dist)); + } + + // 크루스칼 MST 알고리즘 + edges.Sort((a, b) => a.Weight.CompareTo(b.Weight)); + var ds = new DisjointSet(_mainRooms.Count); + + foreach (var edge in edges) + if (ds.Find(edge.U) != ds.Find(edge.V)) + { + ds.Union(edge.U, edge.V); + _connections.Add(new Connection(_mainRooms[edge.U], _mainRooms[edge.V])); + } + } + + private void CreateCorridors() + { + foreach (var conn in _connections) + { + var roomA = conn.RoomA; + var roomB = conn.RoomB; + + // 두 방의 중심점 + var startPos = Vector2Int.RoundToInt(roomA.Position); + var endPos = Vector2Int.RoundToInt(roomB.Position); + + // 두 가지 L자 패턴 + if (Random.Range(0, 2) == 0) + { + // 수평 먼저, 수직 나중 ㄱ + CreateCorridor(_corridorTiles, startPos, new Vector2Int(endPos.x, startPos.y)); + CreateCorridor(_corridorTiles, new Vector2Int(endPos.x, startPos.y), endPos); + } + else + { + // 수직 먼저, 수평 나중 ┌ + CreateCorridor(_corridorTiles, startPos, new Vector2Int(startPos.x, endPos.y)); + CreateCorridor(_corridorTiles, new Vector2Int(startPos.x, endPos.y), endPos); + } + } + } + + /// + /// 중복 없는 복도 생성 + /// + /// + /// + /// + private void CreateCorridor(HashSet corridorTiles, Vector2Int start, Vector2Int end) + { + var position = start; + var direction = new Vector2Int( + Mathf.Clamp(end.x - start.x, -1, 1), + Mathf.Clamp(end.y - start.y, -1, 1) + ); + + while (position != end) + { + corridorTiles.Add(position); + + // 다음 위치로 이동 + if (position.x != end.x) + position.x += direction.x; + else if (position.y != end.y) + position.y += direction.y; + } + } + + #region Debug 테스트용 + + public Color roomColor = Color.blue; + public Color mainRoomColor = Color.red; + public Color corridorColor = Color.green; + + private void OnDrawGizmos() + { + // 방 + Gizmos.color = roomColor; + foreach (var room in _rooms) + { + if (_mainRooms.Contains(room)) + Gizmos.color = mainRoomColor; + else + Gizmos.color = roomColor; + + Gizmos.DrawWireCube(room.Position, room.Size); + } + + // 복도 + Gizmos.color = corridorColor; + var tileSize = 1f; // 타일 크기 + + if (_corridorTiles != null) + foreach (var pos in _corridorTiles) + Gizmos.DrawWireCube(new Vector3(pos.x, pos.y, 0), + new Vector3(tileSize, tileSize, 0)); + } + + #endregion + + #region Class + + // 방 클래스 + public class Room + { + public Vector2 Position; + public Vector2 Size; + + public Room(Vector2 pos, Vector2 size) + { + Position = pos; + Size = size; + } + + public bool Overlaps(Room other) + { + return !(Position.x + Size.x / 2 < other.Position.x - other.Size.x / 2 || + Position.x - Size.x / 2 > other.Position.x + other.Size.x / 2 || + Position.y + Size.y / 2 < other.Position.y - other.Size.y / 2 || + Position.y - Size.y / 2 > other.Position.y + other.Size.y / 2); + } + + public float GetOverlapDistance(Room other) + { + var dx = Mathf.Min(Position.x + Size.x / 2, other.Position.x + other.Size.x / 2) - + Mathf.Max(Position.x - Size.x / 2, other.Position.x - other.Size.x / 2); + var dy = Mathf.Min(Position.y + Size.y / 2, other.Position.y + other.Size.y / 2) - + Mathf.Max(Position.y - Size.y / 2, other.Position.y - other.Size.y / 2); + + // 실제 겹치는 거리 계산 + if (dx > 0 && dy > 0) + return Mathf.Min(dx, dy) + 0.1f; // 약간의 여유 공간 추가 + + return 0; + } + } + + // 연결 클래스 + private class Connection + { + public readonly Room RoomA, RoomB; + + public Connection(Room a, Room b) + { + RoomA = a; + RoomB = b; + } + } + + // MST용 간선 클래스 + private class Edge + { + public readonly int U, V; + public readonly float Weight; + + public Edge(int u, int v, float w) + { + U = u; + V = v; + Weight = w; + } + } + + #endregion + + // Disjoint Set 자료구조 + private class DisjointSet + { + private readonly int[] _parent; + + public DisjointSet(int n) + { + _parent = new int[n]; + for (var i = 0; i < n; i++) _parent[i] = i; + } + + public int Find(int x) + { + if (_parent[x] == x) return x; + return _parent[x] = Find(_parent[x]); + } + + public void Union(int a, int b) + { + var rootA = Find(a); + var rootB = Find(b); + if (rootA != rootB) _parent[rootB] = rootA; + } + } +} \ No newline at end of file diff --git a/Assets/1_Script/Map/MapGenerator.cs.meta b/Assets/1_Script/Map/MapGenerator.cs.meta new file mode 100644 index 0000000..dd28162 --- /dev/null +++ b/Assets/1_Script/Map/MapGenerator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8fdb3db0de214c69a1c56245514dcc63 +timeCreated: 1746712260 \ No newline at end of file