备份CatanBuilding瘦身独立工程
This commit is contained in:
250
Assets/Scripts/GampPlay/Fishing/FishingSkinTool.cs
Normal file
250
Assets/Scripts/GampPlay/Fishing/FishingSkinTool.cs
Normal file
@@ -0,0 +1,250 @@
|
||||
using asap.core;
|
||||
using cfg;
|
||||
using GameCore;
|
||||
using UniRx;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
public enum RodSkinType
|
||||
{
|
||||
skin = 0,
|
||||
glove = 1,
|
||||
//accessories = 2
|
||||
}
|
||||
public struct ChangeSkinEvent
|
||||
{
|
||||
public ChangeSkinEvent(RodSkinType Type)
|
||||
{
|
||||
this.Type = Type;
|
||||
}
|
||||
public RodSkinType Type;
|
||||
}
|
||||
public struct SelectSkinEvent
|
||||
{
|
||||
public SelectSkinEvent(int ID)
|
||||
{
|
||||
this.ID = ID;
|
||||
}
|
||||
public int ID;
|
||||
}
|
||||
public struct SelectGloveEvent
|
||||
{
|
||||
public SelectGloveEvent(int ID)
|
||||
{
|
||||
this.ID = ID;
|
||||
}
|
||||
public int ID;
|
||||
}
|
||||
public struct SelectAccessoriesEvent
|
||||
{
|
||||
public SelectAccessoriesEvent(int ID)
|
||||
{
|
||||
this.ID = ID;
|
||||
}
|
||||
public int ID;
|
||||
}
|
||||
public class FishingSkinTool : MonoBehaviour
|
||||
{
|
||||
Tables _tables;
|
||||
public GameObject _bait;
|
||||
GameObject baitAB;
|
||||
//相机初始化
|
||||
public Animator _cameraAnimator;
|
||||
public GameObject _fishingRodHandle;
|
||||
public Vector2 _lineResolution; // 钓鱼线的分辨率
|
||||
Animator rodHandleAni;
|
||||
Vector3 _targetPos;
|
||||
Animator fishingRodAnimator;
|
||||
Rod RodBehaviour;
|
||||
GameObject rodPrefab;
|
||||
HandController HandController;
|
||||
GameObject handAB;
|
||||
CompositeDisposable disposables = new CompositeDisposable();
|
||||
PlayerData playerData;
|
||||
PlayerFishData playerFishData;
|
||||
float FishingScale = 1;
|
||||
string _rodSkinPath;
|
||||
string _glovePath;
|
||||
private void Awake()
|
||||
{
|
||||
_tables = GContext.container.Resolve<Tables>();
|
||||
playerData = GContext.container.Resolve<PlayerData>();
|
||||
playerFishData = GContext.container.Resolve<PlayerFishData>();
|
||||
GContext.OnEvent<SelectGloveEvent>().Subscribe(OnSelectGloveEvent).AddTo(disposables);
|
||||
GContext.OnEvent<SelectSkinEvent>().Subscribe(OnSelectSkinEvent).AddTo(disposables);
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
LoadHand();
|
||||
LoadFishRod();
|
||||
LoadBait();
|
||||
}
|
||||
void LoadHand()
|
||||
{
|
||||
GloveData gloveData = GContext.container.Resolve<PlayerFishData>().GetGloveData;
|
||||
InitRodHandAsync(gloveData);
|
||||
}
|
||||
|
||||
void OnSelectGloveEvent(SelectGloveEvent e)
|
||||
{
|
||||
var gloveData = _tables.TbGloveData[e.ID];
|
||||
InitRodHandAsync(gloveData);
|
||||
}
|
||||
public async void InitRodHandAsync(GloveData gloveData)
|
||||
{
|
||||
if (gloveData == null)
|
||||
{
|
||||
Debug.LogError("gloveData is null");
|
||||
return;
|
||||
}
|
||||
string path = gloveData.Fbx;
|
||||
if (_glovePath == path)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_glovePath = path;
|
||||
if (handAB != null)
|
||||
{
|
||||
Addressables.ReleaseInstance(handAB);
|
||||
}
|
||||
var hand = await Addressables.InstantiateAsync(path, transform).Task;
|
||||
if (hand != null)
|
||||
{
|
||||
if (path != gloveData.Fbx)
|
||||
{
|
||||
Addressables.ReleaseInstance(hand);
|
||||
return;
|
||||
}
|
||||
handAB = hand;
|
||||
HandController = handAB.GetComponent<HandController>();
|
||||
if (RodBehaviour != null)
|
||||
{
|
||||
HandController.SetHandTargets(RodBehaviour.RightHandTargets, RodBehaviour.LeftHandTargets, RodBehaviour.BodyTargets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async void LoadBait()
|
||||
{
|
||||
baitAB = await Addressables.InstantiateAsync("BaitRoot", _bait.transform).Task;
|
||||
baitAB.transform.localPosition = Vector3.zero;
|
||||
baitAB.transform.localRotation = Quaternion.identity;
|
||||
baitAB.transform.localScale = Vector3.one;
|
||||
}
|
||||
void LoadFishRod()
|
||||
{
|
||||
int equipRodID = playerData.equipRodID;
|
||||
var fishRodData = _tables.TbRodData[equipRodID];
|
||||
InitFishRodAsync(fishRodData);
|
||||
}
|
||||
public void InitFishRodAsync(RodData fishRodData)
|
||||
{
|
||||
int skindID = playerFishData.GetRodSkin(fishRodData.ID);
|
||||
if (skindID > 0)
|
||||
{
|
||||
var fishRodSkinData = _tables.TbRodSkinData.GetOrDefault(skindID);
|
||||
if (fishRodSkinData != null)
|
||||
{
|
||||
FishingScale = fishRodSkinData.FishingScale;
|
||||
LoadFishRodAsync(fishRodSkinData.Fbx);
|
||||
}
|
||||
}
|
||||
}
|
||||
void OnSelectSkinEvent(SelectSkinEvent e)
|
||||
{
|
||||
int skindID = e.ID;
|
||||
if (skindID > 0)
|
||||
{
|
||||
var fishRodSkinData = _tables.TbRodSkinData.GetOrDefault(skindID);
|
||||
if (fishRodSkinData != null)
|
||||
{
|
||||
FishingScale = fishRodSkinData.FishingScale;
|
||||
LoadFishRodAsync(fishRodSkinData.Fbx);
|
||||
}
|
||||
}
|
||||
}
|
||||
async void LoadFishRodAsync(string path)
|
||||
{
|
||||
if (_rodSkinPath == path)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_rodSkinPath = path;
|
||||
var goPrefab = await Addressables.LoadAssetAsync<GameObject>(path).Task;
|
||||
_bait.transform.SetParent(transform);
|
||||
if (goPrefab != null)
|
||||
{
|
||||
if (rodPrefab)
|
||||
{
|
||||
Addressables.Release(rodPrefab);
|
||||
}
|
||||
rodPrefab = goPrefab;
|
||||
var go = Instantiate(goPrefab, transform.Find("FishingRodHandle"));
|
||||
go.transform.localPosition = Vector3.zero;
|
||||
go.transform.localScale = Vector3.one * FishingScale;
|
||||
if (RodBehaviour != null)
|
||||
{
|
||||
DestroyImmediate(RodBehaviour.gameObject);
|
||||
}
|
||||
|
||||
RodBehaviour = go.GetComponent<Rod>();
|
||||
fishingRodAnimator = RodBehaviour.rod.GetComponent<Animator>();
|
||||
//Set HandIK Target
|
||||
if (HandController != null)
|
||||
{
|
||||
HandController.SetHandTargets(RodBehaviour.RightHandTargets, RodBehaviour.LeftHandTargets, RodBehaviour.BodyTargets);
|
||||
}
|
||||
|
||||
if (RodBehaviour.cCDIK != null)
|
||||
{
|
||||
RodBehaviour.cCDIK.enabled = false;
|
||||
}
|
||||
_bait.transform.SetParent(RodBehaviour.Bait);
|
||||
_bait.transform.localPosition = Vector3.zero; //-Vector3.up * 0.1f;
|
||||
_bait.transform.localRotation = Quaternion.Euler(Vector3.forward * 90);
|
||||
}
|
||||
}
|
||||
private void LateUpdate()
|
||||
{
|
||||
DrawFishingLine();
|
||||
}
|
||||
private void DrawFishingLine()
|
||||
{
|
||||
if (RodBehaviour == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// 计算钓鱼线上的点
|
||||
Vector3[] linePoints;
|
||||
int addCount = RodBehaviour.LineList.Count + 4;
|
||||
linePoints = new Vector3[addCount];
|
||||
linePoints[0] = RodBehaviour.LineHole.position;
|
||||
linePoints[1] = RodBehaviour.EndLine.position;
|
||||
|
||||
for (int i = 0; i < RodBehaviour.LineList.Count; i++)
|
||||
{
|
||||
linePoints[i + 2] = RodBehaviour.LineList[i].position;
|
||||
|
||||
}
|
||||
linePoints[addCount - 2] = RodBehaviour.TopLine.position;
|
||||
linePoints[addCount - 1] = _bait.transform.position;
|
||||
// 绘制钓鱼线
|
||||
RodBehaviour.fishingLineRenderer.positionCount = addCount;
|
||||
RodBehaviour.fishingLineRenderer.SetPositions(linePoints);
|
||||
}
|
||||
private float CalculateSegmetLenght(float distance, Vector2 lineResolution)
|
||||
{
|
||||
float x = Mathf.InverseLerp(1, 50, distance);
|
||||
float value = Mathf.Lerp(lineResolution.x, lineResolution.y, x);
|
||||
|
||||
return value;
|
||||
}
|
||||
private void OnDestroy()
|
||||
{
|
||||
disposables?.Dispose();
|
||||
disposables = null;
|
||||
|
||||
Addressables.Release(rodPrefab);
|
||||
Addressables.ReleaseInstance(baitAB);
|
||||
Addressables.ReleaseInstance(handAB);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user