274 lines
8.6 KiB
C#
274 lines
8.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
public class PlayerResidence : MonoBehaviour
|
|
{
|
|
public GameObject feedingPrefab;
|
|
|
|
public GameObject aquariumFishItemPrefab;
|
|
|
|
public GameObject aquariumFishPanel;
|
|
|
|
public Transform aquariumFishListContainer;
|
|
|
|
public GameObject infoPanel;
|
|
|
|
public Text infoPanelLabel;
|
|
|
|
public Text infoPanelText;
|
|
|
|
public Text infoPanelManageText;
|
|
|
|
public Text infoPanelFeedText;
|
|
|
|
public GameObject gamepadAImage;
|
|
|
|
public GameObject gamepadXImage;
|
|
|
|
public Transform canvasTransform;
|
|
|
|
private Aquarium currentLookAquarium;
|
|
|
|
private AquariumFish currentLookFish;
|
|
|
|
public float updateFishmanagerBySec = 5f;
|
|
|
|
public static UnityEvent<Transform> OnResidencePlayerSpawn;
|
|
|
|
public static Action<Transform> OnResidencePlayerSpawnGlobal;
|
|
|
|
private void Start()
|
|
{
|
|
GameManager.Instance.playerResidence = this;
|
|
OnResidencePlayerSpawn?.Invoke(base.transform);
|
|
OnResidencePlayerSpawnGlobal?.Invoke(base.transform);
|
|
Aquarium.OnPlayerStay += Aquarium_OnPlayerStay;
|
|
Aquarium.OnPlayerExit += Aquarium_OnPlayerExit;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
GameManager.Instance.playerResidence = null;
|
|
Aquarium.OnPlayerStay -= Aquarium_OnPlayerStay;
|
|
Aquarium.OnPlayerExit -= Aquarium_OnPlayerExit;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (SRDebug.Instance.IsDebugPanelVisible)
|
|
{
|
|
Cursor.visible = true;
|
|
return;
|
|
}
|
|
ShowAquariumFishManager();
|
|
Feeding();
|
|
if (updateFishmanagerBySec == 0f)
|
|
{
|
|
RefreshFishList();
|
|
updateFishmanagerBySec = 5f;
|
|
}
|
|
else
|
|
{
|
|
updateFishmanagerBySec = Mathf.MoveTowards(updateFishmanagerBySec, 0f, Time.deltaTime);
|
|
}
|
|
}
|
|
|
|
private void Aquarium_OnPlayerStay(Aquarium aquarium)
|
|
{
|
|
float num = Vector3.Angle(aquarium.transform.position - FScriptsHandler.Instance.m_PlayerMain.transform.position, FScriptsHandler.Instance.m_PlayerMain.transform.forward);
|
|
if (num > -90f && num < 90f)
|
|
{
|
|
IsLookOnAquarium(aquarium);
|
|
}
|
|
else
|
|
{
|
|
IsLookOnAquarium(null);
|
|
}
|
|
}
|
|
|
|
private void Aquarium_OnPlayerExit(Aquarium aquarium)
|
|
{
|
|
if (aquarium == currentLookAquarium)
|
|
{
|
|
IsLookOnAquarium(null);
|
|
}
|
|
}
|
|
|
|
private void Feeding()
|
|
{
|
|
if ((bool)currentLookAquarium)
|
|
{
|
|
if (Input.GetMouseButtonDown(1))
|
|
{
|
|
currentLookAquarium.SpawnFeeding();
|
|
}
|
|
else if (GameManager.Instance.player.GetButtonDown("FeedFishes"))
|
|
{
|
|
currentLookAquarium.SpawnFeeding();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void DestroyFishFromCurrentAquarium(int fishUid)
|
|
{
|
|
for (int i = 0; i < currentLookAquarium.aquariumFishList.Count; i++)
|
|
{
|
|
if (currentLookAquarium.aquariumFishList[i].uid == fishUid)
|
|
{
|
|
Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.aquariumFishList[i].currentAquarium.id].fish.RemoveAt(i);
|
|
UnityEngine.Object.Destroy(currentLookAquarium.aquariumFishList[i].gameObject);
|
|
currentLookAquarium.aquariumFishList.RemoveAt(i);
|
|
}
|
|
}
|
|
RefreshFishList();
|
|
if (currentLookAquarium.aquariumFishList.Count == 0)
|
|
{
|
|
CloseAquariumFishManager();
|
|
}
|
|
}
|
|
|
|
private void ShowAquariumFishManager()
|
|
{
|
|
if ((bool)currentLookAquarium && currentLookAquarium.aquariumFishList.Count != 0 && Input.GetMouseButton(0) && !aquariumFishPanel.activeSelf)
|
|
{
|
|
aquariumFishPanel.SetActive(value: true);
|
|
GameManager.Instance.SetMouseCurrsor(val: true);
|
|
FScriptsHandler.Instance.m_PlayerMain.firstPersonController.frezzePosition = true;
|
|
FScriptsHandler.Instance.m_PlayerMain.firstPersonController.frezzeRotation = true;
|
|
BuildFishList();
|
|
}
|
|
}
|
|
|
|
public void BuildFishList()
|
|
{
|
|
if (aquariumFishPanel.activeSelf)
|
|
{
|
|
GameManager.TruncateContainer(aquariumFishListContainer);
|
|
for (int i = 0; i < Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish.Count; i++)
|
|
{
|
|
AquariumFishItem component = UnityEngine.Object.Instantiate(aquariumFishItemPrefab, aquariumFishListContainer).GetComponent<AquariumFishItem>();
|
|
component.aquariumIndex = currentLookAquarium.id;
|
|
component.fishSpecies = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[i].species;
|
|
component.fishWeight = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[i].weight;
|
|
component.growthWeight = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[i].growthWeight;
|
|
component.dropTime = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[i].timeDrop;
|
|
component.lifetime = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[i].lifeTime;
|
|
component.uid = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[i].uid;
|
|
component.indexOfList = i;
|
|
component.canvasTransform = canvasTransform;
|
|
component.currentFeed = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[i].currentFeed;
|
|
}
|
|
ShowInfoPanel();
|
|
}
|
|
}
|
|
|
|
public void RefreshFishList()
|
|
{
|
|
if (!aquariumFishPanel || !aquariumFishPanel.activeSelf)
|
|
{
|
|
return;
|
|
}
|
|
AquariumFishItem[] componentsInChildren = aquariumFishListContainer.GetComponentsInChildren<AquariumFishItem>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
bool flag = false;
|
|
for (int j = 0; j < Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish.Count; j++)
|
|
{
|
|
if (Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[j].uid == componentsInChildren[i].uid)
|
|
{
|
|
componentsInChildren[i].fishWeight = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[j].weight;
|
|
componentsInChildren[i].growthWeight = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[j].growthWeight;
|
|
componentsInChildren[i].lifetime = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[j].lifeTime;
|
|
componentsInChildren[i].currentFeed = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].fish[j].currentFeed;
|
|
flag = true;
|
|
}
|
|
}
|
|
if (!flag)
|
|
{
|
|
UnityEngine.Object.Destroy(componentsInChildren[i].gameObject);
|
|
updateFishmanagerBySec = 0.5f;
|
|
}
|
|
}
|
|
RefreshAllFishItems();
|
|
}
|
|
|
|
private void RefreshAllFishItems()
|
|
{
|
|
AquariumFishItem[] componentsInChildren = aquariumFishListContainer.GetComponentsInChildren<AquariumFishItem>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].indexOfList = i;
|
|
componentsInChildren[i].Refresh();
|
|
}
|
|
ShowInfoPanel();
|
|
}
|
|
|
|
public void CloseAquariumFishManager()
|
|
{
|
|
if (aquariumFishPanel.activeSelf)
|
|
{
|
|
aquariumFishPanel.SetActive(value: false);
|
|
FScriptsHandler.Instance.m_PlayerMain.firstPersonController.frezzePosition = false;
|
|
FScriptsHandler.Instance.m_PlayerMain.firstPersonController.frezzeRotation = false;
|
|
}
|
|
}
|
|
|
|
private void ShowInfoPanel(bool hide = false)
|
|
{
|
|
if (hide)
|
|
{
|
|
infoPanel.SetActive(value: false);
|
|
}
|
|
else if ((bool)currentLookAquarium)
|
|
{
|
|
infoPanel.SetActive(value: true);
|
|
infoPanelLabel.text = LanguageManager.Instance.GetText("RESIDENCE_AQUARIUM") + " " + (currentLookAquarium.id + 1);
|
|
if (GameManager.Instance.controllerType == GameManager.ControllerType.GamePad)
|
|
{
|
|
gamepadAImage.SetActive(value: true);
|
|
gamepadXImage.SetActive(value: true);
|
|
infoPanelManageText.text = LanguageManager.Instance.GetText("RESIDENCE_MANAGE_AQUARIUM_PAD");
|
|
infoPanelFeedText.text = LanguageManager.Instance.GetText("RESIDENCE_FEED_FISH_PAD");
|
|
}
|
|
else
|
|
{
|
|
gamepadAImage.SetActive(value: false);
|
|
gamepadXImage.SetActive(value: false);
|
|
infoPanelManageText.text = LanguageManager.Instance.GetText("RESIDENCE_MANAGE_AQUARIUM_MOUSE");
|
|
infoPanelFeedText.text = LanguageManager.Instance.GetText("RESIDENCE_FEED_FISH_MOUSE");
|
|
}
|
|
int maxCapacity = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerAquarium[currentLookAquarium.id].maxCapacity;
|
|
infoPanelText.text = LanguageManager.Instance.GetText("RESIDENCE_TOTAL_FISH_IN_AQUARIUM") + ": " + currentLookAquarium.aquariumFishList.Count + " / " + maxCapacity;
|
|
}
|
|
}
|
|
|
|
public void IsLookOnAquarium(Aquarium isLook)
|
|
{
|
|
if (isLook != null)
|
|
{
|
|
currentLookAquarium = isLook;
|
|
ShowInfoPanel();
|
|
}
|
|
else
|
|
{
|
|
currentLookAquarium = null;
|
|
ShowInfoPanel(hide: true);
|
|
}
|
|
}
|
|
|
|
public void IsLookOnFish(AquariumFish isLook)
|
|
{
|
|
if (isLook != null)
|
|
{
|
|
currentLookFish = isLook;
|
|
Debug.Log("See fish: " + isLook.name);
|
|
}
|
|
else
|
|
{
|
|
currentLookFish = null;
|
|
}
|
|
}
|
|
}
|