EerieVillage/Assets/Scripts/Mechanics/GameOptimizer.cs

172 lines
10 KiB
C#

using UnityEngine;
using UnityEngine.Tilemaps;
namespace Platformer.Mechanics
{
/// <summary>
/// 게임 시작 시 프레임·렌더·물리 영역 기본 최적화 + One-Way Platform 자동 적용.
/// PD 지시 2026-05-07 — 스크롤 버벅임 보완 + 점프·이동 시 지형 통과(One-Way Platform).
/// </summary>
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 영역 활용)");
}
/// <summary>
/// BT5-Dev #27 — PD 제안: Layer 8(JumpThrough) + Raycast 동적 충돌.
/// PlatformEffector2D 폐기. 모든 일반 BoxCollider2D를 Layer 8로 변환 → 기본 통과 + Player 발 Raycast 시점만 충돌 활성.
/// 제외: Tilemap·Player·Enemy·DeathZone·VictoryZone·TokenInstance·AttackHitbox·Trigger Collider.
/// </summary>
[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<Collider2D>(FindObjectsSortMode.None);
var appliedNames = new System.Collections.Generic.List<string>();
foreach (var c in allColliders)
{
if (c == null) continue;
if (c.isTrigger) { excluded++; continue; }
if (c.GetComponent<PlayerController>() != null
|| c.GetComponent<EnemyController>() != null
|| c.GetComponent<DeathZone>() != null
|| c.GetComponent<TokenInstance>() != null
|| c.GetComponent<VictoryZone>() != null
|| c.GetComponent<AttackHitbox>() != null)
{
excluded++;
continue;
}
// BT46 — Level Tilemap (TilemapCollider2D + name='Level') = Layer 0 (일반 지면·벽 영구 충돌)
if (c.GetComponent<UnityEngine.Tilemaps.TilemapCollider2D>() != null && c.gameObject.name == "Level")
{
if (c.gameObject.layer == 16) c.gameObject.layer = 0; // 잔존 복원
var lvlEffector = c.GetComponent<PlatformEffector2D>();
if (lvlEffector != null) Object.Destroy(lvlEffector);
c.usedByEffector = false;
levelKept++;
continue;
}
var effector = c.GetComponent<PlatformEffector2D>();
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})");
}
// BT54 — PD 제안 채택 (2026-05-07): "그냥 단순히 Foreground에 충돌체크만 없애면 되지 않을까?"
// Foreground Tilemap = 시각만 (TilemapRenderer + Layer 16). TilemapCollider2D 자동 부착 폐기.
// 영향: 모든 Foreground Tile 충돌 X = 발판 위 착지 X (Drop-Through 패턴 폐기·시각만 표시).
// 자동 분류 영역(§83~) 및 사후 복원 영역(§129~)은 fgTilemap null/fgTc null 시 자동 skip.
// PD 시각 검증 후 발판 위 착지 보존 의도 시 BT54 회귀 또는 카탈로그 v1.2 전환.
var foreground = GameObject.Find("Foreground");
UnityEngine.Tilemaps.Tilemap fgTilemap = null;
UnityEngine.Tilemaps.TilemapCollider2D fgTc = null;
if (foreground != null)
{
foreground.layer = 16;
// 기존 TilemapCollider2D 제거 (PD 제안 — 충돌 X)
var existingFgTc = foreground.GetComponent<UnityEngine.Tilemaps.TilemapCollider2D>();
if (existingFgTc != null) Object.Destroy(existingFgTc);
// fgTilemap·fgTc null 유지 = 자동 분류 + 사후 복원 영역 자동 skip
}
// BT53-A1 — 자동 분류 카탈로그 확장 (PD 옵션 A1 채택 2026-05-07).
// BT52 TileFloating* 단일 매칭 → 8종 카탈로그 확장.
// 근거: BT47/BT48 정상 시점(moved=1389)에 PD가 통과 가능 인식한 영역
// = 배경·건물 7종이 자동 Foreground 이동된 결과로 추정.
// 카탈로그 (자동 Foreground 이동):
// 1. TileFloating* (3종) — 공중 발판
// 2. ShortBuilding·TallBuilding·MidgroundFiller (3종) — 건물·배경 채움
// 3. cloud·hillside·midground·mountains (4종) — 배경 (Parallax)
// Level 잔존 (영구 충돌 또는 자연 통과):
// - TileGround·TileGroundDark·TileGroundTop (3종) — 지면 (영구 충돌)
// - tree·plant·fence·house (4종 None) — Tile 자체 None Collider (자연 통과)
// 마이그레이션 X (PD 옵션 A 결정 — Foreground 변경 X·위험 최소화).
var levelGo = GameObject.Find("Level");
if (levelGo != null && fgTilemap != null)
{
var levelTilemap = levelGo.GetComponent<UnityEngine.Tilemaps.Tilemap>();
if (levelTilemap != null)
{
var bounds = levelTilemap.cellBounds;
int movedFloating = 0, movedDecor = 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<UnityEngine.Tilemaps.Tile>(pos);
if (tileAsset == null) continue;
string nm = tileAsset.name ?? string.Empty;
bool isFloating = nm.StartsWith("TileFloating");
bool isDecor = nm == "ShortBuilding" || nm == "TallBuilding" || nm == "MidgroundFiller"
|| nm == "cloud" || nm == "hillside" || nm == "midground" || nm == "mountains";
if (!isFloating && !isDecor) continue;
var tile = levelTilemap.GetTile(pos);
fgTilemap.SetTile(pos, tile);
fgTilemap.SetColliderType(pos, UnityEngine.Tilemaps.Tile.ColliderType.Sprite);
levelTilemap.SetTile(pos, null);
if (isFloating) movedFloating++;
else movedDecor++;
}
}
if (fgTc != null) fgTc.ProcessTilemapChanges();
var lvlTc = levelGo.GetComponent<UnityEngine.Tilemaps.TilemapCollider2D>();
if (lvlTc != null) lvlTc.ProcessTilemapChanges();
Debug.Log($"[BT53-Classify] moved={movedFloating + movedDecor} (floating={movedFloating} TileFloating* / decor={movedDecor} 배경·건물 7종 / TileGround*+None 잔존)");
}
}
// BT49 — Foreground Tilemap 자체에 이미 그려진 배경 Tile (예: tree·cloud — Tile asset colliderType=None) ColliderType=None 복원
// BT47이 SetColliderType(Sprite) 무차별 강제 → 배경 Tile도 발판처럼 충돌 → Tile asset metadata 존중으로 정정
// BT51 — 사후 복원: None Tile만 SetColliderType(None) 복원 (배경 통과 보장).
// BT50의 Grid→Sprite 강제는 폐기 (PD가 직접 Foreground에 그린 Grid Tile의 default Collider 형상 보존).
// 이동 시 Grid→Sprite 강제(상단)는 유지 — Level→Foreground 이동되는 신규 Tile만 Sprite Collider 적용.
if (fgTilemap != null && fgTc != null)
{
int restoredNone = 0;
var fgBounds = fgTilemap.cellBounds;
for (int x = fgBounds.xMin; x <= fgBounds.xMax; x++)
{
for (int y = fgBounds.yMin; y <= fgBounds.yMax; y++)
{
var pos = new Vector3Int(x, y, 0);
if (!fgTilemap.HasTile(pos)) continue;
var fgTileAsset = fgTilemap.GetTile<UnityEngine.Tilemaps.Tile>(pos);
if (fgTileAsset == null) continue;
if (fgTileAsset.colliderType == UnityEngine.Tilemaps.Tile.ColliderType.None)
{
fgTilemap.SetColliderType(pos, UnityEngine.Tilemaps.Tile.ColliderType.None);
restoredNone++;
}
}
}
if (restoredNone > 0) fgTc.ProcessTilemapChanges();
Debug.Log($"[BT51-FgRefine] colliderType=None restored: {restoredNone} (배경 통과·Grid Tile은 default Collider 유지)");
}
Debug.Log($"[BT48-DropThrough] Layer16 applied={applied} levelKeptLayer0={levelKept} excluded={excluded} total={allColliders.Length}");
Debug.Log($"[BT48-DropThrough] appliedSamples=[{string.Join(", ", appliedNames)}]");
}
// BT52-A — IsSmallAirPlatform 헬퍼 폐기 (BT48 휴리스틱 알고리즘 폐기에 따른 dead code 제거).
// 새 알고리즘은 Tile asset 이름 매칭만 사용 — 헬퍼 불요.
}
}