using UnityEngine; using Platformer.Mechanics; using EerieVillage.Skills; namespace EerieVillage.Skills.Effectors { /// /// 투사체 생성기. IEffector 구현체. /// SkillFireEvent.Execute 에서 ActiveCategory.Projectile 분기 시 호출. /// BT12-Dev Phase 2-B §4-4. /// /// 다중 발사: PlayerStats.ExtraProjectiles 반영 (P08 투사체증폭). /// 궤적 분기: ActiveSkillData.Trajectory — Line → Projectile, Homing → HomingProjectile. /// public class ProjectileSpawner : IEffector { public void Trigger(ActiveSkillRuntime runtime, PlayerSkillInventory inventory) { var data = runtime.ActiveData; // 플레이어 위치·방향 취득 Transform playerTransform = inventory.transform; Vector2 spawnPos = playerTransform.position; // PlayerController.Facing 참조 Vector2 facing = Vector2.right; var pc = inventory.GetComponent(); if (pc != null) facing = pc.Facing; // 프리팹 로드 (없으면 fallback — Trigger 영역에서 직접 생성) GameObject prefab = LoadProjectilePrefab(); // 다중 발사 수 (기본 1 + ExtraProjectiles) int count = 1 + Mathf.Max(0, inventory.Stats.ExtraProjectiles); float angleStep = count > 1 ? 10f : 0f; float startAngle = -angleStep * (count - 1) * 0.5f; for (int i = 0; i < count; i++) { float angle = startAngle + angleStep * i; Vector2 dir = RotateVector(facing, angle); // BT12-Dev 2026-05-09 잔존 투사체 fix: // prefab 부재 시 Scene GameObject를 prefab으로 Instantiate하면 원본 Scene GO가 잔존. // → fallback 영역 매번 새 GameObject 직접 생성 (Instantiate X·자기 자신 발사체). GameObject go = prefab != null ? Object.Instantiate(prefab, (Vector3)spawnPos, Quaternion.identity) : CreateFallbackProjectile(data, (Vector3)spawnPos); Projectile proj; if (data.Trajectory == ProjectileTrajectory.Homing) proj = go.GetComponent() ?? go.AddComponent(); else proj = go.GetComponent() ?? go.AddComponent(); proj.Initialize(runtime, inventory, dir); } } /// /// Resources/Skills/Projectiles/Default prefab 로드. 부재 시 null. /// Phase 2-C 에서 data.projectilePrefab 필드를 추가하면 해당 경로 우선 사용 가능. /// private static GameObject LoadProjectilePrefab() { return Resources.Load("Skills/Projectiles/Default"); } /// /// fallback 발사체 GameObject 직접 생성. Instantiate 영역 X — 자기 자신 발사체. /// BT12-Dev 2026-05-09 — 이전 LoadProjectilePrefab fallback 영역에서 분리. /// 원본 Scene GameObject 영구 잔존 버그 근본 해결 (PD 보고 — 맵에 투사체 영구 잔존). /// PD 지시 2026-05-09 시각화 — 16×16 RGBA32 동적 알파 원 Texture2D + 속성별 색상. /// private static GameObject CreateFallbackProjectile(ActiveSkillData data, Vector3 spawnPos) { var go = new GameObject($"Projectile_{data.CardId}"); go.transform.position = spawnPos; var col = go.AddComponent(); col.isTrigger = true; col.radius = 0.2f; // BT12-Dev 2026-05-09 — Trigger 판정 안정성 보강: // Unity 2D OnTriggerEnter2D 발화는 양쪽 중 한쪽 Rigidbody2D 영역 의무. // transform.position 이동 영역 Physics2D 동기화 안정성 확보 위해 Kinematic Rigidbody2D 부착. var rb = go.AddComponent(); rb.bodyType = RigidbodyType2D.Kinematic; rb.gravityScale = 0f; rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous; var sr = go.AddComponent(); sr.sprite = GetOrCreateFallbackSprite(); sr.color = GetColorByAttribute(data.AttributeTags); sr.sortingOrder = 50; // Player·Enemy 위 // 0.4 unit 직경 정도의 작은 원 go.transform.localScale = new Vector3(0.4f, 0.4f, 1f); return go; } // 캐시: Sprite 매번 생성하지 않도록 정적 보존 static Sprite _fallbackSprite; static Sprite GetOrCreateFallbackSprite() { if (_fallbackSprite != null) return _fallbackSprite; // 16×16 원형 알파 텍스처 (둥근 형태) const int size = 16; var tex = new Texture2D(size, size, TextureFormat.RGBA32, false); tex.wrapMode = TextureWrapMode.Clamp; tex.filterMode = FilterMode.Bilinear; float r = size * 0.5f; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { float dx = x - r + 0.5f; float dy = y - r + 0.5f; float dist = Mathf.Sqrt(dx * dx + dy * dy); float alpha = Mathf.Clamp01(1f - (dist - (r - 1.5f))); tex.SetPixel(x, y, new Color(1f, 1f, 1f, alpha)); } } tex.Apply(); _fallbackSprite = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), size); return _fallbackSprite; } static Color GetColorByAttribute(AttributeTag attr) { // 속성별 색상 (시각 구분 영역) if ((attr & AttributeTag.Fire) != 0) return new Color(1f, 0.5f, 0.2f); if ((attr & AttributeTag.Frost) != 0) return new Color(0.5f, 0.85f, 1f); if ((attr & AttributeTag.Dark) != 0) return new Color(0.6f, 0.3f, 0.85f); if ((attr & AttributeTag.Lightning) != 0) return new Color(1f, 1f, 0.4f); if ((attr & AttributeTag.Physical) != 0) return new Color(0.95f, 0.95f, 0.95f); return Color.white; } private static Vector2 RotateVector(Vector2 v, float degrees) { float rad = degrees * Mathf.Deg2Rad; float cos = Mathf.Cos(rad); float sin = Mathf.Sin(rad); return new Vector2(v.x * cos - v.y * sin, v.x * sin + v.y * cos); } } }