109 lines
2.7 KiB
C#
109 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering.HighDefinition;
|
|
|
|
public class FishFX : WaterFXInstance
|
|
{
|
|
private struct DecalInstanceData
|
|
{
|
|
public WaterDecal Decal;
|
|
|
|
public float StartTime;
|
|
|
|
public float StartAmplitude;
|
|
}
|
|
|
|
[SerializeField]
|
|
private WaterDecal _decalPrefab;
|
|
|
|
[SerializeField]
|
|
private float _splashSizeMultiplier = 0.1f;
|
|
|
|
[SerializeField]
|
|
private float _maxFishDepth = -0.2f;
|
|
|
|
private FishController _fishController;
|
|
|
|
private WaterBody _waterBody;
|
|
|
|
private const float INTERVAL = 0.05f;
|
|
|
|
private float _time;
|
|
|
|
private List<DecalInstanceData> _activeDecals;
|
|
|
|
private void Awake()
|
|
{
|
|
_fishController = GetComponent<FishController>();
|
|
_waterBody = GetComponent<WaterBody>();
|
|
WaterBody waterBody = _waterBody;
|
|
waterBody.OnWaterChangeState = (Action<bool>)Delegate.Combine(waterBody.OnWaterChangeState, new Action<bool>(OnWaterEnterExit));
|
|
_activeDecals = new List<DecalInstanceData>();
|
|
IsVisible = false;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Init();
|
|
_pool = _manager.GetPool(_decalPrefab);
|
|
}
|
|
|
|
protected override void OnDestroy()
|
|
{
|
|
base.OnDestroy();
|
|
base.Deactivate();
|
|
WaterBody waterBody = _waterBody;
|
|
waterBody.OnWaterChangeState = (Action<bool>)Delegate.Remove(waterBody.OnWaterChangeState, new Action<bool>(OnWaterEnterExit));
|
|
}
|
|
|
|
private void OnWaterEnterExit(bool inWater)
|
|
{
|
|
if (inWater)
|
|
{
|
|
_manager.CreateSplash(base.transform.position, _splashSizeMultiplier * _fishController.Mass);
|
|
}
|
|
}
|
|
|
|
public override void BatchUpdate()
|
|
{
|
|
UpdateFX();
|
|
}
|
|
|
|
public override void UpdateFX()
|
|
{
|
|
float num = 0f;
|
|
for (int num2 = _activeDecals.Count - 1; num2 >= 0; num2--)
|
|
{
|
|
num = Mathf.Clamp01((Time.realtimeSinceStartup - _activeDecals[num2].StartTime) * _decalDecaySpeed);
|
|
if (num >= 1f)
|
|
{
|
|
_pool.Reclaim(_activeDecals[num2].Decal);
|
|
_activeDecals.RemoveAt(num2);
|
|
}
|
|
else
|
|
{
|
|
_activeDecals[num2].Decal.amplitude = (1f - num) * _activeDecals[num2].StartAmplitude;
|
|
_activeDecals[num2].Decal.transform.localScale = new Vector3(num + 0.1f, 1f, num + 0.1f);
|
|
}
|
|
}
|
|
_time += Time.deltaTime;
|
|
if (IsVisible && !(_time < 0.05f) && !(base.transform.position.y < _maxFishDepth) && !(_fishController.Velocity.sqrMagnitude <= 2f))
|
|
{
|
|
_time = 0f;
|
|
WaterDecal instance = _pool.GetInstance();
|
|
instance.amplitude = _decalAmplitude;
|
|
instance.transform.position = base.transform.position;
|
|
instance.transform.rotation = Quaternion.identity;
|
|
instance.transform.localScale = Vector3.zero;
|
|
instance.gameObject.SetActive(value: true);
|
|
_activeDecals.Add(new DecalInstanceData
|
|
{
|
|
Decal = instance,
|
|
StartTime = Time.realtimeSinceStartup,
|
|
StartAmplitude = _decalAmplitude * (0f - Mathf.Lerp(_maxFishDepth, 0f, base.transform.position.y))
|
|
});
|
|
}
|
|
}
|
|
}
|