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})");
}
// BT59 — PD 명시 (2026-05-07): "BT49 정정 시점에서 Foreground에 충돌체크만 없애면 될거 같은데?"
// BT49 자동 분류 영역 회귀 (commit 9adfc64) + Foreground TilemapCollider 제거 (BT54 영역 결합).
// 결과:
// - Foreground Tilemap = 시각만 (TilemapRenderer + Layer 16). TilemapCollider 부착 X = 충돌 X
// - 자동 분류 영역(BT48 임계값+작은 발판)은 SetTile만 작동·ProcessTilemapChanges 호출은 fgTc null로 skip
// - Level Tilemap = 영구 충돌 (발판·지면 역할)
var foreground = GameObject.Find("Foreground");
UnityEngine.Tilemaps.Tilemap fgTilemap = null;
UnityEngine.Tilemaps.TilemapCollider2D fgTc = null;
if (foreground != null)
{
foreground.layer = 16;
// 기존 TilemapCollider2D 제거 (PD 명시 — Foreground 충돌 X)
var existingFgTc = foreground.GetComponent();
if (existingFgTc != null) Object.Destroy(existingFgTc);
fgTilemap = foreground.GetComponent();
// fgTc null 유지 = 사후 복원 영역(if fgTilemap!=null && fgTc!=null) 자동 skip
}
// BT60 — PD 의도 정합 정정 (2026-05-07 PD 보고: "발판 위에 올라갈 수 없게 되었어").
// BT48/BT49 자동 분류 영역 폐기 — Level → Foreground 이동(SetTile(null))이 Level 발판 Tile 제거 → 영구 충돌 X 유발.
// 결과 (PD 의도 정합):
// - Level Tilemap = Scene yaml 그대로 유지 (영구 충돌·발판·지면 역할)
// - Foreground Tilemap = 시각만 (TilemapRenderer + Layer 16·TilemapCollider X)
// Player가 Level 지면 위 착지 ✅ (영구 충돌) + Foreground 영역 자유 통과 ✅ (충돌 X)
//
// (변경 전 BT48/BT49 자동 분류 영역 코드는 git history `9adfc64`(BT49)·`92f102e`(BT59)에 보존)
// IsSmallAirPlatform 헬퍼는 사용 X (dead code) — 본 메서드 종료 후 헬퍼 제거 별도 정리 권고.
// 사용 변수 unused 컴파일 경고 회피
_ = fgTilemap;
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;
}
}
}