Files
RunRunRunP/Assets/Code/GameplayServices/PassingObjectsManager.cs
T
2026-05-04 12:45:12 +03:00

91 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using Alchemy.Inspector;
using Code.PassingObjects;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Code.GameplayServices
{
public class PassingObjectsManager : MonoBehaviour
{
[TabGroup("Spawn")] [SerializeField] private List<Transform> spawnPoints;
[TabGroup("Spawn")] [SerializeField] private List<GameObject> prefabs;
[TabGroup("Gameplay")] [SerializeField]
private float behindPlayerThreshold;
[TabGroup("Gameplay")] [SerializeField]
private float obstacleCooldown;
[TabGroup("Gameplay")] [SerializeField]
private List<PassingObjectsPreset> obstaclesPresets;
private List<GameObject> _spawnedObjects = new();
public float BehindPlayerThreshold => behindPlayerThreshold;
public static PassingObjectsManager Instance { get; private set; }
private void Awake()
{
Instance = this;
}
public void ResetObjects()
{
foreach (var spawnedObject in _spawnedObjects)
{
Destroy(spawnedObject);
}
_spawnedObjects.Clear();
_timer = 0f;
}
public void KillChild(PassingObject child)
{
if (!_spawnedObjects.Contains(child.gameObject)) return;
_spawnedObjects.Remove(child.gameObject);
Destroy(child.gameObject);
}
private float _timer;
private void Update()
{
if(GameplayManager.Instance.OnPause) return;
_timer += Time.deltaTime;
if (!(_timer > obstacleCooldown)) return;
_timer = 0f;
if (prefabs.Count == 0 && obstaclesPresets.Count > 0) return;
var presetId = Random.Range(0, obstaclesPresets.Count);
_spawnedObjects.Add(
Instantiate(
prefabs[obstaclesPresets[presetId].prefabId1],
spawnPoints[0].position,
spawnPoints[0].rotation));
_spawnedObjects.Add(
Instantiate(
prefabs[obstaclesPresets[presetId].prefabId2],
spawnPoints[1].position,
spawnPoints[1].rotation));
_spawnedObjects.Add(
Instantiate(
prefabs[obstaclesPresets[presetId].prefabId3],
spawnPoints[2].position,
spawnPoints[2].rotation));
}
}
[Serializable]
public class PassingObjectsPreset
{
public int prefabId1;
public int prefabId2;
public int prefabId3;
}
}