完成基本代码迁移重构

This commit is contained in:
Bob.Song
2026-03-10 16:34:31 +08:00
parent cf2e374998
commit d17d13d7e7
11 changed files with 187 additions and 194 deletions

View File

@@ -2,13 +2,14 @@
using Fantasy;
using Fantasy.Entitas;
using NBF.Utils;
using UnityEngine;
namespace NBF
{
/// <summary>
/// 玩家物品
/// </summary>
public abstract class PlayerItem : Entity
public class PlayerItem : Entity
{
public Player Owner;
@@ -17,11 +18,47 @@ namespace NBF
/// </summary>
public int ConfigID;
public List<int> BindItems = new List<int>();
public virtual void Init(Player player, ItemInfo bindInfo)
#region Rod专属
/// <summary>
/// 线长度
/// </summary>
public float LineLength = 1.5f;
/// <summary>
/// 浮漂线长度
/// </summary>
public float FloatLength = 0.5f;
private float _tension;
/// <summary>
/// 拉力
/// </summary>
public float Tension
{
get => _tension;
private set
{
if (!Mathf.Approximately(_tension, value))
{
_tension = value;
// OnTensionChanged?.Invoke(_tension);
}
}
}
#endregion
public void Init(Player player, int configId, List<int> bindItems)
{
Owner = player;
ConfigID = bindInfo.ConfigId;
ConfigID = configId;
BindItems.Clear();
BindItems.AddRange(bindItems);
// var itemType = bindInfo.Item.GetItemType();
// if (itemType == ItemType.Rod)
// {

View File

@@ -1,15 +0,0 @@
using System.Collections.Generic;
using Fantasy;
using Fantasy.Entitas;
namespace NBF
{
public class PlayerItemRod : PlayerItem
{
public override void Init(Player player, ItemInfo bindInfo)
{
//var binds = RoleModel.Instance.GetBindItems(item.Id);
base.Init(player, bindInfo);
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 9e0846296bef4505868010bfe1eee1e3
timeCreated: 1773124278

View File

@@ -103,18 +103,13 @@ namespace NBF
ItemChangeEvent(prevItem);
}
public void UseItem(ItemInfo item)
public void UseItem(int configId, List<int> bindItems)
{
var prevItem = HandItem;
var itemType = item.ConfigId.GetItemType();
if (itemType == ItemType.Rod)
{
var playerItemRod = Create<PlayerItemRod>(Scene);
playerItemRod.Init(this, item);
Items[playerItemRod.Id] = playerItemRod;
HandItemId = playerItemRod.Id;
}
var playerItemRod = Create<PlayerItem>(Scene);
playerItemRod.Init(this, configId, bindItems);
Items[playerItemRod.Id] = playerItemRod;
HandItemId = playerItemRod.Id;
ItemChangeEvent(prevItem);
}

View File

@@ -1,20 +0,0 @@
using Fantasy.Async;
using UnityEngine;
namespace NBF
{
public class PlayerItemRodView : PlayerItemView
{
public RodAsset RodAsset;
public override async FTask InitShow(PlayerItem item)
{
Item = item;
var itemConfig = Game.Tables.TbItem.Get(Item.ConfigID);
RodAsset = itemConfig.InstantiateAndComponent<RodAsset>(SceneSettings.Instance.GearNode, Vector3.zero,
Quaternion.identity);
// await Rod.InitRod(Item);
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 77390e0ed53847f3b190ffc0d9f1319a
timeCreated: 1773124629

View File

@@ -7,21 +7,22 @@ namespace NBF
/// <summary>
/// 玩家物品视图组件
/// </summary>
public abstract class PlayerItemView : Entity
public class PlayerItemView : Entity
{
public PlayerItem Item { get; protected set; }
/// <summary>
/// 竿子
/// </summary>
public FRod Rod { get; private set; }
public abstract FTask InitShow(PlayerItem item);
// public async FTask InitShow()
// {
// // Item = GetParent<PlayerItem>();
// var itemConfig = Game.Tables.TbItem.Get(Item.ConfigID);
// // Rod = itemConfig.InstantiateAndComponent<FRod>(SceneSettings.Instance.GearNode, Vector3.zero,
// // Quaternion.identity);
//
// // await Rod.InitRod(Item);
// }
public async FTask InitShow(PlayerItem item)
{
Item = item;
var itemConfig = Game.Tables.TbItem.Get(Item.ConfigID);
Rod = itemConfig.InstantiateAndComponent<FRod>(SceneSettings.Instance.GearNode, Vector3.zero,
Quaternion.identity);
await Rod.InitRod(Item);
}
}
}

View File

@@ -1,4 +1,6 @@
using Fantasy;
using System.Collections.Generic;
using System.Linq;
using Fantasy;
using Fantasy.Entitas;
using Fantasy.Entitas.Interface;
using UnityEngine;
@@ -90,7 +92,9 @@ namespace NBF
var item = RoleModel.Instance.GetSlotItem(index - 1);
if (item != null)
{
Player.UseItem(item);
List<ItemInfo> children = RoleModel.Instance.GetBindItems(item.Id);
List<int> bindItems = children.Select(itemInfo => itemInfo.ConfigId).ToList();
Player.UseItem(item.ConfigId, bindItems);
}
}
}

View File

@@ -79,7 +79,7 @@ namespace NBF
var itemType = handItem.ConfigID.GetItemType();
if (itemType == ItemType.Rod)
{
var itemView = handItem.GetOrAddComponent<PlayerItemRodView>();
var itemView = handItem.GetOrAddComponent<PlayerItemView>();
await itemView.InitShow(handItem);
Unity.ModelAsset.PlayerAnimator.OnUseItem(itemView);
}

View File

@@ -2,8 +2,8 @@
namespace NBF
{
public class FHandItem : MonoBehaviour
public abstract class FHandItem : MonoBehaviour
{
public int ConfigId;
public abstract int ConfigId { get; }
}
}

View File

@@ -14,7 +14,10 @@ namespace NBF
{
private float _tension;
public PlayerItem PlayerItem;
public override int ConfigId => PlayerItem?.ConfigID ?? 0;
/// <summary>
/// 可用的
/// </summary>
@@ -126,123 +129,122 @@ namespace NBF
public async FTask InitRod(PlayerItem playerItem)
{
// ConfigId = itemBindInfo.Item;
// // Player = player;
//
// var playerView = player.GetComponent<PlayerView>();
//
// 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<ItemInfo> 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<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;
// }
// }
//
// 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;
PlayerItem = playerItem;
var playerView = playerItem.Owner.GetComponent<PlayerView>();
var playerViewUnity = playerView.Unity;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
SceneSettings.Instance.GearNode.position = playerViewUnity.transform.position;
await FTask.WaitFrame(playerView.Scene); //等待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<ItemInfo> children = RoleModel.Instance.GetBindItems(itemInfo.Id);
CreateFishingHandler();
await FTask.WaitFrame(playerView.Scene); //等待1帧
// children.Sort();
foreach (var childConfigId in playerItem.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(playerView.Scene); //等待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);
}
await FTask.WaitFrame(playerView.Scene); //等待1帧
transform.SetParent(playerViewUnity.ModelAsset.RodRoot);
transform.localPosition = Vector3.zero;
transform.rotation = playerViewUnity.ModelAsset.RodRoot.rotation;
Usable = true;
}
@@ -286,11 +288,6 @@ namespace NBF
Line = obj.GetComponent<FLine>();
Line.transform.position = Asset.lineConnector.position;
Line.Init(this);
// var obiSolver = solver.GetComponent<ObiSolver>();
// obiSolver.parameters.ambientWind = Vector3.zero;
// obiSolver.wind.
// obiSolver.simulateWhenInvisible
}
}