86 lines
2.0 KiB
C#
86 lines
2.0 KiB
C#
using Assets.Code.Scripts;
|
|
using Obvious.Soap;
|
|
using UnityEngine;
|
|
|
|
public class FishFinderController : MonoBehaviour
|
|
{
|
|
public bool IsEnabled = true;
|
|
|
|
public float RefreshRate = 1f;
|
|
|
|
public FloatVariable SonarDeepSetting;
|
|
|
|
public FloatVariable CurrentSonarDepth;
|
|
|
|
[SerializeField]
|
|
private SonarViewObject _fishView;
|
|
|
|
[SerializeField]
|
|
private SonarViewObject _ground;
|
|
|
|
[SerializeField]
|
|
private SonarViewObject _water;
|
|
|
|
private float _lastRefreshTime;
|
|
|
|
private Pool<SonarViewObject> _PoolGround;
|
|
|
|
private Pool<SonarViewObject> _PoolFish;
|
|
|
|
private void Awake()
|
|
{
|
|
_PoolGround = Pool<SonarViewObject>.CreateInstance(_ground, 15, _ground.transform.parent);
|
|
_PoolFish = Pool<SonarViewObject>.CreateInstance(_fishView, 50, _fishView.transform.parent);
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (_lastRefreshTime <= Time.time - RefreshRate)
|
|
{
|
|
CheckGround();
|
|
FishCheck();
|
|
_lastRefreshTime = Time.time;
|
|
IsEnabled = true;
|
|
}
|
|
else
|
|
{
|
|
IsEnabled = false;
|
|
}
|
|
}
|
|
|
|
private void BackToPoolObject(SonarViewObject viewObject)
|
|
{
|
|
_PoolGround.Reclaim(viewObject);
|
|
viewObject.ResetObject();
|
|
}
|
|
|
|
private void CheckGround()
|
|
{
|
|
if (Physics.Raycast(base.transform.position, Vector3.down, out var hitInfo, SonarDeepSetting.Value, LayerMask.GetMask("Terrain")))
|
|
{
|
|
SonarViewObject instance = _PoolGround.GetInstance();
|
|
instance.gameObject.SetActive(value: true);
|
|
SetViewObject(instance, hitInfo.distance);
|
|
CurrentSonarDepth.Value = hitInfo.distance;
|
|
instance.OnReachOutScreen += BackToPoolObject;
|
|
}
|
|
}
|
|
|
|
private void FishCheck()
|
|
{
|
|
Collider[] array = Physics.OverlapSphere(base.transform.position, CurrentSonarDepth.Value, LayerMask.GetMask("Fish"));
|
|
if (array.Length != 0)
|
|
{
|
|
SonarViewObject instance = _PoolFish.GetInstance();
|
|
instance.gameObject.SetActive(value: true);
|
|
SetViewObject(instance, Vector3.Distance(base.transform.position, array[0].transform.position));
|
|
}
|
|
}
|
|
|
|
private void SetViewObject(SonarViewObject obj, float distance)
|
|
{
|
|
obj.Enabled = true;
|
|
obj.Depth = distance;
|
|
}
|
|
}
|