迁移 Player/ProcedureGame/ShopUseCase 里的实体方法调用风格

- Player/ShopUseCase:调整了 Weapon 实体的打开方式
- ProcedureGame:调整初始化时的 Hud 和 Player 的初始化写法,流程更清楚
This commit is contained in:
SepComet
2026-06-06 14:12:05 +08:00
parent f896a4a9c8
commit 41ff2c6df8
8 changed files with 107 additions and 118 deletions
@@ -1,13 +1,13 @@
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using SepCore.Components;
using SepCore.Event;
using SepCore.DataTable;
using SepCore.Definition;
using SepCore.Entity;
using GameFramework.Event;
using UnityGameFramework.Runtime;
using SepCore.Entity.Weapon;
using SepCore.CustomUtility;
using SepCore.AsyncTask;
namespace SepCore.Entity
{
@@ -92,6 +92,28 @@ namespace SepCore.Entity
return _backpackComponent.AttachProp(prop);
}
public async UniTask<WeaponBase> AddWeaponAsync(WeaponData weaponData, float timeout = 30f)
{
if (weaponData == null || _backpackComponent == null)
{
return null;
}
WeaponBase weapon = await GameEntry.Entity.ShowWeaponAsync(weaponData, timeout);
if (weapon == null)
{
return null;
}
if (!_backpackComponent.AttachWeapon(weapon))
{
GameEntry.Entity.HideEntity(weapon);
return null;
}
return weapon;
}
public bool RemoveWeapon(WeaponBase weapon)
{
if (weapon == null || _backpackComponent == null)
@@ -141,8 +163,8 @@ namespace SepCore.Entity
// BackpackComponent
Coin = role.Coin;
_backpackComponent.OnInit(this, role.WeaponCapacity);
GameEntry.Entity.ShowWeapon(EntityDataFactory.Create(GameEntry.Entity.NextId(),
WeaponType.WeaponKnife, Id, _playerData.Camp));
AddWeaponAsync(EntityDataFactory.Create(GameEntry.Entity.NextId(),
WeaponType.WeaponKnife, Id, _playerData.Camp)).Forget();
// StatComponent
foreach (var modifier in role.InitialProperties)
@@ -158,8 +180,7 @@ namespace SepCore.Entity
GameEntry.Event.Fire(this, PlayerExpChangeEventArgs.Create(0, 100));
GameEntry.Event.Fire(this,
PlayerCoinChangeEventArgs.Create(Coin, Coin));
GameEntry.Event.Fire(this, PlayerCoinChangeEventArgs.Create(Coin, Coin));
}
#region FSM
@@ -190,8 +211,6 @@ namespace SepCore.Entity
{
base.OnShow(userData);
GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, ShowEntitySuccess);
_statComponent.OnInit();
if (userData is PlayerData playerData)
@@ -230,18 +249,5 @@ namespace SepCore.Entity
}
#endregion
#region Event Handlers
private void ShowEntitySuccess(object sender, GameEventArgs e)
{
if (!(e is ShowEntitySuccessEventArgs args)) return;
if (args.Entity.Logic is WeaponBase weapon)
{
_backpackComponent.AttachWeapon(weapon);
}
}
#endregion
}
}
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using SepCore.DataTable;
using SepCore.Definition;
using SepCore.Entity;
using SepCore.CustomUtility;
using SepCore.Entity.Weapon;
using GameFramework.DataTable;
using UnityEngine;
using UnityGameFramework.Runtime;
@@ -107,7 +109,7 @@ namespace SepCore.UI
};
}
public ShopPurchaseResult TryPurchase(int goodsIndex)
public async UniTask<ShopPurchaseResult> TryPurchaseAsync(int goodsIndex)
{
if (goodsIndex < 0 || goodsIndex >= _selections.Count)
{
@@ -128,7 +130,7 @@ namespace SepCore.UI
return null;
}
DisplayItemRawData displayItem = ApplyGoodsPurchase(selection);
DisplayItemRawData displayItem = await ApplyGoodsPurchaseAsync(selection);
if (displayItem == null)
{
return null;
@@ -318,7 +320,7 @@ namespace SepCore.UI
return Mathf.Max(1, finalPrice);
}
private DisplayItemRawData ApplyGoodsPurchase(ShopGoodsSelection goods)
private async UniTask<DisplayItemRawData> ApplyGoodsPurchaseAsync(ShopGoodsSelection goods)
{
if (goods == null || Player == null)
{
@@ -362,7 +364,13 @@ namespace SepCore.UI
return null;
}
GameEntry.Entity.ShowWeapon(weaponData);
WeaponBase weapon = await Player.AddWeaponAsync(weaponData);
if (weapon == null)
{
Log.Warning(
$"ShopFormUseCase::ApplyGoodsPurchase: Show weapon failed, weapon type id = {goods.GoodsTypeId}");
return null;
}
return new DisplayItemRawData
{