468 lines
10 KiB
C#
468 lines
10 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BitStrap;
|
|
using LE_LevelEditor.Core;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class FishSpawner : MonoBehaviour
|
|
{
|
|
[ReadOnly]
|
|
public Fish.Species species = Fish.Species.COUNT;
|
|
|
|
public Fish fishPrefab;
|
|
|
|
public List<Fish> fishPrefabs = new List<Fish>();
|
|
|
|
public List<Fish> fishPrefabsDLC = new List<Fish>();
|
|
|
|
public int count = 1;
|
|
|
|
public bool fastDurabilityDrain;
|
|
|
|
public bool hideOtherDuringFight;
|
|
|
|
public bool wasUsed;
|
|
|
|
public bool spawnersParent;
|
|
|
|
public bool alwaysSpawn;
|
|
|
|
public bool spawnAtStart;
|
|
|
|
public float maxFishAwayDistance = 500f;
|
|
|
|
public float fishSizeMultiplier = 1f;
|
|
|
|
public List<Fish> fishList = new List<Fish>();
|
|
|
|
public List<ObjectPool.stFishCopy> fishCopies = new List<ObjectPool.stFishCopy>();
|
|
|
|
private Vector3 startPosition = Vector3.zero;
|
|
|
|
private Coroutine checkBehaviorCoroutine;
|
|
|
|
public SphereCollider sphereCollider;
|
|
|
|
[Header("ColldierGroup")]
|
|
public bool useColliderGroup;
|
|
|
|
[HideInInspector]
|
|
public FishSpawnersGroupCollider spawnersGroup;
|
|
|
|
public static bool useJkmSpawn;
|
|
|
|
private void Awake()
|
|
{
|
|
if (GameController.Instance.useFishesPool)
|
|
{
|
|
for (int i = 0; i < fishList.Count; i++)
|
|
{
|
|
fishCopies.Add(default(ObjectPool.stFishCopy));
|
|
}
|
|
}
|
|
sphereCollider = GetComponent<SphereCollider>();
|
|
SetSphereColliderActive(true);
|
|
startPosition = base.transform.position;
|
|
if ((bool)FisheryEditor.Instance)
|
|
{
|
|
SetSphereColliderActive(false);
|
|
}
|
|
else if (!spawnersParent)
|
|
{
|
|
Object.DestroyImmediate(GetComponentInChildren<MeshRenderer>().gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (!GlobalSettings.Instance || spawnersParent)
|
|
{
|
|
return;
|
|
}
|
|
if ((bool)GameController.Instance && (bool)GameController.Instance.fisheryEditorGame)
|
|
{
|
|
int num = (int)GetComponent<LE_Object>().CustomProperties.x;
|
|
if (num >= 0)
|
|
{
|
|
species = GlobalSettings.Instance.fishManager.fishDefinitions[num].fishPrefab.species;
|
|
}
|
|
else
|
|
{
|
|
species = Fish.Species.COUNT;
|
|
}
|
|
if (species < Fish.Species.COUNT && GlobalSettings.Instance.fishManager.GetFishDefinition(species).caughtCount > 0)
|
|
{
|
|
fishPrefab = GlobalSettings.Instance.fishManager.GetFishDefinition(species).fishPrefab;
|
|
count = (int)GetComponent<LE_Object>().CustomProperties.y;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Fish not unlocked: " + species);
|
|
count = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
species = Fish.Species.COUNT;
|
|
}
|
|
}
|
|
|
|
public void CheckBehaviorDistance()
|
|
{
|
|
if (GameController.Instance.newSpawnersDeactivateMethod)
|
|
{
|
|
if (checkBehaviorCoroutine == null)
|
|
{
|
|
checkBehaviorCoroutine = StartCoroutine(CheckBehaviorDistanceCoroutine());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(CheckBehaviorDistanceCoroutine());
|
|
}
|
|
}
|
|
|
|
public void ActivateSpawner(bool activate)
|
|
{
|
|
if (!GameController.Instance.newSpawnersDeactivateMethod)
|
|
{
|
|
return;
|
|
}
|
|
SetSphereColliderActive(!activate);
|
|
if (activate)
|
|
{
|
|
if (checkBehaviorCoroutine == null)
|
|
{
|
|
checkBehaviorCoroutine = StartCoroutine(CheckBehaviorDistanceCoroutine());
|
|
}
|
|
return;
|
|
}
|
|
if (checkBehaviorCoroutine != null)
|
|
{
|
|
StopCoroutine(checkBehaviorCoroutine);
|
|
checkBehaviorCoroutine = null;
|
|
}
|
|
DestroyAllFish();
|
|
}
|
|
|
|
public IEnumerator CheckBehaviorDistanceCoroutine()
|
|
{
|
|
while (true)
|
|
{
|
|
if (Vector3.Distance(startPosition, GameController.Instance.fishingPlayer.transform.position) > GameController.Instance.fishingPlayer.fishDistanceBoatBehavior + maxFishAwayDistance)
|
|
{
|
|
if (!GameController.Instance.useFishesPool)
|
|
{
|
|
for (int i = 0; i < fishList.Count; i++)
|
|
{
|
|
if (!fishList[i].isFighting && !fishList[i].isBoatUpdate)
|
|
{
|
|
fishList[i].gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int j = 0; j < fishList.Count; j++)
|
|
{
|
|
if (!fishCopies[j].isFighting && !fishCopies[j].isBoatUpdate)
|
|
{
|
|
ObjectPool.Instance.SetFishActive(ref fishList, ref fishCopies, j, false);
|
|
}
|
|
}
|
|
}
|
|
ActivateSpawner(false);
|
|
yield return new WaitForSeconds(Random.Range(GameController.Instance.fishSpawnersDisabledDelay.x, GameController.Instance.fishSpawnersDisabledDelay.y));
|
|
continue;
|
|
}
|
|
for (int k = 0; k < fishList.Count; k++)
|
|
{
|
|
if (!GameController.Instance.quickCheckBehaviorDistance)
|
|
{
|
|
yield return new WaitForSeconds(Random.Range(GameController.Instance.fishSpawnersEnabledDelay.x, GameController.Instance.fishSpawnersEnabledDelay.y));
|
|
}
|
|
if ((bool)fishList[k])
|
|
{
|
|
fishList[k].CheckBehaviorDistance();
|
|
}
|
|
else if (fishCopies[k].IsBehaviorDistance(GameController.Instance.fishingPlayer))
|
|
{
|
|
ObjectPool.Instance.SetFishActive(ref fishList, ref fishCopies, k, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void CheckBehaviorDistanceQuick()
|
|
{
|
|
for (int i = 0; i < fishList.Count; i++)
|
|
{
|
|
if ((bool)fishList[i])
|
|
{
|
|
fishList[i].CheckBehaviorDistance();
|
|
}
|
|
}
|
|
}
|
|
|
|
private Fish GetFishPrefab()
|
|
{
|
|
if (fishPrefabsDLC.Count > 0)
|
|
{
|
|
return fishPrefabsDLC[Random.Range(0, fishPrefabsDLC.Count)];
|
|
}
|
|
if (fishPrefabs.Count == 0)
|
|
{
|
|
return fishPrefab;
|
|
}
|
|
return fishPrefabs[Random.Range(0, fishPrefabs.Count)];
|
|
}
|
|
|
|
public Fish SpawnFish()
|
|
{
|
|
Fish fish = null;
|
|
fish = ((!GameController.Instance.useFishesPool) ? Object.Instantiate(GetFishPrefab()) : ObjectPool.Instance.GetFish(GetFishPrefab()));
|
|
fish.transform.parent = base.transform;
|
|
fish.transform.localPosition = Vector3.zero;
|
|
fish.fastDurabilityDrain = fastDurabilityDrain;
|
|
fish.fishSpawner = this;
|
|
fishList.Add(fish);
|
|
return fish;
|
|
}
|
|
|
|
public void SpawnStart(int amount)
|
|
{
|
|
if (!wasUsed)
|
|
{
|
|
if (amount > count)
|
|
{
|
|
amount = count;
|
|
}
|
|
for (int i = 0; i < amount; i++)
|
|
{
|
|
SpawnFish();
|
|
}
|
|
count -= amount;
|
|
}
|
|
}
|
|
|
|
public IEnumerator SpawnAllFish(float delay)
|
|
{
|
|
if (SceneManager.GetActiveScene().name == "FisheryEditor" || wasUsed || count <= 0)
|
|
{
|
|
yield break;
|
|
}
|
|
if (!useColliderGroup)
|
|
{
|
|
SetSphereColliderActive(false);
|
|
}
|
|
wasUsed = true;
|
|
if (!useJkmSpawn)
|
|
{
|
|
for (int k = 0; k < count; k++)
|
|
{
|
|
Fish newFish = SpawnFish();
|
|
newFish.Initialize();
|
|
yield return new WaitForSeconds(delay);
|
|
}
|
|
CheckBehaviorDistanceQuick();
|
|
CheckBehaviorDistance();
|
|
}
|
|
else
|
|
{
|
|
while (FishDelaySpawner.Instance.isLock)
|
|
{
|
|
yield return 0.01;
|
|
}
|
|
FishDelaySpawner.Instance.isLock = true;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
FishDelaySpawner.Instance.AddFish(this);
|
|
}
|
|
FishDelaySpawner.Instance.isLock = false;
|
|
}
|
|
}
|
|
|
|
public void DestroyAllFish()
|
|
{
|
|
SetSphereColliderActive(true);
|
|
wasUsed = false;
|
|
for (int i = 0; i < fishList.Count; i++)
|
|
{
|
|
if (GameController.Instance.useFishesPool)
|
|
{
|
|
ObjectPool.Instance.ReturnFish(fishList[i], false);
|
|
}
|
|
else
|
|
{
|
|
Object.Destroy(fishList[i].gameObject);
|
|
}
|
|
}
|
|
fishList.Clear();
|
|
}
|
|
|
|
public IEnumerator InitializeAllFish(float delay)
|
|
{
|
|
if (!(SceneManager.GetActiveScene().name == "FisheryEditor") && !wasUsed && count > 0)
|
|
{
|
|
Debug.Log("Initialize FishSpawner");
|
|
SetSphereColliderActive(false);
|
|
wasUsed = true;
|
|
for (int k = 0; k < fishList.Count; k++)
|
|
{
|
|
fishList[k].Initialize();
|
|
yield return new WaitForSeconds(delay);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider col)
|
|
{
|
|
Debug.Log("FISHSPAWENER-----------------------FISHSPAWENER_ONTRIGGER-------------------------FISHSPAWENER");
|
|
if (!useColliderGroup)
|
|
{
|
|
DoOnTrigger(col);
|
|
}
|
|
}
|
|
|
|
public void DoOnTrigger(Collider col)
|
|
{
|
|
if (col.gameObject.HasTag("PLAYER") && !wasUsed && !GameController.Instance.spawnAllAtStart)
|
|
{
|
|
StartCoroutine(SpawnAllFish((!GameController.Instance.newSpawnersDeactivateMethod) ? 1f : Random.Range(0.4f, 0.7f)));
|
|
}
|
|
else if (col.gameObject.HasTag("PLAYER") && wasUsed)
|
|
{
|
|
ActivateSpawner(true);
|
|
}
|
|
}
|
|
|
|
public void HideOtherDuringFight(Fish fish, bool hide)
|
|
{
|
|
if (!hideOtherDuringFight)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 0; i < fishList.Count; i++)
|
|
{
|
|
if (fishList[i] != fish)
|
|
{
|
|
if (GameController.Instance.useFishesPool)
|
|
{
|
|
ObjectPool.Instance.SetFishActive(ref fishList, ref fishCopies, i, !hide);
|
|
}
|
|
else
|
|
{
|
|
fishList[i].gameObject.SetActive(!hide);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetSphereColliderActive(bool value)
|
|
{
|
|
if (sphereCollider == null)
|
|
{
|
|
return;
|
|
}
|
|
if (useColliderGroup)
|
|
{
|
|
if (spawnersGroup != null)
|
|
{
|
|
spawnersGroup.SetShpereColliderEnabled(value);
|
|
}
|
|
sphereCollider.enabled = false;
|
|
}
|
|
else
|
|
{
|
|
sphereCollider.enabled = value;
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void SpawnAllFish()
|
|
{
|
|
if (!wasUsed && !spawnersParent)
|
|
{
|
|
startPosition = base.transform.position;
|
|
SetSphereColliderActive(false);
|
|
wasUsed = true;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
SpawnFish();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void InitializeAllFish()
|
|
{
|
|
for (int i = 0; i < fishList.Count; i++)
|
|
{
|
|
fishList[i].Initialize();
|
|
if (!alwaysSpawn)
|
|
{
|
|
if (GameController.Instance.useFishesPool)
|
|
{
|
|
ObjectPool.Instance.SetFishActive(ref fishList, ref fishCopies, i, false);
|
|
}
|
|
else
|
|
{
|
|
fishList[i].gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void SpawnAllFishParent()
|
|
{
|
|
if (spawnersParent)
|
|
{
|
|
FishSpawner[] componentsInChildren = GetComponentsInChildren<FishSpawner>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].SpawnAllFish();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void DestroyAllFishParent()
|
|
{
|
|
if (spawnersParent)
|
|
{
|
|
FishSpawner[] componentsInChildren = GetComponentsInChildren<FishSpawner>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].DestroyAllFish();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void DeactivateAllFish(bool deactivate)
|
|
{
|
|
for (int i = 0; i < fishList.Count; i++)
|
|
{
|
|
if (GameController.Instance.useFishesPool)
|
|
{
|
|
ObjectPool.Instance.SetFishActive(ref fishList, ref fishCopies, i, !deactivate);
|
|
}
|
|
else
|
|
{
|
|
fishList[i].gameObject.SetActive(!deactivate);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void CheckIsUnderTerrain()
|
|
{
|
|
RaycastHit hitInfo = default(RaycastHit);
|
|
if (!Physics.Raycast(base.transform.position + Vector3.down * 0.5f, Vector3.down, out hitInfo, 100f))
|
|
{
|
|
Debug.LogError("CheckIsUnderTerrain " + base.name);
|
|
}
|
|
}
|
|
}
|