113 lines
5.3 KiB
C#
113 lines
5.3 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace Platformer.Mechanics
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 무한 스크롤 적 생성기(리뉴얼 2026-11-10 · PD 지시 「무한 스크롤하는 동안 적은 항상 새롭게 스크롤되는 화면에 랜덤으로 등장」).
|
||
|
|
/// <para>
|
||
|
|
/// 카메라 오른쪽 끝이 다음 생성 지점에 닿으면 그 지점(화면 밖 바로 오른쪽)에 적 프리팹을 만들고,
|
||
|
|
/// 다음 지점은 <see cref="minGap"/>~<see cref="maxGap"/> 사이 무작위로 정한다. 종류는 프리팹의 MonsterRandomizer 가 무작위로 고른다.
|
||
|
|
/// 생성된 적은 러너 모드(제자리 · 캐릭터 발 높이 · 맞으면 밀려남)이며, 화면 왼쪽 뒤로 지나가면 지운다.
|
||
|
|
/// </para>
|
||
|
|
/// <para>체력: 몬스터마다 무작위 <see cref="minHits"/>~<see cref="maxHits"/> 회 공격으로 처치(PD 2026-11-10 「임시로 1~3회」).</para>
|
||
|
|
/// </summary>
|
||
|
|
public class EndlessEnemySpawner : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Tooltip("생성할 적 프리팹 목록(종류별) — 매번 무작위로 하나를 고른다.")]
|
||
|
|
public GameObject[] enemyPrefabs;
|
||
|
|
[Tooltip("적 사이 최소 간격(월드).")]
|
||
|
|
public float minGap = 5f;
|
||
|
|
[Tooltip("적 사이 최대 간격(월드).")]
|
||
|
|
public float maxGap = 10f;
|
||
|
|
[Tooltip("첫 적이 나타날 거리(플레이어 시작 위치 기준 · 월드).")]
|
||
|
|
public float firstSpawnDistance = 12f;
|
||
|
|
[Tooltip("화면 오른쪽 끝에서 이만큼 바깥에 만든다(월드) — 스크롤되며 화면에 들어온다.")]
|
||
|
|
public float spawnOutsideMargin = 1.5f;
|
||
|
|
[Tooltip("화면 왼쪽 끝에서 이만큼 뒤로 지나간 적은 지운다(월드).")]
|
||
|
|
public float despawnBehindMargin = 4f;
|
||
|
|
[Header("임시 체력 (PD 2026-11-10 「임시로 몬스터마다 랜덤 1~3회 공격으로 처치」)")]
|
||
|
|
public int minHits = 1;
|
||
|
|
public int maxHits = 3;
|
||
|
|
|
||
|
|
Camera _cam;
|
||
|
|
PlayerController _player;
|
||
|
|
float _nextSpawnX;
|
||
|
|
float _feetY;
|
||
|
|
bool _feetYReady;
|
||
|
|
Transform _staging; // 꺼진 보관 자리: Awake 전에 설정을 넣기 위해 여기서 만든 뒤 꺼낸다
|
||
|
|
readonly List<EnemyController> _alive = new List<EnemyController>();
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
_cam = Camera.main;
|
||
|
|
_player = FindFirstObjectByType<PlayerController>();
|
||
|
|
if (_cam == null || _player == null || enemyPrefabs == null || enemyPrefabs.Length == 0)
|
||
|
|
{
|
||
|
|
Debug.LogWarning("[EndlessEnemySpawner] 카메라·플레이어·적 프리팹 중 없음 — 꺼짐.");
|
||
|
|
enabled = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
var st = new GameObject("_SpawnStaging");
|
||
|
|
st.SetActive(false);
|
||
|
|
st.transform.SetParent(transform, false);
|
||
|
|
_staging = st.transform;
|
||
|
|
_nextSpawnX = _player.transform.position.x + firstSpawnDistance;
|
||
|
|
}
|
||
|
|
|
||
|
|
void LateUpdate()
|
||
|
|
{
|
||
|
|
// 캐릭터 발 높이 = 땅에 선 플레이어 콜라이더 바닥(새 캐릭터 그림의 발 = 콜라이더 바닥)
|
||
|
|
if (_player.IsGrounded)
|
||
|
|
{
|
||
|
|
_feetY = _player.Bounds.min.y;
|
||
|
|
_feetYReady = true;
|
||
|
|
}
|
||
|
|
if (!_feetYReady) return;
|
||
|
|
|
||
|
|
float halfW = _cam.orthographicSize * _cam.aspect;
|
||
|
|
float camRight = _cam.transform.position.x + halfW;
|
||
|
|
float camLeft = _cam.transform.position.x - halfW;
|
||
|
|
|
||
|
|
// 다음 생성 지점이 화면 오른쪽 끝 바깥 여유 안으로 들어오면 만든다(한 프레임에 여러 마리 가능)
|
||
|
|
int guard = 0;
|
||
|
|
while (camRight + spawnOutsideMargin >= _nextSpawnX && guard++ < 8)
|
||
|
|
{
|
||
|
|
Spawn(Mathf.Max(_nextSpawnX, camRight + spawnOutsideMargin));
|
||
|
|
_nextSpawnX += Random.Range(minGap, maxGap);
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = _alive.Count - 1; i >= 0; i--)
|
||
|
|
{
|
||
|
|
var e = _alive[i];
|
||
|
|
if (e == null) { _alive.RemoveAt(i); continue; }
|
||
|
|
if (e.transform.position.x < camLeft - despawnBehindMargin)
|
||
|
|
{
|
||
|
|
Destroy(e.gameObject);
|
||
|
|
_alive.RemoveAt(i);
|
||
|
|
}
|
||
|
|
else e.runnerFeetY = _feetY;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Spawn(float x)
|
||
|
|
{
|
||
|
|
var prefab = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
|
||
|
|
var go = Instantiate(prefab, new Vector3(x, _feetY + 1f, 0f), Quaternion.identity, _staging);
|
||
|
|
var ec = go.GetComponent<EnemyController>();
|
||
|
|
ec.runnerMode = true;
|
||
|
|
ec.runnerFeetY = _feetY;
|
||
|
|
ec.flyingYOffset = 0f; // 날으는 몬스터도 같은 높이(PD 「몬스터 y = 캐릭터 y」)
|
||
|
|
var h = go.GetComponent<Health>();
|
||
|
|
if (h != null)
|
||
|
|
{
|
||
|
|
int dmg = _player.attackHitbox != null ? Mathf.Max(1, _player.attackHitbox.damage) : 1;
|
||
|
|
int hits = Random.Range(minHits, maxHits + 1);
|
||
|
|
h.RandomMaxHPRange = new Vector2Int(hits * dmg, hits * dmg); // Awake 에서 maxHP 로 반영
|
||
|
|
}
|
||
|
|
go.transform.SetParent(null, true); // 켜진 곳으로 꺼냄 → 여기서 Awake(종류 무작위 · 체력) 실행
|
||
|
|
_alive.Add(ec);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|