using UnityEngine;
using UnityEngine.Tilemaps;
namespace Platformer.Mechanics
{
///
/// 게임 시작 시 프레임·렌더·물리 영역 기본 최적화 + One-Way Platform 자동 적용.
/// PD 지시 2026-05-07 — 스크롤 버벅임 보완 + 점프·이동 시 지형 통과(One-Way Platform).
///
public static class GameOptimizer
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Init()
{
Application.targetFrameRate = 60;
QualitySettings.vSyncCount = 0;
Time.fixedDeltaTime = 1f / 60f;
// BT5-Dev #34 — Layer Matrix 영역 = Player(13) ↔ Enemy(14)만 OFF. JumpThrough Layer 8 영역 폐기.
// OneWay = PlatformEffector2D 영역 표준 패턴 활용 (Layer Matrix 폐기)
Physics2D.IgnoreLayerCollision(13, 14, true);
Debug.Log($"[BT34-LayerSep] Player(13) ↔ Enemy(14) collision OFF (Layer 8 영역 폐기·PlatformEffector2D 영역 활용)");
}
///
/// BT5-Dev #27 — PD 제안: Layer 8(JumpThrough) + Raycast 동적 충돌.
/// PlatformEffector2D 폐기. 모든 일반 BoxCollider2D를 Layer 8로 변환 → 기본 통과 + Player 발 Raycast 시점만 충돌 활성.
/// 제외: Tilemap·Player·Enemy·DeathZone·VictoryZone·TokenInstance·AttackHitbox·Trigger Collider.
///
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void SetupJumpThroughPlatforms()
{
// BT5-Dev #46 — PD 제안 채택: Level Tilemap = Layer 0 (일반 지면·벽 영구 충돌) / Foreground Tilemap = Layer 16 (공중 발판 Drop-Through·Tile m_ColliderType=Sprite 런타임 강제)
int applied = 0, excluded = 0, levelKept = 0;
var allColliders = Object.FindObjectsByType(FindObjectsSortMode.None);
var appliedNames = new System.Collections.Generic.List();
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;
}
// BT46 — Level Tilemap (TilemapCollider2D + name='Level') = Layer 0 (일반 지면·벽 영구 충돌)
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++;
if (appliedNames.Count < 8) appliedNames.Add($"{c.gameObject.name}({c.GetType().Name})");
}
// BT66 — PD 결정 R2 (2026-05-08): 자동 분류 결과 = 별도 GameObject (AutoForeground).
// PD Foreground GameObject = PD 직접 그린 가림막 시각만 (TilemapCollider 제거).
// AutoForeground GameObject (신규·Grid 자식) = 본 PM 자동 분류 발판 + TilemapCollider + Layer 16 + Drop-Through.
// 결과: PD 시각 의도 (가림막 영역) 분리 + 자동 분류 발판 영역 별도 + Drop-Through 패턴 작동.
// 1. PD Foreground = 가림막 시각만. 기존 TilemapCollider2D 제거 (충돌 X).
var pdForeground = GameObject.Find("Foreground");
if (pdForeground != null)
{
var pdFgTc = pdForeground.GetComponent();
if (pdFgTc != null) Object.Destroy(pdFgTc);
}
// 2. AutoForeground GameObject 신규 생성 (Grid 자식) — 자동 분류 발판 전용.
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;
// BT67 — PD 보고 (2026-05-08): "발판의 위치가 1만큼 y로 올라갔는데 왜 그런거지?"
// 원인: AutoForeground 신규 생성 시 transform default (0,0,0) ↔ PD가 Level/Foreground를 y=-1로 직접 변경
// 정정: 매 Play 시점 PD Foreground(또는 Level) localPosition와 동기화
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();
}
// BT48 — Level → Foreground 자동 분류 강화. 분류 조건 2종 결합:
// (a) 임계값 위 (worldY >= playerY + 1.5) = 공중 = 무조건 Foreground (BT47 호환)
// (b) 임계값 아래여도 위·아래 인접 Tile이 모두 빈 공간 + 가로 연속 길이 8 tile 이하 = 작은 공중 발판
// → 일반 지면(통상 10+ tile 가로) 잘못 분류 방지 + 첨부 이미지 같은 작은 발판 자동 분류
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; // Player 시작 Y + 1.5 위 = 공중 (BT47 호환)
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;
// BT63 — Tile asset의 m_ColliderType=None = 배경 의도 (tree·plant·fence·house) = 자동 분류 제외
// BT48 시점 + 나무 충돌 정정만 추가 (PD 명시 채택 2026-05-07).
// 결과: 나무·plant·fence·house Tile은 Level 잔존·자체 None Collider로 자연 통과.
var tileAsset = levelTilemap.GetTile(pos);
if (tileAsset != null && tileAsset.colliderType == UnityEngine.Tilemaps.Tile.ColliderType.None) 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($"[BT48-MoveTiles] moved={movedHigh + movedSmall} (high={movedHigh} thresholdY={airThresholdY:F2} / smallAir={movedSmall} maxWidth={MAX_PLATFORM_WIDTH})");
}
}
Debug.Log($"[BT48-DropThrough] Layer16 applied={applied} levelKeptLayer0={levelKept} excluded={excluded} total={allColliders.Length}");
Debug.Log($"[BT48-DropThrough] appliedSamples=[{string.Join(", ", appliedNames)}]");
}
///
/// BT48 — 작은 공중 발판 판별. 위·아래 인접 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;
}
}
}