112 lines
2.8 KiB
C#
112 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using Assets.Code.Scripts;
|
|
using UFS3.Management;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(SpherePointSampler))]
|
|
public class FishZoneObject : MonoBehaviour
|
|
{
|
|
public LayerMask terrainMask;
|
|
|
|
public LayerMask objectMask;
|
|
|
|
public float radius = 20f;
|
|
|
|
public int maxFishes;
|
|
|
|
public FishSystemManager fishSystemManager;
|
|
|
|
public FishOnMapPreset fishPreset;
|
|
|
|
[Tooltip("How deep is terrain under zone")]
|
|
[SerializeField]
|
|
private List<FishController> fishControllers;
|
|
|
|
private SpherePointSampler _sphereSampler;
|
|
|
|
private bool _isInitialized;
|
|
|
|
private Pool<FishController> fishControllerPool;
|
|
|
|
public List<FishController> FishControllers => fishControllers;
|
|
|
|
private void Awake()
|
|
{
|
|
_sphereSampler = GetComponent<SpherePointSampler>();
|
|
_sphereSampler.terrainMask = terrainMask;
|
|
_sphereSampler.objectMask = objectMask;
|
|
_sphereSampler.OnInitialized += InitializeFishes;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
for (int i = 0; i < fishControllers.Count; i++)
|
|
{
|
|
if (fishControllers[i] == null)
|
|
{
|
|
fishControllers[i] = InstantiateRandomFish((!(Random.value < 0.5f)) ? UpdateManager.UpdateMode.BucketB : UpdateManager.UpdateMode.BucketA);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Cull(bool cull)
|
|
{
|
|
if (cull)
|
|
{
|
|
CullFishes(cull: true);
|
|
base.gameObject.SetActive(value: false);
|
|
return;
|
|
}
|
|
base.gameObject.SetActive(value: true);
|
|
if (!_isInitialized)
|
|
{
|
|
_sphereSampler.Generate();
|
|
Debug.Log("Fish zone initialized " + base.transform.position.ToString());
|
|
_isInitialized = true;
|
|
}
|
|
CullFishes(cull: false);
|
|
}
|
|
|
|
public Vector3 GetRandomPoint()
|
|
{
|
|
return _sphereSampler.GetRandomPoint();
|
|
}
|
|
|
|
private FishController InstantiateRandomFish(UpdateManager.UpdateMode updateMode)
|
|
{
|
|
FishOnMapPreset.Fish randomFish = fishPreset.GetRandomFish();
|
|
float randomWeight = randomFish.GetRandomWeight();
|
|
FishController fishController = Object.Instantiate(SceneSingleton<FishSystemManager>.Instance.fishControllerPrefab, _sphereSampler.GetRandomPoint(), Quaternion.identity, base.transform);
|
|
FishData fishData = Object.Instantiate(randomFish.FishData);
|
|
fishController.Initialize(fishData, this, randomWeight);
|
|
fishController.UpdateMode = updateMode;
|
|
return fishController;
|
|
}
|
|
|
|
private void InitializeFishes()
|
|
{
|
|
fishControllers = ((fishControllers == null) ? new List<FishController>() : fishControllers);
|
|
int num = Mathf.Max(maxFishes - fishControllers.Count, 0);
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
fishControllers.Add(InstantiateRandomFish((i % 2 != 0) ? UpdateManager.UpdateMode.BucketB : UpdateManager.UpdateMode.BucketA));
|
|
}
|
|
_isInitialized = true;
|
|
}
|
|
|
|
private void CullFishes(bool cull)
|
|
{
|
|
if (fishControllers == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (FishController fishController in fishControllers)
|
|
{
|
|
if (!(fishController == null) && !fishController.isFightMode)
|
|
{
|
|
fishController.gameObject.SetActive(!cull);
|
|
}
|
|
}
|
|
}
|
|
}
|