Spawner added
This commit is contained in:
175
Assets/AI/Summons/SummonerSpawner.cs
Normal file
175
Assets/AI/Summons/SummonerSpawner.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class SummonerSpawner : MonoBehaviour
|
||||
{
|
||||
[Header("Objects to spawn")]
|
||||
[SerializeField] private GameObject[] spawnableObjects;
|
||||
|
||||
[Header("Spawner config")]
|
||||
[SerializeField] private int objectCount = 8;
|
||||
|
||||
[SerializeField] private float spawnRadius = 5f;
|
||||
[SerializeField] private bool lookAtCenter = true;
|
||||
|
||||
[Header("Spawn timing")]
|
||||
[SerializeField] private float delayBeforeSpawn = 1f;
|
||||
|
||||
[SerializeField] private float timeBetweenSpawns = 0.2f;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool drawGizmos = true;
|
||||
|
||||
private int currentObjectIndex = 0;
|
||||
private List<GameObject> spawnedObjects = new List<GameObject>();
|
||||
private bool isSpawning = false;
|
||||
|
||||
public void SelectNextObject()
|
||||
{
|
||||
if (spawnableObjects.Length == 0) return;
|
||||
if (isSpawning) return;
|
||||
|
||||
currentObjectIndex = (currentObjectIndex + 1) % spawnableObjects.Length;
|
||||
Debug.Log($"Wybrano obiekt: {spawnableObjects[currentObjectIndex].name} (Index: {currentObjectIndex})");
|
||||
}
|
||||
|
||||
public void SelectPreviousObject()
|
||||
{
|
||||
if (spawnableObjects.Length == 0) return;
|
||||
if (isSpawning) return;
|
||||
|
||||
currentObjectIndex--;
|
||||
if (currentObjectIndex < 0)
|
||||
{
|
||||
currentObjectIndex = spawnableObjects.Length - 1;
|
||||
}
|
||||
|
||||
Debug.Log($"Wybrano obiekt: {spawnableObjects[currentObjectIndex].name} (Index: {currentObjectIndex})");
|
||||
}
|
||||
|
||||
public void ResetSpawn()
|
||||
{
|
||||
if (isSpawning)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
isSpawning = false;
|
||||
}
|
||||
|
||||
foreach (GameObject obj in spawnedObjects)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
Destroy(obj);
|
||||
}
|
||||
}
|
||||
|
||||
spawnedObjects.Clear();
|
||||
Debug.Log("Spawn zresetowany - usuniêto wszystkie obiekty");
|
||||
|
||||
StartCoroutine(SpawnObjectsCoroutine());
|
||||
}
|
||||
|
||||
private IEnumerator SpawnObjectsCoroutine()
|
||||
{
|
||||
if (spawnableObjects.Length == 0)
|
||||
{
|
||||
Debug.LogWarning("Brak obiektów do spawnowania!");
|
||||
yield break;
|
||||
}
|
||||
|
||||
GameObject prefabToSpawn = spawnableObjects[currentObjectIndex];
|
||||
|
||||
if (prefabToSpawn == null)
|
||||
{
|
||||
Debug.LogWarning("Wybrany obiekt jest null!");
|
||||
yield break;
|
||||
}
|
||||
|
||||
isSpawning = true;
|
||||
|
||||
yield return new WaitForSeconds(delayBeforeSpawn);
|
||||
|
||||
for (int i = 0; i < objectCount; i++)
|
||||
{
|
||||
float angle = Random.Range(0f, 360f) * Mathf.Deg2Rad;
|
||||
|
||||
float x = transform.position.x + spawnRadius * Mathf.Cos(angle);
|
||||
float z = transform.position.z + spawnRadius * Mathf.Sin(angle);
|
||||
Vector3 spawnPosition = new Vector3(x, transform.position.y, z);
|
||||
|
||||
GameObject spawnedObj = Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity);
|
||||
|
||||
if (lookAtCenter)
|
||||
{
|
||||
spawnedObj.transform.LookAt(transform.position);
|
||||
}
|
||||
|
||||
spawnedObjects.Add(spawnedObj);
|
||||
|
||||
if (i < objectCount - 1)
|
||||
{
|
||||
yield return new WaitForSeconds(timeBetweenSpawns);
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log($"Spawnowano {objectCount} obiektów typu: {prefabToSpawn.name}");
|
||||
isSpawning = false;
|
||||
}
|
||||
|
||||
public void SetSpawnRadius(float newRadius)
|
||||
{
|
||||
spawnRadius = Mathf.Max(0.1f, newRadius);
|
||||
Debug.Log($"Nowy promieñ spawnu: {spawnRadius}");
|
||||
}
|
||||
|
||||
public void SetObjectCount(int count)
|
||||
{
|
||||
objectCount = Mathf.Max(1, count);
|
||||
Debug.Log($"Nowa liczba obiektów: {objectCount}");
|
||||
}
|
||||
|
||||
public void SetObjectIndex(int index)
|
||||
{
|
||||
if (index >= 0 && index < spawnableObjects.Length)
|
||||
{
|
||||
currentObjectIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDelayBeforeSpawn(float delay)
|
||||
{
|
||||
delayBeforeSpawn = Mathf.Max(0f, delay);
|
||||
}
|
||||
|
||||
public void SetTimeBetweenSpawns(float time)
|
||||
{
|
||||
timeBetweenSpawns = Mathf.Max(0f, time);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (!drawGizmos) return;
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
|
||||
float angleStep = 360f / Mathf.Max(1, objectCount);
|
||||
Vector3 previousPoint = transform.position + new Vector3(spawnRadius, 0, 0);
|
||||
|
||||
for (int i = 1; i <= objectCount; i++)
|
||||
{
|
||||
float angle = i * angleStep * Mathf.Deg2Rad;
|
||||
float x = transform.position.x + spawnRadius * Mathf.Cos(angle);
|
||||
float z = transform.position.z + spawnRadius * Mathf.Sin(angle);
|
||||
Vector3 newPoint = new Vector3(x, transform.position.y, z);
|
||||
|
||||
Gizmos.DrawLine(previousPoint, newPoint);
|
||||
Gizmos.DrawWireSphere(newPoint, 0.2f);
|
||||
|
||||
previousPoint = newPoint;
|
||||
}
|
||||
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, 0.3f);
|
||||
}
|
||||
}
|
||||
2
Assets/AI/Summons/SummonerSpawner.cs.meta
Normal file
2
Assets/AI/Summons/SummonerSpawner.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6113015e377161a4d992fd48dd08f887
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user