116 lines
2.9 KiB
C#
116 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 = -3.5f;
|
|
public float maxX = 3.5f;
|
|
public float minY = -5f;
|
|
public float maxY = 4f;
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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(minX + margin, maxX - margin),
|
|
Random.Range(minY + margin, maxY - margin),
|
|
0.5f
|
|
);
|
|
}
|
|
}
|