210 lines
5.6 KiB
C#
210 lines
5.6 KiB
C#
using UnityEngine;
|
|
|
|
public class AquariumFish : MonoBehaviour
|
|
{
|
|
public GameManager.FishSpecies fishSpecies;
|
|
|
|
public float fishWeight;
|
|
|
|
public float feedLevel;
|
|
|
|
public float growthWeight;
|
|
|
|
public int uid = -1;
|
|
|
|
[HideInInspector]
|
|
public Aquarium currentAquarium;
|
|
|
|
private Rigidbody rigidbody;
|
|
|
|
private Animator animator;
|
|
|
|
public Vector3 currentDstPoint;
|
|
|
|
private AquariumFeedBait currentFeedingBait;
|
|
|
|
[HideInInspector]
|
|
public PlayerResidence playerResidence;
|
|
|
|
[HideInInspector]
|
|
public Transform baitContainer;
|
|
|
|
private float speed = 0.5f;
|
|
|
|
private float addSpeed = 0.5f;
|
|
|
|
private float dstTimer;
|
|
|
|
private float dstTimerDone = 8f;
|
|
|
|
private float updateStatsTimer = 5f;
|
|
|
|
private void Start()
|
|
{
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
animator = GetComponent<Animator>();
|
|
rigidbody.isKinematic = true;
|
|
rigidbody.useGravity = false;
|
|
currentDstPoint = RandomDstPoint();
|
|
base.transform.position = currentDstPoint;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Feeding();
|
|
Move();
|
|
UpdateFeedGrowth();
|
|
}
|
|
|
|
private void UpdateFeedGrowth()
|
|
{
|
|
updateStatsTimer = Mathf.MoveTowards(updateStatsTimer, 0f, Time.deltaTime);
|
|
if (updateStatsTimer != 0f)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 0; i < Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentAquarium.id].fish.Count; i++)
|
|
{
|
|
if (Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentAquarium.id].fish[i].uid == uid)
|
|
{
|
|
feedLevel = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentAquarium.id].fish[i].currentFeed;
|
|
growthWeight = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentAquarium.id].fish[i].growthWeight;
|
|
float weight = fishWeight + growthWeight;
|
|
base.transform.localScale = GameManager.Instance.gameFish[(int)fishSpecies].GetFishScale(weight);
|
|
}
|
|
}
|
|
updateStatsTimer = 5f;
|
|
}
|
|
|
|
private void AddFeed()
|
|
{
|
|
for (int i = 0; i < Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentAquarium.id].fish.Count; i++)
|
|
{
|
|
if (Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentAquarium.id].fish[i].uid == uid)
|
|
{
|
|
Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentAquarium.id].fish[i].AddFeed(currentFeedingBait.feedValue);
|
|
feedLevel = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentAquarium.id].fish[i].currentFeed;
|
|
playerResidence.RefreshFishList();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Feeding()
|
|
{
|
|
if (feedLevel == 1f)
|
|
{
|
|
return;
|
|
}
|
|
float num = 0f;
|
|
float num2 = 99f;
|
|
int num3 = -1;
|
|
for (int i = 0; i < currentAquarium.feedList.Count; i++)
|
|
{
|
|
if (currentAquarium.feedList[i] == null)
|
|
{
|
|
currentAquarium.feedList.RemoveAt(i);
|
|
}
|
|
}
|
|
if (!currentFeedingBait && baitContainer.childCount == 0)
|
|
{
|
|
for (int j = 0; j < currentAquarium.feedList.Count; j++)
|
|
{
|
|
if (!currentAquarium.feedList[j].isInFish)
|
|
{
|
|
num = Vector3.Distance(base.transform.position, currentAquarium.feedList[j].transform.position);
|
|
if (num < num2)
|
|
{
|
|
num2 = num;
|
|
num3 = j;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!currentFeedingBait)
|
|
{
|
|
if (num3 > -1)
|
|
{
|
|
currentFeedingBait = currentAquarium.feedList[num3];
|
|
currentDstPoint = currentFeedingBait.transform.position;
|
|
dstTimer = 0f;
|
|
}
|
|
return;
|
|
}
|
|
if (Vector3.Distance(currentFeedingBait.transform.position, baitContainer.transform.position) <= 0.3f)
|
|
{
|
|
animator.SetFloat("AngularVelocityY", 1.2f);
|
|
}
|
|
if (Vector3.Distance(currentFeedingBait.transform.position, baitContainer.transform.position) <= 0.15f)
|
|
{
|
|
AddFeed();
|
|
currentFeedingBait.isInFish = true;
|
|
currentFeedingBait.GetComponent<Rigidbody>().isKinematic = true;
|
|
currentFeedingBait.GetComponent<Collider>().enabled = false;
|
|
currentFeedingBait.transform.SetParent(baitContainer);
|
|
currentFeedingBait.transform.localPosition = Vector3.zero;
|
|
currentAquarium.feedList.Remove(currentFeedingBait);
|
|
currentFeedingBait = null;
|
|
}
|
|
else
|
|
{
|
|
currentDstPoint = currentFeedingBait.transform.position;
|
|
if (currentFeedingBait.isInFish)
|
|
{
|
|
currentFeedingBait = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
private Vector3 RandomDstPoint()
|
|
{
|
|
int num = Random.Range(0, currentAquarium.fishDstPoints.Length);
|
|
return currentAquarium.fishDstPoints[num].transform.position;
|
|
}
|
|
|
|
private void Move()
|
|
{
|
|
dstTimer += Time.deltaTime;
|
|
speed = Vector3.Distance(currentDstPoint, base.transform.position);
|
|
speed = addSpeed + Mathf.Clamp(speed, 0.01f, 0.2f);
|
|
if ((bool)currentFeedingBait)
|
|
{
|
|
addSpeed = Mathf.MoveTowards(addSpeed, 1f, Time.deltaTime * 0.3f);
|
|
}
|
|
Vector3 vector = currentDstPoint - base.transform.position;
|
|
float num = Vector3.Angle(vector, base.transform.forward);
|
|
Quaternion b = Quaternion.LookRotation(vector);
|
|
base.transform.rotation = Quaternion.Slerp(base.transform.rotation, b, Time.deltaTime * Mathf.Clamp(Mathf.Abs(num) * 0.1f, 1f, 5f));
|
|
base.transform.Translate(Vector3.forward * Time.deltaTime * speed);
|
|
if (!currentFeedingBait)
|
|
{
|
|
if (num > 80f)
|
|
{
|
|
animator.SetFloat("AngularVelocityY", num);
|
|
addSpeed = Random.Range(0.05f, 0.2f);
|
|
}
|
|
else if (num < -80f)
|
|
{
|
|
animator.SetFloat("AngularVelocityY", 0f - num);
|
|
addSpeed = Random.Range(0.05f, 0.2f);
|
|
}
|
|
else
|
|
{
|
|
animator.SetFloat("AngularVelocityY", 0f);
|
|
}
|
|
}
|
|
animator.SetFloat("speed", speed * 2f);
|
|
if (Vector3.Distance(currentDstPoint, base.transform.position) < 0.2f || dstTimer > dstTimerDone)
|
|
{
|
|
currentDstPoint = RandomDstPoint();
|
|
addSpeed = Random.Range(0.01f, 0.2f);
|
|
dstTimer = 0f;
|
|
currentFeedingBait = null;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
playerResidence.RefreshFishList();
|
|
}
|
|
}
|