Squashed commit of the following:
commit7912604fafAuthor: AsanoRema <asanorema2036@gmail.com> Date: Tue Aug 11 17:22:28 2026 +0300 update .gitignore commitd71b90c8bdAuthor: henjkey <henjkey@gmail.com> Date: Tue Aug 11 18:42:09 2026 +0500 Edited Добавлен таймер, конфиг и публичный метод SpawnItems() CooldownRemaining - оставшееся время до спавна float CooldownSecondsRemaining - оставшееся время до спавна int commit5b888fba95Author: AsanoRema <asanorema2036@gmail.com> Date: Tue Aug 11 10:30:09 2026 +0300 Changed .gitignore commite3d5561fc8Author: henjkey <henjkey@gmail.com> Date: Tue Aug 11 10:47:35 2026 +0500 New Второй спавнер с нормальными настройками и обновленные префабы
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
@@ -6,77 +5,66 @@ using UnityEngine.InputSystem;
|
||||
|
||||
public class ItemPipe : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
private class GuaranteedItem
|
||||
{
|
||||
public GameObject prefab;
|
||||
|
||||
[Min(1)]
|
||||
public int amount = 1;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
private class RandomItem
|
||||
{
|
||||
public GameObject prefab;
|
||||
|
||||
[Min(0)]
|
||||
public float weight = 1f;
|
||||
}
|
||||
|
||||
[Header("Pipe")]
|
||||
[SerializeField] private Transform spawnPoint;
|
||||
[SerializeField, Min(0.05f)] private float spawnInterval = 0.5f;
|
||||
|
||||
[Header("Instant velocity change")]
|
||||
[SerializeField] private Vector3 localVelocityChange = Vector3.forward * 4.5f;
|
||||
|
||||
[Header("Guaranteed items")]
|
||||
[SerializeField] private List<GuaranteedItem> guaranteedItems = new();
|
||||
|
||||
[Header("Random items")]
|
||||
[SerializeField, Min(0)] private int randomItemCount = 5;
|
||||
[SerializeField] private List<RandomItem> randomItems = new();
|
||||
|
||||
[Header("Spawn position randomization")]
|
||||
[SerializeField, Min(0f)] private float randomSpawnX = 0.2f;
|
||||
[SerializeField, Min(0f)] private float randomSpawnZ = 0.2f;
|
||||
[Header("Configuration")]
|
||||
[SerializeField] private ItemPipeConfig config;
|
||||
|
||||
[Header("Temporary testing")]
|
||||
[SerializeField] private bool spawnOnE = true;
|
||||
|
||||
private bool isSpawning;
|
||||
|
||||
public float CooldownDuration => config != null ? config.SpawnCooldown : 0f;
|
||||
public float CooldownRemaining { get; private set; }
|
||||
public int CooldownSecondsRemaining => Mathf.CeilToInt(CooldownRemaining);
|
||||
public bool CanSpawnItems => config != null && !isSpawning && CooldownRemaining <= 0f;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!spawnOnE || isSpawning || Keyboard.current == null)
|
||||
if (CooldownRemaining > 0f)
|
||||
CooldownRemaining = Mathf.Max(0f, CooldownRemaining - Time.deltaTime);
|
||||
|
||||
if (!spawnOnE || Keyboard.current == null)
|
||||
return;
|
||||
|
||||
if (Keyboard.current.eKey.wasPressedThisFrame)
|
||||
StartCoroutine(SpawnSequence());
|
||||
SpawnItems();
|
||||
}
|
||||
|
||||
private IEnumerator SpawnSequence()
|
||||
public void SpawnItems()
|
||||
{
|
||||
if (!CanSpawnItems)
|
||||
return;
|
||||
|
||||
StartCoroutine(SpawnSequence(config));
|
||||
}
|
||||
|
||||
private IEnumerator SpawnSequence(ItemPipeConfig activeConfig)
|
||||
{
|
||||
isSpawning = true;
|
||||
|
||||
List<GameObject> spawnQueue = BuildSpawnQueue();
|
||||
List<GameObject> spawnQueue = BuildSpawnQueue(activeConfig);
|
||||
|
||||
foreach (GameObject prefab in spawnQueue)
|
||||
for (int i = 0; i < spawnQueue.Count; i++)
|
||||
{
|
||||
Spawn(prefab);
|
||||
yield return new WaitForSeconds(spawnInterval);
|
||||
Spawn(spawnQueue[i], activeConfig);
|
||||
|
||||
if (i < spawnQueue.Count - 1)
|
||||
yield return new WaitForSeconds(activeConfig.SpawnInterval);
|
||||
}
|
||||
|
||||
isSpawning = false;
|
||||
CooldownRemaining = activeConfig.SpawnCooldown;
|
||||
}
|
||||
|
||||
private List<GameObject> BuildSpawnQueue()
|
||||
private List<GameObject> BuildSpawnQueue(ItemPipeConfig activeConfig)
|
||||
{
|
||||
List<GameObject> queue = new();
|
||||
|
||||
// Добавляем предметы в точном количестве.
|
||||
foreach (GuaranteedItem item in guaranteedItems)
|
||||
foreach (ItemPipeConfig.GuaranteedItem item in activeConfig.GuaranteedItems)
|
||||
{
|
||||
if (item.prefab == null)
|
||||
continue;
|
||||
@@ -86,9 +74,9 @@ public class ItemPipe : MonoBehaviour
|
||||
}
|
||||
|
||||
// Выбираем указанное количество случайных предметов.
|
||||
for (int i = 0; i < randomItemCount; i++)
|
||||
for (int i = 0; i < activeConfig.RandomItemCount; i++)
|
||||
{
|
||||
GameObject selectedPrefab = ChooseWeightedRandom();
|
||||
GameObject selectedPrefab = ChooseWeightedRandom(activeConfig);
|
||||
|
||||
if (selectedPrefab != null)
|
||||
queue.Add(selectedPrefab);
|
||||
@@ -100,11 +88,11 @@ public class ItemPipe : MonoBehaviour
|
||||
return queue;
|
||||
}
|
||||
|
||||
private GameObject ChooseWeightedRandom()
|
||||
private GameObject ChooseWeightedRandom(ItemPipeConfig activeConfig)
|
||||
{
|
||||
float totalWeight = 0f;
|
||||
|
||||
foreach (RandomItem item in randomItems)
|
||||
foreach (ItemPipeConfig.RandomItem item in activeConfig.RandomItems)
|
||||
{
|
||||
if (item.prefab != null && item.weight > 0f)
|
||||
totalWeight += item.weight;
|
||||
@@ -113,10 +101,10 @@ public class ItemPipe : MonoBehaviour
|
||||
if (totalWeight <= 0f)
|
||||
return null;
|
||||
|
||||
float roll = UnityEngine.Random.value * totalWeight;
|
||||
float roll = Random.value * totalWeight;
|
||||
float currentWeight = 0f;
|
||||
|
||||
foreach (RandomItem item in randomItems)
|
||||
foreach (ItemPipeConfig.RandomItem item in activeConfig.RandomItems)
|
||||
{
|
||||
if (item.prefab == null || item.weight <= 0f)
|
||||
continue;
|
||||
@@ -134,20 +122,20 @@ public class ItemPipe : MonoBehaviour
|
||||
{
|
||||
for (int i = queue.Count - 1; i > 0; i--)
|
||||
{
|
||||
int randomIndex = UnityEngine.Random.Range(0, i + 1);
|
||||
int randomIndex = Random.Range(0, i + 1);
|
||||
|
||||
(queue[i], queue[randomIndex]) =
|
||||
(queue[randomIndex], queue[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void Spawn(GameObject prefab)
|
||||
private void Spawn(GameObject prefab, ItemPipeConfig activeConfig)
|
||||
{
|
||||
if (prefab == null || spawnPoint == null)
|
||||
return;
|
||||
|
||||
float offsetX = RandomRange(-randomSpawnX, randomSpawnX);
|
||||
float offsetZ = RandomRange(-randomSpawnZ, randomSpawnZ);
|
||||
float offsetX = RandomRange(-activeConfig.RandomSpawnX, activeConfig.RandomSpawnX);
|
||||
float offsetZ = RandomRange(-activeConfig.RandomSpawnZ, activeConfig.RandomSpawnZ);
|
||||
|
||||
Vector3 spawnPosition =
|
||||
spawnPoint.position +
|
||||
@@ -157,13 +145,13 @@ public class ItemPipe : MonoBehaviour
|
||||
GameObject item = Instantiate(
|
||||
prefab,
|
||||
spawnPosition,
|
||||
UnityEngine.Random.rotationUniform
|
||||
Random.rotationUniform
|
||||
);
|
||||
|
||||
if (item.TryGetComponent(out Rigidbody body))
|
||||
{
|
||||
Vector3 worldVelocityChange =
|
||||
spawnPoint.TransformDirection(localVelocityChange);
|
||||
spawnPoint.TransformDirection(activeConfig.LocalVelocityChange);
|
||||
|
||||
body.AddForce(worldVelocityChange, ForceMode.VelocityChange);
|
||||
}
|
||||
@@ -171,6 +159,6 @@ public class ItemPipe : MonoBehaviour
|
||||
|
||||
private float RandomRange(float min, float max)
|
||||
{
|
||||
return UnityEngine.Random.Range(min, max);
|
||||
return Random.Range(min, max);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user