182 lines
3.9 KiB
C#
182 lines
3.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BitStrap;
|
|
using UnityEngine;
|
|
|
|
public class FishSpawnersGroupCollider : MonoBehaviour
|
|
{
|
|
public List<FishSpawner> spawners = new List<FishSpawner>();
|
|
|
|
private List<FishSpawner> spawnersToUse = new List<FishSpawner>();
|
|
|
|
[SerializeField]
|
|
private SphereCollider sphereCollider;
|
|
|
|
[SerializeField]
|
|
[Header("Spawners Grid")]
|
|
private int rows;
|
|
|
|
[SerializeField]
|
|
private float distanceX = 5f;
|
|
|
|
[SerializeField]
|
|
private float distanceY = 5f;
|
|
|
|
[SerializeField]
|
|
private float randomizer = 3f;
|
|
|
|
private void Start()
|
|
{
|
|
for (int i = 0; i < spawners.Count; i++)
|
|
{
|
|
if (spawners[i].isActiveAndEnabled)
|
|
{
|
|
spawners[i].spawnersGroup = this;
|
|
return;
|
|
}
|
|
}
|
|
if (sphereCollider == null)
|
|
{
|
|
GetSphereCollider();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider col)
|
|
{
|
|
DoOnTrigger(col);
|
|
}
|
|
|
|
public void DoOnTrigger(Collider col)
|
|
{
|
|
spawnersToUse.Clear();
|
|
for (int i = 0; i < spawners.Count; i++)
|
|
{
|
|
FishSpawner fishSpawner = spawners[i];
|
|
if (fishSpawner.isActiveAndEnabled)
|
|
{
|
|
if (col.gameObject.HasTag("PLAYER") && !fishSpawner.wasUsed && !GameController.Instance.spawnAllAtStart)
|
|
{
|
|
spawnersToUse.Add(fishSpawner);
|
|
}
|
|
else if (col.gameObject.HasTag("PLAYER") && fishSpawner.wasUsed)
|
|
{
|
|
fishSpawner.ActivateSpawner(true);
|
|
}
|
|
}
|
|
}
|
|
if (spawnersToUse.Count > 0)
|
|
{
|
|
StartCoroutine(SpawnAllFish((!GameController.Instance.newSpawnersDeactivateMethod) ? 1f : Random.Range(0.4f, 0.7f)));
|
|
}
|
|
}
|
|
|
|
public IEnumerator SpawnAllFish(float delay)
|
|
{
|
|
List<FishSpawner> spawnersToUseCopy = new List<FishSpawner>();
|
|
spawnersToUseCopy.AddRange(spawnersToUse);
|
|
for (int i = 0; i < spawnersToUseCopy.Count; i++)
|
|
{
|
|
FishSpawner fishSpawner = spawnersToUseCopy[i];
|
|
if (!fishSpawner.isActiveAndEnabled)
|
|
{
|
|
continue;
|
|
}
|
|
if (fishSpawner.wasUsed || fishSpawner.count <= 0)
|
|
{
|
|
break;
|
|
}
|
|
sphereCollider.enabled = false;
|
|
fishSpawner.wasUsed = true;
|
|
if (!FishSpawner.useJkmSpawn)
|
|
{
|
|
for (int k = 0; k < fishSpawner.count; k++)
|
|
{
|
|
Fish newFish = fishSpawner.SpawnFish();
|
|
newFish.Initialize();
|
|
yield return new WaitForSeconds(delay);
|
|
}
|
|
fishSpawner.CheckBehaviorDistanceQuick();
|
|
fishSpawner.CheckBehaviorDistance();
|
|
continue;
|
|
}
|
|
while (FishDelaySpawner.Instance.isLock)
|
|
{
|
|
yield return 0.01;
|
|
}
|
|
FishDelaySpawner.Instance.isLock = true;
|
|
for (int j = 0; j < fishSpawner.count; j++)
|
|
{
|
|
FishDelaySpawner.Instance.AddFish(fishSpawner);
|
|
}
|
|
FishDelaySpawner.Instance.isLock = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void FillSpawners()
|
|
{
|
|
spawners = GetComponentsInChildren<FishSpawner>().ToDynList();
|
|
}
|
|
|
|
[Button]
|
|
public void InitilizeSpawners()
|
|
{
|
|
for (int i = 0; i < spawners.Count; i++)
|
|
{
|
|
spawners[i].useColliderGroup = true;
|
|
spawners[i].GetComponent<SphereCollider>().enabled = false;
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void PlaceSpawnersInGrid()
|
|
{
|
|
int count = spawners.Count;
|
|
int num = count / rows;
|
|
Vector3 vector = new Vector3((float)(-num / 2) * distanceX, 0f, (float)(-num / 2) * distanceY);
|
|
Vector3 localPosition = vector;
|
|
int num2 = 0;
|
|
while (num2 < count)
|
|
{
|
|
int num3 = 0;
|
|
while (num3 < num)
|
|
{
|
|
spawners[num2].transform.localPosition = localPosition;
|
|
localPosition.x += distanceX;
|
|
num3++;
|
|
num2++;
|
|
}
|
|
localPosition.x = vector.x;
|
|
localPosition.z += distanceY;
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void RandomisePositions()
|
|
{
|
|
for (int i = 0; i < spawners.Count; i++)
|
|
{
|
|
Vector3 vector = new Vector3(Random.Range(0f - randomizer, randomizer), 0f, Random.Range(0f - randomizer, randomizer));
|
|
spawners[i].transform.localPosition += vector;
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void ShuffleList()
|
|
{
|
|
spawners.RandomizeList();
|
|
}
|
|
|
|
[Button]
|
|
public void GetSphereCollider()
|
|
{
|
|
sphereCollider = GetComponent<SphereCollider>();
|
|
}
|
|
|
|
public void SetShpereColliderEnabled(bool value)
|
|
{
|
|
sphereCollider.enabled = value;
|
|
}
|
|
}
|