using System.Collections.Generic; using UnityEngine; namespace DanroJump.Gameplay { public sealed class PlatformController : MonoBehaviour, IPlatformRuntime, IPlatformLifecycle { [SerializeField] private List collectibleSpawnPoints = new(); [SerializeField] private List equipmentSpawnPoints = new(); [SerializeField] private List enemySpawnPoints = new(); public Transform Transform => transform; public IReadOnlyList CollectibleSpawnPoints => collectibleSpawnPoints; public IReadOnlyList EquipmentSpawnPoints => equipmentSpawnPoints; public IReadOnlyList EnemySpawnPoints => enemySpawnPoints; public int SpawnIndex { get; private set; } public bool IsStartPlatform { get; private set; } public float WorldY { get; private set; } private void Awake() { CollectSpawnPoints(); } private void OnValidate() { CollectSpawnPoints(); } public void Initialize(PlatformSpawnContext context) { ApplyContext(context); NotifyChildrenInitialize(context); } public void Recycle(PlatformSpawnContext context) { ApplyContext(context); NotifyChildrenRecycle(context); } public void ResetPlatform() { NotifyChildrenReset(); } private void ApplyContext(PlatformSpawnContext context) { SpawnIndex = context.SpawnIndex; IsStartPlatform = context.IsStartPlatform; WorldY = context.WorldY; } private void CollectSpawnPoints() { collectibleSpawnPoints.Clear(); equipmentSpawnPoints.Clear(); enemySpawnPoints.Clear(); GetComponentsInChildren(true, collectibleBuffer); foreach (var spawnPoint in collectibleBuffer) { if (spawnPoint == null) { continue; } GetSpawnPointList(spawnPoint.Kind).Add(spawnPoint); } collectibleBuffer.Clear(); } private List GetSpawnPointList(PlatformContentKind kind) { switch (kind) { case PlatformContentKind.Collectible: return collectibleSpawnPoints; case PlatformContentKind.Equipment: return equipmentSpawnPoints; case PlatformContentKind.Enemy: return enemySpawnPoints; default: return collectibleSpawnPoints; } } private void NotifyChildrenInitialize(PlatformSpawnContext context) { GetComponentsInChildren(true, lifecycleBuffer); foreach (var behaviour in lifecycleBuffer) { if (behaviour is IPlatformLifecycle lifecycle && !ReferenceEquals(lifecycle, this)) { lifecycle.Initialize(context); } } lifecycleBuffer.Clear(); } private void NotifyChildrenRecycle(PlatformSpawnContext context) { GetComponentsInChildren(true, lifecycleBuffer); foreach (var behaviour in lifecycleBuffer) { if (behaviour is IPlatformLifecycle lifecycle && !ReferenceEquals(lifecycle, this)) { lifecycle.Recycle(context); } } lifecycleBuffer.Clear(); } private void NotifyChildrenReset() { GetComponentsInChildren(true, lifecycleBuffer); foreach (var behaviour in lifecycleBuffer) { if (behaviour is IPlatformLifecycle lifecycle && !ReferenceEquals(lifecycle, this)) { lifecycle.ResetPlatform(); } } lifecycleBuffer.Clear(); } private readonly List collectibleBuffer = new(); private readonly List lifecycleBuffer = new(); } }