using UnityEngine; using UnityEngine.Tilemaps; namespace Platformer.Mechanics { /// /// 게임 시작 시 프레임·렌더·물리 기본 최적화 + One-Way Platform 자동 적용. /// BT5-Dev 발판 시스템 영구 영역 (2026-05-08 PD 정합 마무리). /// /// 동작 요약: /// 1. Layer Matrix: Player(13) ↔ Enemy(14) 충돌 OFF. /// 2. Level Tilemap = Layer 0 (지면·벽 영구 충돌). 그 외 비-trigger Collider2D = Layer 16 강제. /// 3. PD Foreground GameObject = 가림막 시각만 (TilemapCollider2D 제거). /// 4. AutoForeground GameObject (Grid 자식·Tilemap·TilemapCollider·Layer 16) = 자동 분류 발판. /// 5. 자동 분류 (Level → AutoForeground): /// - colliderType=None Tile (tree·plant·fence·house) 제외 /// - 이름 prefix "TileGround" Tile (지면·벽) 제외 /// - 임계값 (worldY >= playerY+1.5) 또는 작은 공중 발판 (가로≤8 + 위·아래 빈) 분류 /// - SetColliderType(Sprite) 강제 /// public static class GameOptimizer { [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] static void Init() { Application.targetFrameRate = 60; QualitySettings.vSyncCount = 0; Time.fixedDeltaTime = 1f / 60f; // Layer Matrix: Player(13) ↔ Enemy(14) 충돌 OFF. Physics2D.IgnoreLayerCollision(13, 14, true); } [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] static void SetupJumpThroughPlatforms() { // 1. Level Tilemap = Layer 0 보존. 그 외 비-trigger Collider2D = Layer 16 강제 (Drop-Through). int applied = 0, excluded = 0, levelKept = 0; var allColliders = Object.FindObjectsByType(FindObjectsSortMode.None); foreach (var c in allColliders) { if (c == null) continue; if (c.isTrigger) { excluded++; continue; } if (c.GetComponent() != null || c.GetComponent() != null || c.GetComponent() != null || c.GetComponent() != null || c.GetComponent() != null || c.GetComponent() != null) { excluded++; continue; } if (c.GetComponent() != null && c.gameObject.name == "Level") { if (c.gameObject.layer == 16) c.gameObject.layer = 0; var lvlEffector = c.GetComponent(); if (lvlEffector != null) Object.Destroy(lvlEffector); c.usedByEffector = false; levelKept++; continue; } var effector = c.GetComponent(); if (effector != null) Object.Destroy(effector); c.usedByEffector = false; c.gameObject.layer = 16; applied++; } // 2. PD Foreground = 가림막 시각만 (TilemapCollider2D 제거 → 충돌 X). var pdForeground = GameObject.Find("Foreground"); if (pdForeground != null) { var pdFgTc = pdForeground.GetComponent(); if (pdFgTc != null) Object.Destroy(pdFgTc); } // 3. AutoForeground GameObject (Grid 자식·신규) — 자동 분류 발판 전용. // transform.localPosition = PD Foreground/Level과 동기화 (시각 위치 정합). GameObject gridGo = (pdForeground != null && pdForeground.transform.parent != null) ? pdForeground.transform.parent.gameObject : GameObject.Find("Grid"); GameObject autoFg = GameObject.Find("AutoForeground"); UnityEngine.Tilemaps.Tilemap fgTilemap = null; UnityEngine.Tilemaps.TilemapCollider2D fgTc = null; if (autoFg == null && gridGo != null) { autoFg = new GameObject("AutoForeground"); autoFg.transform.SetParent(gridGo.transform, false); } if (autoFg != null) { autoFg.layer = 16; var refGo = pdForeground != null ? pdForeground : GameObject.Find("Level"); if (refGo != null) autoFg.transform.localPosition = refGo.transform.localPosition; fgTilemap = autoFg.GetComponent(); if (fgTilemap == null) fgTilemap = autoFg.AddComponent(); if (autoFg.GetComponent() == null) autoFg.AddComponent(); fgTc = autoFg.GetComponent(); if (fgTc == null) fgTc = autoFg.AddComponent(); } // 4. Level → AutoForeground 자동 분류: // - colliderType=None (배경) 제외 / 이름 prefix "TileGround" (지면·벽) 제외 // - 임계값 위 OR 작은 공중 발판 = Foreground 이동 + Sprite 강제 var levelGo = GameObject.Find("Level"); if (levelGo != null && fgTilemap != null) { var levelTilemap = levelGo.GetComponent(); var player = Object.FindFirstObjectByType(); if (levelTilemap != null && player != null) { float playerY = player.transform.position.y; float airThresholdY = playerY + 1.5f; const int MAX_PLATFORM_WIDTH = 8; var bounds = levelTilemap.cellBounds; int movedHigh = 0, movedSmall = 0; for (int x = bounds.xMin; x <= bounds.xMax; x++) { for (int y = bounds.yMin; y <= bounds.yMax; y++) { var pos = new Vector3Int(x, y, 0); if (!levelTilemap.HasTile(pos)) continue; var tileAsset = levelTilemap.GetTile(pos); if (tileAsset != null && tileAsset.colliderType == UnityEngine.Tilemaps.Tile.ColliderType.None) continue; string nm = tileAsset != null ? tileAsset.name : string.Empty; if (nm.StartsWith("TileGround")) continue; Vector3 worldPos = levelTilemap.CellToWorld(pos); bool aboveThreshold = worldPos.y >= airThresholdY; bool isSmallAir = !aboveThreshold && IsSmallAirPlatform(levelTilemap, pos, MAX_PLATFORM_WIDTH); if (!aboveThreshold && !isSmallAir) continue; var tile = levelTilemap.GetTile(pos); fgTilemap.SetTile(pos, tile); fgTilemap.SetColliderType(pos, UnityEngine.Tilemaps.Tile.ColliderType.Sprite); levelTilemap.SetTile(pos, null); if (aboveThreshold) movedHigh++; else movedSmall++; } } if (fgTc != null) fgTc.ProcessTilemapChanges(); var lvlTc = levelGo.GetComponent(); if (lvlTc != null) lvlTc.ProcessTilemapChanges(); Debug.Log($"[GameOptimizer] AutoForeground moved={movedHigh + movedSmall} (high={movedHigh} small={movedSmall} threshold={airThresholdY:F2}) / Layer16 applied={applied} levelKept0={levelKept} excluded={excluded}"); } } } /// /// 작은 공중 발판 판별: 위·아래 인접 Tile이 모두 빈 공간 + 가로 연속 길이 maxWidth 이하. /// 일반 지면(10+ tile 가로) 잘못 분류 방지. /// static bool IsSmallAirPlatform(UnityEngine.Tilemaps.Tilemap tm, Vector3Int pos, int maxWidth) { if (tm.HasTile(pos + Vector3Int.up)) return false; if (tm.HasTile(pos + Vector3Int.down)) return false; int width = 1; for (int dx = 1; dx <= maxWidth; dx++) { if (!tm.HasTile(pos + new Vector3Int(dx, 0, 0))) break; width++; if (width > maxWidth) return false; } for (int dx = 1; dx <= maxWidth; dx++) { if (!tm.HasTile(pos + new Vector3Int(-dx, 0, 0))) break; width++; if (width > maxWidth) return false; } return width <= maxWidth; } } }