161 lines
5.1 KiB
C#
161 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Alchemy.Inspector;
|
|
using Code.PassingObjects;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace Code.GameplayServices
|
|
{
|
|
// [OnListViewChanged(
|
|
// OnItemChanged = nameof(OnPassingObjectsPresetsChanged),
|
|
// OnItemsAdded = nameof(OnPassingObjectsPresetsAdded),
|
|
// OnItemsRemoved = nameof(OnPassingObjectsPresetsRemoved),
|
|
// OnSelectedIndicesChanged = nameof(OnPassingObjectsPresetsIndicesChanged),
|
|
// OnItemIndexChanged = nameof(OnPassingObjectsPresetsIndexChanged))
|
|
// ]
|
|
// public float[] array;
|
|
//
|
|
// void OnPassingObjectsPresetsChanged(int index, PassingObjectsPreset item)
|
|
// {
|
|
// Debug.Log($"Changed: [{index}] -> {item}");
|
|
// }
|
|
//
|
|
// void OnPassingObjectsPresetsAdded(IEnumerable<int> indices)
|
|
// {
|
|
// Debug.Log($"Added: [{string.Join(',', indices)}]");
|
|
// }
|
|
//
|
|
// void OnPassingObjectsPresetsRemoved(IEnumerable<int> indices)
|
|
// {
|
|
// Debug.Log($"Removed: [{string.Join(',', indices)}]");
|
|
// }
|
|
//
|
|
// void OnPassingObjectsPresetsIndicesChanged(IEnumerable<int> indices)
|
|
// {
|
|
// Debug.Log($"Selected: [{string.Join(',', indices)}]");
|
|
// }
|
|
//
|
|
// void OnPassingObjectsPresetsIndexChanged(int before, int after)
|
|
// {
|
|
// Debug.Log($"Index Changed: [{before} -> {after}]");
|
|
// }
|
|
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;
|
|
|
|
[OnListViewChanged(
|
|
OnItemChanged = nameof(OnPassingObjectsPresetsChanged),
|
|
OnItemsAdded = nameof(OnPassingObjectsPresetsAdded),
|
|
OnItemsRemoved = nameof(OnPassingObjectsPresetsRemoved),
|
|
OnSelectedIndicesChanged = nameof(OnPassingObjectsPresetsIndicesChanged),
|
|
OnItemIndexChanged = nameof(OnPassingObjectsPresetsIndexChanged))
|
|
]
|
|
[TabGroup("Gameplay")] [SerializeField]
|
|
private List<PassingObjectsPreset> obstaclesPresets;
|
|
|
|
void OnPassingObjectsPresetsChanged(int index, PassingObjectsPreset item)
|
|
{
|
|
float totalChance = 0f;
|
|
for (var i = 0; i < obstaclesPresets.Count; i++)
|
|
{
|
|
if (i == index) continue;
|
|
totalChance += obstaclesPresets[i].chance;
|
|
}
|
|
|
|
if(item.chance > 1 - totalChance) item.chance = 1 - totalChance;
|
|
}
|
|
|
|
void OnPassingObjectsPresetsAdded(IEnumerable<int> indices)
|
|
{
|
|
}
|
|
|
|
void OnPassingObjectsPresetsRemoved(IEnumerable<int> indices)
|
|
{
|
|
}
|
|
|
|
void OnPassingObjectsPresetsIndicesChanged(IEnumerable<int> indices)
|
|
{
|
|
}
|
|
|
|
void OnPassingObjectsPresetsIndexChanged(int before, int after)
|
|
{
|
|
}
|
|
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);
|
|
var chance = Random.Range(0,1f);
|
|
|
|
_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;
|
|
[Range(0,1f)]
|
|
public float chance;
|
|
}
|
|
} |