443 lines
13 KiB
C#
443 lines
13 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Fantasy;
|
||
using Fantasy.Async;
|
||
using NBC.Asset;
|
||
using NBF.Utils;
|
||
using UnityEngine;
|
||
using Object = UnityEngine.Object;
|
||
|
||
namespace NBF
|
||
{
|
||
public class FRod : FHandItem
|
||
{
|
||
public RodAsset Asset;
|
||
|
||
public FReel Reel;
|
||
public FHook Hook;
|
||
public FBobber Bobber;
|
||
public FBait Bait;
|
||
public FLure Lure;
|
||
public FWeight Weight;
|
||
public FLine Line;
|
||
|
||
|
||
public Transform GearRoot;
|
||
|
||
|
||
#region Rod专属
|
||
|
||
private bool _stretchRope = true;
|
||
|
||
public bool StretchRope
|
||
{
|
||
get => _stretchRope;
|
||
set
|
||
{
|
||
_stretchRope = value;
|
||
// Scene.EventComponent.Publish(new PlayerItemRodLingChangeEvent
|
||
// {
|
||
// Item = this
|
||
// });
|
||
}
|
||
}
|
||
|
||
private float _lineLength = 4.2f;
|
||
|
||
/// <summary>
|
||
/// 线长度
|
||
/// </summary>
|
||
public float LineLength
|
||
{
|
||
get => _lineLength;
|
||
set
|
||
{
|
||
_lineLength = value;
|
||
// Scene.EventComponent.Publish(new PlayerItemRodLingChangeEvent
|
||
// {
|
||
// Item = this
|
||
// });
|
||
}
|
||
}
|
||
|
||
private float _floatLength = 0.7f;
|
||
|
||
/// <summary>
|
||
/// 浮漂线长度
|
||
/// </summary>
|
||
public float FloatLength
|
||
{
|
||
get => _floatLength;
|
||
set
|
||
{
|
||
_floatLength = value;
|
||
// Scene.EventComponent.Publish(new PlayerItemRodLingChangeEvent
|
||
// {
|
||
// Item = this
|
||
// });
|
||
}
|
||
}
|
||
|
||
private float _tension;
|
||
|
||
/// <summary>
|
||
/// 拉力
|
||
/// </summary>
|
||
public float Tension
|
||
{
|
||
get => _tension;
|
||
set
|
||
{
|
||
if (!Mathf.Approximately(_tension, value))
|
||
{
|
||
_tension = value;
|
||
}
|
||
|
||
// Scene.EventComponent.Publish(new PlayerItemRodLingChangeEvent
|
||
// {
|
||
// Item = this
|
||
// });
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
// public FWaterDisplacement LureHookWaterDisplacement;
|
||
|
||
[HideInInspector] public FFish currentFish;
|
||
public RodRingNode[] rings;
|
||
|
||
|
||
private void Awake()
|
||
{
|
||
Asset = GetComponent<RodAsset>();
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
Asset.CCDIK.enabled = true;
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
BendControl();
|
||
}
|
||
|
||
public void SetLineLength()
|
||
{
|
||
if (!Line) return;
|
||
if (Line.LineType == LineType.Spinning)
|
||
{
|
||
//没有浮漂类型
|
||
Line.SetLenght(LineLength);
|
||
}
|
||
else
|
||
{
|
||
//有浮漂
|
||
Line.SetLenght(LineLength - FloatLength);
|
||
Line.SetLenght(FloatLength, FLineLogicNodeType.End);
|
||
}
|
||
}
|
||
|
||
private void LateUpdate()
|
||
{
|
||
Test();
|
||
}
|
||
|
||
public IEnumerator Destroy()
|
||
{
|
||
if (GearRoot != null)
|
||
{
|
||
Object.Destroy(GearRoot.gameObject);
|
||
}
|
||
|
||
yield return 1;
|
||
}
|
||
|
||
protected override async FTask OnInit()
|
||
{
|
||
transform.localPosition = Vector3.zero;
|
||
transform.localRotation = Quaternion.identity;
|
||
transform.localScale = Vector3.one;
|
||
Map.Instance.GearsNode.position = transform.position;
|
||
await FTask.WaitFrame(Game.Main); //等待1帧
|
||
|
||
var obj = new GameObject($"rod_{ConfigID}");
|
||
obj.transform.SetParent(Map.Instance.GearsNode);
|
||
obj.transform.position = transform.position;
|
||
obj.transform.rotation = transform.rotation;
|
||
obj.transform.localScale = Vector3.one;
|
||
GearRoot = obj.transform;
|
||
|
||
var parent = GearRoot;
|
||
|
||
CreateFishingHandler();
|
||
await FTask.WaitFrame(Game.Main); //等待1帧
|
||
// children.Sort();
|
||
foreach (var childConfigId in BindItems)
|
||
{
|
||
var itemType = childConfigId.GetItemType();
|
||
var config = Game.Tables.TbItem.Get(childConfigId);
|
||
if (itemType == ItemType.Reel)
|
||
{
|
||
Reel = config.InstantiateAndComponent<FReel>(Asset.ReelConnector, Vector3.zero,
|
||
Quaternion.identity);
|
||
Reel.SetItemConfigId(childConfigId);
|
||
}
|
||
else if (itemType == ItemType.Bobber)
|
||
{
|
||
Bobber = config.InstantiateAndComponent<FBobber>(parent, Vector3.zero,
|
||
Quaternion.identity);
|
||
Bobber.SetItemConfigId(childConfigId);
|
||
}
|
||
else if (itemType == ItemType.Hook)
|
||
{
|
||
Hook = config.InstantiateAndComponent<FHook>(parent, Vector3.zero,
|
||
Quaternion.identity);
|
||
Hook.SetItemConfigId(childConfigId);
|
||
}
|
||
else if (itemType == ItemType.Bait)
|
||
{
|
||
Bait = config.InstantiateAndComponent<FBait>(parent, Vector3.zero,
|
||
Quaternion.identity);
|
||
Bait.SetItemConfigId(childConfigId);
|
||
}
|
||
else if (itemType == ItemType.Lure)
|
||
{
|
||
Lure = config.InstantiateAndComponent<FLure>(parent, Vector3.zero,
|
||
Quaternion.identity);
|
||
Lure.SetItemConfigId(childConfigId);
|
||
}
|
||
else if (itemType == ItemType.Weight)
|
||
{
|
||
Weight = config.InstantiateAndComponent<FWeight>(parent, Vector3.zero,
|
||
Quaternion.identity);
|
||
Weight.SetItemConfigId(childConfigId);
|
||
}
|
||
else if (itemType == ItemType.Line)
|
||
{
|
||
// lineItemInfo = child;
|
||
}
|
||
}
|
||
|
||
await FTask.WaitFrame(Game.Main); //等待1帧
|
||
SetLineLength();
|
||
if (Reel)
|
||
{
|
||
Reel.reelingDrag = 0.699f;
|
||
Reel.transform.SetParent(Asset.ReelConnector);
|
||
Reel.transform.localPosition = Vector3.zero;
|
||
Reel.transform.localEulerAngles = Vector3.zero;
|
||
Reel.Init(this);
|
||
}
|
||
|
||
if (Bobber)
|
||
{
|
||
Bobber.Init(this);
|
||
}
|
||
|
||
if (Hook)
|
||
{
|
||
Hook.Init(this);
|
||
}
|
||
|
||
if (Bait)
|
||
{
|
||
Bait.Init(this);
|
||
}
|
||
|
||
if (Lure)
|
||
{
|
||
Lure.Init(this);
|
||
}
|
||
|
||
if (Weight)
|
||
{
|
||
Weight.Init(this);
|
||
}
|
||
|
||
await FTask.WaitFrame(Game.Main); //等待1帧
|
||
|
||
transform.SetParent(Owner.ModelAsset.RodRoot);
|
||
transform.localPosition = Vector3.zero;
|
||
transform.rotation = Owner.ModelAsset.RodRoot.rotation;
|
||
}
|
||
|
||
|
||
public void CreateFishingHandler()
|
||
{
|
||
if (Line == null)
|
||
{
|
||
Debug.LogError("创建钓组=====");
|
||
var itemConfig = Game.Tables.TbItem.Get(ConfigID);
|
||
var rodType = (ItemSubType)itemConfig.Type;
|
||
if (rodType == ItemSubType.RodTele)
|
||
{
|
||
CreateObiFishingLine(0);
|
||
}
|
||
else if (rodType == ItemSubType.RodSpine || rodType == ItemSubType.RodBolo)
|
||
{
|
||
CreateObiFishingLine(1);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public void CreateObiFishingLine(int currentLineTypeIndex)
|
||
{
|
||
if (!Line)
|
||
{
|
||
var lineSolverPrefab = Assets.Load<GameObject>("Assets/ResRaw/Prefabs/Line/LineSolver.prefab");
|
||
var solver = Instantiate(lineSolverPrefab, GearRoot);
|
||
solver.transform.position = Asset.lineConnector.position;
|
||
solver.transform.rotation = Asset.lineConnector.rotation;
|
||
var indexNames = new[] { "LineHand1", "LineHand1" };
|
||
var path =
|
||
$"Assets/ResRaw/Prefabs/Line/{indexNames[currentLineTypeIndex]}.prefab";
|
||
var prefab = Assets.Load<GameObject>(path);
|
||
|
||
GameObject obj = Instantiate(prefab, solver.transform);
|
||
obj.transform.localPosition = Vector3.zero;
|
||
obj.transform.localScale = Vector3.one;
|
||
obj.transform.rotation = Quaternion.identity;
|
||
|
||
Line = obj.GetComponent<FLine>();
|
||
Line.transform.position = Asset.lineConnector.position;
|
||
Line.Init(this);
|
||
}
|
||
}
|
||
|
||
public void SetRing(RodRingAsset ringAsset)
|
||
{
|
||
if (Asset.rings == null || Asset.rings.Length < 1) return;
|
||
|
||
var trans = ringAsset.rings;
|
||
RodRingNode lastRingNode = null;
|
||
List<RodRingNode> list = new List<RodRingNode>();
|
||
for (int i = 0; i < Asset.rings.Length; i++)
|
||
{
|
||
var ring = Asset.rings[i];
|
||
if (ring == null)
|
||
{
|
||
Log.Error($"ring is null,index={i}");
|
||
continue;
|
||
}
|
||
|
||
var lastName = ring.name.GetLastString();
|
||
foreach (var tran in trans)
|
||
{
|
||
var ringNode = tran.ring;
|
||
var lastName2 = ringNode.name.GetLastString();
|
||
if (lastName != lastName2) continue;
|
||
list.Add(tran);
|
||
ringNode.SetParent(ring);
|
||
ringNode.localPosition = Vector3.zero;
|
||
ringNode.localRotation = Quaternion.identity;
|
||
lastRingNode = tran;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (lastRingNode != null)
|
||
{
|
||
Asset.lineConnector = lastRingNode.point;
|
||
}
|
||
|
||
rings = list.ToArray();
|
||
}
|
||
|
||
|
||
#region 鱼竿弯曲
|
||
|
||
private List<float> previousWeights = Enumerable.Repeat(0f, 10).ToList();
|
||
private float bendSmooth = 1f;
|
||
public float maxRodStrength = 1f;
|
||
|
||
public float CurrentTension01
|
||
{
|
||
get
|
||
{
|
||
float value = 0f;
|
||
// if ((bool)currentReel && (bool)fishingLine)
|
||
// {
|
||
// float linePullingForce = currentReel.LinePullingForce;
|
||
// value = Mathf.Min(fishingLine.CurrentLineTension, linePullingForce) / maxRodStrength;
|
||
// }
|
||
if (Line)
|
||
{
|
||
value = Tension / maxRodStrength;
|
||
}
|
||
|
||
return Mathf.Clamp01(value);
|
||
}
|
||
}
|
||
|
||
private void BendControl()
|
||
{
|
||
// var _ccdik = Asset.CCDIK;
|
||
//
|
||
// var state = Owner.State;
|
||
//
|
||
//
|
||
// Vector3 vector = Line.EndNode.transform.position;
|
||
//
|
||
// // 当前物体的朝向与指向 Lure 的方向之间的夹角,在 0(完全对齐)到 1(完全相反)之间的一个比例值
|
||
// float headingAlignment = Vector3.Angle(base.transform.forward,
|
||
// (Line.EndNode.transform.position - transform.position).normalized) / 180f;
|
||
// // 经过朝向调制后的有效张力
|
||
// var effectiveTension = Mathf.Clamp(CurrentTension01 * headingAlignment, 0f, 1f);
|
||
//
|
||
// float multiple = 0;
|
||
// if (state == PlayerState.Fight)
|
||
// {
|
||
// multiple = 1;
|
||
// }
|
||
// else
|
||
// {
|
||
// multiple = 0.1f;
|
||
// }
|
||
//
|
||
// float targetWeight = effectiveTension * multiple;
|
||
// if (targetWeight > 0)
|
||
// {
|
||
// // Debug.Log($"CurrentTension01={CurrentTension01} LinelenghtDiferent={Line.LinelenghtDiferent} Tension={PlayerItem.Tension} num2={effectiveTension} num3={headingAlignment} num6={multiple}");
|
||
// }
|
||
//
|
||
// if (state == PlayerState.Prepare)
|
||
// {
|
||
// targetWeight = 0.002f;
|
||
// }
|
||
//
|
||
// float item = Mathf.MoveTowards(_ccdik.solver.IKPositionWeight, targetWeight, Time.deltaTime * bendSmooth);
|
||
// previousWeights.RemoveAt(0);
|
||
// previousWeights.Add(item);
|
||
// float averageWeight = previousWeights.Average();
|
||
// if (currentFish)
|
||
// {
|
||
// averageWeight = Math.Min(averageWeight, 0.01f);
|
||
// }
|
||
//
|
||
// _ccdik.solver.SetIKPosition(vector);
|
||
// _ccdik.solver.SetIKPositionWeight(averageWeight);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 杆子弯曲
|
||
|
||
private void BendingRod()
|
||
{
|
||
}
|
||
|
||
#endregion
|
||
|
||
private void Test()
|
||
{
|
||
// var root = Player.ModelAsset.RodRoot;
|
||
// if (!root) return;
|
||
// transform.SetPositionAndRotation(root.position, root.rotation);
|
||
}
|
||
}
|
||
} |