Files
2026-05-31 20:55:36 +02:00

118 lines
2.9 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class ObjectSpawner : MonoBehaviour
{
public static ObjectSpawner Instance;
[SerializeField]
private List<GameObject> spawnedHoles = new List<GameObject>();
[Header("Prefaby")]
public GameObject[] objectPrefabs;
[Header("Počty")]
[Tooltip("Kolik předmětů spawnovat")]
public int objectCount = 3;
[Tooltip("Kolik děr spawnovat")]
public int holeCount = 1;
[Header("Pohyb děr")]
public bool holesMove = false;
public float holeMoveSpeed = 2f;
[Header("Spawn hranice (odpovídají kameře)")]
public float minX = 1f;
public float maxX = 1f;
public float minY = 0f;
public float maxY = 1f;
private List<GameObject> spawnedObjects = new List<GameObject>();
void Awake()
{
if (Instance == null) Instance = this;
else Destroy(gameObject);
}
void Start()
{
Spawn();
}
public void Spawn()
{
Clear();
// LevelManager na aktuální počet
if (LevelManager.Instance != null)
{
LevelManager.Instance.itemsToScore = objectCount;
LevelManager.Instance.ResetCounter();
}
//SpawnHoles();
SpawnObjects();
}
//void SpawnHoles()
//{
// for (int i = 0; i < holeCount; i++)
// {
// Vector2 pos = RandomPos(1f);
// GameObject hole = Instantiate(holePrefab, pos, Quaternion.identity, holeParent);
// Hole h = hole.GetComponent<Hole>();
// if (h != null && holesMove)
// {
// h.hasMovement = true;
// h.moveSpeed = holeMoveSpeed;
// h.moveRange = new Vector2(Random.Range(0.8f, 1.8f), 0f);
// }
// spawnedHoles.Add(hole);
// }
//}
public void SpawnObjects()
{
Debug.Log($"Spawning {objectCount} objects...");
for (int i = 0; i < objectCount; i++)
{
GameObject prefab = objectPrefabs[Random.Range(0, objectPrefabs.Length)];
Vector3 pos = RandomPos(0.5f);
GameObject obj = Instantiate(prefab, pos, Quaternion.identity);
// Náhodná barva
Renderer sr = obj.GetComponent<Renderer>();
if (sr != null)
sr.sharedMaterial.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.9f, 1f);
spawnedObjects.Add(obj);
}
Time.timeScale = 1f;
}
public void Clear()
{
foreach (var o in spawnedObjects) if (o != null) Destroy(o);
//foreach (var h in spawnedHoles) if (h != null) Destroy(h);
//spawnedObjects.Clear();
spawnedHoles.Clear();
}
Vector3 RandomPos(float margin)
{
return new Vector3(
Random.Range(-3, -4),
-0.3f,
Random.Range(-2f, 1.5f)
);
}
}