using System;
using System.Collections;
using System.Collections.Generic;
using Fantasy;
using Fantasy.Async;
using NBC.Asset;
using NBF.Utils;
using UnityEngine;
using Object = UnityEngine.Object;
namespace NBF
{
public class FRod : FHandItem
{
private float _tension;
///
/// 可用的
///
public bool Usable { get; private set; }
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;
// public FWaterDisplacement LureHookWaterDisplacement;
[HideInInspector] public FFish currentFish;
public RodRingNode[] rings;
///
/// 线长度
///
public float lineLength = 1.5f;
///
/// 浮漂线长度
///
public float floatLength = 0.5f;
public float Tension
{
get => _tension;
private set
{
if (!Mathf.Approximately(_tension, value))
{
_tension = value;
// OnTensionChanged?.Invoke(_tension);
}
}
}
private void Awake()
{
Asset = GetComponent();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha0))
{
SetLineLength(lineLength);
}
else if (Input.GetKeyDown(KeyCode.Plus) || Input.GetKeyDown(KeyCode.Equals))
{
lineLength += 0.1f;
SetLineLength(lineLength);
}
else if (Input.GetKeyDown(KeyCode.Minus))
{
lineLength -= 0.1f;
SetLineLength(lineLength);
}
}
public void SetLineLength(float lineLength, bool stretchRope = true)
{
if (!Line) return;
if (Line.LineType == LineType.Spinning)
{
//没有浮漂类型
Line.Lure.SetJointDistance(lineLength);
if (stretchRope)
{
Line.SetTargetLength(Tension > 0f ? 0f : lineLength);
}
}
else
{
//有浮漂
Line.Lure.SetJointDistance(floatLength);
Line.Bobber.SetJointDistance(lineLength - floatLength);
if (stretchRope)
{
Line.SetTargetLength(Tension > 0f ? 0f : lineLength - floatLength);
}
}
}
private void LateUpdate()
{
Test();
}
public IEnumerator Destroy()
{
if (GearRoot != null)
{
Object.Destroy(GearRoot.gameObject);
}
yield return 1;
}
public async FTask InitRod(PlayerItem playerItem)
{
// ConfigId = itemBindInfo.Item;
// // Player = player;
//
// var playerView = player.GetComponent();
//
// var playerViewUnity = playerView.Unity;
//
// transform.localPosition = Vector3.zero;
// transform.localRotation = Quaternion.identity;
// transform.localScale = Vector3.one;
// SceneSettings.Instance.GearNode.position = playerViewUnity.transform.position;
// yield return 1;
// var obj = new GameObject($"rod_{ConfigId}");
// obj.transform.SetParent(SceneSettings.Instance.GearNode);
// // obj.transform.SetParent(player.transform);
// // obj.transform.localPosition = Vector3.zero;
// obj.transform.position = playerViewUnity.transform.position;
// obj.transform.rotation = playerViewUnity.transform.rotation;
// obj.transform.localScale = Vector3.one;
// GearRoot = obj.transform;
//
// var parent = GearRoot;
//
// // List children = RoleModel.Instance.GetBindItems(itemInfo.Id);
//
//
// CreateFishingHandler();
// yield return 1; //等待1帧
// // children.Sort();
// foreach (var childConfigId in itemBindInfo.BindItems)
// {
// var itemType = childConfigId.GetItemType();
// var config = Game.Tables.TbItem.Get(childConfigId);
// if (itemType == ItemType.Reel)
// {
// Reel = config.InstantiateAndComponent(Asset.ReelConnector, Vector3.zero,
// Quaternion.identity);
// Reel.SetItemConfigId(childConfigId);
// }
// else if (itemType == ItemType.Bobber)
// {
// Bobber = config.InstantiateAndComponent(parent, Vector3.zero,
// Quaternion.identity);
// Bobber.SetItemConfigId(childConfigId);
// }
// else if (itemType == ItemType.Hook)
// {
// Hook = config.InstantiateAndComponent(parent, Vector3.zero,
// Quaternion.identity);
// Hook.SetItemConfigId(childConfigId);
// }
// else if (itemType == ItemType.Bait)
// {
// Bait = config.InstantiateAndComponent(parent, Vector3.zero,
// Quaternion.identity);
// Bait.SetItemConfigId(childConfigId);
// }
// else if (itemType == ItemType.Lure)
// {
// Lure = config.InstantiateAndComponent(parent, Vector3.zero,
// Quaternion.identity);
// Lure.SetItemConfigId(childConfigId);
// }
// else if (itemType == ItemType.Weight)
// {
// Weight = config.InstantiateAndComponent(parent, Vector3.zero,
// Quaternion.identity);
// Weight.SetItemConfigId(childConfigId);
// }
// else if (itemType == ItemType.Line)
// {
// // lineItemInfo = child;
// }
// }
//
// yield return 1;
// 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);
// }
//
// yield return 1; //等待1帧
//
// transform.SetParent(playerViewUnity.ModelAsset.RodRoot);
// transform.localPosition = Vector3.zero;
// transform.rotation = playerViewUnity.ModelAsset.RodRoot.rotation;
//
// Usable = true;
}
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("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[] { "fishing line float set", "fishing line spinning" };
var path =
$"Assets/ResRaw/Prefabs/Line/{indexNames[currentLineTypeIndex]}.prefab";
var prefab = Assets.Load(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();
Line.transform.position = Asset.lineConnector.position;
Line.Init(this);
// var obiSolver = solver.GetComponent();
// obiSolver.parameters.ambientWind = Vector3.zero;
// obiSolver.wind.
// obiSolver.simulateWhenInvisible
}
}
public void SetRing(RodRingAsset ringAsset)
{
if (Asset.rings == null || Asset.rings.Length < 1) return;
var trans = ringAsset.rings;
RodRingNode lastRingNode = null;
List list = new List();
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();
}
private void Test()
{
// var root = Player.ModelAsset.RodRoot;
// if (!root) return;
// transform.SetPositionAndRotation(root.position, root.rotation);
}
}
}